From bc5a7d0519a09d21c98e02677862b3e98fb040a7 Mon Sep 17 00:00:00 2001 From: HugoCasa Date: Tue, 27 Aug 2024 21:58:51 +0200 Subject: [PATCH] optional starred info on get item + update name of user instance (#4295) --- ...51eb3bd5621ae081a418f1d4dbcb8359a8adb.json | 15 +++ backend/tests/worker.rs | 2 +- backend/windmill-api/openapi.yaml | 18 ++++ backend/windmill-api/src/apps.rs | 47 ++++++++-- backend/windmill-api/src/flows.rs | 26 +++++- backend/windmill-api/src/scripts.rs | 92 +++++++++++++++---- backend/windmill-api/src/users.rs | 11 +++ backend/windmill-api/src/utils.rs | 6 ++ backend/windmill-common/src/flows.rs | 2 + backend/windmill-common/src/scripts.rs | 2 + 10 files changed, 189 insertions(+), 32 deletions(-) create mode 100644 backend/.sqlx/query-aa5bdf0ab8781cf984711ae4f4351eb3bd5621ae081a418f1d4dbcb8359a8adb.json diff --git a/backend/.sqlx/query-aa5bdf0ab8781cf984711ae4f4351eb3bd5621ae081a418f1d4dbcb8359a8adb.json b/backend/.sqlx/query-aa5bdf0ab8781cf984711ae4f4351eb3bd5621ae081a418f1d4dbcb8359a8adb.json new file mode 100644 index 0000000000..9a38a434b0 --- /dev/null +++ b/backend/.sqlx/query-aa5bdf0ab8781cf984711ae4f4351eb3bd5621ae081a418f1d4dbcb8359a8adb.json @@ -0,0 +1,15 @@ +{ + "db_name": "PostgreSQL", + "query": "UPDATE password SET name = $1 WHERE email = $2", + "describe": { + "columns": [], + "parameters": { + "Left": [ + "Varchar", + "Text" + ] + }, + "nullable": [] + }, + "hash": "aa5bdf0ab8781cf984711ae4f4351eb3bd5621ae081a418f1d4dbcb8359a8adb" +} diff --git a/backend/tests/worker.rs b/backend/tests/worker.rs index 6e81aabbd3..bb6fe37065 100644 --- a/backend/tests/worker.rs +++ b/backend/tests/worker.rs @@ -2675,7 +2675,7 @@ async fn test_flow_lock_all(db: Pool) { in_test_worker(&db, listen_first_job, port).await; let modules = client - .get_flow_by_path("test-workspace", "g/all/flow_lock_all") + .get_flow_by_path("test-workspace", "g/all/flow_lock_all", None) .await .unwrap() .into_inner() diff --git a/backend/windmill-api/openapi.yaml b/backend/windmill-api/openapi.yaml index 1d4e753d8f..892f3dab66 100644 --- a/backend/windmill-api/openapi.yaml +++ b/backend/windmill-api/openapi.yaml @@ -342,6 +342,8 @@ paths: properties: is_super_admin: type: boolean + name: + type: string responses: "200": description: user updated @@ -3924,6 +3926,10 @@ paths: parameters: - $ref: "#/components/parameters/WorkspaceId" - $ref: "#/components/parameters/ScriptPath" + - name: with_starred_info + in: query + schema: + type: boolean responses: "200": description: script details @@ -4059,6 +4065,10 @@ paths: parameters: - $ref: "#/components/parameters/WorkspaceId" - $ref: "#/components/parameters/ScriptHash" + - name: with_starred_info + in: query + schema: + type: boolean responses: "200": description: script details @@ -4515,6 +4525,10 @@ paths: parameters: - $ref: "#/components/parameters/WorkspaceId" - $ref: "#/components/parameters/ScriptPath" + - name: with_starred_info + in: query + schema: + type: boolean responses: "200": description: flow details @@ -4932,6 +4946,10 @@ paths: parameters: - $ref: "#/components/parameters/WorkspaceId" - $ref: "#/components/parameters/ScriptPath" + - name: with_starred_info + in: query + schema: + type: boolean responses: "200": description: app details diff --git a/backend/windmill-api/src/apps.rs b/backend/windmill-api/src/apps.rs index 9bfb84e34f..bc684ef228 100644 --- a/backend/windmill-api/src/apps.rs +++ b/backend/windmill-api/src/apps.rs @@ -11,6 +11,7 @@ use crate::{ db::{ApiAuthed, DB}, resources::get_resource_value_interpolated_internal, users::{require_owner_of_path, OptAuthed}, + utils::WithStarredInfoQuery, variables::encrypt, webhook_util::{WebhookMessage, WebhookShared}, HTTP_CLIENT, @@ -116,6 +117,8 @@ pub struct AppWithLastVersion { pub created_by: String, pub created_at: chrono::DateTime, pub extra_perms: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub starred: Option, } #[derive(Serialize, Deserialize, FromRow)] @@ -316,20 +319,44 @@ async fn get_app( authed: ApiAuthed, Extension(user_db): Extension, Path((w_id, path)): Path<(String, StripPath)>, + Query(query): Query, ) -> JsonResult { let path = path.to_path(); let mut tx = user_db.begin(&authed).await?; - let app_o = sqlx::query_as::<_, AppWithLastVersion>( - "SELECT app.id, app.path, app.summary, app.versions, app.policy, - app.extra_perms, app_version.value, - app_version.created_at, app_version.created_by from app, app_version - WHERE app.path = $1 AND app.workspace_id = $2 AND app_version.id = app.versions[array_upper(app.versions, 1)]", - ) - .bind(path.to_owned()) - .bind(&w_id) - .fetch_optional(&mut *tx) - .await?; + let app_o = if query.with_starred_info.unwrap_or(false) { + sqlx::query_as::<_, AppWithLastVersion>( + "SELECT app.id, app.path, app.summary, app.versions, app.policy, + app.extra_perms, app_version.value, + app_version.created_at, app_version.created_by, favorite.path IS NOT NULL as starred + FROM app + JOIN app_version + ON app_version.id = app.versions[array_upper(app.versions, 1)] + LEFT JOIN favorite + ON favorite.favorite_kind = 'app' + AND favorite.workspace_id = app.workspace_id + AND favorite.path = app.path + AND favorite.usr = $3 + WHERE app.path = $1 AND app.workspace_id = $2", + ) + .bind(path.to_owned()) + .bind(&w_id) + .bind(&authed.username) + .fetch_optional(&mut *tx) + .await? + } else { + sqlx::query_as::<_, AppWithLastVersion>( + "SELECT app.id, app.path, app.summary, app.versions, app.policy, + app.extra_perms, app_version.value, + app_version.created_at, app_version.created_by, NULL as starred + FROM app, app_version + WHERE app.path = $1 AND app.workspace_id = $2 AND app_version.id = app.versions[array_upper(app.versions, 1)]", + ) + .bind(path.to_owned()) + .bind(&w_id) + .fetch_optional(&mut *tx) + .await? + }; tx.commit().await?; let app = not_found_if_none(app_o, "App", path)?; diff --git a/backend/windmill-api/src/flows.rs b/backend/windmill-api/src/flows.rs index 0dbe994040..37f9b44143 100644 --- a/backend/windmill-api/src/flows.rs +++ b/backend/windmill-api/src/flows.rs @@ -9,6 +9,7 @@ use std::collections::HashMap; use crate::db::ApiAuthed; +use crate::utils::WithStarredInfoQuery; use crate::{ db::DB, schedule::clear_schedule, @@ -876,13 +877,31 @@ async fn get_flow_by_path( authed: ApiAuthed, Extension(user_db): Extension, Path((w_id, path)): Path<(String, StripPath)>, + Query(query): Query, ) -> JsonResult { let path = path.to_path(); let mut tx = user_db.begin(&authed).await?; - let flow_o = + let flow_o = if query.with_starred_info.unwrap_or(false) { sqlx::query_as::<_, Flow>( - "SELECT flow.workspace_id, flow.path, flow.summary, flow.description, flow.archived, flow.extra_perms, flow.draft_only, flow.dedicated_worker, flow.tag, flow.ws_error_handler_muted, flow.timeout, flow.visible_to_runner_only, flow_version.schema, flow_version.value, flow_version.created_at as edited_at, flow_version.created_by as edited_by + "SELECT flow.workspace_id, flow.path, flow.summary, flow.description, flow.archived, flow.extra_perms, flow.draft_only, flow.dedicated_worker, flow.tag, flow.ws_error_handler_muted, flow.timeout, flow.visible_to_runner_only, flow_version.schema, flow_version.value, flow_version.created_at as edited_at, flow_version.created_by as edited_by, favorite.path IS NOT NULL as starred + FROM flow + LEFT JOIN favorite + ON favorite.favorite_kind = 'flow' + AND favorite.workspace_id = flow.workspace_id + AND favorite.path = flow.path + AND favorite.usr = $3 + LEFT JOIN flow_version ON flow_version.id = flow.versions[array_upper(flow.versions, 1)] + WHERE flow.path = $1 AND flow.workspace_id = $2" + ) + .bind(path) + .bind(w_id) + .bind(&authed.username) + .fetch_optional(&mut *tx) + .await? + } else { + sqlx::query_as::<_, Flow>( + "SELECT flow.workspace_id, flow.path, flow.summary, flow.description, flow.archived, flow.extra_perms, flow.draft_only, flow.dedicated_worker, flow.tag, flow.ws_error_handler_muted, flow.timeout, flow.visible_to_runner_only, flow_version.schema, flow_version.value, flow_version.created_at as edited_at, flow_version.created_by as edited_by, NULL as starred FROM flow LEFT JOIN flow_version ON flow_version.id = flow.versions[array_upper(flow.versions, 1)] WHERE flow.path = $1 AND flow.workspace_id = $2" @@ -890,7 +909,8 @@ async fn get_flow_by_path( .bind(path) .bind(w_id) .fetch_optional(&mut *tx) - .await?; + .await? + }; tx.commit().await?; let flow = not_found_if_none(flow_o, "Flow", path)?; diff --git a/backend/windmill-api/src/scripts.rs b/backend/windmill-api/src/scripts.rs index 41214dfcbb..9a08d8d5fd 100644 --- a/backend/windmill-api/src/scripts.rs +++ b/backend/windmill-api/src/scripts.rs @@ -10,6 +10,7 @@ use crate::{ db::{ApiAuthed, DB}, schedule::clear_schedule, users::{maybe_refresh_folders, require_owner_of_path, AuthCache}, + utils::WithStarredInfoQuery, webhook_util::{WebhookMessage, WebhookShared}, HTTP_CLIENT, }; @@ -526,7 +527,7 @@ async fn create_script_internal<'c>( ))); }; - let ps = get_script_by_hash_internal(tx.transaction_mut(), &w_id, p_hash).await?; + let ps = get_script_by_hash_internal(tx.transaction_mut(), &w_id, p_hash, None).await?; if ps.path != ns.path { require_owner_of_path(&authed, &ps.path)?; @@ -826,19 +827,40 @@ async fn get_script_by_path( authed: ApiAuthed, Extension(user_db): Extension, Path((w_id, path)): Path<(String, StripPath)>, + Query(query): Query, ) -> JsonResult