Skip to content

Commit

Permalink
fix: fix workspace specific custom tags
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenfiszel committed Jan 17, 2025
1 parent 501c44b commit 0d153a0
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 9 deletions.
11 changes: 11 additions & 0 deletions backend/windmill-api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4297,6 +4297,17 @@ paths:
operationId: getCustomTags
tags:
- worker
parameters:
- name: workspace
in: query
schema:
type: string
required: false
- name: show_workspace_restriction
in: query
schema:
type: boolean
required: false
responses:
"200":
description: list of custom tags
Expand Down
46 changes: 43 additions & 3 deletions backend/windmill-api/src/workers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use windmill_common::{
db::UserDB,
error::JsonResult,
utils::{paginate, Pagination},
worker::{ALL_TAGS, DEFAULT_TAGS, DEFAULT_TAGS_PER_WORKSPACE},
worker::{ALL_TAGS, CUSTOM_TAGS_PER_WORKSPACE, DEFAULT_TAGS, DEFAULT_TAGS_PER_WORKSPACE},
DB,
};

Expand Down Expand Up @@ -133,8 +133,48 @@ async fn exists_worker_with_tag(
Ok(Json(row.exists.unwrap_or(false)))
}

async fn get_custom_tags() -> Json<Vec<String>> {
Json(ALL_TAGS.read().await.clone().into())
#[derive(Deserialize)]
struct CustomTagQuery {
workspace: Option<String>,
show_workspace_restriction: Option<bool>,
}
async fn get_custom_tags(Query(query): Query<CustomTagQuery>) -> JsonResult<Vec<String>> {
if query.show_workspace_restriction.is_some_and(|x| x) && query.workspace.is_some() {
return Err(windmill_common::error::Error::BadRequest(
"Cannot use both workspace and show_workspace_restriction".to_string(),
));
}
if let Some(workspace) = query.workspace {
let tags_o = CUSTOM_TAGS_PER_WORKSPACE.read().await;
let workspace_tags = tags_o
.1
.get(&workspace)
.map(|x| x.clone())
.unwrap_or_default();
let all_tags = tags_o.0.clone();
return Ok(Json(
all_tags
.into_iter()
.chain(workspace_tags.into_iter())
.collect(),
));
} else if query.show_workspace_restriction.is_some_and(|x| x) {
let tags_o = CUSTOM_TAGS_PER_WORKSPACE.read().await;
let workspace_tags = tags_o
.1
.iter()
.map(|(workspace, tags)| tags.iter().map(move |tag| format!("{tag}({workspace})")))
.flatten()
.collect::<Vec<String>>();
let all_tags = tags_o.0.clone();
return Ok(Json(
all_tags
.into_iter()
.chain(workspace_tags.into_iter())
.collect(),
));
}
Ok(Json(ALL_TAGS.read().await.clone().into()))
}

async fn get_default_tags_per_workspace() -> JsonResult<bool> {
Expand Down
2 changes: 1 addition & 1 deletion backend/windmill-common/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ lazy_static::lazy_static! {

pub static ref ALL_TAGS: Arc<RwLock<Vec<String>>> = Arc::new(RwLock::new(vec![]));

static ref CUSTOM_TAG_REGEX: Regex = Regex::new(r"^(\w+)\(((?:\w+)\+?)+\)$").unwrap();
static ref CUSTOM_TAG_REGEX: Regex = Regex::new(r"^([\w-]+)\(((?:[\w-])+\+?)+\)$").unwrap();

pub static ref DISABLE_BUNDLING: bool = std::env::var("DISABLE_BUNDLING")
.ok()
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/lib/components/AssignableTags.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
export let placement: 'bottom-end' | 'top-end' = 'bottom-end'
export let color: 'nord' | 'dark' = 'dark'
export let disabled = false
export let showWorkspaceRestriction = false
</script>

<Popup
Expand All @@ -27,5 +28,5 @@
>
</Button>
</svelte:fragment>
<AssignableTagsInner on:refresh />
<AssignableTagsInner {showWorkspaceRestriction} on:refresh />
</Popup>
6 changes: 5 additions & 1 deletion frontend/src/lib/components/AssignableTagsInner.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@
import { base } from '$lib/base'
import { createEventDispatcher } from 'svelte'
export let showWorkspaceRestriction = false
let newTag: string = ''
let customTags: string[] | undefined = undefined
async function loadCustomTags() {
try {
customTags = (await WorkerService.getCustomTags()) ?? []
customTags =
(await WorkerService.getCustomTags({
showWorkspaceRestriction
})) ?? []
} catch (err) {
sendUserToast(`Could not load global cache: ${err}`, true)
}
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/lib/components/WorkerTagSelect.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { workerTags } from '$lib/stores'
import { workerTags, workspaceStore } from '$lib/stores'
import { WorkerService } from '$lib/gen'
export let tag: string | undefined
Expand All @@ -11,7 +11,7 @@
async function loadWorkerGroups() {
if (!$workerTags) {
$workerTags = await WorkerService.getCustomTags()
$workerTags = await WorkerService.getCustomTags({ workspace: $workspaceStore })
}
}
</script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
let timeout: NodeJS.Timeout | undefined = undefined
let visible = true
async function lookForTag(): Promise<void> {
try {
const existsWorkerWithTag = await WorkerService.existsWorkerWithTag({ tag })
noWorkerWithTag = !existsWorkerWithTag
if (noWorkerWithTag) {
timeout = setTimeout(() => {
lookForTag()
if (visible) {
lookForTag()
}
}, 1000)
}
} catch (err) {
Expand All @@ -26,6 +29,7 @@
lookForTag()
onDestroy(() => {
visible = false
if (timeout) {
clearTimeout(timeout)
}
Expand Down
1 change: 1 addition & 0 deletions frontend/src/routes/(root)/(logged)/workers/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@
<div class="flex flex-row-reverse w-full pb-2 items-center gap-4">
<div>
<AssignableTags
showWorkspaceRestriction
on:refresh={() => {
loadCustomTags()
}}
Expand Down

0 comments on commit 0d153a0

Please sign in to comment.