Skip to content

Commit

Permalink
Make deployment webhook only execute for actual deployments
Browse files Browse the repository at this point in the history
Maybe fix the actual message content?
  • Loading branch information
valentinegb committed Apr 25, 2024
1 parent 95b6cba commit 81c1577
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 34 deletions.
25 changes: 13 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ publish = false
[dependencies]
anyhow = "1.0.68"
poise = "0.6.1"
shuttle-runtime = "0.43.0"
shuttle-runtime = "0.44.0"
# Since poise is a serenity command framework, it can run on Shuttle with shuttle-serenity
shuttle-serenity = "0.43.0"
shuttle-serenity = "0.44.0"
tracing = "0.1.37"
tokio = "1.26.0"
rand = "0.8.5"
shuttle-common = { version = "0.44.0", features = ["service"] }
46 changes: 26 additions & 20 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ use anyhow::Context as _;
use poise::serenity_prelude::{
ClientBuilder, ExecuteWebhook, FullEvent, GatewayIntents, GuildId, UserId, Webhook,
};
use shuttle_runtime::SecretStore;
use shuttle_common::deployment;
use shuttle_runtime::{DeploymentMetadata, SecretStore};
use shuttle_serenity::ShuttleSerenity;
use tracing::info;

Expand Down Expand Up @@ -123,7 +124,10 @@ impl fmt::Display for FloofEmoji {
}

#[shuttle_runtime::main]
async fn main(#[shuttle_runtime::Secrets] secret_store: SecretStore) -> ShuttleSerenity {
async fn main(
#[shuttle_runtime::Secrets] secret_store: SecretStore,
#[shuttle_runtime::Metadata] metadata: DeploymentMetadata,
) -> ShuttleSerenity {
// Get the discord token set in `Secrets.toml`
let discord_token = secret_store
.get("DISCORD_TOKEN")
Expand All @@ -134,9 +138,6 @@ async fn main(#[shuttle_runtime::Secrets] secret_store: SecretStore) -> ShuttleS
let collective_webhook_url = secret_store
.get("COLLECTIVE_WEBHOOK_URL")
.context("'COLLECTIVE_WEBHOOK_URL' was not found")?;
let deployment_webhook_url = secret_store
.get("DEPLOYMENT_WEBHOOK_URL")
.context("'COLLECTIVE_WEBHOOK_URL' was not found")?;
let framework = poise::Framework::builder()
.options(poise::FrameworkOptions {
commands: vec![
Expand Down Expand Up @@ -205,21 +206,26 @@ async fn main(#[shuttle_runtime::Secrets] secret_store: SecretStore) -> ShuttleS

info!("Constructed client");

let deployment_webhook = Webhook::from_url(&client.http, &deployment_webhook_url)
.await
.map_err(shuttle_runtime::CustomError::new)?;

deployment_webhook
.execute(
&client.http,
false,
ExecuteWebhook::new().content(format!(
"Commit `{}` deployed",
env::var("shuttle_static_rev").map_err(shuttle_runtime::CustomError::new)?,
)),
)
.await
.map_err(shuttle_runtime::CustomError::new)?;
if metadata.env == deployment::Environment::Deployment {
let deployment_webhook_url = secret_store
.get("DEPLOYMENT_WEBHOOK_URL")
.context("'COLLECTIVE_WEBHOOK_URL' was not found")?;
let deployment_webhook = Webhook::from_url(&client.http, &deployment_webhook_url)
.await
.map_err(shuttle_runtime::CustomError::new)?;

deployment_webhook
.execute(
&client.http,
false,
ExecuteWebhook::new().content(format!(
"Commit `{}` deployed",
env::var("SHUTTLE_STATIC_REV").map_err(shuttle_runtime::CustomError::new)?,
)),
)
.await
.map_err(shuttle_runtime::CustomError::new)?;
}

Ok(client.into())
}

0 comments on commit 81c1577

Please sign in to comment.