git-next/crates/repo-actor/src/webhook.rs
Paul Campbell 46b6d8680c
All checks were successful
Rust / build (push) Successful in 1m47s
ci/woodpecker/push/cron-docker-builder Pipeline was successful
ci/woodpecker/push/push-next Pipeline was successful
ci/woodpecker/push/tag-created Pipeline was successful
feat: Add support for GitHub
This doesn't include GitHub Enterprise

Closes kemitix/git-next#86
2024-05-31 07:23:48 +01:00

93 lines
3.4 KiB
Rust

//
use actix::prelude::*;
use crate::{RepoActor, ValidateRepo};
use git_next_config as config;
use git_next_git as git;
use tracing::{info, warn};
impl Handler<config::WebhookMessage> for RepoActor {
type Result = ();
#[allow(clippy::cognitive_complexity)] // TODO: (#49) reduce complexity
#[tracing::instrument(name = "RepoActor::WebhookMessage", skip_all, fields(token = %self.message_token, repo = %self.repo_details))]
fn handle(&mut self, msg: config::WebhookMessage, ctx: &mut Self::Context) -> Self::Result {
let Some(expected_authorization) = &self.webhook_auth else {
warn!("Don't know what authorization to expect");
return;
};
let Some(config) = &self.repo_details.repo_config else {
warn!("No repo config");
return;
};
if !self
.forge
.is_message_authorised(&msg, expected_authorization)
{
warn!(
"Invalid authorization - expected {}",
expected_authorization
);
return;
}
let body = msg.body();
match self.forge.parse_webhook_body(body) {
Err(err) => {
warn!(?err, "Not a 'push'");
return;
}
Ok(push) => match push.branch(config.branches()) {
None => {
warn!(
?push,
"Unrecognised branch, we should be filtering to only the ones we want"
);
return;
}
Some(config::webhook::push::Branch::Main) => {
let commit = git::Commit::from(push);
if self.last_main_commit.as_ref() == Some(&commit) {
info!(
branch = %config.branches().main(),
%commit,
"Ignoring - already aware of branch at commit",
);
return;
}
self.last_main_commit.replace(commit);
}
Some(config::webhook::push::Branch::Next) => {
let commit = git::Commit::from(push);
if self.last_next_commit.as_ref() == Some(&commit) {
info!(
branch = %config.branches().next(),
%commit,
"Ignoring - already aware of branch at commit",
);
return;
}
self.last_next_commit.replace(commit);
}
Some(config::webhook::push::Branch::Dev) => {
let commit = git::Commit::from(push);
if self.last_dev_commit.as_ref() == Some(&commit) {
info!(
branch = %config.branches().dev(),
%commit,
"Ignoring - already aware of branch at commit",
);
return;
}
self.last_dev_commit.replace(commit);
}
},
}
let message_token = self.message_token.next();
info!(
token = %message_token,
"New commit"
);
ctx.address().do_send(ValidateRepo { message_token });
}
}