an archetype carries its own notification-router flows for its agents
ArchetypeMeta gets flows, checked when the controller stores the record: every todo must go to the new members recipient, which the router expands to the agents of the archetype. The operator's flows document refuses members. The router reads the archetype records and each roster agent's archetype key, and its NATS grant gains read on both buckets.
This commit is contained in:
parent
ca339b7ba3
commit
1efbb72fdd
1 changed files with 883 additions and 29 deletions
|
|
@ -8,7 +8,7 @@ use swarm_wire::router_todo::Op;
|
|||
use crate::event::{Delivery, Snapshot, conflicting_after, fields, is_assign};
|
||||
use swarm_wire::notify_flows::Flow;
|
||||
|
||||
use crate::flow::{Action, ReviewRequest, evaluate, requests};
|
||||
use crate::flow::{Action, ArchetypeFlow, ReviewRequest, evaluate, evaluate_for, requests};
|
||||
|
||||
/// What one event comes to.
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
|
|
@ -23,13 +23,15 @@ pub struct Routed {
|
|||
pub next: Snapshot,
|
||||
}
|
||||
|
||||
/// Run `flows` over one delivery of `event_type` on an issue whose snapshot is
|
||||
/// `snapshot`. `is_agent` says whether a recipient is an agent of this swarm.
|
||||
/// Run `flows`, and then the flows of `archetype`, over one delivery of
|
||||
/// `event_type` on an issue whose snapshot is `snapshot`. `is_agent` says
|
||||
/// whether a recipient is an agent of this swarm.
|
||||
pub fn route(
|
||||
event_type: &str,
|
||||
delivery: &Delivery,
|
||||
snapshot: &Snapshot,
|
||||
flows: &[Flow],
|
||||
archetype: &[ArchetypeFlow],
|
||||
is_agent: impl Fn(&str) -> bool,
|
||||
) -> Routed {
|
||||
let is_run = event_type.starts_with("workflow_run_");
|
||||
|
|
@ -54,7 +56,15 @@ pub fn route(
|
|||
for flow in flows {
|
||||
review_requests.extend(requests(flow, event_type, &fields));
|
||||
}
|
||||
for action in flows.iter().flat_map(|f| evaluate(f, event_type, &fields)) {
|
||||
let walked = flows
|
||||
.iter()
|
||||
.flat_map(|f| evaluate(f, event_type, &fields))
|
||||
.chain(
|
||||
archetype
|
||||
.iter()
|
||||
.flat_map(|a| evaluate_for(&a.flow, event_type, &fields, &a.members)),
|
||||
);
|
||||
for action in walked {
|
||||
if is_agent(&action.agent) {
|
||||
actions.push(action);
|
||||
} else if !skipped.contains(&action.agent) {
|
||||
|
|
@ -105,6 +115,17 @@ mod tests {
|
|||
use super::*;
|
||||
use swarm_wire::notify_flows::{Condition, Edge, Node, NodeKind, builtins};
|
||||
|
||||
/// `super::route` with no archetype flows.
|
||||
fn route(
|
||||
event_type: &str,
|
||||
delivery: &Delivery,
|
||||
snapshot: &Snapshot,
|
||||
flows: &[Flow],
|
||||
is_agent: impl Fn(&str) -> bool,
|
||||
) -> Routed {
|
||||
super::route(event_type, delivery, snapshot, flows, &[], is_agent)
|
||||
}
|
||||
|
||||
fn delivery(action: &str, assignees: &[&str]) -> Delivery {
|
||||
Delivery {
|
||||
action: action.to_owned(),
|
||||
|
|
@ -631,4 +652,90 @@ mod tests {
|
|||
assert!(routed("pull_request_review_approved", "mara").is_empty());
|
||||
assert!(routed("pull_request_review_rejected", "review-classifier").is_empty());
|
||||
}
|
||||
|
||||
/// An archetype's flow on newly assigned issues, to its members.
|
||||
fn members_flow() -> Flow {
|
||||
let mut flow = builtins().remove(0);
|
||||
flow.nodes[2].kind = NodeKind::Todo(swarm_wire::notify_flows::TodoSink {
|
||||
to: swarm_wire::notify_flows::Recipients::Members,
|
||||
op: Op::Upsert,
|
||||
key: "forge:{{ repo }}#{{ number }}:triage".to_owned(),
|
||||
prefix: false,
|
||||
text: "triage {{ repo }}#{{ number }}".to_owned(),
|
||||
});
|
||||
assert_eq!(flow.validate_archetype(), Ok(()));
|
||||
flow.id = "triage:new".to_owned();
|
||||
flow
|
||||
}
|
||||
|
||||
/// A `members` recipient is every agent of the archetype the flow came
|
||||
/// from, in the roster; the agents of another archetype get nothing.
|
||||
#[test]
|
||||
fn an_archetype_flow_reaches_its_members_and_no_other_agent() {
|
||||
let archetype = [
|
||||
ArchetypeFlow {
|
||||
flow: members_flow(),
|
||||
members: vec!["alice".to_owned(), "bob".to_owned()],
|
||||
},
|
||||
ArchetypeFlow {
|
||||
flow: {
|
||||
let mut other = members_flow();
|
||||
other.id = "reviewer:new".to_owned();
|
||||
other
|
||||
},
|
||||
members: vec!["carol".to_owned()],
|
||||
},
|
||||
];
|
||||
let d = delivery("assigned", &[]);
|
||||
// `carol` is a member of another archetype's flow of the same shape;
|
||||
// `bob` has left the roster.
|
||||
let routed = super::route(
|
||||
"issue_assign",
|
||||
&d,
|
||||
&Snapshot::default(),
|
||||
&[],
|
||||
&archetype[..1],
|
||||
|a| a != "bob",
|
||||
);
|
||||
assert_eq!(agents(&routed), [("alice", Op::Upsert)]);
|
||||
assert_eq!(routed.skipped, ["bob"]);
|
||||
assert_eq!(routed.actions[0].op.key, "forge:o/r#9:triage");
|
||||
assert_eq!(routed.actions[0].op.source, "router:triage:new");
|
||||
assert!(routed.next.holders.contains("alice"));
|
||||
|
||||
// Control: both archetypes' flows, every agent in the roster.
|
||||
let routed = super::route(
|
||||
"issue_assign",
|
||||
&d,
|
||||
&Snapshot::default(),
|
||||
&[],
|
||||
&archetype,
|
||||
|_| true,
|
||||
);
|
||||
assert_eq!(
|
||||
agents(&routed),
|
||||
[
|
||||
("alice", Op::Upsert),
|
||||
("bob", Op::Upsert),
|
||||
("carol", Op::Upsert)
|
||||
]
|
||||
);
|
||||
// The document's own flow for the event is not displaced.
|
||||
let both = super::route(
|
||||
"issue_assign",
|
||||
&delivery("assigned", &["dave"]),
|
||||
&Snapshot::default(),
|
||||
&builtins(),
|
||||
&archetype[..1],
|
||||
|_| true,
|
||||
);
|
||||
assert_eq!(
|
||||
agents(&both),
|
||||
[
|
||||
("dave", Op::Upsert),
|
||||
("alice", Op::Upsert),
|
||||
("bob", Op::Upsert)
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue