feat: workspace error handler (#1799)

* feat: add workspace error handler

* feat: run error handler as group

* fix: handler picker initial path

* fix(backend): separate global / workspace handlers

* fix(frontend): error handler picker tab change

---------

Co-authored-by: Ruben Fiszel <ruben@windmill.dev>
This commit is contained in:
HugoCasa
2023-07-06 21:43:15 +02:00
committed by GitHub
co-authored by Ruben Fiszel
parent 3690dd5bd1
commit 54cd5ce569
9 changed files with 361 additions and 54 deletions
+65 -1
View File
@@ -34,6 +34,7 @@ use magic_crypt::MagicCryptTrait;
#[cfg(feature = "enterprise")]
use stripe::CustomerId;
use windmill_audit::{audit_log, ActionKind};
use windmill_common::users::username_to_permissioned_as;
use windmill_common::{
error::{to_anyhow, Error, JsonResult, Result},
flows::Flow,
@@ -64,7 +65,8 @@ pub fn workspaced_service() -> Router {
.route("/edit_auto_invite", post(edit_auto_invite))
.route("/edit_deploy_to", post(edit_deploy_to))
.route("/tarball", get(tarball_workspace))
.route("/premium_info", get(premium_info));
.route("/premium_info", get(premium_info))
.route("/edit_error_handler", post(edit_error_handler));
#[cfg(feature = "enterprise")]
tracing::info!("stripe enabled");
@@ -111,6 +113,7 @@ pub struct WorkspaceSettings {
pub plan: Option<String>,
pub webhook: Option<String>,
pub deploy_to: Option<String>,
pub error_handler: Option<String>,
}
#[derive(FromRow, Serialize, Debug)]
@@ -202,6 +205,11 @@ pub struct NewWorkspaceUser {
pub operator: bool,
}
#[derive(Deserialize)]
pub struct EditErrorHandler {
pub error_handler: Option<String>,
}
async fn list_pending_invites(
authed: Authed,
Extension(user_db): Extension<UserDB>,
@@ -644,6 +652,62 @@ async fn edit_webhook(
Ok(format!("Edit webhook for workspace {}", &w_id))
}
async fn edit_error_handler(
authed: Authed,
Extension(db): Extension<DB>,
Path(w_id): Path<String>,
Authed { is_admin, username, .. }: Authed,
Json(ee): Json<EditErrorHandler>,
) -> Result<String> {
require_admin(is_admin, &username)?;
let mut tx = db.begin().await?;
sqlx::query_as!(
Group,
"INSERT INTO group_ (workspace_id, name, summary, extra_perms) VALUES ($1, $2, $3, $4) ON CONFLICT DO NOTHING",
w_id,
"error_handler",
"The group the error handler acts on belhalf of",
serde_json::json!({username_to_permissioned_as(&authed.username): true})
)
.execute(&mut tx)
.await?;
if let Some(error_handler) = &ee.error_handler {
sqlx::query!(
"UPDATE workspace_settings SET error_handler = $1 WHERE workspace_id = $2",
error_handler,
&w_id
)
.execute(&mut tx)
.await?;
} else {
sqlx::query!(
"UPDATE workspace_settings SET error_handler = NULL WHERE workspace_id = $1",
&w_id,
)
.execute(&mut tx)
.await?;
}
audit_log(
&mut tx,
&authed.username,
"workspaces.edit_error_handler",
ActionKind::Update,
&w_id,
Some(&authed.email),
Some([("error_handler", &format!("{:?}", ee.error_handler)[..])].into()),
)
.await?;
tx.commit().await?;
Ok(format!("Edit error_handler for workspace {}", &w_id))
}
async fn list_workspaces_as_super_admin(
authed: Authed,
Extension(user_db): Extension<UserDB>,