Compare commits

...

2 commits

Author SHA1 Message Date
31859a0c69 WIP: post webhook - apply some to previous commits
All checks were successful
ci/woodpecker/push/push-next Pipeline was successful
ci/woodpecker/push/tag-created Pipeline was successful
ci/woodpecker/push/cron-docker-builder Pipeline was successful
2024-07-21 20:22:42 +01:00
731c22826c WIP: feat: post webhook to user 2024-07-21 19:43:56 +01:00
17 changed files with 185 additions and 27 deletions

View file

@ -78,6 +78,8 @@ git-conventional = "0.12"
bytes = "1.6"
ulid = "1.1"
warp = "0.3"
time = "0.3"
standardwebhooks = "1.0"
# boilerplate
derive_more = { version = "1.0.0-beta.6", features = [

View file

@ -1 +1,3 @@
crate::newtype!(BranchName: String, derive_more::Display, Default: "The name of a Git branch");
use serde::Serialize;
crate::newtype!(BranchName: String, derive_more::Display, Default, Serialize: "The name of a Git branch");

View file

@ -1,4 +1,6 @@
crate::newtype!(ForgeAlias: String, Hash, PartialOrd, Ord, derive_more::Display, Default: "The name of a Forge to connect to");
use serde::Serialize;
crate::newtype!(ForgeAlias: String, Hash, PartialOrd, Ord, derive_more::Display, Default, Serialize: "The name of a Forge to connect to");
impl From<&ForgeAlias> for std::path::PathBuf {
fn from(value: &ForgeAlias) -> Self {
Self::from(&value.0)

View file

@ -1,7 +1,8 @@
use derive_more::Display;
use serde::Serialize;
use crate::newtype;
newtype!(RepoAlias: String, Display, Default, PartialOrd, Ord: r#"The alias of a repo.
newtype!(RepoAlias: String, Display, Default, PartialOrd, Ord, Serialize: r#"The alias of a repo.
This is the alias for the repo within `git-next-server.toml`."#);

View file

@ -8,6 +8,7 @@ use std::{
};
use kxio::fs::FileSystem;
use secrecy::Secret;
use tracing::info;
use crate::{newtype, ForgeAlias, ForgeConfig, RepoAlias};
@ -42,7 +43,7 @@ type Result<T> = core::result::Result<T, Error>;
pub struct ServerConfig {
http: Http,
webhook: InboundWebhook,
notifications: Notification,
notification: Notification,
storage: ServerStorage,
pub forge: BTreeMap<String, ForgeConfig>,
}
@ -65,11 +66,11 @@ impl ServerConfig {
&self.storage
}
pub const fn notifications(&self) -> &Notification {
&self.notifications
pub const fn notification(&self) -> &Notification {
&self.notification
}
pub const fn webhook(&self) -> &InboundWebhook {
pub const fn inbound_webhook(&self) -> &InboundWebhook {
&self.webhook
}
@ -205,9 +206,17 @@ impl Notification {
}
}
pub const fn r#type(&self) -> NotificationType {
self.r#type
}
pub fn webhook_url(&self) -> Option<String> {
self.webhook.clone().map(|x| x.url)
}
pub fn webhook_secret(&self) -> Option<Secret<String>> {
self.webhook.clone().map(|x| x.secret).map(Secret::new)
}
}
impl Default for Notification {
fn default() -> Self {
@ -223,10 +232,10 @@ impl Default for Notification {
Eq,
PartialOrd,
Ord,
derive_more::AsRef,
serde::Deserialize,
derive_more::Constructor,
)]
pub struct OutboundWebhook {
url: String,
secret: String,
}

View file

@ -468,10 +468,10 @@ mod server {
let http = &server_config.http()?;
let http_addr = http.ip();
let http_port = server_config.http()?.port();
let webhook_url = server_config.webhook().base_url();
let webhook_url = server_config.inbound_webhook().base_url();
let storage_path = server_config.storage().path();
let notification_webhook_url = server_config
.notifications()
.notification()
.webhook_url()
.unwrap_or_default();
let forge_alias = server_config

View file

@ -25,11 +25,13 @@ async-trait = { workspace = true }
# fs/network
kxio = { workspace = true }
# # TOML parsing
# serde = { workspace = true }
# TOML parsing
serde_json = { workspace = true }
# toml = { workspace = true }
# webhooks - user notification
serde = { workspace = true }
# Secrets and Password
secrecy = { workspace = true }

View file

@ -2,6 +2,7 @@ use config::newtype;
use derive_more::Display;
//
use git_next_config as config;
use serde::Serialize;
#[derive(
Clone,
@ -13,6 +14,7 @@ use git_next_config as config;
Ord,
derive_more::Constructor,
derive_more::Display,
Serialize,
)]
#[display("{}", sha)]
pub struct Commit {
@ -37,8 +39,8 @@ impl From<config::webhook::Push> for Commit {
}
}
newtype!(Sha: String, Display, Hash,PartialOrd, Ord: "The unique SHA for a git commit.");
newtype!(Message: String, Hash, PartialOrd, Ord: "The commit message for a git commit.");
newtype!(Sha: String, Display, Hash,PartialOrd, Ord, Serialize: "The unique SHA for a git commit.");
newtype!(Message: String, Display, Hash, PartialOrd, Ord, Serialize: "The commit message for a git commit.");
#[derive(Clone, Debug)]
pub struct Histories {

View file

@ -43,6 +43,7 @@ git-conventional = { workspace = true }
# Webhooks
bytes = { workspace = true }
ulid = { workspace = true }
time = { workspace = true }
# boilerplate
derive_more = { workspace = true }

View file

@ -2,6 +2,7 @@ mod branch;
pub mod handlers;
mod load;
pub mod messages;
mod notifications;
#[cfg(test)]
mod tests;

View file

@ -0,0 +1,72 @@
use derive_more::Deref as _;
use git_next_git::UserNotification;
use serde_json::json;
use crate::messages::NotifyUser;
impl NotifyUser {
pub fn as_json(self, timestamp: time::OffsetDateTime) -> serde_json::Value {
let timestamp = timestamp.unix_timestamp().to_string();
match self.deref() {
UserNotification::CICheckFailed {
forge_alias,
repo_alias,
commit,
} => json!({
"type": "cicheck.failed",
"timestamp": timestamp,
"data": {
"forge_alias": forge_alias,
"repo_alias": repo_alias,
"commit": {
"sha": commit.sha(),
"message": commit.message()
}
}
}),
UserNotification::RepoConfigLoadFailure {
forge_alias,
repo_alias,
reason,
} => json!({
"type": "config.load.failed",
"timestamp": timestamp,
"data": {
"forge_alias": forge_alias,
"repo_alias": repo_alias,
"reason": reason
}
}),
UserNotification::WebhookRegistration {
forge_alias,
repo_alias,
reason,
} => json!({
"type": "webhook.registration.failed",
"timestamp": timestamp,
"data": {
"forge_alias": forge_alias,
"repo_alias": repo_alias,
"reason": reason
}
}),
UserNotification::DevNotBasedOnMain {
forge_alias,
repo_alias,
dev_branch,
main_branch,
} => json!({
"type": "branch.dev.not-on-main",
"timestamp": timestamp,
"data": {
"forge_alias": forge_alias,
"repo_alias": repo_alias,
"branches": {
"dev": dev_branch,
"main": main_branch
}
}
}),
}
}
}

View file

@ -28,6 +28,14 @@ derive-with = { workspace = true }
# Actors
actix = { workspace = true }
# Webhooks
serde = { workspace = true }
serde_json = { workspace = true }
ulid = { workspace = true }
time = { workspace = true }
secrecy = { workspace = true }
standardwebhooks = { workspace = true }
[dev-dependencies]
# Testing
# assert2 = { workspace = true }

View file

@ -1,5 +1,7 @@
//
use actix::prelude::*;
use git_next_config::server::NotificationType;
use git_next_repo_actor::messages::NotifyUser;
use crate::ServerActor;
@ -7,8 +9,32 @@ use crate::ServerActor;
impl Handler<NotifyUser> for ServerActor {
type Result = ();
fn handle(&mut self, _msg: NotifyUser, _ctx: &mut Self::Context) -> Self::Result {
fn handle(&mut self, msg: NotifyUser, _ctx: &mut Self::Context) -> Self::Result {
let Some(server_config) = &self.server_config else {
return;
};
let notification_type = server_config.notification().r#type();
match notification_type {
NotificationType::None => { /* do nothing */ }
NotificationType::Webhook => {
let message_id = format!("msg_{}", ulid::Ulid::new());
let timestamp = time::OffsetDateTime::now_utc(); //.unix_timestamp().to_string();
let payload = msg.as_json(timestamp).to_string();
let timestamp = timestamp.unix_timestamp();
let to_sign = format!("{message_id}.{timestamp}.{payload}");
tracing::info!(?to_sign, "");
let Some(webhook) = self.webhook.as_ref() else {
tracing::warn!("Invalid notification configuration - can't sent notification");
return;
};
#[allow(clippy::expect_used)]
let signature = webhook
.sign(&message_id, timestamp, payload.as_ref())
.expect("signature");
tracing::info!(?signature, "");
// TODO: (#95) should notify user
// send post to notification webhook url
}
}
}
}

View file

@ -21,7 +21,7 @@ impl Handler<ReceiveServerConfig> for ServerActor {
return;
};
if msg.webhook().base_url().ends_with('/') {
if msg.inbound_webhook().base_url().ends_with('/') {
tracing::error!("webhook.url must not end with a '/'");
return;
}

View file

@ -1,5 +1,7 @@
use actix::prelude::*;
use git_next_config::server::NotificationType;
use git_next_webhook_actor::{AddWebhookRecipient, ShutdownWebhook, WebhookActor, WebhookRouter};
use standardwebhooks::Webhook;
use crate::{
messages::{ReceiveValidServerConfig, ValidServerConfig},
@ -15,14 +17,35 @@ impl Handler<ReceiveValidServerConfig> for ServerActor {
socket_address,
server_storage,
} = msg.unwrap();
if let Some(webhook) = self.webhook.take() {
webhook.do_send(ShutdownWebhook);
// shutdown any existing webhook actor
if let Some(webhook_actor_addr) = self.webhook_actor_addr.take() {
webhook_actor_addr.do_send(ShutdownWebhook);
}
match server_config.notification().r#type() {
NotificationType::None => { /* do nothing */ }
NotificationType::Webhook => {
// Create webhook signer
use secrecy::ExposeSecret;
let webhook = server_config
.notification()
.webhook_secret()
.map(|secret| Webhook::new(secret.expose_secret()))
.transpose()
.map_err(|e| {
tracing::error!(
"Invalid notification webhook secret (will not send notifications): {e}"
)
})
.ok()
.flatten();
self.webhook = webhook;
}
}
self.generation.inc();
// Webhook Server
tracing::info!("Starting Webhook Server...");
let webhook_router = WebhookRouter::default().start();
let webhook = server_config.webhook();
let inbound_webhook = server_config.inbound_webhook();
// Forge Actors
for (forge_alias, forge_config) in server_config.forges() {
let repo_actors = self
@ -30,7 +53,7 @@ impl Handler<ReceiveValidServerConfig> for ServerActor {
forge_config,
forge_alias.clone(),
&server_storage,
webhook,
inbound_webhook,
ctx.address().recipient(),
)
.into_iter()
@ -51,7 +74,9 @@ impl Handler<ReceiveValidServerConfig> for ServerActor {
.insert((forge_alias.clone(), repo_alias), addr);
});
}
let webhook = WebhookActor::new(socket_address, webhook_router.recipient()).start();
self.webhook.replace(webhook);
let webhook_actor_addr =
WebhookActor::new(socket_address, webhook_router.recipient()).start();
self.webhook_actor_addr.replace(webhook_actor_addr);
self.server_config.replace(server_config);
}
}

View file

@ -16,7 +16,7 @@ impl Handler<Shutdown> for ServerActor {
tracing::debug!(%forge_alias, %repo_alias, "removed webhook");
});
tracing::debug!("server shutdown");
if let Some(webhook) = self.webhook.take() {
if let Some(webhook) = self.webhook_actor_addr.take() {
tracing::debug!("shuting down webhook");
webhook.do_send(ShutdownWebhook);
tracing::debug!("webhook shutdown");

View file

@ -14,6 +14,7 @@ use git_next_repo_actor::messages::NotifyUser;
use git_next_repo_actor::{messages::CloneRepo, RepoActor};
use git_next_webhook_actor as webhook;
use kxio::{fs::FileSystem, network::Network};
use standardwebhooks::Webhook;
use std::{
collections::BTreeMap,
path::PathBuf,
@ -45,13 +46,15 @@ type Result<T> = core::result::Result<T, Error>;
#[derive(derive_with::With)]
#[with(message_log)]
pub struct ServerActor {
server_config: Option<ServerConfig>,
generation: Generation,
webhook: Option<Addr<WebhookActor>>,
webhook_actor_addr: Option<Addr<WebhookActor>>,
fs: FileSystem,
net: Network,
repository_factory: Box<dyn RepositoryFactory>,
sleep_duration: std::time::Duration,
repo_actors: BTreeMap<(ForgeAlias, RepoAlias), Addr<RepoActor>>,
webhook: Option<Webhook>,
// testing
message_log: Option<Arc<RwLock<Vec<String>>>>,
@ -69,13 +72,15 @@ impl ServerActor {
) -> Self {
let generation = Generation::default();
Self {
server_config: None,
generation,
webhook: None,
webhook_actor_addr: None,
fs,
net,
repository_factory: repo,
sleep_duration,
repo_actors: BTreeMap::new(),
webhook: None,
message_log: None,
}
}