2024-05-25 11:25:13 +01:00
|
|
|
//
|
2024-04-13 16:16:09 +01:00
|
|
|
use actix::prelude::*;
|
2024-04-15 22:44:56 +01:00
|
|
|
|
2024-05-25 11:25:13 +01:00
|
|
|
use crate::{RepoActor, ValidateRepo};
|
|
|
|
use git_next_config as config;
|
|
|
|
use git_next_git as git;
|
2024-04-15 22:44:56 +01:00
|
|
|
|
2024-05-25 11:25:13 +01:00
|
|
|
use tracing::{info, warn};
|
2024-04-13 16:16:09 +01:00
|
|
|
|
2024-05-25 11:25:13 +01:00
|
|
|
impl Handler<config::WebhookMessage> for RepoActor {
|
2024-04-13 16:16:09 +01:00
|
|
|
type Result = ();
|
|
|
|
|
2024-04-14 15:46:21 +01:00
|
|
|
#[allow(clippy::cognitive_complexity)] // TODO: (#49) reduce complexity
|
2024-05-26 08:35:45 +01:00
|
|
|
#[tracing::instrument(name = "RepoActor::WebhookMessage", skip_all, fields(token = %self.message_token, repo = %self.repo_details))]
|
2024-05-25 11:25:13 +01:00
|
|
|
fn handle(&mut self, msg: config::WebhookMessage, ctx: &mut Self::Context) -> Self::Result {
|
2024-05-19 18:20:54 +01:00
|
|
|
let Some(expected_authorization) = &self.webhook_auth else {
|
|
|
|
warn!("Don't know what authorization to expect");
|
|
|
|
return;
|
|
|
|
};
|
2024-05-25 11:25:13 +01:00
|
|
|
let Some(config) = &self.repo_details.repo_config else {
|
|
|
|
warn!("No repo config");
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
if !self
|
|
|
|
.forge
|
|
|
|
.is_message_authorised(&msg, expected_authorization)
|
|
|
|
{
|
2024-05-19 18:20:54 +01:00
|
|
|
warn!(
|
|
|
|
"Invalid authorization - expected {}",
|
|
|
|
expected_authorization
|
|
|
|
);
|
|
|
|
return;
|
2024-04-14 19:12:51 +01:00
|
|
|
}
|
2024-04-13 16:16:09 +01:00
|
|
|
let body = msg.body();
|
2024-05-25 11:25:13 +01:00
|
|
|
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;
|
2024-04-14 15:46:21 +01:00
|
|
|
}
|
2024-05-25 11:25:13 +01:00
|
|
|
self.last_main_commit.replace(commit);
|
2024-04-14 15:46:21 +01:00
|
|
|
}
|
2024-05-25 11:25:13 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
},
|
2024-04-14 15:46:21 +01:00
|
|
|
}
|
2024-05-25 11:25:13 +01:00
|
|
|
let message_token = self.message_token.next();
|
|
|
|
info!(
|
|
|
|
token = %message_token,
|
|
|
|
"New commit"
|
|
|
|
);
|
|
|
|
ctx.address().do_send(ValidateRepo { message_token });
|
2024-05-22 08:41:30 +01:00
|
|
|
}
|
|
|
|
}
|