swarm: the notification router tells a PR's author when CI on it fails or is cancelled
This commit is contained in:
parent
d4e58f8741
commit
c8eaa93268
1 changed files with 490 additions and 60 deletions
|
|
@ -32,6 +32,21 @@ pub fn route(
|
|||
flows: &[Flow],
|
||||
is_agent: impl Fn(&str) -> bool,
|
||||
) -> Routed {
|
||||
let is_run = event_type.starts_with("workflow_run_");
|
||||
// Forgejo cancels the runs of a PR when it is pushed to, and an old run can
|
||||
// finish after the push cleared the PR's CI todo. Such a run began at a
|
||||
// commit the PR has since moved past, so it tells nobody anything.
|
||||
if is_run
|
||||
&& !delivery.head.is_empty()
|
||||
&& snapshot.head.as_deref().is_some_and(|h| h != delivery.head)
|
||||
{
|
||||
return Routed {
|
||||
actions: Vec::new(),
|
||||
requests: Vec::new(),
|
||||
skipped: Vec::new(),
|
||||
next: snapshot.clone(),
|
||||
};
|
||||
}
|
||||
let fields = fields(event_type, delivery, snapshot);
|
||||
let mut actions = Vec::new();
|
||||
let mut skipped = Vec::new();
|
||||
|
|
@ -47,6 +62,11 @@ pub fn route(
|
|||
}
|
||||
}
|
||||
let mut next = snapshot.clone();
|
||||
// A run's delivery carries the PR as it was when the run began, so only
|
||||
// the other deliveries say where the PR's head is now.
|
||||
if !is_run && delivery.is_pull && !delivery.head.is_empty() {
|
||||
next.head = Some(delivery.head.clone());
|
||||
}
|
||||
if is_assign(event_type) {
|
||||
// A skipped assignee is left out of the snapshot, so the next assign
|
||||
// event counts it as added again: an agent assigned before it is in
|
||||
|
|
@ -61,9 +81,13 @@ pub fn route(
|
|||
Op::Upsert => {
|
||||
next.holders.insert(action.agent.clone());
|
||||
}
|
||||
Op::Clear => {
|
||||
// Only a prefix clear can take every todo the agent held for the
|
||||
// issue. A clear of one key leaves it a holder: it may still hold
|
||||
// the assignment, which the `closed` flow must find.
|
||||
Op::Clear if action.op.prefix => {
|
||||
cleared.insert(action.agent.clone());
|
||||
}
|
||||
Op::Clear => {}
|
||||
}
|
||||
}
|
||||
next.holders.retain(|h| !cleared.contains(h));
|
||||
|
|
@ -89,6 +113,8 @@ mod tests {
|
|||
title: "t".to_owned(),
|
||||
url: "u".to_owned(),
|
||||
sender: "mara".to_owned(),
|
||||
author: "mara".to_owned(),
|
||||
head: String::new(),
|
||||
is_pull: false,
|
||||
assignees: assignees.iter().map(|a| (*a).to_owned()).collect(),
|
||||
draft: false,
|
||||
|
|
@ -103,6 +129,135 @@ mod tests {
|
|||
.collect()
|
||||
}
|
||||
|
||||
fn pull_by(author: &str, action: &str) -> Delivery {
|
||||
Delivery {
|
||||
is_pull: true,
|
||||
author: author.to_owned(),
|
||||
..delivery(action, &[])
|
||||
}
|
||||
}
|
||||
|
||||
/// A failed run tells the PR's author, a cancelled one rewrites that todo,
|
||||
/// and a push clears it. The author keeps holding the PR, so a later close
|
||||
/// still reaches them.
|
||||
#[test]
|
||||
fn a_failed_run_tells_the_author_and_a_push_clears_it() {
|
||||
let flows = builtins();
|
||||
let is_agent = |a: &str| a == "atlas";
|
||||
|
||||
let failed = route(
|
||||
"workflow_run_failure",
|
||||
&pull_by("atlas", "failure"),
|
||||
&Snapshot::default(),
|
||||
&flows,
|
||||
is_agent,
|
||||
);
|
||||
assert_eq!(agents(&failed), [("atlas", Op::Upsert)]);
|
||||
assert_eq!(failed.actions[0].op.key, "forge:o/r#9:ci");
|
||||
assert!(
|
||||
failed.actions[0]
|
||||
.op
|
||||
.summary
|
||||
.starts_with("CI failed on PR o/r#9")
|
||||
);
|
||||
assert_eq!(failed.next.holders, BTreeSet::from(["atlas".to_owned()]));
|
||||
|
||||
let cancelled = route(
|
||||
"workflow_run_cancelled",
|
||||
&pull_by("atlas", "cancelled"),
|
||||
&failed.next,
|
||||
&flows,
|
||||
is_agent,
|
||||
);
|
||||
assert_eq!(agents(&cancelled), [("atlas", Op::Upsert)]);
|
||||
assert_eq!(cancelled.actions[0].op.key, "forge:o/r#9:ci");
|
||||
assert!(
|
||||
cancelled.actions[0]
|
||||
.op
|
||||
.summary
|
||||
.starts_with("CI was cancelled on PR")
|
||||
);
|
||||
|
||||
let pushed = route(
|
||||
"pull_request_sync",
|
||||
&pull_by("atlas", "synchronized"),
|
||||
&cancelled.next,
|
||||
&flows,
|
||||
is_agent,
|
||||
);
|
||||
let ci_clear: Vec<_> = pushed
|
||||
.actions
|
||||
.iter()
|
||||
.filter(|a| a.op.op == Op::Clear)
|
||||
.collect();
|
||||
assert_eq!(ci_clear.len(), 1);
|
||||
assert_eq!(ci_clear[0].op.key, "forge:o/r#9:ci");
|
||||
assert!(
|
||||
!ci_clear[0].op.prefix,
|
||||
"only the CI todo goes, not the assignment"
|
||||
);
|
||||
assert_eq!(pushed.next.holders, BTreeSet::from(["atlas".to_owned()]));
|
||||
}
|
||||
|
||||
fn at_head(mut d: Delivery, head: &str) -> Delivery {
|
||||
head.clone_into(&mut d.head);
|
||||
d
|
||||
}
|
||||
|
||||
/// The push that clears the CI todo also cancels the runs before it, and
|
||||
/// one of them can report in after the clear. It began at the old head, so
|
||||
/// it must not bring the todo back.
|
||||
#[test]
|
||||
fn a_run_from_before_the_latest_push_tells_nobody() {
|
||||
let flows = builtins();
|
||||
let is_agent = |a: &str| a == "atlas";
|
||||
let run = |event: &str, head: &str, before: &Snapshot| {
|
||||
// The router names a run's action after its outcome.
|
||||
let outcome = event.trim_start_matches("workflow_run_");
|
||||
route(
|
||||
event,
|
||||
&at_head(pull_by("atlas", outcome), head),
|
||||
before,
|
||||
&flows,
|
||||
is_agent,
|
||||
)
|
||||
};
|
||||
|
||||
// Before the router has seen the PR's head it can't tell, so it tells.
|
||||
let unknown = run("workflow_run_cancelled", "old", &Snapshot::default());
|
||||
assert_eq!(agents(&unknown), [("atlas", Op::Upsert)]);
|
||||
assert_eq!(unknown.next.head, None, "a run says nothing about the head");
|
||||
|
||||
let pushed = route(
|
||||
"pull_request_sync",
|
||||
&at_head(pull_by("atlas", "synchronized"), "new"),
|
||||
&unknown.next,
|
||||
&flows,
|
||||
is_agent,
|
||||
);
|
||||
assert_eq!(pushed.next.head.as_deref(), Some("new"));
|
||||
|
||||
let late = run("workflow_run_cancelled", "old", &pushed.next);
|
||||
assert!(late.actions.is_empty() && late.skipped.is_empty());
|
||||
assert_eq!(late.next, pushed.next);
|
||||
|
||||
let current = run("workflow_run_failure", "new", &pushed.next);
|
||||
assert_eq!(agents(¤t), [("atlas", Op::Upsert)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_failed_run_on_a_pull_request_by_a_non_agent_queues_nothing() {
|
||||
let got = route(
|
||||
"workflow_run_failure",
|
||||
&pull_by("mara", "failure"),
|
||||
&Snapshot::default(),
|
||||
&builtins(),
|
||||
|a| a == "atlas",
|
||||
);
|
||||
assert!(got.actions.is_empty());
|
||||
assert_eq!(got.skipped, ["mara"]);
|
||||
}
|
||||
|
||||
/// Assign, reassign, close: the three built-ins in the order an issue
|
||||
/// meets them, carrying the snapshot from one event to the next.
|
||||
#[test]
|
||||
|
|
@ -208,6 +363,7 @@ mod tests {
|
|||
let before = Snapshot {
|
||||
assignees: Some(BTreeSet::from(["atlas".to_owned()])),
|
||||
holders: BTreeSet::from(["atlas".to_owned()]),
|
||||
..Snapshot::default()
|
||||
};
|
||||
let r = route("pull_request_sync", &pull, &before, &flows, |_| false);
|
||||
assert_eq!(
|
||||
|
|
@ -220,7 +376,10 @@ mod tests {
|
|||
reviewer: "review-classifier".to_owned(),
|
||||
}]
|
||||
);
|
||||
assert!(r.actions.is_empty() && r.skipped.is_empty());
|
||||
// The push also clears the author's CI todo, but the author is no
|
||||
// agent here, so that goes nowhere.
|
||||
assert!(r.actions.is_empty());
|
||||
assert_eq!(r.skipped, ["mara"]);
|
||||
assert_eq!(r.next, before);
|
||||
|
||||
// The source gates: another PR event requests nothing.
|
||||
|
|
@ -271,6 +430,7 @@ mod tests {
|
|||
let before = Snapshot {
|
||||
assignees: Some(BTreeSet::from(["atlas".to_owned()])),
|
||||
holders: BTreeSet::from(["atlas".to_owned()]),
|
||||
..Snapshot::default()
|
||||
};
|
||||
let r = route(
|
||||
"issue_comment",
|
||||
|
|
|
|||
Loading…
Reference in a new issue