From ea440235c4b4ba4d88c534671320527bb9ee2f1c Mon Sep 17 00:00:00 2001 From: mbecker20 Date: Fri, 10 May 2024 21:54:41 -0700 Subject: [PATCH] configure enable / disable action on webhook recieve --- bin/core/src/listener/github.rs | 17 ++- bin/migrator/src/legacy/v0/build.rs | 1 + client/core/rs/src/entities/build.rs | 10 ++ client/core/rs/src/entities/procedure.rs | 15 ++- client/core/rs/src/entities/repo.rs | 10 ++ client/core/ts/src/types.ts | 8 +- .../src/components/resources/build/config.tsx | 1 + .../components/resources/procedure/config.tsx | 122 ++++++++++-------- .../src/components/resources/repo/config.tsx | 1 + 9 files changed, 127 insertions(+), 58 deletions(-) diff --git a/bin/core/src/listener/github.rs b/bin/core/src/listener/github.rs index 739a26dc0..36e20476e 100644 --- a/bin/core/src/listener/github.rs +++ b/bin/core/src/listener/github.rs @@ -6,7 +6,9 @@ use hex::ToHex; use hmac::{Hmac, Mac}; use monitor_client::{ api::execute, - entities::{build::Build, repo::Repo, user::github_user}, + entities::{ + build::Build, procedure::Procedure, repo::Repo, user::github_user, + }, }; use resolver_api::Resolve; use serde::Deserialize; @@ -129,6 +131,9 @@ async fn handle_build_webhook( verify_gh_signature(headers, &body).await?; let request_branch = extract_branch(&body)?; let build = resource::get::(&build_id).await?; + if !build.config.webhook_enabled { + return Err(anyhow!("build does not have webhook enabled")); + } if request_branch != build.config.branch { return Err(anyhow!("request branch does not match expected")); } @@ -155,6 +160,9 @@ async fn handle_repo_clone_webhook( verify_gh_signature(headers, &body).await?; let request_branch = extract_branch(&body)?; let repo = resource::get::(&repo_id).await?; + if !repo.config.webhook_enabled { + return Err(anyhow!("repo does not have webhook enabled")); + } if request_branch != repo.config.branch { return Err(anyhow!("request branch does not match expected")); } @@ -181,6 +189,9 @@ async fn handle_repo_pull_webhook( verify_gh_signature(headers, &body).await?; let request_branch = extract_branch(&body)?; let repo = resource::get::(&repo_id).await?; + if !repo.config.webhook_enabled { + return Err(anyhow!("repo does not have webhook enabled")); + } if request_branch != repo.config.branch { return Err(anyhow!("request branch does not match expected")); } @@ -211,6 +222,10 @@ async fn handle_procedure_webhook( if request_branch != target_branch { return Err(anyhow!("request branch does not match expected")); } + let procedure = resource::get::(&procedure_id).await?; + if !procedure.config.webhook_enabled { + return Err(anyhow!("procedure does not have webhook enabled")); + } State .resolve( execute::RunProcedure { diff --git a/bin/migrator/src/legacy/v0/build.rs b/bin/migrator/src/legacy/v0/build.rs index 6e797ffe4..28f867fb7 100644 --- a/bin/migrator/src/legacy/v0/build.rs +++ b/bin/migrator/src/legacy/v0/build.rs @@ -230,6 +230,7 @@ impl TryFrom for monitor_client::entities::build::Build { extra_args, use_buildx, labels: Default::default(), + webhook_enabled: true, }, }; Ok(build) diff --git a/client/core/rs/src/entities/build.rs b/client/core/rs/src/entities/build.rs index a9e28260c..542ae320f 100644 --- a/client/core/rs/src/entities/build.rs +++ b/client/core/rs/src/entities/build.rs @@ -128,6 +128,12 @@ pub struct BuildConfig { #[serde(default)] #[builder(default)] pub use_buildx: bool, + + /// Whether incoming webhooks actually trigger action. + #[serde(default = "default_webhook_enabled")] + #[builder(default = "default_webhook_enabled()")] + #[partial_default(default_webhook_enabled())] + pub webhook_enabled: bool, } impl BuildConfig { @@ -148,6 +154,10 @@ fn default_dockerfile_path() -> String { String::from("Dockerfile") } +fn default_webhook_enabled() -> bool { + true +} + #[typeshare] #[derive(Debug, Clone, Copy, Default, Serialize, Deserialize)] pub struct BuildActionState { diff --git a/client/core/rs/src/entities/procedure.rs b/client/core/rs/src/entities/procedure.rs index 032659ea3..7c5375c7f 100644 --- a/client/core/rs/src/entities/procedure.rs +++ b/client/core/rs/src/entities/procedure.rs @@ -1,3 +1,4 @@ +use derive_builder::Builder; use derive_default_builder::DefaultBuilder; use mungos::mongodb::bson::{doc, Document}; use partial_derive2::Partial; @@ -29,16 +30,28 @@ pub type Procedure = Resource; pub type _PartialProcedureConfig = PartialProcedureConfig; #[typeshare] -#[derive(Debug, Clone, Default, Serialize, Deserialize, Partial)] +#[derive(Debug, Clone, Default, Serialize, Deserialize, Partial, Builder)] #[partial_derive(Debug, Clone, Default, Serialize, Deserialize)] #[partial(skip_serializing_none, from, diff)] pub struct ProcedureConfig { /// Whether executions in the procedure runs sequentially or in parallel. #[serde(default)] + #[builder(default)] pub procedure_type: ProcedureType, /// The executions to be run by the procedure. #[serde(default)] + #[builder(default)] pub executions: Vec, + + /// Whether incoming webhooks actually trigger action. + #[serde(default = "default_webhook_enabled")] + #[builder(default = "default_webhook_enabled()")] + #[partial_default(default_webhook_enabled())] + pub webhook_enabled: bool, +} + +fn default_webhook_enabled() -> bool { + true } #[typeshare] diff --git a/client/core/rs/src/entities/repo.rs b/client/core/rs/src/entities/repo.rs index 8f4028fcf..a230ccadb 100644 --- a/client/core/rs/src/entities/repo.rs +++ b/client/core/rs/src/entities/repo.rs @@ -74,6 +74,12 @@ pub struct RepoConfig { #[serde(default)] #[builder(default)] pub on_pull: SystemCommand, + + /// Whether incoming webhooks actually trigger action. + #[serde(default = "default_webhook_enabled")] + #[builder(default = "default_webhook_enabled()")] + #[partial_default(default_webhook_enabled())] + pub webhook_enabled: bool, } impl RepoConfig { @@ -86,6 +92,10 @@ fn default_branch() -> String { String::from("main") } +fn default_webhook_enabled() -> bool { + true +} + #[typeshare] #[derive(Serialize, Deserialize, Debug, Clone, Copy, Default)] pub struct RepoActionState { diff --git a/client/core/ts/src/types.ts b/client/core/ts/src/types.ts index bde18c1df..335e80cac 100644 --- a/client/core/ts/src/types.ts +++ b/client/core/ts/src/types.ts @@ -1,5 +1,5 @@ /* - Generated by typeshare 1.7.0 + Generated by typeshare 1.9.2 */ /** JSON containing an authentication token. */ @@ -317,6 +317,8 @@ export interface BuildConfig { extra_args?: string[]; /** Whether to use buildx to build (eg `docker buildx build ...`) */ use_buildx?: boolean; + /** Whether incoming webhooks actually trigger action. */ + webhook_enabled: boolean; } export interface BuildInfo { @@ -628,6 +630,8 @@ export interface ProcedureConfig { procedure_type?: ProcedureType; /** The executions to be run by the procedure. */ executions?: EnabledExecution[]; + /** Whether incoming webhooks actually trigger action. */ + webhook_enabled: boolean; } export type Procedure = Resource; @@ -670,6 +674,8 @@ export interface RepoConfig { * The path is relative to the root of the repo. */ on_pull?: SystemCommand; + /** Whether incoming webhooks actually trigger action. */ + webhook_enabled: boolean; } export interface RepoInfo { diff --git a/frontend/src/components/resources/build/config.tsx b/frontend/src/components/resources/build/config.tsx index c9dff1a43..aa5801354 100644 --- a/frontend/src/components/resources/build/config.tsx +++ b/frontend/src/components/resources/build/config.tsx @@ -161,6 +161,7 @@ export const BuildConfig = ({ id }: { id: string }) => { ), + webhook_enabled: true, }, }, "Build Args": { diff --git a/frontend/src/components/resources/procedure/config.tsx b/frontend/src/components/resources/procedure/config.tsx index d4083ea72..15bde58da 100644 --- a/frontend/src/components/resources/procedure/config.tsx +++ b/frontend/src/components/resources/procedure/config.tsx @@ -210,6 +210,57 @@ const ProcedureConfigInner = ({ { header: "Modify", cell: ({ row }) => { + const on_move_up = () => + setConfig({ + ...config, + executions: executions.map((item, i) => { + // Make sure its not the first row + if (i === row.index && row.index !== 0) { + return executions[row.index - 1]; + } else if (i === row.index - 1) { + // Reverse the entry, moving this row "Up" + return executions[row.index]; + } else { + return item; + } + }), + }); + const on_move_down = () => + setConfig({ + ...config, + executions: executions.map((item, i) => { + // The index also cannot be the last index, which cannot be moved down + if ( + i === row.index && + row.index !== executions.length - 1 + ) { + return executions[row.index + 1]; + } else if (i === row.index + 1) { + // Move the row "Down" + return executions[row.index]; + } else { + return item; + } + }), + }); + const on_insert_above = () => + setConfig({ + ...config, + executions: [ + ...executions.slice(0, row.index), + default_enabled_execution(), + ...executions.slice(row.index), + ], + }); + const on_insert_below = () => + setConfig({ + ...config, + executions: [ + ...executions.slice(0, row.index + 1), + default_enabled_execution(), + ...executions.slice(row.index + 1), + ], + }); return ( @@ -228,22 +279,7 @@ const ProcedureConfigInner = ({ {row.index ? ( - setConfig({ - ...config, - executions: executions.map((item, i) => { - // Make sure its not the first row - if (i === row.index && row.index !== 0) { - return executions[row.index - 1]; - } else if (i === row.index - 1) { - // Reverse the entry, moving this row "Up" - return executions[row.index]; - } else { - return item; - } - }), - }) - } + onClick={on_move_up} > Move Up @@ -251,25 +287,7 @@ const ProcedureConfigInner = ({ {row.index < executions.length - 1 && ( - setConfig({ - ...config, - executions: executions.map((item, i) => { - // The index also cannot be the last index, which cannot be moved down - if ( - i === row.index && - row.index !== executions.length - 1 - ) { - return executions[row.index + 1]; - } else if (i === row.index + 1) { - // Move the row "Down" - return executions[row.index]; - } else { - return item; - } - }), - }) - } + onClick={on_move_down} > Move Down @@ -277,16 +295,7 @@ const ProcedureConfigInner = ({ - setConfig({ - ...config, - executions: [ - ...executions.slice(0, row.index), - default_enabled_execution(), - ...executions.slice(row.index), - ], - }) - } + onClick={on_insert_above} > Insert Above{" "}
@@ -296,16 +305,7 @@ const ProcedureConfigInner = ({ - setConfig({ - ...config, - executions: [ - ...executions.slice(0, row.index + 1), - default_enabled_execution(), - ...executions.slice(row.index + 1), - ], - }) - } + onClick={on_insert_below} > Insert Below{" "}
@@ -356,6 +356,18 @@ const ProcedureConfigInner = ({ +
+
+ Enabled: +
+ + setConfig({ ...config, webhook_enabled }) + } + disabled={disabled} + /> +
diff --git a/frontend/src/components/resources/repo/config.tsx b/frontend/src/components/resources/repo/config.tsx index fbfc424f6..0c13b3b43 100644 --- a/frontend/src/components/resources/repo/config.tsx +++ b/frontend/src/components/resources/repo/config.tsx @@ -83,6 +83,7 @@ export const RepoConfig = ({ id }: { id: string }) => { ), + webhook_enabled: true, }, }, }}