Compare commits
28 changed files with 198 additions and 1601 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
|
@ -1844,7 +1844,6 @@ dependencies = [
|
|||
"clap",
|
||||
"clap-markdown",
|
||||
"forgejo-api",
|
||||
"hive-types",
|
||||
"libc",
|
||||
"reqwest",
|
||||
"serde",
|
||||
|
|
|
|||
|
|
@ -32,8 +32,7 @@ grafana, the controller, authelia, …) →
|
|||
its repo's own yes/no rules and posts the result as a review. →
|
||||
[`review-classifier.md`](review-classifier.md)
|
||||
- **notification router** — forge events turned into todos on the agents
|
||||
they concern: assigned, unassigned, cleared on close; and the classifier's
|
||||
review requested on a push. →
|
||||
they concern: assigned, unassigned, cleared on close. →
|
||||
[`notify-router.md`](notify-router.md)
|
||||
- **swarm UI** — the operator's day-to-day surface, on the swarm apex,
|
||||
`admins` only. → [`ui.md`](ui.md)
|
||||
|
|
@ -416,7 +415,7 @@ itself, all under `https://<swarm.domain>/webhook/forge/`:
|
|||
- an instance-wide `pull_request_review_request` system hook, which starts
|
||||
the [review classifier](review-classifier.md#trigger-and-lifecycle)
|
||||
- instance-wide `issues` + `issue_assign` and `pull_request` +
|
||||
`pull_request_assign` + `pull_request_sync` system hooks, whose deliveries it
|
||||
`pull_request_assign` system hooks, whose deliveries it
|
||||
keeps in the `SWARM_EVENTS` stream for the
|
||||
[notification router](notify-router.md#the-path)
|
||||
|
||||
|
|
|
|||
|
|
@ -41,9 +41,8 @@ router notices an unassign only on an issue it has seen an assign event for.
|
|||
|
||||
1. swarm-controller registers two instance-wide hooks, one for the `issues`
|
||||
and `issue_assign` events at `/webhook/forge/issues` and one for
|
||||
`pull_request`, `pull_request_assign` and `pull_request_sync` (a push to an
|
||||
open PR's branch) at `/webhook/forge/pull-request`. At each start it adds
|
||||
any of these events a registered hook lacks.
|
||||
`pull_request` and `pull_request_assign` at `/webhook/forge/pull-request`.
|
||||
At each start it adds any of these events a registered hook lacks.
|
||||
2. On each delivery the controller checks the HMAC and publishes the body,
|
||||
unchanged, to the `SWARM_EVENTS` stream on
|
||||
`$SWARM.events.forge.<X-Forgejo-Event-Type>`, such as
|
||||
|
|
@ -69,19 +68,16 @@ The router holds no forge token and no webhook secret, and registers no hook.
|
|||
|
||||
A flow is a graph of nodes joined by edges: one `source` node naming the forge
|
||||
event types it starts from, `match` nodes that pass when every condition
|
||||
holds, `todo` nodes that emit one todo op per recipient, and `request_reviewer`
|
||||
nodes that ask for a review. A match node can have more than one child, and
|
||||
each one runs, so one event can reach several leaves. A flow must be a tree:
|
||||
one source, one parent for every other node, every node reachable, no children
|
||||
under a `todo` or a `request_reviewer`. The most is 64 nodes, 16
|
||||
holds, and `todo` nodes that emit one todo op per recipient. A match node can
|
||||
have more than one child, and each one runs, so one event can reach several
|
||||
todos. A flow must be a tree: one source, one parent for every other node,
|
||||
every node reachable, no children under a `todo`. The most is 64 nodes, 16
|
||||
levels deep, and 100 custom flows. A `todo` node sends to the agents an event
|
||||
field holds (`assignees_added`, `todo_holders`, …) or to one named agent.
|
||||
|
||||
The router ships the three flows above, each a source → match on `action` →
|
||||
todo, and `head-classifier`: source `pull_request_sync` → `request_reviewer`
|
||||
for `review-classifier` ([below](#requesting-a-review)). The code defines them
|
||||
(`swarm_wire::notify_flows::builtins`), so a build that changes one reaches
|
||||
every swarm. Each switches off on `/notifications`. A custom flow has the same type and
|
||||
todo. The code defines them (`swarm_wire::notify_flows::builtins`), so a
|
||||
build that changes one reaches every swarm. A custom flow has the same type and
|
||||
the same evaluator, so it can do anything a built-in does.
|
||||
|
||||
**The stored document.** swarm-controller keeps the rest in one document, the
|
||||
|
|
@ -90,8 +86,8 @@ switched off, and the custom flows. Only the controller writes it, through
|
|||
`GET` and `PUT /api/notify/flows`. A `PUT` replaces the whole document in one
|
||||
compare-and-set. The controller answers 400 unless every flow passes the tree
|
||||
check above and reads only what exists: a source event from `issues`,
|
||||
`issue_assign`, `pull_request`, `pull_request_assign` or `pull_request_sync`,
|
||||
and condition and recipient fields from the event's field list. The same goes for the `{field}`
|
||||
`issue_assign`, `pull_request` or `pull_request_assign`, and condition and
|
||||
recipient fields from the event's field list. The same goes for the `{field}`
|
||||
placeholders in a todo's key and text. A `PUT` must carry the `revision` it read, `0` for no
|
||||
document, and gets 409 when the stored revision is another. Swarm UI's
|
||||
[`/notifications`](ui.md#notification-flows) page shows the document and
|
||||
|
|
@ -104,8 +100,8 @@ accepts:
|
|||
- `source_events`: `[{name, description}]`, the source events.
|
||||
- `fields`: `[{name, description}]`, the condition and recipient fields, and
|
||||
the `{field}` placeholders.
|
||||
- `node_kinds`: `[{kind, description, fields}]` for `source`, `match`, `todo`
|
||||
and `request_reviewer`; each field is `{name, description, required, values}`, and `values`
|
||||
- `node_kinds`: `[{kind, description, fields}]` for `source`, `match` and
|
||||
`todo`; each field is `{name, description, required, values}`, and `values`
|
||||
lists the accepted `{name, description}` pairs (the `upsert` and `clear` of a
|
||||
todo's `op`), empty for other fields.
|
||||
- `limits`: `{max_nodes, max_depth, max_flows}`.
|
||||
|
|
@ -113,39 +109,6 @@ accepts:
|
|||
A controller that predates `schema` leaves it out of the answer; a client reads
|
||||
it as optional.
|
||||
|
||||
**Requesting a review.** A `request_reviewer` node has one field, `reviewer`,
|
||||
a forge login. For the PR the event concerns (`repo` and `number`; an event
|
||||
about an issue sends nothing), the router sends a NATS request on
|
||||
`$SWARM.router.sink.request-reviewer` with `{repo, number, reviewer}` and
|
||||
swarm-controller answers with one line:
|
||||
|
||||
| Reply | Meaning |
|
||||
| --------------- | ---------------------------------------------------------------------------------- |
|
||||
| `requested` | the forge accepted the request |
|
||||
| `skipped:<why>` | the reviewer's review of the PR's head already stands; the controller sent nothing |
|
||||
| `error:<msg>` | the forge refused or didn't answer, or the request didn't decode |
|
||||
|
||||
The controller asks the forge under its own token, which the router never
|
||||
holds. It skips a reviewer whose latest review is neither stale (the PR's head
|
||||
moved past the commit it reviewed) nor dismissed, because requesting a review
|
||||
from someone who already submitted one makes the forge dismiss it, approval
|
||||
included. The controller asks again once the review is older than the head.
|
||||
`hive-forge pr assign-reviewer` applies the same rule (`hive_types::review`).
|
||||
|
||||
The router sends the request once, after it queued the event's todo ops and
|
||||
wrote the snapshot, and logs the answer. A controller that's down or answers
|
||||
`error:` doesn't hold up the event or any agent's todos, and the request is
|
||||
not retried: the next push asks again. A request repeated for the same head is
|
||||
harmless: a reviewer with a pending request is a no-op for the forge, and one
|
||||
who has reviewed gets `skipped`.
|
||||
|
||||
`head-classifier` requests the review classifier's review on a push to any open
|
||||
PR. The classifier then runs as it does for any requested review
|
||||
([`review-classifier.md`](review-classifier.md#trigger-and-lifecycle)), through
|
||||
its own request queue, which replaces a waiting request for the same PR. A
|
||||
repo the classifier's account can't read, or a PR that account opened, answers
|
||||
`error:`.
|
||||
|
||||
**What the router runs.** It reads the key at most every 10 seconds, when an
|
||||
event arrives, and runs the built-ins you left on, then the custom flows that
|
||||
are on and pass the tree check. It logs a custom flow that fails the check,
|
||||
|
|
@ -164,25 +127,20 @@ document never leaves it with no flows. The controller refuses to overwrite a
|
|||
document of a version it doesn't know.
|
||||
|
||||
**Deploying in any order.** The controller, the router and the UI can be on
|
||||
different hosts. A router that predates `request_reviewer` reads a document
|
||||
holding such a node (a custom flow with one) as one it can't read and keeps the
|
||||
flows it had; it also has no `head-classifier`. A router that has it, with a
|
||||
controller that predates it, gets no answer to the request and logs that. A controller that predates this leaves the key absent, and the
|
||||
different hosts. A controller that predates this leaves the key absent, and the
|
||||
router runs the built-ins as before. A router that predates this ignores the
|
||||
document, so the page and the API show a state the router isn't yet running.
|
||||
|
||||
## Credentials and grants
|
||||
|
||||
| Principal | Grant |
|
||||
| --------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `swarm-notify-router`, in its container | store: `read` on `swarm/services/<router client id>/oidc/client`. Queue: its own consumer on `SWARM_EVENTS` (create, pull, ack), publish on `$SWARM.router.todo.*` and on `$SWARM.router.sink.request-reviewer`, create `ROUTER_TODOS`, its own `notify-router-state` bucket, read on the `notify-flows` bucket, and `STREAM.INFO` on the agent-status bucket, which lists the agents |
|
||||
| swarm-controller | publish on `$SWARM.events.forge.*`; subscribe to `$SWARM.router.sink.request-reviewer` and answer each request once, for 30 seconds; creates and writes the `notify-flows` bucket (every bucket, by its `$KV.*.>` grant) |
|
||||
| an agent, under its own credential | create its own consumer `todo-<agent>` in the form whose filter is `$SWARM.router.todo.<agent>`, pull from it, and ack what it delivered |
|
||||
| Principal | Grant |
|
||||
| --------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `swarm-notify-router`, in its container | store: `read` on `swarm/services/<router client id>/oidc/client`. Queue: its own consumer on `SWARM_EVENTS` (create, pull, ack), publish on `$SWARM.router.todo.*`, create `ROUTER_TODOS`, its own `notify-router-state` bucket, read on the `notify-flows` bucket, and `STREAM.INFO` on the agent-status bucket, which lists the agents |
|
||||
| swarm-controller | publish on `$SWARM.events.forge.*`; creates and writes the `notify-flows` bucket (every bucket, by its `$KV.*.>` grant) |
|
||||
| an agent, under its own credential | create its own consumer `todo-<agent>` in the form whose filter is `$SWARM.router.todo.<agent>`, pull from it, and ack what it delivered |
|
||||
|
||||
No other client may publish on `$SWARM.router.todo.*`, `$SWARM.router.sink.request-reviewer` or
|
||||
`$SWARM.events.forge.*`, so each has one writer, and the controller alone
|
||||
hears the review requests. The review request needs no secret of its own: the
|
||||
controller asks the forge under the token it already holds. The agent's create subject
|
||||
No other client may publish on `$SWARM.router.todo.*` or
|
||||
`$SWARM.events.forge.*`, so each has one writer. The agent's create subject
|
||||
carries the consumer's filter, and the queue server checks it against the
|
||||
request, so an agent can't create a consumer that delivers another agent's
|
||||
todos.
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ that reviewer.
|
|||
|
||||
## Repo configuration
|
||||
|
||||
A repo defines its rules in a file at its root, in YAML as
|
||||
A repo opts in with a rules file at its root, in YAML as
|
||||
`.hyperhive/review-classifier.yml` or in JSON as
|
||||
`.hyperhive/review-classifier.json`. It holds one of the two; with both, the
|
||||
rules don't load. `swarm-controller` always reads the file from the repo's
|
||||
|
|
@ -37,14 +37,8 @@ with exactly these four keys:
|
|||
|
||||
Any other key, at the top or in an entry, is an error: a misspelt key fails
|
||||
the load instead of vanishing. An empty list, more than ten entries, an empty
|
||||
name or description, and two entries with one name are errors too. A file
|
||||
that exists but fails these checks never falls back to a pass: the run posts
|
||||
no verdict.
|
||||
A repo whose default branch holds neither file has no rules. When someone
|
||||
requests the classifier's review on its PR, the account approves at once. The
|
||||
body reads `no review rules are configured for owner/name; approved without
|
||||
checks`. The controller makes no model call. The instance has no per-repo
|
||||
filter yet, so every repo without a rules file gets this approval.
|
||||
name or description, and two entries with one name are errors too. Nothing
|
||||
falls back to a pass: a repo whose file doesn't load has no rules.
|
||||
|
||||
An example:
|
||||
|
||||
|
|
@ -92,10 +86,6 @@ as a reviewer on a PR starts a run:
|
|||
4. The controller posts the result on the PR as the service account's
|
||||
review, on the commit it read.
|
||||
|
||||
A push starts no run by itself. The notification router's `head-classifier`
|
||||
flow requests the account's review on every push to an open PR, unless its
|
||||
review of the new head already stands ([`notify-router.md`](notify-router.md#requesting-a-review)).
|
||||
|
||||
When a post can't reach the secret store or the forge, because the
|
||||
connection fails or a try gets no answer within two minutes, the controller
|
||||
tries it up to six times over about five minutes, enough to outlast a
|
||||
|
|
@ -427,8 +417,7 @@ Repos that rely on it typically raise their required approval count to
|
|||
account for it.
|
||||
|
||||
A run with no clean verdict never approves. When the PR or the rules don't
|
||||
load (a repo with no rules file gets an approval instead), the run fails, the classifier
|
||||
answers a different number of
|
||||
load, the run fails, the classifier answers a different number of
|
||||
classifications than the controller sent it, or **any** classification has no answer,
|
||||
the account posts a **comment** instead, starting with `**No verdict:**` and the reason,
|
||||
followed by the table when there is one. A comment neither approves nor
|
||||
|
|
|
|||
|
|
@ -491,8 +491,7 @@ swarm-controller keeps every `issues` and `pull_request` delivery from its
|
|||
instance-wide hooks in the `SWARM_EVENTS` stream. The router reads that
|
||||
stream and queues todos for the agents an event concerns: an agent assigned to
|
||||
an issue or pull request gets a todo, one taken off it gets that todo rewritten
|
||||
to say so, and closing it clears them. A push to an open pull request makes
|
||||
it ask swarm-controller to request the review classifier's review. Each agent's harness takes its todos
|
||||
to say so, and closing it clears them. Each agent's harness takes its todos
|
||||
from the `ROUTER_TODOS` stream, so an agent that's down gets them when it's
|
||||
back.
|
||||
|
||||
|
|
|
|||
|
|
@ -224,7 +224,7 @@ in two groups. Each **built-in** flow has an on/off switch; switching one off
|
|||
stops the router running it from its next event, and switching it on again
|
||||
restores it. Each **custom** flow shows its name and whether it's on. Every
|
||||
flow draws as an indented tree: the forge events it starts from, the
|
||||
conditions under it, and the todo or review request each branch ends in.
|
||||
conditions under it, and the todo each branch ends in.
|
||||
|
||||
A built-in flow can be **duplicated** into a custom one, and **new flow** starts a
|
||||
custom flow with just a source; either opens the flow in the editor. A custom
|
||||
|
|
|
|||
|
|
@ -73,14 +73,6 @@ test("describeNode: says what each kind does", () => {
|
|||
"clear todos forge:{repo}#{number}:* of {todo_holders}",
|
||||
);
|
||||
assert.equal(describeNode(flow.nodes[4]), "todo to triage, key k: look");
|
||||
assert.equal(
|
||||
describeNode({
|
||||
id: "r",
|
||||
kind: "request_reviewer",
|
||||
reviewer: "review-classifier",
|
||||
}),
|
||||
"request a review from review-classifier",
|
||||
);
|
||||
});
|
||||
|
||||
test("withBuiltin: switching off adds once, switching on removes", () => {
|
||||
|
|
|
|||
|
|
@ -19,8 +19,6 @@ clap-markdown = "0.1"
|
|||
# not about the workspace. See the pin note on `forgejo-api` in the root
|
||||
# `Cargo.toml`.
|
||||
forgejo-api = { workspace = true, features = ["sync"] }
|
||||
# The review rule `pr assign-reviewer` shares with swarm-controller.
|
||||
hive-types.workspace = true
|
||||
url.workspace = true
|
||||
time.workspace = true
|
||||
reqwest = { workspace = true, features = [
|
||||
|
|
|
|||
|
|
@ -346,15 +346,66 @@ fn edit_distance(a: &str, b: &str) -> usize {
|
|||
prev[b.len()]
|
||||
}
|
||||
|
||||
pub(crate) use hive_types::review::ReviewInfo;
|
||||
/// One reviewer's latest verdict on a PR, plus forgejo's `stale` /
|
||||
/// `dismissed` bits.
|
||||
///
|
||||
/// `stale` is set by forgejo when the PR head commit changed after this
|
||||
/// review was submitted (branch protection then wants a fresh review);
|
||||
/// `dismissed` is set when the review was explicitly dismissed. Either way
|
||||
/// the review no longer applies to the current head even though its `state`
|
||||
/// string still reads `APPROVED` / `REQUEST_CHANGES` — so surfacing them
|
||||
/// stops the CLI from reporting a no-longer-valid review as still-good, and
|
||||
/// [`ReviewInfo::superseded`] rolls both into one "doesn't count" check.
|
||||
///
|
||||
/// ⚠️ **`stale` here is not forgejo's flag alone.** That flag is eventually
|
||||
/// consistent: seconds after a push it still reports the pre-push answer, so
|
||||
/// a verdict against the previous head reads as current in exactly the window
|
||||
/// where someone runs the CLI right after pushing. [`latest_reviews`] therefore
|
||||
/// ORs it with a direct comparison of the review's own `commit_id` against the
|
||||
/// PR head — the flag is right *eventually*, the comparison is right
|
||||
/// *immediately*, and either alone is worse than both.
|
||||
pub(crate) struct ReviewInfo {
|
||||
pub login: String,
|
||||
pub state: String,
|
||||
pub stale: bool,
|
||||
pub dismissed: bool,
|
||||
}
|
||||
|
||||
impl ReviewInfo {
|
||||
/// True when the review no longer applies to the current head — stale
|
||||
/// (head moved) or dismissed. Such a verdict neither blocks a merge nor
|
||||
/// counts as a fresh approval.
|
||||
pub(crate) fn superseded(&self) -> bool {
|
||||
self.stale || self.dismissed
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether this review was submitted against a commit that is no longer the
|
||||
/// PR head — the race-free half of the staleness check.
|
||||
///
|
||||
/// Fails **closed on unknowns**, i.e. "not stale": either side missing means
|
||||
/// we cannot show the head moved, and the cost of guessing wrong in that
|
||||
/// direction is one redundant re-review, where the other direction would
|
||||
/// silently void every verdict on the PR (blocking nothing, but reporting a
|
||||
/// ready PR as unreviewed and inviting a re-request that dismisses a real
|
||||
/// approval).
|
||||
fn reviewed_older_head(reviewed_sha: Option<&str>, head_sha: Option<&str>) -> bool {
|
||||
let (Some(head), Some(reviewed)) = (head_sha, reviewed_sha) else {
|
||||
return false;
|
||||
};
|
||||
// Both emptiness checks matter, and for the same reason: a blank string
|
||||
// is a value the forge sent, not a sha it has. Treating one as real would
|
||||
// make every review compare unequal and mark the whole PR stale — the
|
||||
// direction this whole function exists to avoid.
|
||||
!head.is_empty() && !reviewed.is_empty() && reviewed != head
|
||||
}
|
||||
|
||||
/// Latest non-comment review per reviewer on a PR. Reviews come
|
||||
/// oldest-first, so a later verdict from the same user supersedes an
|
||||
/// earlier one; `COMMENT` / `PENDING` reviews carry no verdict and are
|
||||
/// skipped. Shared by `pr status` (health view + readiness verdict),
|
||||
/// `pr merge` (pre-merge changes-requested gate) and `pr assign-reviewer`,
|
||||
/// and by swarm-controller's `request-reviewer` sink, through
|
||||
/// [`hive_types::review`], so the verdict semantics stay in one place.
|
||||
/// skipped. Shared by `pr status` (health view + readiness verdict) and
|
||||
/// `pr merge` (pre-merge changes-requested gate) so the verdict semantics
|
||||
/// stay in one place.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
|
|
@ -388,16 +439,32 @@ pub(crate) fn latest_reviews(client: &Client, repo: &str, pr: u64) -> Result<Vec
|
|||
break;
|
||||
}
|
||||
}
|
||||
Ok(hive_types::review::latest_reviews(
|
||||
reviews.iter().map(|r| hive_types::review::Listed {
|
||||
login: r.user.as_ref().and_then(|u| u.login.as_deref()),
|
||||
state: r.state.as_deref(),
|
||||
commit_id: r.commit_id.as_deref(),
|
||||
stale: r.stale,
|
||||
dismissed: r.dismissed,
|
||||
}),
|
||||
head_sha.as_deref(),
|
||||
))
|
||||
let mut latest: Vec<ReviewInfo> = Vec::new();
|
||||
for r in &reviews {
|
||||
let Some(login) = r.user.as_ref().and_then(|u| u.login.as_deref()) else {
|
||||
continue;
|
||||
};
|
||||
let st = r.state.as_deref().unwrap_or("");
|
||||
if st == "COMMENT" || st == "PENDING" || st.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let stale = r.stale.unwrap_or(false)
|
||||
|| reviewed_older_head(r.commit_id.as_deref(), head_sha.as_deref());
|
||||
let dismissed = r.dismissed.unwrap_or(false);
|
||||
if let Some(slot) = latest.iter_mut().find(|info| info.login == login) {
|
||||
st.clone_into(&mut slot.state);
|
||||
slot.stale = stale;
|
||||
slot.dismissed = dismissed;
|
||||
} else {
|
||||
latest.push(ReviewInfo {
|
||||
login: login.to_owned(),
|
||||
state: st.to_owned(),
|
||||
stale,
|
||||
dismissed,
|
||||
});
|
||||
}
|
||||
}
|
||||
Ok(latest)
|
||||
}
|
||||
|
||||
/// The current dependency list for an issue or PR — each entry names
|
||||
|
|
@ -617,7 +684,7 @@ pub(crate) fn attachment_json(assets: Option<&[Attachment]>) -> Vec<Value> {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{NullableVec, pct_encode, refuse_json};
|
||||
use super::{NullableVec, pct_encode, refuse_json, reviewed_older_head};
|
||||
|
||||
#[test]
|
||||
fn refuse_json_bails_with_the_verb_and_reason() {
|
||||
|
|
@ -648,6 +715,32 @@ mod tests {
|
|||
assert_eq!(v.0, vec![1, 2, 3]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn review_on_an_older_commit_is_stale() {
|
||||
// The bug this exists for: forgejo still reports `stale: false` here
|
||||
// in the seconds after a push, so the comparison has to catch it.
|
||||
assert!(reviewed_older_head(Some("81292f14"), Some("f4c47088")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn review_on_the_current_head_is_not_stale() {
|
||||
assert!(!reviewed_older_head(Some("f4c47088"), Some("f4c47088")));
|
||||
}
|
||||
|
||||
/// Every unknown fails *toward* keeping the verdict. Voiding every review
|
||||
/// on a forge that stopped reporting one of these would be a far louder
|
||||
/// wrong answer than one redundant re-review.
|
||||
#[test]
|
||||
fn unknown_commit_or_head_is_not_stale() {
|
||||
assert!(!reviewed_older_head(None, Some("f4c47088")));
|
||||
assert!(!reviewed_older_head(Some("81292f14"), None));
|
||||
assert!(!reviewed_older_head(Some(""), Some("f4c47088")));
|
||||
// Absent and blank have to behave the same on BOTH sides — a blank
|
||||
// head that counted as real would mark every review on the PR stale.
|
||||
assert!(!reviewed_older_head(Some("81292f14"), Some("")));
|
||||
assert!(!reviewed_older_head(Some(""), Some("")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pct_encode_passes_unreserved_through() {
|
||||
// Plain artifact names round-trip verbatim — no performance
|
||||
|
|
|
|||
|
|
@ -55,8 +55,10 @@ pub fn run(client: &Client, args: Args) -> Result<()> {
|
|||
// Guard the add path only — withdrawing a request is always safe.
|
||||
// Re-requesting from someone who already reviewed dismisses that review
|
||||
// instead of no-op'ing (see the module doc-comment above).
|
||||
let reviews = super::latest_reviews(client, client.repo()?, args.number)?;
|
||||
if let Some(existing) = hive_types::review::standing(&reviews, &args.user) {
|
||||
if let Some(existing) = super::latest_reviews(client, client.repo()?, args.number)?
|
||||
.into_iter()
|
||||
.find(|r| r.login == args.user && !r.superseded())
|
||||
{
|
||||
println!(
|
||||
"not re-requesting: {} already has a standing {} review on #{} (re-requesting would dismiss it)",
|
||||
args.user, existing.state, args.number
|
||||
|
|
|
|||
|
|
@ -17,10 +17,6 @@ Foundational shared **newtypes** for the hyperhive workspace. A zero-dependency
|
|||
- **`term_msg`** — the icons and summary prefixes of a terminal row that sent a
|
||||
message or started a turn from one, and the parsers that read the recipient
|
||||
or sender back out.
|
||||
- **`review`** — which of a PR's reviews still stand (latest verdict per
|
||||
reviewer, stale or dismissed ones superseded). `hive-forge pr assign-reviewer`
|
||||
and the notification router's `request-reviewer` sink share it, so both skip a
|
||||
request on the same grounds.
|
||||
|
||||
The crate is deliberately a leaf with the smallest possible dependency footprint
|
||||
so anything in the workspace can depend on it. See `docs/process/conventions.md`
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
//! Foundational shared types for the hyperhive workspace: [`Ident`], [`Permission`],
|
||||
//! the [`term_msg`] row markers and the [`review`] rule.
|
||||
//! Foundational shared types for the hyperhive workspace: [`Ident`], [`Permission`]
|
||||
//! and the [`term_msg`] row markers.
|
||||
//!
|
||||
//! A zero-dependency (bar `serde`) leaf crate so every wire-type crate
|
||||
//! (`hive-sh4re`, `hive-host-sock`, `hive-core-agent-sock`) and both binaries
|
||||
|
|
@ -11,7 +11,6 @@ mod permission;
|
|||
|
||||
pub use permission::Permission;
|
||||
|
||||
pub mod review;
|
||||
pub mod term_msg;
|
||||
|
||||
/// The environment variable nix uses to hand this process the blacklist of
|
||||
|
|
|
|||
|
|
@ -1,231 +0,0 @@
|
|||
//! Which of a PR's reviews still stand: the rule `hive-forge pr
|
||||
//! assign-reviewer` and the notification router's `request-reviewer` sink
|
||||
//! share, so a request is skipped or made on the same grounds in both.
|
||||
//!
|
||||
//! The forge keeps a review's `state` (`APPROVED`, `REQUEST_CHANGES`, …) after
|
||||
//! the head moves or the review is dismissed, so reading the state alone
|
||||
//! reports a review that no longer applies as still good. [`latest_reviews`]
|
||||
//! folds a PR's reviews to one verdict per reviewer, and
|
||||
//! [`ReviewInfo::superseded`] says whether it still counts.
|
||||
//!
|
||||
//! Re-requesting a review from a reviewer whose review stands makes the forge
|
||||
//! dismiss it, approval included. [`standing`] is the check a caller makes
|
||||
//! before it requests.
|
||||
|
||||
/// One review as the forge lists it, reduced to what the rule reads. Every
|
||||
/// field is optional because the forge's listing is.
|
||||
#[derive(Clone, Copy, Debug, Default)]
|
||||
pub struct Listed<'a> {
|
||||
pub login: Option<&'a str>,
|
||||
pub state: Option<&'a str>,
|
||||
/// The commit the review was submitted against.
|
||||
pub commit_id: Option<&'a str>,
|
||||
/// The forge's own `stale` flag.
|
||||
pub stale: Option<bool>,
|
||||
pub dismissed: Option<bool>,
|
||||
}
|
||||
|
||||
/// One reviewer's latest verdict on a PR, plus the `stale` / `dismissed` bits.
|
||||
///
|
||||
/// `stale` is set by the forge when the PR head commit changed after this
|
||||
/// review was submitted; `dismissed` is set when the review was explicitly
|
||||
/// dismissed. Either way the review no longer applies to the current head even
|
||||
/// though its `state` string still reads `APPROVED` / `REQUEST_CHANGES`.
|
||||
///
|
||||
/// ⚠️ **`stale` here is not the forge's flag alone.** That flag is eventually
|
||||
/// consistent: seconds after a push it still reports the pre-push answer, so a
|
||||
/// verdict against the previous head reads as current in exactly the window
|
||||
/// where a push has just happened. [`latest_reviews`] therefore ORs it with a
|
||||
/// direct comparison of the review's own `commit_id` against the PR head: the
|
||||
/// flag is right *eventually*, the comparison is right *immediately*.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct ReviewInfo {
|
||||
pub login: String,
|
||||
pub state: String,
|
||||
pub stale: bool,
|
||||
pub dismissed: bool,
|
||||
}
|
||||
|
||||
impl ReviewInfo {
|
||||
/// True when the review no longer applies to the current head: stale
|
||||
/// (head moved) or dismissed. Such a verdict neither blocks a merge nor
|
||||
/// counts as a fresh approval.
|
||||
#[must_use]
|
||||
pub fn superseded(&self) -> bool {
|
||||
self.stale || self.dismissed
|
||||
}
|
||||
}
|
||||
|
||||
/// `login`'s review that still stands, if it has one. Its presence is the
|
||||
/// reason not to request a review from `login` again.
|
||||
#[must_use]
|
||||
pub fn standing<'a>(reviews: &'a [ReviewInfo], login: &str) -> Option<&'a ReviewInfo> {
|
||||
reviews.iter().find(|r| r.login == login && !r.superseded())
|
||||
}
|
||||
|
||||
/// Whether this review was submitted against a commit that is no longer the PR
|
||||
/// head: the race-free half of the staleness check.
|
||||
///
|
||||
/// Fails **closed on unknowns**, i.e. "not stale": either side missing means
|
||||
/// the head's move can't be shown, and guessing wrong in that direction costs
|
||||
/// one redundant re-review, where the other direction would void every
|
||||
/// verdict on the PR and invite a re-request that dismisses a real approval.
|
||||
#[must_use]
|
||||
pub fn reviewed_older_head(reviewed_sha: Option<&str>, head_sha: Option<&str>) -> bool {
|
||||
let (Some(head), Some(reviewed)) = (head_sha, reviewed_sha) else {
|
||||
return false;
|
||||
};
|
||||
// Both emptiness checks matter, and for the same reason: a blank string is
|
||||
// a value the forge sent, not a sha it has. Treating one as real would make
|
||||
// every review compare unequal and mark the whole PR stale.
|
||||
!head.is_empty() && !reviewed.is_empty() && reviewed != head
|
||||
}
|
||||
|
||||
/// Latest non-comment review per reviewer. `reviews` come oldest first, so a
|
||||
/// later verdict from the same reviewer supersedes an earlier one; `COMMENT`
|
||||
/// and `PENDING` reviews carry no verdict and are skipped. `head_sha` is the
|
||||
/// PR's current head, or `None` when it couldn't be read, which leaves the
|
||||
/// forge's `stale` flag alone to decide.
|
||||
#[must_use]
|
||||
pub fn latest_reviews<'a>(
|
||||
reviews: impl IntoIterator<Item = Listed<'a>>,
|
||||
head_sha: Option<&str>,
|
||||
) -> Vec<ReviewInfo> {
|
||||
let mut latest: Vec<ReviewInfo> = Vec::new();
|
||||
for r in reviews {
|
||||
let Some(login) = r.login else { continue };
|
||||
let verdict = r.state.unwrap_or("");
|
||||
if verdict == "COMMENT" || verdict == "PENDING" || verdict.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let stale = r.stale.unwrap_or(false) || reviewed_older_head(r.commit_id, head_sha);
|
||||
let dismissed = r.dismissed.unwrap_or(false);
|
||||
if let Some(slot) = latest.iter_mut().find(|info| info.login == login) {
|
||||
verdict.clone_into(&mut slot.state);
|
||||
slot.stale = stale;
|
||||
slot.dismissed = dismissed;
|
||||
} else {
|
||||
latest.push(ReviewInfo {
|
||||
login: login.to_owned(),
|
||||
state: verdict.to_owned(),
|
||||
stale,
|
||||
dismissed,
|
||||
});
|
||||
}
|
||||
}
|
||||
latest
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn review<'a>(login: &'a str, state: &'a str, commit: &'a str) -> Listed<'a> {
|
||||
Listed {
|
||||
login: Some(login),
|
||||
state: Some(state),
|
||||
commit_id: Some(commit),
|
||||
..Listed::default()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn review_on_an_older_commit_is_stale() {
|
||||
// The forge still reports `stale: false` here in the seconds after a
|
||||
// push, so the comparison has to catch it.
|
||||
assert!(reviewed_older_head(Some("81292f14"), Some("f4c47088")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn review_on_the_current_head_is_not_stale() {
|
||||
assert!(!reviewed_older_head(Some("f4c47088"), Some("f4c47088")));
|
||||
}
|
||||
|
||||
/// Every unknown fails *toward* keeping the verdict. Voiding every review
|
||||
/// on a forge that stopped reporting one of these would be a far louder
|
||||
/// wrong answer than one redundant re-review.
|
||||
#[test]
|
||||
fn unknown_commit_or_head_is_not_stale() {
|
||||
assert!(!reviewed_older_head(None, Some("f4c47088")));
|
||||
assert!(!reviewed_older_head(Some("81292f14"), None));
|
||||
assert!(!reviewed_older_head(Some(""), Some("f4c47088")));
|
||||
// Absent and blank have to behave the same on BOTH sides: a blank head
|
||||
// that counted as real would mark every review on the PR stale.
|
||||
assert!(!reviewed_older_head(Some("81292f14"), Some("")));
|
||||
assert!(!reviewed_older_head(Some(""), Some("")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_review_of_the_current_head_stands_and_one_of_an_older_head_does_not() {
|
||||
let reviews = latest_reviews(
|
||||
[
|
||||
review("review-classifier", "APPROVED", "new"),
|
||||
review("argus", "APPROVED", "old"),
|
||||
],
|
||||
Some("new"),
|
||||
);
|
||||
assert_eq!(
|
||||
standing(&reviews, "review-classifier").map(|r| r.state.as_str()),
|
||||
Some("APPROVED")
|
||||
);
|
||||
// The forge's flag is still false for argus: only the commit says so.
|
||||
assert!(standing(&reviews, "argus").is_none());
|
||||
// Control: a reviewer who never reviewed has nothing standing.
|
||||
assert!(standing(&reviews, "iris").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_dismissed_or_flagged_review_does_not_stand() {
|
||||
let reviews = latest_reviews(
|
||||
[
|
||||
Listed {
|
||||
dismissed: Some(true),
|
||||
..review("a", "APPROVED", "new")
|
||||
},
|
||||
Listed {
|
||||
stale: Some(true),
|
||||
..review("b", "REQUEST_CHANGES", "new")
|
||||
},
|
||||
review("c", "APPROVED", "new"),
|
||||
],
|
||||
Some("new"),
|
||||
);
|
||||
assert!(standing(&reviews, "a").is_none());
|
||||
assert!(standing(&reviews, "b").is_none());
|
||||
assert!(standing(&reviews, "c").is_some(), "control");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_latest_verdict_per_reviewer_wins_and_comments_carry_none() {
|
||||
let reviews = latest_reviews(
|
||||
[
|
||||
review("a", "REQUEST_CHANGES", "old"),
|
||||
review("a", "APPROVED", "new"),
|
||||
review("a", "COMMENT", "new"),
|
||||
review("b", "PENDING", "new"),
|
||||
Listed {
|
||||
login: None,
|
||||
..review("x", "APPROVED", "new")
|
||||
},
|
||||
],
|
||||
Some("new"),
|
||||
);
|
||||
assert_eq!(
|
||||
reviews,
|
||||
[ReviewInfo {
|
||||
login: "a".to_owned(),
|
||||
state: "APPROVED".to_owned(),
|
||||
stale: false,
|
||||
dismissed: false,
|
||||
}]
|
||||
);
|
||||
}
|
||||
|
||||
/// With the head unread, the forge's flag decides alone, so a review of an
|
||||
/// unknown commit stands.
|
||||
#[test]
|
||||
fn an_unread_head_leaves_the_forges_flag_to_decide() {
|
||||
let reviews = latest_reviews([review("a", "APPROVED", "old")], None);
|
||||
assert!(standing(&reviews, "a").is_some());
|
||||
}
|
||||
}
|
||||
|
|
@ -55,7 +55,6 @@ pub mod legacy_tokens;
|
|||
pub mod objects;
|
||||
pub mod operator_profiles;
|
||||
pub mod pull_diff;
|
||||
pub mod review_request;
|
||||
pub mod runner_token;
|
||||
pub mod site_admin;
|
||||
|
||||
|
|
@ -1761,12 +1760,12 @@ fn hook_registration(kind: DeliveryKind) -> (&'static [&'static str], HookScope<
|
|||
// classifier's account access can request its review.
|
||||
DeliveryKind::ReviewRequest => (&["pull_request_review_request"], HookScope::Instance),
|
||||
// Instance-wide, for the notification router, which reads the assign
|
||||
// events and a push to a PR's branch. They are named: Forgejo's web
|
||||
// form shows each as a box of its own, so a hook edited there can drop
|
||||
// one while keeping `issues` or `pull_request`.
|
||||
// events. They are named: Forgejo's web form shows assignment as a
|
||||
// box of its own, so a hook edited there can drop it while keeping
|
||||
// `issues` or `pull_request`.
|
||||
DeliveryKind::Issues => (&["issues", "issue_assign"], HookScope::Instance),
|
||||
DeliveryKind::PullRequest => (
|
||||
&["pull_request", "pull_request_assign", "pull_request_sync"],
|
||||
&["pull_request", "pull_request_assign"],
|
||||
HookScope::Instance,
|
||||
),
|
||||
}
|
||||
|
|
@ -2112,7 +2111,7 @@ mod tests {
|
|||
assert_eq!(events(DeliveryKind::Issues), ["issues", "issue_assign"]);
|
||||
assert_eq!(
|
||||
events(DeliveryKind::PullRequest),
|
||||
["pull_request", "pull_request_assign", "pull_request_sync"]
|
||||
["pull_request", "pull_request_assign"]
|
||||
);
|
||||
// The events a stored flow's source may name are the ones the hooks
|
||||
// deliver to the router.
|
||||
|
|
|
|||
|
|
@ -1,194 +0,0 @@
|
|||
//! A PR's reviews, and asking for one more, under the controller's token.
|
||||
//!
|
||||
//! The router's `request_reviewer` sink reaches the forge through here
|
||||
//! ([`crate::request_reviewer`]). Which reviews still stand is
|
||||
//! [`hive_types::review`], the rule `hive-forge pr assign-reviewer` applies
|
||||
//! too.
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use forgejo_api::structs::PullReviewRequestOptions;
|
||||
use hive_types::review::{Listed, ReviewInfo, latest_reviews};
|
||||
|
||||
use super::Client;
|
||||
|
||||
/// One review listing takes this many pages at most, 50 reviews a page.
|
||||
const MAX_PAGES: u32 = 10;
|
||||
const PAGE_SIZE: u32 = 50;
|
||||
|
||||
impl Client {
|
||||
/// The latest verdict of each reviewer of PR `number` of `owner/repo`,
|
||||
/// judged against the PR's head as it is now.
|
||||
///
|
||||
/// # Errors
|
||||
/// A forge failure, with the [`forgejo_api::ForgejoError`] underneath.
|
||||
pub async fn pull_reviews(
|
||||
&self,
|
||||
owner: &str,
|
||||
repo: &str,
|
||||
number: i64,
|
||||
) -> Result<Vec<ReviewInfo>> {
|
||||
let pull = self
|
||||
.api
|
||||
.call(|api| api.repo_get_pull_request(owner, repo, number).into_future())
|
||||
.await
|
||||
.with_context(|| format!("read PR {number} of {owner}/{repo}"))?;
|
||||
let head = pull.head.and_then(|h| h.sha);
|
||||
let mut reviews = Vec::new();
|
||||
for page in 1..=MAX_PAGES {
|
||||
let (_, batch) = self
|
||||
.api
|
||||
.call(|api| {
|
||||
api.repo_list_pull_reviews(owner, repo, number)
|
||||
.page(page)
|
||||
.page_size(PAGE_SIZE)
|
||||
.into_future()
|
||||
})
|
||||
.await
|
||||
.with_context(|| format!("list the reviews of PR {number} of {owner}/{repo}"))?;
|
||||
let short = batch.len() < PAGE_SIZE as usize;
|
||||
reviews.extend(batch);
|
||||
if short {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Ok(latest_reviews(
|
||||
reviews.iter().map(|r| Listed {
|
||||
login: r.user.as_ref().and_then(|u| u.login.as_deref()),
|
||||
state: r.state.as_deref(),
|
||||
commit_id: r.commit_id.as_deref(),
|
||||
stale: r.stale,
|
||||
dismissed: r.dismissed,
|
||||
}),
|
||||
head.as_deref(),
|
||||
))
|
||||
}
|
||||
|
||||
/// Ask `reviewer` to review PR `number` of `owner/repo`. From a reviewer
|
||||
/// whose review stands, the forge dismisses it: check
|
||||
/// [`hive_types::review::standing`] first.
|
||||
///
|
||||
/// # Errors
|
||||
/// The forge refused: no such reviewer, the PR's author, a reviewer with
|
||||
/// no access to the repo.
|
||||
pub async fn request_review(
|
||||
&self,
|
||||
owner: &str,
|
||||
repo: &str,
|
||||
number: i64,
|
||||
reviewer: &str,
|
||||
) -> Result<()> {
|
||||
let body = PullReviewRequestOptions {
|
||||
reviewers: Some(vec![reviewer.to_owned()]),
|
||||
team_reviewers: None,
|
||||
};
|
||||
self.api
|
||||
.call(|api| {
|
||||
api.repo_create_pull_review_requests(owner, repo, number, body.clone())
|
||||
.into_future()
|
||||
})
|
||||
.await
|
||||
.with_context(|| {
|
||||
format!("request {reviewer}'s review of PR {number} of {owner}/{repo}")
|
||||
})?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use axum::http::{Method, StatusCode, Uri};
|
||||
|
||||
use super::*;
|
||||
|
||||
/// A stand-in forge holding PR 7 of `o/r` at head `new`, with `reviews`
|
||||
/// listed, answering the request post with `post`. Records each request as
|
||||
/// method, path and body.
|
||||
async fn forge(
|
||||
reviews: &'static str,
|
||||
post: u16,
|
||||
) -> (Client, Arc<Mutex<Vec<(String, String, String)>>>) {
|
||||
let seen = Arc::new(Mutex::new(Vec::new()));
|
||||
let recorded = Arc::clone(&seen);
|
||||
let app = axum::Router::new().fallback(
|
||||
move |method: Method, uri: Uri, body: axum::body::Bytes| {
|
||||
let recorded = Arc::clone(&recorded);
|
||||
async move {
|
||||
recorded.lock().unwrap().push((
|
||||
method.to_string(),
|
||||
uri.path().to_owned(),
|
||||
String::from_utf8_lossy(&body).into_owned(),
|
||||
));
|
||||
match (method.as_str(), uri.path()) {
|
||||
("GET", "/api/v1/repos/o/r/pulls/7") => (
|
||||
StatusCode::OK,
|
||||
r#"{"number":7,"head":{"sha":"new"},"requested_reviewers":[]}"#
|
||||
.to_owned(),
|
||||
),
|
||||
("GET", "/api/v1/repos/o/r/pulls/7/reviews") => {
|
||||
(StatusCode::OK, reviews.to_owned())
|
||||
}
|
||||
("POST", "/api/v1/repos/o/r/pulls/7/requested_reviewers") => (
|
||||
StatusCode::from_u16(post).unwrap(),
|
||||
if post == 201 {
|
||||
"[]".to_owned()
|
||||
} else {
|
||||
r#"{"message":"refused"}"#.to_owned()
|
||||
},
|
||||
),
|
||||
_ => (StatusCode::NOT_FOUND, r#"{"message":"no"}"#.to_owned()),
|
||||
}
|
||||
}
|
||||
},
|
||||
);
|
||||
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let url = url::Url::parse(&format!("http://{}/", listener.local_addr().unwrap())).unwrap();
|
||||
tokio::spawn(async move { axum::serve(listener, app).await });
|
||||
(Client::for_test_forge(url), seen)
|
||||
}
|
||||
|
||||
const REVIEWS: &str = r#"[
|
||||
{"id":1,"user":{"login":"review-classifier"},"state":"APPROVED","commit_id":"old","stale":false,"dismissed":false},
|
||||
{"id":2,"user":{"login":"argus"},"state":"APPROVED","commit_id":"new","stale":false,"dismissed":false}
|
||||
]"#;
|
||||
|
||||
/// The head is read from the PR, so a review of an older commit is
|
||||
/// superseded though the forge still reports `stale: false`.
|
||||
#[tokio::test]
|
||||
async fn reviews_are_judged_against_the_head_read_now() {
|
||||
let (client, _) = forge(REVIEWS, 201).await;
|
||||
let reviews = client.pull_reviews("o", "r", 7).await.unwrap();
|
||||
let standing = |login| hive_types::review::standing(&reviews, login).is_some();
|
||||
assert!(
|
||||
!standing("review-classifier"),
|
||||
"reviewed `old`, head is `new`"
|
||||
);
|
||||
assert!(standing("argus"), "control: reviewed the head");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_request_posts_the_reviewer_to_the_pull_requests_reviewer_list() {
|
||||
let (client, seen) = forge("[]", 201).await;
|
||||
client
|
||||
.request_review("o", "r", 7, "review-classifier")
|
||||
.await
|
||||
.unwrap();
|
||||
let seen = seen.lock().unwrap();
|
||||
let (method, path, body) = seen.last().unwrap();
|
||||
assert_eq!(method, "POST");
|
||||
assert_eq!(path, "/api/v1/repos/o/r/pulls/7/requested_reviewers");
|
||||
let body: serde_json::Value = serde_json::from_str(body).unwrap();
|
||||
assert_eq!(body["reviewers"], serde_json::json!(["review-classifier"]));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_refused_request_is_an_error_naming_the_reviewer() {
|
||||
let (client, _) = forge("[]", 422).await;
|
||||
let err = client
|
||||
.request_review("o", "r", 7, "review-classifier")
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert!(format!("{err:#}").contains("review-classifier"), "{err:#}");
|
||||
}
|
||||
}
|
||||
|
|
@ -55,7 +55,6 @@ mod otel_http_client;
|
|||
mod pulls;
|
||||
mod queue_identity;
|
||||
mod read_policy;
|
||||
mod request_reviewer;
|
||||
mod review_classifier;
|
||||
mod routes;
|
||||
mod schedules;
|
||||
|
|
@ -240,11 +239,9 @@ async fn main() -> Result<()> {
|
|||
serve(listener, state).await
|
||||
}
|
||||
|
||||
/// Start taking the queued review requests and answering the notification
|
||||
/// router's, then serve `state` on `listener`.
|
||||
/// Start taking the queued review requests, then serve `state` on `listener`.
|
||||
async fn serve(listener: tokio::net::UnixListener, state: AppState) -> Result<()> {
|
||||
review_classifier::review::spawn_queue(&state);
|
||||
request_reviewer::spawn(&state);
|
||||
axum::serve(listener, build_app(state))
|
||||
.await
|
||||
.context("serving swarm-controller")
|
||||
|
|
|
|||
|
|
@ -294,8 +294,7 @@ mod tests {
|
|||
let Json(view) = get_flows(State(state.clone())).await.expect("get");
|
||||
assert_eq!(view.revision, 0);
|
||||
assert!(view.disabled_builtins.is_empty() && view.flows.is_empty());
|
||||
assert_eq!(view.builtins.len(), 4);
|
||||
assert!(view.builtins.iter().any(|f| f.id == "head-classifier"));
|
||||
assert_eq!(view.builtins.len(), 3);
|
||||
let json = serde_json::to_value(&view).expect("serializes");
|
||||
assert_eq!(json["schema"]["limits"]["max_nodes"], 64);
|
||||
assert_eq!(json["schema"]["node_kinds"][0]["kind"], "source");
|
||||
|
|
@ -322,7 +321,7 @@ mod tests {
|
|||
.expect("read")
|
||||
.expect("stored");
|
||||
let doc = Document::decode(&bytes).expect("the router reads it");
|
||||
assert_eq!(doc.active().0.len(), 4, "3 built-ins and the custom flow");
|
||||
assert_eq!(doc.active().0.len(), 3, "2 built-ins and the custom flow");
|
||||
|
||||
// A refused save changes nothing: an orphan node, and a built-in that does not exist.
|
||||
let mut orphan = custom("orphan");
|
||||
|
|
|
|||
|
|
@ -1,388 +0,0 @@
|
|||
//! Requesting a review for the notification router, which holds no forge
|
||||
//! token.
|
||||
//!
|
||||
//! The router sends a [`RequestReviewer`] on
|
||||
//! `swarm_wire::router_sink::REQUEST_REVIEWER_SUBJECT` and [`spawn`]'s
|
||||
//! responder answers with an [`Outcome`] on the reply subject, having acted
|
||||
//! under the controller's token. A reviewer whose review of the PR's head
|
||||
//! already stands is skipped: requesting it again makes the forge dismiss it,
|
||||
//! approval included. What stands is [`hive_types::review`], the rule
|
||||
//! `hive-forge pr assign-reviewer` applies too.
|
||||
//!
|
||||
//! The request may be repeated (the router's event comes back after a failed
|
||||
//! step): a reviewer with a request pending is a no-op for the forge, and one
|
||||
//! who has reviewed since stands and is skipped.
|
||||
|
||||
use std::future::Future;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use futures_util::StreamExt as _;
|
||||
use hive_types::review::{ReviewInfo, standing};
|
||||
use swarm_wire::router_sink::{Outcome, REQUEST_REVIEWER_SUBJECT, RequestReviewer, WORK_DEADLINE};
|
||||
|
||||
use crate::forge;
|
||||
use crate::jobq::Late;
|
||||
use crate::review_classifier::split_repo;
|
||||
use crate::routes::AppState;
|
||||
|
||||
/// How long to wait before subscribing again.
|
||||
const RETRY: Duration = Duration::from_secs(5);
|
||||
|
||||
/// The forge as the responder uses it: [`forge::Client`] in production, a fake
|
||||
/// in the tests.
|
||||
pub(crate) trait ReviewForge {
|
||||
fn reviews(
|
||||
&self,
|
||||
owner: &str,
|
||||
repo: &str,
|
||||
number: i64,
|
||||
) -> impl Future<Output = anyhow::Result<Vec<ReviewInfo>>> + Send;
|
||||
|
||||
fn request(
|
||||
&self,
|
||||
owner: &str,
|
||||
repo: &str,
|
||||
number: i64,
|
||||
reviewer: &str,
|
||||
) -> impl Future<Output = anyhow::Result<()>> + Send;
|
||||
}
|
||||
|
||||
impl ReviewForge for forge::Client {
|
||||
async fn reviews(
|
||||
&self,
|
||||
owner: &str,
|
||||
repo: &str,
|
||||
number: i64,
|
||||
) -> anyhow::Result<Vec<ReviewInfo>> {
|
||||
self.pull_reviews(owner, repo, number).await
|
||||
}
|
||||
|
||||
async fn request(
|
||||
&self,
|
||||
owner: &str,
|
||||
repo: &str,
|
||||
number: i64,
|
||||
reviewer: &str,
|
||||
) -> anyhow::Result<()> {
|
||||
self.request_review(owner, repo, number, reviewer).await
|
||||
}
|
||||
}
|
||||
|
||||
/// Answer the router's review requests for as long as the controller runs.
|
||||
pub(crate) fn spawn(state: &AppState) {
|
||||
let Some(client) = state.status.as_ref().map(|s| s.queue_client()) else {
|
||||
tracing::warn!("no swarm queue is configured on this host; review requests go unanswered");
|
||||
return;
|
||||
};
|
||||
let forge = Arc::clone(&state.forge);
|
||||
tokio::spawn(async move { follow(&client, &forge).await });
|
||||
}
|
||||
|
||||
/// Subscribe to the requests and answer each, and subscribe again if the
|
||||
/// subscription ends.
|
||||
async fn follow<F: ReviewForge>(client: &async_nats::Client, forge: &Late<Arc<F>>) {
|
||||
loop {
|
||||
match client.subscribe(REQUEST_REVIEWER_SUBJECT).await {
|
||||
Ok(mut requests) => {
|
||||
tracing::info!(
|
||||
subject = REQUEST_REVIEWER_SUBJECT,
|
||||
"request reviewer: answering requests"
|
||||
);
|
||||
while let Some(msg) = requests.next().await {
|
||||
let Some(reply) = msg.reply.clone() else {
|
||||
continue;
|
||||
};
|
||||
let outcome = answer(forge.get().map(Arc::as_ref), &msg.payload).await;
|
||||
match &outcome {
|
||||
Outcome::Error(why) => {
|
||||
tracing::warn!(%why, "request reviewer: not requested");
|
||||
}
|
||||
other => {
|
||||
tracing::info!(outcome = %other.encode(), "request reviewer: answered");
|
||||
}
|
||||
}
|
||||
if let Err(e) = client.publish(reply, outcome.encode().into()).await {
|
||||
tracing::warn!(error = %e, "request reviewer: answering failed");
|
||||
}
|
||||
}
|
||||
tracing::warn!("request reviewer: subscription ended");
|
||||
}
|
||||
Err(e) => tracing::warn!(error = %e, "request reviewer: subscribe failed"),
|
||||
}
|
||||
tokio::time::sleep(RETRY).await;
|
||||
}
|
||||
}
|
||||
|
||||
/// What to answer to one request `payload`, `forge` being `None` while the
|
||||
/// controller has no forge access.
|
||||
async fn answer<F: ReviewForge>(forge: Option<&F>, payload: &[u8]) -> Outcome {
|
||||
let request: RequestReviewer = match serde_json::from_slice(payload) {
|
||||
Ok(request) => request,
|
||||
Err(e) => return Outcome::Error(format!("the request does not decode: {e}")),
|
||||
};
|
||||
let Some(forge) = forge else {
|
||||
return Outcome::Error("this controller has no forge access yet".to_owned());
|
||||
};
|
||||
match tokio::time::timeout(WORK_DEADLINE, act(forge, &request)).await {
|
||||
Ok(outcome) => outcome,
|
||||
Err(_) => Outcome::Error(format!(
|
||||
"the forge did not answer within {}s",
|
||||
WORK_DEADLINE.as_secs()
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
async fn act<F: ReviewForge>(forge: &F, request: &RequestReviewer) -> Outcome {
|
||||
let RequestReviewer {
|
||||
repo,
|
||||
number,
|
||||
reviewer,
|
||||
} = request;
|
||||
let (owner, name) = match split_repo(repo) {
|
||||
Ok(split) => split,
|
||||
Err(why) => return Outcome::Error(why),
|
||||
};
|
||||
let Ok(number_i64) = i64::try_from(*number) else {
|
||||
return Outcome::Error(format!("PR number {number} is out of range"));
|
||||
};
|
||||
if reviewer.trim().is_empty() {
|
||||
return Outcome::Error("no reviewer named".to_owned());
|
||||
}
|
||||
let reviews = match forge.reviews(owner, name, number_i64).await {
|
||||
Ok(reviews) => reviews,
|
||||
Err(e) => return Outcome::Error(format!("{e:#}")),
|
||||
};
|
||||
if let Some(review) = standing(&reviews, reviewer) {
|
||||
return Outcome::Skipped(format!(
|
||||
"{reviewer} already has a standing {} review on {repo}#{number}",
|
||||
review.state
|
||||
));
|
||||
}
|
||||
match forge.request(owner, name, number_i64, reviewer).await {
|
||||
Ok(()) => Outcome::Requested,
|
||||
Err(e) => Outcome::Error(format!("{e:#}")),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::sync::Mutex;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[derive(Default)]
|
||||
struct Fake {
|
||||
reviews: Vec<ReviewInfo>,
|
||||
reviews_fail: bool,
|
||||
request_fail: bool,
|
||||
asked: Mutex<Vec<(String, i64, String)>>,
|
||||
}
|
||||
|
||||
impl ReviewForge for Fake {
|
||||
async fn reviews(&self, _: &str, _: &str, _: i64) -> anyhow::Result<Vec<ReviewInfo>> {
|
||||
if self.reviews_fail {
|
||||
anyhow::bail!("the forge is down");
|
||||
}
|
||||
Ok(self.reviews.clone())
|
||||
}
|
||||
|
||||
async fn request(
|
||||
&self,
|
||||
owner: &str,
|
||||
repo: &str,
|
||||
number: i64,
|
||||
reviewer: &str,
|
||||
) -> anyhow::Result<()> {
|
||||
if self.request_fail {
|
||||
anyhow::bail!("422: the reviewer is the PR's author");
|
||||
}
|
||||
self.asked.lock().unwrap().push((
|
||||
format!("{owner}/{repo}"),
|
||||
number,
|
||||
reviewer.to_owned(),
|
||||
));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn review(login: &str, verdict: &str, stale: bool, dismissed: bool) -> ReviewInfo {
|
||||
ReviewInfo {
|
||||
login: login.to_owned(),
|
||||
state: verdict.to_owned(),
|
||||
stale,
|
||||
dismissed,
|
||||
}
|
||||
}
|
||||
|
||||
fn payload(repo: &str, number: u64, reviewer: &str) -> Vec<u8> {
|
||||
serde_json::to_vec(&RequestReviewer {
|
||||
repo: repo.to_owned(),
|
||||
number,
|
||||
reviewer: reviewer.to_owned(),
|
||||
})
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
fn asked(fake: &Fake) -> Vec<(String, i64, String)> {
|
||||
fake.asked.lock().unwrap().clone()
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_reviewer_with_no_review_is_requested() {
|
||||
let fake = Fake::default();
|
||||
let got = answer(Some(&fake), &payload("o/r", 7, "review-classifier")).await;
|
||||
assert_eq!(got, Outcome::Requested);
|
||||
assert_eq!(
|
||||
asked(&fake),
|
||||
[("o/r".to_owned(), 7, "review-classifier".to_owned())]
|
||||
);
|
||||
}
|
||||
|
||||
/// A review that still stands is skipped, because requesting from its
|
||||
/// reviewer would make the forge dismiss it.
|
||||
#[tokio::test]
|
||||
async fn a_standing_review_is_skipped_and_a_stale_or_dismissed_one_is_requested_again() {
|
||||
let reviewer = "review-classifier";
|
||||
let standing = Fake {
|
||||
reviews: vec![review(reviewer, "APPROVED", false, false)],
|
||||
..Fake::default()
|
||||
};
|
||||
let got = answer(Some(&standing), &payload("o/r", 7, reviewer)).await;
|
||||
assert!(
|
||||
matches!(&got, Outcome::Skipped(why) if why.contains("APPROVED") && why.contains("o/r#7")),
|
||||
"{got:?}"
|
||||
);
|
||||
assert!(asked(&standing).is_empty(), "nothing was requested");
|
||||
|
||||
for (stale, dismissed) in [(true, false), (false, true)] {
|
||||
let fake = Fake {
|
||||
reviews: vec![review(reviewer, "APPROVED", stale, dismissed)],
|
||||
..Fake::default()
|
||||
};
|
||||
let got = answer(Some(&fake), &payload("o/r", 7, reviewer)).await;
|
||||
assert_eq!(
|
||||
got,
|
||||
Outcome::Requested,
|
||||
"stale {stale}, dismissed {dismissed}"
|
||||
);
|
||||
assert_eq!(asked(&fake).len(), 1);
|
||||
}
|
||||
|
||||
// Another reviewer's standing review is no reason to skip this one.
|
||||
let other = Fake {
|
||||
reviews: vec![review("argus", "APPROVED", false, false)],
|
||||
..Fake::default()
|
||||
};
|
||||
let got = answer(Some(&other), &payload("o/r", 7, reviewer)).await;
|
||||
assert_eq!(got, Outcome::Requested);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_forge_failure_is_an_error_and_a_failed_read_requests_nothing() {
|
||||
let down = Fake {
|
||||
reviews_fail: true,
|
||||
..Fake::default()
|
||||
};
|
||||
let got = answer(Some(&down), &payload("o/r", 7, "review-classifier")).await;
|
||||
assert!(
|
||||
matches!(&got, Outcome::Error(e) if e.contains("the forge is down")),
|
||||
"{got:?}"
|
||||
);
|
||||
assert!(asked(&down).is_empty());
|
||||
|
||||
let refused = Fake {
|
||||
request_fail: true,
|
||||
..Fake::default()
|
||||
};
|
||||
let got = answer(Some(&refused), &payload("o/r", 7, "review-classifier")).await;
|
||||
assert!(
|
||||
matches!(&got, Outcome::Error(e) if e.contains("422")),
|
||||
"{got:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_request_that_is_malformed_or_has_no_forge_is_an_error() {
|
||||
let fake = Fake::default();
|
||||
for bad in [
|
||||
b"not json".to_vec(),
|
||||
payload("no-slash", 7, "review-classifier"),
|
||||
payload("o/r", 7, " "),
|
||||
payload("o/r", u64::MAX, "review-classifier"),
|
||||
] {
|
||||
let got = answer(Some(&fake), &bad).await;
|
||||
assert!(
|
||||
matches!(got, Outcome::Error(_)),
|
||||
"{:?}",
|
||||
String::from_utf8_lossy(&bad)
|
||||
);
|
||||
}
|
||||
assert!(asked(&fake).is_empty());
|
||||
let none: Option<&Fake> = None;
|
||||
let got = answer(none, &payload("o/r", 7, "review-classifier")).await;
|
||||
assert!(
|
||||
matches!(&got, Outcome::Error(e) if e.contains("no forge")),
|
||||
"{got:?}"
|
||||
);
|
||||
}
|
||||
|
||||
/// The built-in flow's reviewer is the account the controller makes.
|
||||
#[test]
|
||||
fn the_built_in_flow_requests_the_classifiers_account() {
|
||||
assert_eq!(
|
||||
swarm_wire::notify_flows::CLASSIFIER_ACCOUNT,
|
||||
forge::classifier_account::LOGIN
|
||||
);
|
||||
}
|
||||
|
||||
/// Against a real server: a request sent the way the router sends it comes
|
||||
/// back with the outcome, and a subscriber before the controller is up
|
||||
/// leaves the sender with no responders.
|
||||
#[tokio::test]
|
||||
async fn a_request_over_the_queue_is_answered_with_its_outcome() {
|
||||
let nats = crate::test_nats::TestNats::start();
|
||||
let router = nats.client().await;
|
||||
let ask = |body: Vec<u8>| {
|
||||
let router = router.clone();
|
||||
async move {
|
||||
let sent = async_nats::Request::new()
|
||||
.payload(body.into())
|
||||
.timeout(Some(Duration::from_secs(5)));
|
||||
router.send_request(REQUEST_REVIEWER_SUBJECT, sent).await
|
||||
}
|
||||
};
|
||||
assert!(
|
||||
ask(payload("o/r", 7, "review-classifier")).await.is_err(),
|
||||
"no controller listening"
|
||||
);
|
||||
|
||||
let fake = Arc::new(Fake {
|
||||
reviews: vec![review("argus", "APPROVED", false, false)],
|
||||
..Fake::default()
|
||||
});
|
||||
let late: Late<Arc<Fake>> = Arc::default();
|
||||
late.set(Arc::clone(&fake)).ok().unwrap();
|
||||
let controller = nats.client().await;
|
||||
tokio::spawn(async move { follow(&controller, &late).await });
|
||||
// The subscription is processed once a request gets through.
|
||||
let mut reply = None;
|
||||
for _ in 0..50 {
|
||||
if let Ok(msg) = ask(payload("o/r", 7, "review-classifier")).await {
|
||||
reply = Some(msg);
|
||||
break;
|
||||
}
|
||||
tokio::time::sleep(Duration::from_millis(100)).await;
|
||||
}
|
||||
let reply = reply.expect("the controller answers");
|
||||
assert_eq!(
|
||||
Outcome::decode(&String::from_utf8_lossy(&reply.payload)),
|
||||
Outcome::Requested
|
||||
);
|
||||
let skipped = ask(payload("o/r", 7, "argus")).await.unwrap();
|
||||
assert!(matches!(
|
||||
Outcome::decode(&String::from_utf8_lossy(&skipped.payload)),
|
||||
Outcome::Skipped(_)
|
||||
));
|
||||
}
|
||||
}
|
||||
|
|
@ -468,34 +468,6 @@ async fn rules(
|
|||
owner: &str,
|
||||
name: &str,
|
||||
) -> Result<Json<Rules>, ProblemDetails> {
|
||||
match load_rules(files, owner, name).await? {
|
||||
Loaded::Rules(rules) => Ok(Json(rules)),
|
||||
Loaded::Absent { branch } => {
|
||||
let [yml, json] = RULES_PATHS;
|
||||
Err(error_problem(
|
||||
StatusCode::NOT_FOUND,
|
||||
&format!("{owner}/{name}'s default branch {branch} holds neither {yml} nor {json}"),
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// What the default branch holds of a repo's rules.
|
||||
enum Loaded {
|
||||
Rules(Rules),
|
||||
/// Neither rules file exists on `branch`.
|
||||
Absent {
|
||||
branch: String,
|
||||
},
|
||||
}
|
||||
|
||||
/// Read the rules file of `owner/name` and parse it; a repo whose default
|
||||
/// branch holds no rules file is [`Loaded::Absent`], not an error.
|
||||
async fn load_rules(
|
||||
files: &impl FileSource,
|
||||
owner: &str,
|
||||
name: &str,
|
||||
) -> Result<Loaded, ProblemDetails> {
|
||||
let repo = format!("{owner}/{name}");
|
||||
let read = forge::within_forge_deadline(
|
||||
"review rules",
|
||||
|
|
@ -518,9 +490,13 @@ async fn load_rules(
|
|||
let [yml, json] = RULES_PATHS;
|
||||
let (path, bytes) = match read.files.as_slice() {
|
||||
[] => {
|
||||
return Ok(Loaded::Absent {
|
||||
branch: read.branch,
|
||||
});
|
||||
return Err(error_problem(
|
||||
StatusCode::NOT_FOUND,
|
||||
&format!(
|
||||
"{repo}'s default branch {} holds neither {yml} nor {json}",
|
||||
read.branch
|
||||
),
|
||||
));
|
||||
}
|
||||
[file] => (&file.path, &file.bytes),
|
||||
_ => {
|
||||
|
|
@ -536,7 +512,7 @@ async fn load_rules(
|
|||
let classifications = parse_rules(path, bytes)
|
||||
.map_err(|d| error_problem(StatusCode::UNPROCESSABLE_ENTITY, &d))?;
|
||||
tracing::info!(%repo, branch = %read.branch, commit = %read.commit, %path, "review rules read");
|
||||
Ok(Loaded::Rules(Rules {
|
||||
Ok(Json(Rules {
|
||||
path: path.clone(),
|
||||
branch: read.branch,
|
||||
commit: read.commit,
|
||||
|
|
|
|||
|
|
@ -2,11 +2,10 @@
|
|||
//! ([`LOGIN`]) runs the repo's rules against the PR and posts the result as
|
||||
//! that account's review, on the commit the run read.
|
||||
//!
|
||||
//! Only a review request starts a run. A push makes none itself: the
|
||||
//! notification router's `head-classifier` flow requests the review. A fresh
|
||||
//! request retires the account's earlier review: the forge clears the
|
||||
//! `official` flag on a reviewer's previous reviews when they are requested
|
||||
//! again, and only official reviews count toward approvals or block a merge.
|
||||
//! Only a review request starts a run; a push does not. A fresh request
|
||||
//! retires the account's earlier review: the forge clears the `official` flag
|
||||
//! on a reviewer's previous reviews when they are requested again, and only
|
||||
//! official reviews count toward approvals or block a merge.
|
||||
//!
|
||||
//! A request for a head whose newest recorded review approved or requested
|
||||
//! changes runs nothing: the account posts that verdict again, saying so. A
|
||||
|
|
@ -825,8 +824,6 @@ impl SettingsSource for Queue {
|
|||
/// How far a run got.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
enum Outcome {
|
||||
/// The repo has no rules file: nothing was asked of the classifier.
|
||||
NoRules { repo: String },
|
||||
/// Nothing reached the classifier, for this reason.
|
||||
NotRun(String),
|
||||
/// The classifier answered a run of `sent` classifications.
|
||||
|
|
@ -861,7 +858,7 @@ async fn run(
|
|||
let sent = post(poster, req, head.as_deref(), ruling.review()).await;
|
||||
let answer = match &outcome {
|
||||
Outcome::Ran { reply, .. } => runs::answer(reply),
|
||||
Outcome::NotRun(_) | Outcome::NoRules { .. } => (None, None),
|
||||
Outcome::NotRun(_) => (None, None),
|
||||
};
|
||||
record(
|
||||
req,
|
||||
|
|
@ -1035,9 +1032,8 @@ async fn classify(
|
|||
};
|
||||
let head = Some(pull.head_sha.clone());
|
||||
let narrowed = pull.narrowed;
|
||||
let rules = match super::load_rules(files, &req.owner, &req.name).await {
|
||||
Ok(super::Loaded::Rules(rules)) => rules,
|
||||
Ok(super::Loaded::Absent { .. }) => return (head, narrowed, Outcome::NoRules { repo }),
|
||||
let rules = match super::rules(files, &req.owner, &req.name).await {
|
||||
Ok(axum::Json(rules)) => rules,
|
||||
Err(problem) => {
|
||||
let detail = problem.detail.unwrap_or_default();
|
||||
return (
|
||||
|
|
@ -1118,11 +1114,6 @@ impl Ruling {
|
|||
/// The ruling on a run's outcome.
|
||||
fn review_for(outcome: &Outcome) -> Ruling {
|
||||
match outcome {
|
||||
Outcome::NoRules { repo } => Ruling {
|
||||
event: Event::Approve,
|
||||
body: format!("no review rules are configured for {repo}; approved without checks\n"),
|
||||
reason: None,
|
||||
},
|
||||
Outcome::NotRun(reason) => Ruling::no_verdict(reason.clone(), &[]),
|
||||
Outcome::Ran {
|
||||
reply: Reply::Failed { error },
|
||||
|
|
@ -2318,8 +2309,7 @@ mod tests {
|
|||
}
|
||||
}
|
||||
|
||||
let [yml, _] = crate::review_classifier::RULES_PATHS;
|
||||
let fake = Fake::new(failed, vec![rules_json(), (yml, String::new())]);
|
||||
let fake = Fake::new(failed, Vec::new());
|
||||
let run = run_read(&fake, &fake, &Posted::default(), &request("r")).await;
|
||||
assert_eq!(run.outcome, RunOutcome::NoVerdict);
|
||||
assert!(
|
||||
|
|
@ -2740,65 +2730,13 @@ mod tests {
|
|||
assert_eq!(posted_commit.as_deref(), commit, "{repo}");
|
||||
}
|
||||
|
||||
let [yml, _] = crate::review_classifier::RULES_PATHS;
|
||||
let fake = Fake::new(approve(), vec![rules_json(), (yml, String::new())]);
|
||||
let fake = Fake::new(approve(), Vec::new());
|
||||
let poster = Posted::default();
|
||||
run_read(&fake, &fake, &poster, &request("r")).await;
|
||||
let (_, _, review) = poster.0.into_inner().expect("unpoisoned").remove(0);
|
||||
assert_eq!(review.event, "COMMENT");
|
||||
assert!(
|
||||
review.body.contains("the rules did not load") && review.body.contains("holds both"),
|
||||
"{}",
|
||||
review.body
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_repo_without_rules_is_approved_without_a_model_call() {
|
||||
let fake = Fake::new(
|
||||
Reply::Failed {
|
||||
error: "must not run".to_owned(),
|
||||
},
|
||||
Vec::new(),
|
||||
);
|
||||
let poster = Posted::default();
|
||||
let run = run_read(&fake, &fake, &poster, &request("r")).await;
|
||||
assert_eq!(run.outcome, RunOutcome::Approved);
|
||||
assert_eq!(run.detail, None);
|
||||
assert_eq!(run.head_sha.as_deref(), Some("abc123"));
|
||||
assert!(fake.sent.lock().expect("unpoisoned").is_empty());
|
||||
let reviews = poster.0.into_inner().expect("unpoisoned");
|
||||
assert_eq!(reviews.len(), 1);
|
||||
let (pr, commit, review) = &reviews[0];
|
||||
assert_eq!(pr, "o/r#7");
|
||||
assert_eq!(commit.as_deref(), Some("abc123"));
|
||||
assert_eq!(review.event, "APPROVED");
|
||||
assert_eq!(
|
||||
review.body,
|
||||
"no review rules are configured for o/r; approved without checks\n"
|
||||
);
|
||||
|
||||
// A request for the same head approves again, still without a run.
|
||||
let poster = Posted::default();
|
||||
let again = run_read(&fake, &fake, &poster, &request("r")).await;
|
||||
assert_eq!(again.outcome, RunOutcome::Approved);
|
||||
assert!(fake.sent.lock().expect("unpoisoned").is_empty());
|
||||
assert_eq!(poster.0.into_inner().expect("unpoisoned").len(), 1);
|
||||
|
||||
// Control: a repo with rules still runs the classifier and posts its verdict.
|
||||
let approve = Reply::Done {
|
||||
rows: vec![row(None, true, true)],
|
||||
overall: Overall::Approve,
|
||||
};
|
||||
let with = Fake::new(approve, vec![rules_json()]);
|
||||
let poster = Posted::default();
|
||||
let run = run_read(&with, &with, &poster, &request("r")).await;
|
||||
assert_eq!(run.outcome, RunOutcome::Approved);
|
||||
assert_eq!(with.sent.lock().expect("unpoisoned").len(), 1);
|
||||
let (_, _, review) = poster.0.into_inner().expect("unpoisoned").remove(0);
|
||||
assert_eq!(review.event, "APPROVED");
|
||||
assert!(
|
||||
review.body.contains("Overall: ✅ approve"),
|
||||
review.body.contains("the rules did not load") && review.body.contains("neither"),
|
||||
"{}",
|
||||
review.body
|
||||
);
|
||||
|
|
@ -2954,7 +2892,7 @@ mod tests {
|
|||
let body = narrowed(None, true).await;
|
||||
assert!(!body.contains("forge's diff"), "{body}");
|
||||
let body = narrowed(reason(), false).await;
|
||||
assert!(body.starts_with("no review rules are configured"), "{body}");
|
||||
assert!(body.starts_with("**No verdict:**"), "{body}");
|
||||
assert!(
|
||||
!body.contains("forge's diff"),
|
||||
"a run that never ran read no diff: {body}"
|
||||
|
|
|
|||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue