configure enable / disable action on webhook recieve

This commit is contained in:
mbecker20
2024-05-10 21:54:41 -07:00
parent f9949bf988
commit ea440235c4
9 changed files with 127 additions and 58 deletions

View File

@@ -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>(&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>(&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>(&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>(&procedure_id).await?;
if !procedure.config.webhook_enabled {
return Err(anyhow!("procedure does not have webhook enabled"));
}
State
.resolve(
execute::RunProcedure {

View File

@@ -230,6 +230,7 @@ impl TryFrom<Build> for monitor_client::entities::build::Build {
extra_args,
use_buildx,
labels: Default::default(),
webhook_enabled: true,
},
};
Ok(build)

View File

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

View File

@@ -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<ProcedureConfig, ()>;
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<EnabledExecution>,
/// 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]

View File

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

View File

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

View File

@@ -161,6 +161,7 @@ export const BuildConfig = ({ id }: { id: string }) => {
<CopyGithubWebhook path={`/build/${id}`} />
</ConfigItem>
),
webhook_enabled: true,
},
},
"Build Args": {

View File

@@ -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 (
<DropdownMenu>
<DropdownMenuTrigger asChild disabled={disabled}>
@@ -228,22 +279,7 @@ const ProcedureConfigInner = ({
{row.index ? (
<DropdownMenuItem
className="flex gap-4 justify-between cursor-pointer"
onClick={() =>
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 <ArrowUp className="w-4 h-4" />
</DropdownMenuItem>
@@ -251,25 +287,7 @@ const ProcedureConfigInner = ({
{row.index < executions.length - 1 && (
<DropdownMenuItem
className="flex gap-4 justify-between cursor-pointer"
onClick={() =>
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 <ArrowDown className="w-4 h-4" />
</DropdownMenuItem>
@@ -277,16 +295,7 @@ const ProcedureConfigInner = ({
<DropdownMenuSeparator />
<DropdownMenuItem
className="flex gap-4 justify-between cursor-pointer"
onClick={() =>
setConfig({
...config,
executions: [
...executions.slice(0, row.index),
default_enabled_execution(),
...executions.slice(row.index),
],
})
}
onClick={on_insert_above}
>
Insert Above{" "}
<div className="flex">
@@ -296,16 +305,7 @@ const ProcedureConfigInner = ({
</DropdownMenuItem>
<DropdownMenuItem
className="flex gap-4 justify-between cursor-pointer"
onClick={() =>
setConfig({
...config,
executions: [
...executions.slice(0, row.index + 1),
default_enabled_execution(),
...executions.slice(row.index + 1),
],
})
}
onClick={on_insert_below}
>
Insert Below{" "}
<div className="flex">
@@ -356,6 +356,18 @@ const ProcedureConfigInner = ({
<CopyGithubWebhook
path={`/procedure/${procedure._id?.$oid!}/${branch}`}
/>
<div className="flex items-center justify-end gap-4 w-full">
<div className="text-muted-foreground">
Enabled:
</div>
<Switch
checked={procedure.config.webhook_enabled}
onCheckedChange={(webhook_enabled) =>
setConfig({ ...config, webhook_enabled })
}
disabled={disabled}
/>
</div>
</div>
</ConfigItem>
</CardHeader>

View File

@@ -83,6 +83,7 @@ export const RepoConfig = ({ id }: { id: string }) => {
<CopyGithubWebhook path={`/repo/${id}/pull`} />
</ConfigItem>
),
webhook_enabled: true,
},
},
}}