fix: fix workspace specific custom tags

This commit is contained in:
Ruben Fiszel
2025-01-17 19:48:33 +01:00
parent 66f567858e
commit c3daefda30
8 changed files with 70 additions and 9 deletions

View File

@@ -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

View File

@@ -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,
};
@@ -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> {

View File

@@ -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()

View File

@@ -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
@@ -27,5 +28,5 @@
>
</Button>
</svelte:fragment>
<AssignableTagsInner on:refresh />
<AssignableTagsInner {showWorkspaceRestriction} on:refresh />
</Popup>

View File

@@ -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)
}

View File

@@ -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
@@ -11,7 +11,7 @@
async function loadWorkerGroups() {
if (!$workerTags) {
$workerTags = await WorkerService.getCustomTags()
$workerTags = await WorkerService.getCustomTags({ workspace: $workspaceStore })
}
}
</script>

View File

@@ -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) {
@@ -26,6 +29,7 @@
lookForTag()
onDestroy(() => {
visible = false
if (timeout) {
clearTimeout(timeout)
}

View File

@@ -282,6 +282,7 @@
<div class="flex flex-row-reverse w-full pb-2 items-center gap-4">
<div>
<AssignableTags
showWorkspaceRestriction
on:refresh={() => {
loadCustomTags()
}}