Compare commits
2 commits
f8fefcdedd
...
c104dfedc1
Author | SHA1 | Date | |
---|---|---|---|
c104dfedc1 | |||
06292c2711 |
3 changed files with 97 additions and 65 deletions
|
@ -3,7 +3,8 @@ resolver = "2"
|
||||||
members = ["crates/*"]
|
members = ["crates/*"]
|
||||||
|
|
||||||
[workspace.package]
|
[workspace.package]
|
||||||
version = "0.10.0"
|
version = "0.10.0" # Update git-next-* under workspace.dependencies
|
||||||
|
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
repository = "https://git.kemitix.net/kemitix/git-next"
|
repository = "https://git.kemitix.net/kemitix/git-next"
|
||||||
|
|
|
@ -1,41 +1,27 @@
|
||||||
//
|
//
|
||||||
use actix::prelude::*;
|
use actix::prelude::*;
|
||||||
|
|
||||||
use crate::{self as actor, messages::WebhookNotification};
|
use crate::{self as actor, messages::WebhookNotification, RepoActorLog};
|
||||||
use git_next_config as config;
|
use git_next_config::WebhookAuth;
|
||||||
use git_next_git as git;
|
use git_next_config::{
|
||||||
|
webhook::{push::Branch, Push},
|
||||||
|
BranchName,
|
||||||
|
};
|
||||||
|
use git_next_git::{self as git, Commit, ForgeLike};
|
||||||
|
|
||||||
use tracing::{info, warn};
|
use tracing::{info, warn};
|
||||||
|
|
||||||
impl Handler<WebhookNotification> for actor::RepoActor {
|
impl Handler<WebhookNotification> for actor::RepoActor {
|
||||||
type Result = ();
|
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))]
|
#[tracing::instrument(name = "RepoActor::WebhookMessage", skip_all, fields(token = %self.message_token, repo = %self.repo_details))]
|
||||||
fn handle(&mut self, msg: WebhookNotification, ctx: &mut Self::Context) -> Self::Result {
|
fn handle(&mut self, msg: WebhookNotification, ctx: &mut Self::Context) -> Self::Result {
|
||||||
let Some(expected_authorization) = &self.webhook_auth else {
|
|
||||||
actor::logger(&self.log, "server has no auth token");
|
|
||||||
warn!("Don't know what authorization to expect");
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
let Some(config) = &self.repo_details.repo_config else {
|
let Some(config) = &self.repo_details.repo_config else {
|
||||||
actor::logger(&self.log, "server has no repo config");
|
actor::logger(&self.log, "server has no repo config");
|
||||||
warn!("No repo config");
|
warn!("No repo config");
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
if !self
|
if validate_notification(&msg, &self.webhook_auth, &*self.forge, &self.log).is_err() {
|
||||||
.forge
|
|
||||||
.is_message_authorised(&msg, expected_authorization)
|
|
||||||
{
|
|
||||||
actor::logger(&self.log, "message authorisation is invalid");
|
|
||||||
warn!(
|
|
||||||
"Invalid authorization - expected {}",
|
|
||||||
expected_authorization
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if self.forge.should_ignore_message(&msg) {
|
|
||||||
actor::logger(&self.log, "forge sent ignorable message");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let body = msg.body();
|
let body = msg.body();
|
||||||
|
@ -54,47 +40,41 @@ impl Handler<WebhookNotification> for actor::RepoActor {
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Some(config::webhook::push::Branch::Main) => {
|
Some(Branch::Main) => {
|
||||||
actor::logger(&self.log, "message is for main branch");
|
if handle_push(
|
||||||
let commit = git::Commit::from(push);
|
push,
|
||||||
if self.last_main_commit.as_ref() == Some(&commit) {
|
config.branches().main(),
|
||||||
actor::logger(&self.log, "not a new commit on main");
|
&mut self.last_main_commit,
|
||||||
info!(
|
&self.log,
|
||||||
branch = %config.branches().main(),
|
)
|
||||||
%commit,
|
.is_err()
|
||||||
"Ignoring - already aware of branch at commit",
|
{
|
||||||
);
|
|
||||||
return;
|
return;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
self.last_main_commit.replace(commit);
|
Some(Branch::Next) => {
|
||||||
}
|
if handle_push(
|
||||||
Some(config::webhook::push::Branch::Next) => {
|
push,
|
||||||
actor::logger(&self.log, "message is for next branch");
|
config.branches().next(),
|
||||||
let commit = git::Commit::from(push);
|
&mut self.last_next_commit,
|
||||||
if self.last_next_commit.as_ref() == Some(&commit) {
|
&self.log,
|
||||||
actor::logger(&self.log, "not a new commit on next");
|
)
|
||||||
info!(
|
.is_err()
|
||||||
branch = %config.branches().next(),
|
{
|
||||||
%commit,
|
|
||||||
"Ignoring - already aware of branch at commit",
|
|
||||||
);
|
|
||||||
return;
|
return;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
self.last_next_commit.replace(commit);
|
Some(Branch::Dev) => {
|
||||||
}
|
if handle_push(
|
||||||
Some(config::webhook::push::Branch::Dev) => {
|
push,
|
||||||
actor::logger(&self.log, "message is for dev branch");
|
config.branches().dev(),
|
||||||
let commit = git::Commit::from(push);
|
&mut self.last_dev_commit,
|
||||||
if self.last_dev_commit.as_ref() == Some(&commit) {
|
&self.log,
|
||||||
actor::logger(&self.log, "not a new commit on dev");
|
)
|
||||||
info!(
|
.is_err()
|
||||||
branch = %config.branches().dev(),
|
{
|
||||||
%commit,
|
|
||||||
"Ignoring - already aware of branch at commit",
|
|
||||||
);
|
|
||||||
return;
|
return;
|
||||||
}
|
};
|
||||||
self.last_dev_commit.replace(commit);
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -110,3 +90,51 @@ impl Handler<WebhookNotification> for actor::RepoActor {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn validate_notification(
|
||||||
|
msg: &WebhookNotification,
|
||||||
|
webhook_auth: &Option<WebhookAuth>,
|
||||||
|
forge: &dyn ForgeLike,
|
||||||
|
log: &Option<RepoActorLog>,
|
||||||
|
) -> Result<(), ()> {
|
||||||
|
let Some(expected_authorization) = webhook_auth else {
|
||||||
|
actor::logger(log, "server has no auth token");
|
||||||
|
warn!("Don't know what authorization to expect");
|
||||||
|
return Err(());
|
||||||
|
};
|
||||||
|
|
||||||
|
if !forge.is_message_authorised(msg, expected_authorization) {
|
||||||
|
actor::logger(log, "message authorisation is invalid");
|
||||||
|
warn!(
|
||||||
|
"Invalid authorization - expected {}",
|
||||||
|
expected_authorization
|
||||||
|
);
|
||||||
|
return Err(());
|
||||||
|
}
|
||||||
|
if forge.should_ignore_message(msg) {
|
||||||
|
actor::logger(log, "forge sent ignorable message");
|
||||||
|
return Err(());
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_push(
|
||||||
|
push: Push,
|
||||||
|
branch: BranchName,
|
||||||
|
last_commit: &mut Option<Commit>,
|
||||||
|
log: &Option<RepoActorLog>,
|
||||||
|
) -> Result<(), ()> {
|
||||||
|
actor::logger(log, "message is for dev branch");
|
||||||
|
let commit = git::Commit::from(push);
|
||||||
|
if last_commit.as_ref() == Some(&commit) {
|
||||||
|
actor::logger(log, format!("not a new commit on {branch}"));
|
||||||
|
info!(
|
||||||
|
%branch ,
|
||||||
|
%commit,
|
||||||
|
"Ignoring - already aware of branch at commit",
|
||||||
|
);
|
||||||
|
return Err(());
|
||||||
|
}
|
||||||
|
last_commit.replace(commit);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
|
@ -252,8 +252,9 @@ async fn when_message_is_push_already_seen_commit_to_main() -> TestResult {
|
||||||
let forge_notification = ForgeNotification::new(forge_alias, repo_alias, headers, body);
|
let forge_notification = ForgeNotification::new(forge_alias, repo_alias, headers, body);
|
||||||
let repository_factory = MockRepositoryFactory::new();
|
let repository_factory = MockRepositoryFactory::new();
|
||||||
let commit = given::a_commit();
|
let commit = given::a_commit();
|
||||||
|
let main = repo_config.branches().main();
|
||||||
let push = given::a_push()
|
let push = given::a_push()
|
||||||
.with_branch(repo_config.branches().main())
|
.with_branch(main.clone())
|
||||||
.with_sha(commit.sha().to_string())
|
.with_sha(commit.sha().to_string())
|
||||||
.with_message(commit.message().to_string());
|
.with_message(commit.message().to_string());
|
||||||
let mut forge = given::a_forge();
|
let mut forge = given::a_forge();
|
||||||
|
@ -283,7 +284,7 @@ async fn when_message_is_push_already_seen_commit_to_main() -> TestResult {
|
||||||
|
|
||||||
//then
|
//then
|
||||||
log.no_message_contains("send")?;
|
log.no_message_contains("send")?;
|
||||||
log.require_message_containing("not a new commit on main")?;
|
log.require_message_containing(format!("not a new commit on {main}"))?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -300,8 +301,9 @@ async fn when_message_is_push_already_seen_commit_to_next() -> TestResult {
|
||||||
let forge_notification = ForgeNotification::new(forge_alias, repo_alias, headers, body);
|
let forge_notification = ForgeNotification::new(forge_alias, repo_alias, headers, body);
|
||||||
let repository_factory = MockRepositoryFactory::new();
|
let repository_factory = MockRepositoryFactory::new();
|
||||||
let commit = given::a_commit();
|
let commit = given::a_commit();
|
||||||
|
let next = repo_config.branches().next();
|
||||||
let push = given::a_push()
|
let push = given::a_push()
|
||||||
.with_branch(repo_config.branches().next())
|
.with_branch(next.clone())
|
||||||
.with_sha(commit.sha().to_string())
|
.with_sha(commit.sha().to_string())
|
||||||
.with_message(commit.message().to_string());
|
.with_message(commit.message().to_string());
|
||||||
let mut forge = given::a_forge();
|
let mut forge = given::a_forge();
|
||||||
|
@ -331,7 +333,7 @@ async fn when_message_is_push_already_seen_commit_to_next() -> TestResult {
|
||||||
|
|
||||||
//then
|
//then
|
||||||
log.no_message_contains("send")?;
|
log.no_message_contains("send")?;
|
||||||
log.require_message_containing("not a new commit on next")?;
|
log.require_message_containing(format!("not a new commit on {next}"))?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -348,8 +350,9 @@ async fn when_message_is_push_already_seen_commit_to_dev() -> TestResult {
|
||||||
let forge_notification = ForgeNotification::new(forge_alias, repo_alias, headers, body);
|
let forge_notification = ForgeNotification::new(forge_alias, repo_alias, headers, body);
|
||||||
let repository_factory = MockRepositoryFactory::new();
|
let repository_factory = MockRepositoryFactory::new();
|
||||||
let commit = given::a_commit();
|
let commit = given::a_commit();
|
||||||
|
let dev = repo_config.branches().dev();
|
||||||
let push = given::a_push()
|
let push = given::a_push()
|
||||||
.with_branch(repo_config.branches().dev())
|
.with_branch(dev.clone())
|
||||||
.with_sha(commit.sha().to_string())
|
.with_sha(commit.sha().to_string())
|
||||||
.with_message(commit.message().to_string());
|
.with_message(commit.message().to_string());
|
||||||
let mut forge = given::a_forge();
|
let mut forge = given::a_forge();
|
||||||
|
@ -379,7 +382,7 @@ async fn when_message_is_push_already_seen_commit_to_dev() -> TestResult {
|
||||||
|
|
||||||
//then
|
//then
|
||||||
log.no_message_contains("send")?;
|
log.no_message_contains("send")?;
|
||||||
log.require_message_containing("not a new commit on dev")?;
|
log.require_message_containing(format!("not a new commit on {dev}"))?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue