diff --git a/docs/incidents/mx_gate_keeps_the_branch_current.md b/docs/incidents/mx_gate_keeps_the_branch_current.md new file mode 100644 index 000000000..04aa0925c --- /dev/null +++ b/docs/incidents/mx_gate_keeps_the_branch_current.md @@ -0,0 +1,5 @@ +# mx. `mx_gate_keeps_the_branch_current` + +**What happened:** A person approved someone's pull request and the auto-merge gate armed. Three seconds later the gate found the pull request conflicting with its base and stopped: on a person's pull request it never brought the base in (`updating:false`), it only told a person to resolve it. Twelve minutes later a person pushed the conflict resolution. CoderHelm reviewed only the pull request's own change since its approval and approved again, and the gate armed and stopped at once with "waiting on approvals". The person's approval was of the earlier commit, and the gate counted only approvals of the head, though GitHub itself still counted it. The gate waited on a review event that never came, said nothing more on the pull request or in the team's channel, and the person had to dismiss their approval and approve again for it to move. + +**The guarantee:** A pull request behind or conflicting with its base is CoderHelm's maintenance, on anyone's pull request: as soon as the gate is armed (before a person's approval lands where the rules dismiss approvals on push) it merges the base in through the same conflict resolution runs use, once per head, compares the pull request's own change before and after with the reviewer's comparison (`review_scope::same_own_change`), queues CoderHelm's review of the new head itself, labelled pull request or not (a push's review, so the earlier approval is carried with the review it rests on when the pull request's own change is the one approved), and arms itself on the new head, where the review, CI and the keys are read again. The gate never waits on a review it caused. Only a conflict it can't resolve, or a fork it can't push to, is reported, once, with the files. The person key follows GitHub's own required-review rule (`reviewDecision`): an approval GitHub keeps counts, and with no rule an approval of an earlier commit counts when every commit since only brought the base in. When the gate waits on a person it names exactly what is missing (a dismissed approval and the commit that dismissed it, the branch's required count, who was asked) on the run page, in the pull request's one status comment and once in the team's channel, and it reads the pull request again every 15 minutes for a day, so a missed webhook can't strand it. diff --git a/services/worker/src/clients/github.rs b/services/worker/src/clients/github.rs index aea185278..e6c98e89b 100644 --- a/services/worker/src/clients/github.rs +++ b/services/worker/src/clients/github.rs @@ -5841,6 +5841,41 @@ impl GitHubClient { .await } + /// Classic branch protection of a branch (needs read access to + /// administration; callers treat an error as not known). + pub async fn branch_protection( + &self, + owner: &str, + repo: &str, + branch: &str, + ) -> Result> { + self.get(&format!( + "{API_BASE}/repos/{owner}/{repo}/branches/{}/protection", + urlencoding::encode(branch) + )) + .await + } + + /// GitHub's own reading of a pull request's required reviews: its + /// `reviewDecision` (`APPROVED`, `REVIEW_REQUIRED`, `CHANGES_REQUESTED`), + /// `None` when the branch requires no reviews. + pub async fn pr_review_decision( + &self, + owner: &str, + repo: &str, + pr_number: u64, + ) -> Result, Box> { + let data = self + .graphql_read( + "query($owner:String!,$name:String!,$pr:Int!){ repository(owner:$owner,name:$name){ pullRequest(number:$pr){ reviewDecision } } }", + serde_json::json!({ "owner": owner, "name": repo, "pr": pr_number }), + ) + .await?; + Ok(data["repository"]["pullRequest"]["reviewDecision"] + .as_str() + .map(str::to_string)) + } + /// Classic branch protection's required status checks (needs read /// access to administration; callers treat an error as none). pub async fn required_status_checks( diff --git a/services/worker/src/passes/await_merge.rs b/services/worker/src/passes/await_merge.rs index 522bd2881..2547d29a0 100644 --- a/services/worker/src/passes/await_merge.rs +++ b/services/worker/src/passes/await_merge.rs @@ -64,6 +64,13 @@ const MAX_ERROR_RETRIES: u32 = 100; /// After the gate adds deploy labels, how long a "green" CI result is not /// trusted unless a check has started since the labels were added. const LABEL_CI_GRACE_SECS: i64 = 180; +/// While the gate waits on people it still reads the PR again on this +/// delay (the longest a queue delay can be), so a review event the webhook +/// missed, or one it never sends, can't strand an approved PR. +const PEOPLE_RECHECK_SECS: i32 = 900; +/// How long one arming keeps re-reading while it waits on people. Any review +/// event re-arms it after that. +const PEOPLE_WATCH_SECS: i64 = 24 * 60 * 60; /// How long a gate record is kept after its last arming. const RECORD_TTL_SECS: u64 = 30 * 86_400; @@ -272,6 +279,179 @@ pub(crate) struct GateView { /// not read for a PR that can't merge yet). pub ci: Option, pub now: i64, + /// The head commit and the base branch, to name them. + pub head: String, + pub base: String, + /// GitHub's own reading of the branch's required reviews + /// (`reviewDecision`): `APPROVED`, `REVIEW_REQUIRED`, + /// `CHANGES_REQUESTED`, or `None` when the branch requires none. + pub review_decision: Option, + /// A person approved an earlier commit and every commit since only + /// brought the base in: the PR's own change is the one they approved + /// (`review_scope::same_own_change`, the reviewer's own comparison). + pub earlier_approval_holds: bool, + /// The branch's rules for reviews, where they could be read. + pub rules: ReviewRules, + /// People asked to review who haven't yet. + pub requested: Vec, + /// This head is CoderHelm's own merge of the base into the branch. + pub base_merge_at_head: bool, +} + +/// What the base branch's rules (classic protection and rulesets) say +/// about reviews. `None` where they couldn't be read. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub(crate) struct ReviewRules { + pub required_approvals: Option, + pub dismiss_stale: Option, +} + +/// Pure: the review rules from classic protection and the branch's +/// rulesets (`pull_request` rules); the strictest of them wins. +pub(crate) fn review_rules( + protection: Option<&serde_json::Value>, + rulesets: Option<&serde_json::Value>, +) -> ReviewRules { + let mut out = ReviewRules::default(); + let mut take = |count: Option, dismiss: Option| { + if let Some(c) = count { + out.required_approvals = Some(out.required_approvals.map_or(c, |o| o.max(c))); + } + if let Some(d) = dismiss { + out.dismiss_stale = Some(out.dismiss_stale.unwrap_or(false) || d); + } + }; + if let Some(p) = protection { + let r = &p["required_pull_request_reviews"]; + if r.is_object() { + take( + r["required_approving_review_count"].as_u64(), + r["dismiss_stale_reviews"].as_bool(), + ); + } else if p.is_object() { + take(Some(0), Some(false)); + } + } + for rule in rulesets.and_then(|r| r.as_array()).into_iter().flatten() { + if rule["type"].as_str() == Some("pull_request") { + let params = &rule["parameters"]; + take( + params["required_approving_review_count"].as_u64(), + params["dismiss_stale_reviews_on_push"].as_bool(), + ); + } + } + out +} + +/// Pure: the person key. Nobody's standing request for changes, and either +/// a person approved this very commit, or a person's approval of an earlier +/// commit still counts: GitHub's own required-review rule keeps it +/// (`reviewDecision` is `APPROVED`), or every commit since only brought the +/// base in. GitHub saying reviews are still required, or changes requested, +/// is never outvoted. +pub(crate) fn person_key(v: &GateView) -> bool { + let a = &v.approvals; + if !a.blocking_by.is_empty() { + return false; + } + match v.review_decision.as_deref() { + Some("REVIEW_REQUIRED") | Some("CHANGES_REQUESTED") => false, + Some("APPROVED") => !a.approved_by.is_empty(), + _ => { + !a.approved_at_head.is_empty() + || (!a.approved_by.is_empty() && v.earlier_approval_holds) + } + } +} + +/// Pure: exactly what the person key is missing, in one line. Names a +/// dismissed approval and the commit that dismissed it, the branch's +/// required count, an approval of an earlier commit, and who was asked. +pub(crate) fn missing_person_line(v: &GateView, rest_done: bool) -> (Mark, String) { + let a = &v.approvals; + let head = short(&v.head); + if !a.blocking_by.is_empty() { + return ( + Mark::Blocked, + format!("Changes requested by {}", mention_list(&a.blocking_by)), + ); + } + let tail = if rest_done { + "is all that's left" + } else { + "is needed too" + }; + let dismissed: Vec<&(String, String)> = + a.dismissed.iter().filter(|(_, c)| c != &v.head).collect(); + if !dismissed.is_empty() && a.approved_at_head.is_empty() { + let who: Vec = dismissed + .iter() + .map(|(l, c)| format!("@{l}'s approval of {}", short(c))) + .collect(); + let by = if v.base_merge_at_head { + format!("by CoderHelm's merge of `{}` at {head}", v.base) + } else { + format!("when new commits were pushed (the head is now {head})") + }; + let was = if who.len() == 1 { "was" } else { "were" }; + return ( + Mark::Waiting, + format!( + "{} {was} dismissed {by}; a re-approval of {head} {tail}", + who.join(", ") + ), + ); + } + if v.review_decision.as_deref() == Some("REVIEW_REQUIRED") { + let have = a.approved_by.len(); + let need = match v.rules.required_approvals { + Some(n) if n > 0 => format!( + "{n} approving review{} from people", + if n == 1 { "" } else { "s" } + ), + _ => "an approving review from a person".to_string(), + }; + let mut line = format!("The branch's rules need {need}; "); + if have == 0 { + line.push_str("none is in yet"); + } else { + line.push_str(&format!("in so far: {}", mention_list(&a.approved_by))); + } + line.push_str(". CoderHelm's approval doesn't count toward them"); + if !v.requested.is_empty() { + line.push_str(&format!( + ". Asked to review: {}", + mention_list(&v.requested) + )); + } + return (Mark::Waiting, line); + } + if !a.approved_on.is_empty() { + let who: Vec = a + .approved_on + .iter() + .map(|(l, c)| format!("@{l} ({})", short(c))) + .collect(); + return ( + Mark::Waiting, + format!( + "Approved by {} on an earlier commit, and the PR's own change moved since; a \ + re-approval of {head} {tail}", + who.join(", ") + ), + ); + } + if !v.requested.is_empty() { + return ( + Mark::Waiting, + format!( + "Waiting for a person's approval (asked: {})", + mention_list(&v.requested) + ), + ); + } + (Mark::Waiting, "Waiting for a person's approval".to_string()) } /// What the gate does next. @@ -310,7 +490,7 @@ fn short(sha: &str) -> &str { pub(crate) fn decide_gate(v: &GateView) -> GateDecision { let bot_dismissed = v.approvals.bot_dismissed_at_head; let bot_approved = v.bot_verdict.as_deref() == Some("APPROVE") && !bot_dismissed; - let human_present = v.approvals.human_key_at_head(); + let human_present = person_key(v); let mut lines: Vec<(Mark, String)> = Vec::new(); match (v.bot_verdict.as_deref(), bot_dismissed) { @@ -326,32 +506,14 @@ pub(crate) fn decide_gate(v: &GateView) -> GateDecision { } if v.require_human { lines.push(if human_present { - ( - Mark::Done, - format!( - "Approved by {}", - mention_list(&v.approvals.approved_at_head) - ), - ) - } else if !v.approvals.blocking_by.is_empty() { - ( - Mark::Blocked, - format!( - "Changes requested by {}", - mention_list(&v.approvals.blocking_by) - ), - ) - } else if !v.approvals.approved_by.is_empty() { - ( - Mark::Waiting, - format!( - "Approved by {} on an earlier commit; waiting for a person's approval of \ - this one", - mention_list(&v.approvals.approved_by) - ), - ) + let by = if v.approvals.approved_at_head.is_empty() { + &v.approvals.approved_by + } else { + &v.approvals.approved_at_head + }; + (Mark::Done, format!("Approved by {}", mention_list(by))) } else { - (Mark::Waiting, "Waiting for a person's approval".to_string()) + missing_person_line(v, bot_approved && !v.draft) }); } if v.draft { @@ -637,6 +799,10 @@ struct GateRecord { labels_added_at: Option, /// A person's stop (see `common::merge_gate::stop`). stopped: Option, + /// The head CoderHelm's own merge of the base made, if it made one. + base_merge_head: String, + /// When the current chain started waiting on people (epoch seconds). + waiting_since: Option, } fn num(item: &HashMap, k: &str) -> Option { @@ -676,6 +842,8 @@ async fn load_record(state: &WorkerState, msg: &AwaitMergeMessage) -> Result Result<(), match evaluate(state, &github, &msg, &mut record, now).await { Ok(Next::Stop) => {} + Ok(Next::Recheck) => { + msg.error_retries = 0; + recheck_people(state, &msg, &mut record, now).await; + } Ok(Next::Poll) => { msg.error_retries = 0; if !poll_again(state, &msg, now, record.armed_at).await { @@ -1224,12 +1399,41 @@ async fn evaluate( return Ok(Next::Stop); } - let approvals = fetch_approvals(github, owner, repo, msg.pr_number, &msg.head_sha).await?; let bot = bot_verdict_at_head(state, msg).await?; let (bot_verdict, bot_holding) = match bot { Some(b) => (Some(b.verdict), b.holding), None => (None, Vec::new()), }; + // Behind or conflicting with the base: bring the base in now, as soon + // as the gate is armed (before a person's approval lands, where the + // branch's rules dismiss approvals on push), not at merge time. A review + // that holds changes is left to its author, who pushes anyway. + if let Some(need) = needs_base(&pr) { + if bot_verdict.as_deref().is_none_or(|v| v == "APPROVE") { + return keep_current(state, github, msg, record, &pr, need, Vec::new()).await; + } + } + let approvals = fetch_approvals(github, owner, repo, msg.pr_number, &msg.head_sha).await?; + // GitHub's own required-review rule decides whether an approval of an + // earlier commit still counts; unreadable, the gate's own rule does. + let review_decision = match github.pr_review_decision(owner, repo, msg.pr_number).await { + Ok(d) => d, + Err(e) => { + warn!(pr = msg.pr_number, error = %e, "await-merge: could not read GitHub's review decision"); + None + } + }; + let earlier_approval_holds = approvals.approved_at_head.is_empty() + && review_decision.is_none() + && earlier_approval_holds(github, msg, &approvals).await; + let requested: Vec = pr["requested_reviewers"] + .as_array() + .map(|a| { + a.iter() + .filter_map(|r| r["login"].as_str().map(str::to_string)) + .collect() + }) + .unwrap_or_default(); let mut view = GateView { draft: pr["draft"].as_bool().unwrap_or(false), require_human: cfg.require_human_approval || msg.self_authored, @@ -1248,8 +1452,22 @@ async fn evaluate( .flatten(), ci: None, now, + head: msg.head_sha.clone(), + base: msg.base_branch.clone(), + review_decision, + earlier_approval_holds, + rules: ReviewRules::default(), + requested, + base_merge_at_head: !record.base_merge_head.is_empty() + && record.base_merge_head == msg.head_sha, }; let mut decision = decide_gate(&view); + // Waiting on a person: read the branch's review rules, so the wait names + // the required count. + if decision.action == Action::WaitForPeople && view.require_human && !person_key(&view) { + view.rules = read_review_rules(github, owner, repo, &msg.base_branch).await; + decision = decide_gate(&view); + } let mut quarantined: Option> = None; if decision.action == Action::ReadCi { let summary = ci_summary(github, owner, repo, &msg.head_sha).await?; @@ -1285,9 +1503,12 @@ async fn evaluate( decision = decide_gate(&view); } + // What the gate waits on, for the run page: the first item not done. let detail = decision .lines - .last() + .iter() + .find(|(m, _)| *m != Mark::Done) + .or(decision.lines.last()) .map(|(_, l)| l.clone()) .unwrap_or_default(); let status = render_status( @@ -1304,10 +1525,19 @@ async fn evaluate( "await-merge: waiting on approvals — stopping until the next review event" ); set_state(state, msg, decision.state, &detail).await; - if watched { + // A person's approval that no longer counts is never waited on + // silently: the PR's one status comment, and the team's channel + // once per commit. + let person_gap = view.require_human + && !person_key(&view) + && (!view.approvals.approved_by.is_empty() || !view.approvals.dismissed.is_empty()); + if watched || person_gap { publish_status(state, github, msg, record, &status).await; } - Ok(Next::Stop) + if person_gap { + tell_team(state, msg, &pr, &detail).await; + } + Ok(Next::Recheck) } Action::ReadCi => Err("the gate asked for CI after reading it".into()), Action::AddLabels(missing) => { @@ -1464,14 +1694,12 @@ async fn merge( Ok(Next::Stop) } MergeOutcome::NotMergeable(reason) => { - // A conflict never clears by waiting: polling it kept the gate - // (and its status edits) going for hours. Stop on it. - let conflicted = github - .get_pull_request(owner, repo, msg.pr_number) - .await - .is_ok_and(|pr| common::merge_gate::is_conflict(&pr)); - if conflicted { - return on_conflict(state, github, msg, record, lines).await; + // A conflict, or a branch the rules want current, never clears by + // waiting: CoderHelm brings the base in itself. + if let Ok(pr) = github.get_pull_request(owner, repo, msg.pr_number).await { + if let Some(need) = needs_base(&pr) { + return keep_current(state, github, msg, record, &pr, need, lines).await; + } } // Branch protection, or a required check GitHub still counts as // missing. Keep polling: most of these clear on their own, and @@ -1503,8 +1731,29 @@ async fn merge( } } -/// The line the status comment shows for a conflict: CoderHelm updates its -/// own PR's branch once, or a person resolves it. +/// Why the base has to come in before the PR can merge. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum BaseNeed { + /// The PR conflicts with its base. + Conflict, + /// The branch's rules want it current with its base, and it isn't. + Behind, +} + +/// Pure: whether GitHub reports the PR conflicting with, or behind, its +/// base (`mergeable_state`). +pub(crate) fn needs_base(pr: &serde_json::Value) -> Option { + if common::merge_gate::is_conflict(pr) { + Some(BaseNeed::Conflict) + } else if pr["mergeable_state"].as_str() == Some("behind") { + Some(BaseNeed::Behind) + } else { + None + } +} + +/// The line the status comment shows while CoderHelm's own PR's run +/// resolves a conflict in its conflict round. fn conflict_line(base: &str, updating: bool) -> (Mark, String) { if updating { ( @@ -1525,19 +1774,94 @@ fn conflict_line(base: &str, updating: bool) -> (Mark, String) { } } -/// The PR conflicts with its base: the gate stops (no polls, one status -/// update). On CoderHelm's own PR the first conflict starts a conflict round -/// on its run, once per PR; after that, or on anyone else's PR, a person -/// resolves it. -async fn on_conflict( +/// How bringing the base in went. +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) enum BaseMerge { + /// Merged in; the PR's own change is (or isn't) the one before. + Merged { sha: String, own_unchanged: bool }, + /// Conflicts CoderHelm couldn't resolve on its own; nothing pushed. + Unresolved { files: Vec }, + /// The branch is somewhere CoderHelm can't push (a fork). + CannotPush, +} + +/// Pure: the status line for a base merge. +pub(crate) fn base_merge_line(base: &str, outcome: &BaseMerge) -> (Mark, String) { + match outcome { + BaseMerge::Merged { + sha, + own_unchanged: true, + } => ( + Mark::Waiting, + format!( + "CoderHelm merged `{base}` into this branch ({}). The PR's own change is the \ + one approved, so the approval carries to it; auto-merge goes on once its checks \ + pass", + short(sha) + ), + ), + BaseMerge::Merged { sha, .. } => ( + Mark::Waiting, + format!( + "CoderHelm merged `{base}` into this branch ({}). Resolving the conflict touched \ + the PR's own change, so CoderHelm reviews that part before auto-merge goes on", + short(sha) + ), + ), + BaseMerge::Unresolved { files } if !files.is_empty() => ( + Mark::Blocked, + format!( + "Merge conflict with `{base}` that needs a person's call: {}. Nothing was pushed; \ + the review of the commit that resolves it starts auto-merge again", + code_list(files) + ), + ), + BaseMerge::Unresolved { .. } => ( + Mark::Blocked, + format!( + "Merge conflict with `{base}` that needs a person's call. Nothing was pushed; \ + the review of the commit that resolves it starts auto-merge again" + ), + ), + BaseMerge::CannotPush => ( + Mark::Blocked, + format!( + "Behind or conflicting with `{base}`, and the branch is in a fork CoderHelm can't \ + push to: merge `{base}` into it; the review of that commit starts auto-merge \ + again" + ), + ), + } +} + +/// The PR is behind or conflicts with its base: CoderHelm brings the base +/// in itself, once per head, on anyone's PR. The run's conflict round does +/// it on CoderHelm's own PR when it has a run. After the merge the PR's own +/// change is compared with the one before (the reviewer's comparison, +/// `review_scope::same_own_change`); the push's review carries the approval +/// or reviews what moved, CI runs on the new head, and the gate is armed on +/// it. Only a conflict it can't resolve is reported, once, with the files. +async fn keep_current( state: &WorkerState, github: &GitHubClient, msg: &AwaitMergeMessage, record: &mut GateRecord, + pr: &serde_json::Value, + need: BaseNeed, mut lines: Vec<(Mark, String)>, ) -> Result { let (owner, repo) = (msg.repo_owner.as_str(), msg.repo_name.as_str()); - let updating = msg.self_authored + let base = msg.base_branch.as_str(); + if !claim_base_update(state, msg).await { + info!( + pr = msg.pr_number, + ?need, + "await-merge: the base was brought in for this head already — the gate stops" + ); + return Ok(Next::Stop); + } + if need == BaseNeed::Conflict + && msg.self_authored && claim_conflict_round(state, msg).await && super::resume::start_conflict_round( state, @@ -1547,25 +1871,229 @@ async fn on_conflict( repo, msg.pr_number, ) - .await; + .await + { + info!( + pr = msg.pr_number, + "await-merge: merge conflict — the run's conflict round resolves it" + ); + let line = conflict_line(base, true); + set_state(state, msg, common::merge_gate::STATE_CONFLICT, &line.1).await; + lines.push(line); + let body = render_status( + &msg.head_sha, + base, + &lines, + None, + Some(&control_url(state, msg, Control::Stop)), + ); + publish_status(state, github, msg, record, &body).await; + return Ok(Next::Stop); + } + let outcome = match bring_base_in(state, github, msg, pr).await { + Ok(o) => o, + Err(e) => { + release_base_update(state, msg).await; + return Err(e); + } + }; info!( pr = msg.pr_number, - updating, "await-merge: merge conflict — the gate stops" + ?need, + ?outcome, + "await-merge: brought the base in" ); - let line = conflict_line(&msg.base_branch, updating); - set_state(state, msg, common::merge_gate::STATE_CONFLICT, &line.1).await; - lines.push(line); + let line = base_merge_line(base, &outcome); + lines.push(line.clone()); let body = render_status( &msg.head_sha, - &msg.base_branch, + base, &lines, None, Some(&control_url(state, msg, Control::Stop)), ); - publish_status(state, github, msg, record, &body).await; + match &outcome { + BaseMerge::Merged { sha, .. } => { + record_base_merge(state, msg, sha).await; + set_state(state, msg, STATE_HEAD_MOVED, &line.1).await; + publish_status(state, github, msg, record, &body).await; + // The gate never waits on a review it caused: it queues + // CoderHelm's review of the new head itself, labelled PR or not, + // which carries the approval when the PR's own change is the one + // approved and arms the gate again. + super::review_pr::review_after_base_merge( + state, + &msg.team_id, + msg.installation_id, + (owner, repo, msg.pr_number), + sha, + ) + .await; + // The gate goes on on the new head: it waits for the review of + // it, then CI, then merges. + arm( + state, + &msg.team_id, + msg.installation_id, + owner, + repo, + msg.pr_number, + sha, + base, + msg.self_authored, + ) + .await; + } + BaseMerge::Unresolved { .. } | BaseMerge::CannotPush => { + set_state(state, msg, common::merge_gate::STATE_CONFLICT, &line.1).await; + publish_status(state, github, msg, record, &body).await; + tell_team(state, msg, pr, &line.1).await; + } + } Ok(Next::Stop) } +/// Merges the base into the PR's branch through the conflict resolution +/// every run uses (`pr::resolve_conflicts`: a clean merge, or the +/// conflicting files merged and checked, one merge commit). +async fn bring_base_in( + state: &WorkerState, + github: &GitHubClient, + msg: &AwaitMergeMessage, + pr: &serde_json::Value, +) -> Result { + let (owner, repo) = (msg.repo_owner.as_str(), msg.repo_name.as_str()); + let base = msg.base_branch.as_str(); + let same_repo = pr["head"]["repo"]["full_name"].as_str().is_some() + && pr["head"]["repo"]["full_name"] == pr["base"]["repo"]["full_name"]; + let Some(branch) = pr["head"]["ref"].as_str().filter(|_| same_repo) else { + return Ok(BaseMerge::CannotPush); + }; + let before = github.get_diff(owner, repo, base, &msg.head_sha).await?; + let provider = crate::agent::provider::ModelProvider::load_for_team( + &state.dynamo, + &state.config.settings_table_name, + &msg.team_id, + ) + .await? + .for_run( + "", + &format!("{owner}/{repo}"), + "", + &format!("Base merge for pull request #{}", msg.pr_number), + ); + let ticket = gate_ticket(msg)?; + let mut usage = crate::models::TokenUsage::default(); + super::pr::resolve_conflicts(state, &ticket, github, branch, &provider, &mut usage).await?; + let head = github.get_ref(owner, repo, branch).await?; + if head == msg.head_sha { + let files = changed_on_both_sides(github, owner, repo, base, branch).await; + return Ok(BaseMerge::Unresolved { files }); + } + let own_unchanged = match github.get_diff(owner, repo, base, &head).await { + Ok(after) => super::review_scope::same_own_change(&before, &after), + Err(_) => false, + }; + Ok(BaseMerge::Merged { + sha: head, + own_unchanged, + }) +} + +/// Files both the branch and the base changed since they parted. +async fn changed_on_both_sides( + github: &GitHubClient, + owner: &str, + repo: &str, + base: &str, + branch: &str, +) -> Vec { + let Ok(cmp) = github.get_diff(owner, repo, base, branch).await else { + return Vec::new(); + }; + let Some(fork) = cmp + .pointer("/merge_base_commit/sha") + .and_then(|v| v.as_str()) + else { + return Vec::new(); + }; + match ( + github + .compare_changed_files(owner, repo, fork, branch) + .await, + github.compare_changed_files(owner, repo, fork, base).await, + ) { + (Ok(ours), Ok(theirs)) => ours.into_iter().filter(|f| theirs.contains(f)).collect(), + _ => Vec::new(), + } +} + +/// The ticket the conflict resolution's merge commit is written for: a +/// pull request with no tracker key, in the team's commit convention. +fn gate_ticket(msg: &AwaitMergeMessage) -> Result { + Ok(serde_json::from_value(serde_json::json!({ + "team_id": msg.team_id, + "installation_id": msg.installation_id, + "source": "github", + "ticket_id": "", + "title": format!("Pull request #{}", msg.pr_number), + "body": "", + "repo_owner": msg.repo_owner, + "repo_name": msg.repo_name, + "issue_number": 0, + "sender": "", + "base_branch": msg.base_branch, + }))?) +} + +/// Claims the one base merge a head gets. False when it was claimed for +/// this head before (or the record can't be written). +async fn claim_base_update(state: &WorkerState, msg: &AwaitMergeMessage) -> bool { + state + .dynamo + .update_item() + .table_name(&state.config.settings_table_name) + .key("pk", attr_s(&msg.team_id)) + .key("sk", attr_s(&record_key(msg))) + .update_expression("SET base_update_head = :h") + .condition_expression( + "attribute_exists(sk) AND (attribute_not_exists(base_update_head) OR base_update_head <> :h)", + ) + .expression_attribute_values(":h", attr_s(&msg.head_sha)) + .send() + .await + .is_ok() +} + +/// A base merge that failed on an error is tried again on the retry. +async fn release_base_update(state: &WorkerState, msg: &AwaitMergeMessage) { + let _ = state + .dynamo + .update_item() + .table_name(&state.config.settings_table_name) + .key("pk", attr_s(&msg.team_id)) + .key("sk", attr_s(&record_key(msg))) + .update_expression("REMOVE base_update_head") + .send() + .await; +} + +async fn record_base_merge(state: &WorkerState, msg: &AwaitMergeMessage, sha: &str) { + if let Err(e) = state + .dynamo + .update_item() + .table_name(&state.config.settings_table_name) + .key("pk", attr_s(&msg.team_id)) + .key("sk", attr_s(&record_key(msg))) + .update_expression("SET base_merge_head = :h") + .expression_attribute_values(":h", attr_s(sha)) + .send() + .await + { + warn!(pr = msg.pr_number, error = %e, "await-merge: could not record the base merge"); + } +} + /// Claims the one conflict round a PR gets. False when it was claimed /// before (or the record can't be written). async fn claim_conflict_round(state: &WorkerState, msg: &AwaitMergeMessage) -> bool { @@ -1583,6 +2111,160 @@ async fn claim_conflict_round(state: &WorkerState, msg: &AwaitMergeMessage) -> b .is_ok() } +/// Whether a person's approval of an earlier commit still covers the PR: +/// every commit since only brought the base in, so the PR's own change is +/// the one they approved (the reviewer's comparison). Read only when GitHub +/// has no required-review rule to say. +async fn earlier_approval_holds( + github: &GitHubClient, + msg: &AwaitMergeMessage, + approvals: &Approvals, +) -> bool { + let (owner, repo) = (msg.repo_owner.as_str(), msg.repo_name.as_str()); + let Ok(now) = github + .get_diff(owner, repo, &msg.base_branch, &msg.head_sha) + .await + else { + return false; + }; + for (_, commit) in approvals.approved_on.iter().take(3) { + if let Ok(then) = github.get_diff(owner, repo, &msg.base_branch, commit).await { + if super::review_scope::same_own_change(&then, &now) { + return true; + } + } + } + false +} + +/// The base branch's review rules, from classic protection and rulesets. +async fn read_review_rules( + github: &GitHubClient, + owner: &str, + repo: &str, + base: &str, +) -> ReviewRules { + let protection = github.branch_protection(owner, repo, base).await.ok(); + let rulesets = github.branch_rules(owner, repo, base).await.ok(); + review_rules(protection.as_ref(), rulesets.as_ref()) +} + +/// One message to the team's channel, once per PR commit and wait: what +/// the merge waits on. Silent where no channel is set up. +async fn tell_team( + state: &WorkerState, + msg: &AwaitMergeMessage, + pr: &serde_json::Value, + line: &str, +) { + let channels = super::security_fixes::team_channels(state, &msg.team_id).await; + if channels.is_empty() { + return; + } + let sk = format!( + "MERGEGATETOLD#{}/{}#{}#{}#{}", + msg.repo_owner, + msg.repo_name, + msg.pr_number, + msg.head_sha, + common::content_hash(line) + ); + let claim = common::claim::claim( + &state.dynamo, + &state.config.settings_table_name, + &msg.team_id, + &sk, + RECORD_TTL_SECS, + ) + .await; + if !claim.won_or_failed_open() { + return; + } + let message = wait_message( + &msg.repo_owner, + &msg.repo_name, + msg.pr_number, + pr["title"].as_str().unwrap_or(""), + pr["html_url"].as_str().unwrap_or(""), + line, + ); + common::notify::deliver_recorded( + &common::notify::Http(&state.http), + &channels, + &message, + "merge_gate_wait", + &state.dynamo, + &state.config.settings_table_name, + &msg.team_id, + ) + .await; +} + +/// Pure: the team channel's message for a merge that waits on something. +pub(crate) fn wait_message( + owner: &str, + repo: &str, + pr: u64, + title: &str, + url: &str, + line: &str, +) -> common::notify::Message { + use common::notify::{Block, Button, Message, Text}; + let head = format!("Auto-merge waits on {owner}/{repo}#{pr}"); + let mut buttons = Vec::new(); + if !url.is_empty() { + buttons.push(Button::new("Open the pull request", url)); + } + Message { + summary: format!("{head}: {}", common::truncate_str(line, 200)), + blocks: vec![ + Block::Text(Text::new(head).bold()), + Block::Text(Text::new(common::truncate_str(title, 200).to_string()).subtle()), + Block::Text(Text::new(common::truncate_str(line, 600).to_string())), + ], + buttons, + full_width: false, + } +} + +/// Waiting on people: read again on the slow timer while the watch lasts, +/// so a review event the webhook missed can't strand the PR. The CI window +/// starts when the keys are in, not when the wait began. +async fn recheck_people( + state: &WorkerState, + msg: &AwaitMergeMessage, + record: &mut GateRecord, + now: i64, +) { + let since = record.waiting_since.unwrap_or(now); + if now - since >= PEOPLE_WATCH_SECS { + info!( + pr = msg.pr_number, + "await-merge: waited a day on people — the next review event re-arms the gate" + ); + return; + } + let res = state + .dynamo + .update_item() + .table_name(&state.config.settings_table_name) + .key("pk", attr_s(&msg.team_id)) + .key("sk", attr_s(&record_key(msg))) + .update_expression( + "SET waiting_since = if_not_exists(waiting_since, :now), armed_at = :now", + ) + .condition_expression("chain_id = :c") + .expression_attribute_values(":now", attr_n(now)) + .expression_attribute_values(":c", attr_s(&msg.chain_id)) + .send() + .await; + if res.is_err() { + // Superseded (a newer arming or a stop), or unwritable: no timer. + return; + } + send(state, msg, 0, PEOPLE_RECHECK_SECS).await; +} + /// Arm the gate for a PR (called when CoderHelm approves; the gateway arms on /// approvals, dismissals, new reviews and completed check suites). Every /// arming starts a new chain. Returns false if the queue isn't configured. @@ -1846,6 +2528,7 @@ mod ci_tests { approvals: Approvals { approved_by: vec!["alice".into()], approved_at_head: vec!["alice".into()], + approved_on: vec![("alice".into(), "0516eab0000".into())], ..Default::default() }, want_labels: vec![], @@ -1853,6 +2536,13 @@ mod ci_tests { labels_added_at: None, ci: None, now: 10_000, + head: "0516eab0000".into(), + base: "main".into(), + review_decision: None, + earlier_approval_holds: false, + rules: ReviewRules::default(), + requested: vec![], + base_merge_at_head: false, } } diff --git a/services/worker/src/passes/incidents/mx_gate_keeps_the_branch_current.rs b/services/worker/src/passes/incidents/mx_gate_keeps_the_branch_current.rs new file mode 100644 index 000000000..5944bec6a --- /dev/null +++ b/services/worker/src/passes/incidents/mx_gate_keeps_the_branch_current.rs @@ -0,0 +1,274 @@ +//! mx. A person approved someone's pull request (21:29:27) and the +//! auto-merge gate armed. At 21:29:30 it found the pull request conflicting +//! with its base and stopped (`updating:false`): on a person's pull request +//! it never brought the base in. At 21:41 a person pushed the conflict +//! resolution; CoderHelm reviewed only the pull request's own change since +//! its approval and approved 0516eab. The gate armed at 21:45:51 and +//! stopped a second later "waiting on approvals": the person's approval +//! was of 78e785a, and the gate counted only approvals of the head, though +//! GitHub still counted it. Nothing woke it; the person dismissed their +//! approval and approved again to move it. +//! +//! Guarantee: a conflict or a branch behind its base is CoderHelm's +//! maintenance on anyone's pull request, done as soon as the gate is armed; +//! the person key follows GitHub's own required-review rule; a wait on a +//! person names exactly what is missing, and the gate reads again on a timer +//! while it waits. + +use super::fixture::fn_src; +use crate::passes::await_merge::{ + base_merge_line, decide_gate, missing_person_line, needs_base, person_key, review_rules, + wait_message, Action, BaseMerge, BaseNeed, GateView, Mark, ReviewRules, +}; +use crate::passes::review_actions::approval_state; +use common::merge_gate::STATE_WAITING_APPROVAL; +use serde_json::{json, Value}; + +const APPROVED: &str = "78e785a1c0ffee00000000000000000000000000"; +const HEAD: &str = "0516eab2c0ffee00000000000000000000000000"; + +fn src() -> &'static str { + include_str!("../await_merge.rs") +} + +/// The reviews at 21:45:52: the person's approval of the earlier commit and +/// CoderHelm's approval of the head. +fn reviews(person_state: &str) -> Value { + json!([ + {"user": {"login": "a-person"}, "state": person_state, "commit_id": APPROVED}, + {"user": {"login": "coderhelm[bot]"}, "state": "APPROVED", "commit_id": APPROVED}, + {"user": {"login": "coderhelm[bot]"}, "state": "APPROVED", "commit_id": HEAD}, + ]) +} + +fn view(person_state: &str, decision: Option<&str>) -> GateView { + GateView { + draft: false, + require_human: true, + bot_verdict: Some("APPROVE".into()), + bot_holding: vec![], + approvals: approval_state(&reviews(person_state), HEAD), + want_labels: vec![], + present_labels: vec![], + labels_added_at: None, + ci: None, + now: 1_000, + head: HEAD.into(), + base: "main".into(), + review_decision: decision.map(str::to_string), + earlier_approval_holds: false, + rules: ReviewRules::default(), + requested: vec![], + base_merge_at_head: false, + } +} + +#[test] +fn a_conflict_on_a_persons_pr_is_brought_in_by_coderhelm_not_a_stop() { + // 21:29:30: GitHub reports the pull request conflicting. + let pr = json!({"mergeable": false, "mergeable_state": "dirty"}); + assert_eq!(needs_base(&pr), Some(BaseNeed::Conflict)); + assert_eq!( + needs_base(&json!({"mergeable_state": "behind"})), + Some(BaseNeed::Behind) + ); + assert_eq!(needs_base(&json!({"mergeable_state": "clean"})), None); + // The gate brings the base in as soon as it is armed, before reading + // the approvals, and again when GitHub declines the merge for it. + let eval = fn_src(src(), "async fn evaluate("); + let at = |needle: &str| eval.find(needle).unwrap_or_else(|| panic!("{needle}")); + assert!(at("needs_base(&pr)") < at("fetch_approvals(")); + assert!(fn_src(src(), "async fn merge(").contains("keep_current(")); + // On anyone's PR: only the run's conflict round is CoderHelm's own; the + // base merge itself never asks who wrote the PR. + let keep = fn_src(src(), "async fn keep_current("); + assert!(keep.contains("bring_base_in(")); + assert!(!fn_src(src(), "async fn bring_base_in(").contains("self_authored")); + // The same conflict resolution runs use, and the reviewer's comparison + // of the PR's own change. + let bring = fn_src(src(), "async fn bring_base_in("); + assert!(bring.contains("pr::resolve_conflicts(")); + assert!(bring.contains("review_scope::same_own_change(")); + // After the merge the gate goes on on the new head. + assert!(keep.contains("arm(")); + let (mark, text) = base_merge_line( + "main", + &BaseMerge::Merged { + sha: HEAD.into(), + own_unchanged: true, + }, + ); + assert_eq!(mark, Mark::Waiting); + assert!(text.contains("CoderHelm merged `main` into this branch (0516eab)")); + assert!(text.contains("the approval carries to it")); + // Only a conflict it can't resolve is reported, with the files, once + // per head (`claim_base_update`). + let (mark, text) = base_merge_line( + "main", + &BaseMerge::Unresolved { + files: vec!["src/schedule.py".into()], + }, + ); + assert_eq!(mark, Mark::Blocked); + assert!(text.contains("`src/schedule.py`")); + assert!(keep.contains("claim_base_update(")); +} + +#[test] +fn an_approval_github_still_counts_is_the_person_key() { + // 21:45:52: the person's approval of 78e785a stands on GitHub. + let mut v = view("APPROVED", Some("APPROVED")); + assert!(v.approvals.approved_at_head.is_empty()); + assert!(person_key(&v)); + assert_eq!(decide_gate(&v).action, Action::ReadCi); + v.ci = Some(Default::default()); + assert_eq!(decide_gate(&v).action, Action::Merge); + // No required-review rule: the approval counts when every commit since + // only brought the base in. + let mut v = view("APPROVED", None); + assert!(!person_key(&v)); + v.earlier_approval_holds = true; + assert!(person_key(&v)); + // GitHub saying more reviews are needed is never outvoted. + let v = view("APPROVED", Some("REVIEW_REQUIRED")); + assert!(!person_key(&v)); +} + +#[test] +fn a_wait_on_a_person_names_exactly_what_is_missing() { + // Where the rules dismiss approvals on push. + let v = view("DISMISSED", Some("REVIEW_REQUIRED")); + let d = decide_gate(&v); + assert_eq!(d.action, Action::WaitForPeople); + assert_eq!(d.state, STATE_WAITING_APPROVAL); + let (_, line) = missing_person_line(&v, true); + assert_eq!( + line, + "@a-person's approval of 78e785a was dismissed when new commits were pushed (the head \ + is now 0516eab); a re-approval of 0516eab is all that's left" + ); + assert!(d.lines.iter().any(|(_, l)| l == &line)); + // When the push was CoderHelm's own base merge, it says so. + let mut v = view("DISMISSED", Some("REVIEW_REQUIRED")); + v.base_merge_at_head = true; + assert!(missing_person_line(&v, true) + .1 + .contains("dismissed by CoderHelm's merge of `main` at 0516eab")); + // The branch's required count, and that CoderHelm's approval doesn't + // count toward it. + let mut v = view("COMMENTED", Some("REVIEW_REQUIRED")); + v.rules.required_approvals = Some(2); + v.requested = vec!["a-reviewer".into()]; + let (_, line) = missing_person_line(&v, true); + assert!( + line.contains("need 2 approving reviews from people"), + "{line}" + ); + assert!(line.contains("CoderHelm's approval doesn't count")); + assert!(line.contains("@a-reviewer")); + // A pending review request is named, never counted as a missing + // approval once GitHub's rule is met. + let mut v = view("APPROVED", Some("APPROVED")); + v.requested = vec!["a-reviewer".into()]; + assert!(person_key(&v)); + // The same line goes to the team's channel. + let m = wait_message("acme", "web", 7, "Fix the schedule", "https://x/7", &line); + assert!(m.summary.contains("acme/web#7")); + assert_eq!(m.buttons.len(), 1); + // And on the run page (the record's detail) and the one status comment. + let eval = fn_src(src(), "async fn evaluate("); + assert!(eval.contains("tell_team(")); + assert!(eval.contains("find(|(m, _)| *m != Mark::Done)")); +} + +#[test] +fn the_rules_are_read_from_protection_and_rulesets() { + let protection = json!({"required_pull_request_reviews": { + "required_approving_review_count": 1, "dismiss_stale_reviews": true}}); + let rulesets = json!([{"type": "pull_request", "parameters": { + "required_approving_review_count": 2, "dismiss_stale_reviews_on_push": false}}]); + assert_eq!( + review_rules(Some(&protection), Some(&rulesets)), + ReviewRules { + required_approvals: Some(2), + dismiss_stale: Some(true), + } + ); + assert_eq!(review_rules(None, None), ReviewRules::default()); +} + +#[test] +fn waiting_on_people_still_reads_again_on_a_timer() { + // "stopping until the next review event" is not the only wake-up. + let eval = fn_src(src(), "async fn evaluate("); + let wait = &eval[eval.find("Action::WaitForPeople =>").unwrap()..]; + let wait = &wait[..wait.find("Action::ReadCi =>").unwrap()]; + assert!(wait.contains("Ok(Next::Recheck)"), "{wait}"); + assert!(!wait.contains("Ok(Next::Stop)")); + let run = fn_src(src(), "pub async fn run("); + assert!(run.contains("recheck_people(")); + let recheck = fn_src(src(), "async fn recheck_people("); + assert!(recheck.contains("PEOPLE_RECHECK_SECS")); + assert!(recheck.contains("PEOPLE_WATCH_SECS")); + // Bound to the chain: a newer arming or a stop ends the timer. + assert!(recheck.contains("chain_id = :c")); +} + +#[test] +fn the_gate_queues_the_review_of_its_own_base_merge_and_the_approval_carries() { + use crate::passes::review_pr::base_merge_review; + use crate::passes::review_scope::{approved_base, same_own_change, TRIGGER_PUSH}; + use crate::passes::review_sweep::ReviewRecord; + // The gate merged `main` into an unlabelled PR's branch, making HEAD. + // It queues CoderHelm's review of HEAD itself: no push webhook review + // runs on a PR without the label, and the gate never waits on a review + // it caused. + let keep = fn_src(src(), "async fn keep_current("); + let at = |needle: &str| keep.find(needle).unwrap_or_else(|| panic!("{needle}")); + assert!(at("review_after_base_merge(") < at("arm(")); + let review_pr = include_str!("../review_pr.rs"); + let queue = fn_src(review_pr, "pub(crate) async fn review_after_base_merge("); + assert!(queue.contains("enqueue_review(")); + // Review on for the repo is all it asks; the PR's labels are never read. + assert!(!queue.contains("labels")); + let m = base_merge_review("team", 1, ("acme", "web", 7), HEAD, "coderhelm-review"); + assert_eq!(m.trigger, TRIGGER_PUSH); + assert_eq!(m.head_sha, HEAD); + // Keyed like the gateway's push review: a labelled PR is reviewed once. + assert_eq!(m.dedup_key, format!("head#{HEAD}")); + // The review takes the carry-forward path: CoderHelm's latest review + // approved 78e785a in full. + let records = vec![ReviewRecord { + verdict: "APPROVE".into(), + posted_as: "APPROVE".into(), + head_sha: APPROVED.into(), + created_at: "2026-11-18T21:29:00Z".into(), + body: "Looks right.".into(), + posted_body: "**Approved.** What was checked: the schedule's window.".into(), + ..Default::default() + }]; + assert_eq!( + approved_base(&m.trigger, m.question.is_some(), &records, &m.head_sha), + Some(APPROVED.to_string()) + ); + // The PR's own change is the one approved (line numbers moved with the + // base), so the approval is carried with that review, and the carry + // arms the gate on HEAD. + let own = |patch: &str| json!({"files": [{"filename": "src/schedule.py", "status": "modified", "patch": patch}]}); + assert!(same_own_change( + &own("@@ -10,2 +10,2 @@\n-window = 1\n+window = 2"), + &own("@@ -14,2 +14,2 @@\n-window = 1\n+window = 2"), + )); + let carried = &review_pr[review_pr + .find("review skipped, approval carried") + .expect("the carry path")..]; + let carried = &carried[..carried.find("let reason =").unwrap()]; + assert!(carried.contains("carry_approval(")); + assert!(carried.contains("run_on_approve(")); + // Armed on HEAD: CoderHelm's carried approval, the person's approval + // GitHub keeps, green CI: it merges. + let mut v = view("APPROVED", Some("APPROVED")); + v.base_merge_at_head = true; + v.ci = Some(Default::default()); + assert_eq!(decide_gate(&v).action, Action::Merge); +} diff --git a/services/worker/src/passes/review_actions.rs b/services/worker/src/passes/review_actions.rs index 46257ada9..0648b41fc 100644 --- a/services/worker/src/passes/review_actions.rs +++ b/services/worker/src/passes/review_actions.rs @@ -234,6 +234,12 @@ pub struct Approvals { pub blocking_by: Vec, /// CoderHelm's own review of the head was dismissed and not posted again. pub bot_dismissed_at_head: bool, + /// Each approving person's approved commit, `(login, commit)`. + pub approved_on: Vec<(String, String)>, + /// People whose latest review was dismissed, with the commit it was of: + /// `(login, commit)`. A dismissal clears a verdict, so they are named + /// when the gate waits on a person. + pub dismissed: Vec<(String, String)>, } impl Approvals { @@ -290,11 +296,20 @@ pub fn approval_state(reviews: &serde_json::Value, head_sha: &str) -> Approvals .filter(|(_, (s, c))| *s == "APPROVED" && *c == head_sha) .map(|(l, _)| l.clone()) .collect(); + let on = |want: &str| -> Vec<(String, String)> { + latest + .iter() + .filter(|(_, (s, _))| *s == want) + .map(|(l, (_, c))| (l.clone(), c.to_string())) + .collect() + }; Approvals { approved_by: with("APPROVED"), approved_at_head, blocking_by: with("CHANGES_REQUESTED"), bot_dismissed_at_head: bot_at_head == Some("DISMISSED"), + approved_on: on("APPROVED"), + dismissed: on("DISMISSED"), } } diff --git a/services/worker/src/passes/review_eval.rs b/services/worker/src/passes/review_eval.rs index 4930146ed..7d3491ebc 100644 --- a/services/worker/src/passes/review_eval.rs +++ b/services/worker/src/passes/review_eval.rs @@ -518,6 +518,13 @@ impl Sim { labels_added_at: None, ci: None, now: 1_800_000_000 + self.now * 60, + head: self.head.clone(), + base: "main".to_string(), + review_decision: None, + earlier_approval_holds: false, + rules: Default::default(), + requested: vec![], + base_merge_at_head: false, }; let mut d = decide_gate(&view); if d.action == Action::ReadCi { diff --git a/services/worker/src/passes/review_pr.rs b/services/worker/src/passes/review_pr.rs index f59a3fc92..dbff4aa03 100644 --- a/services/worker/src/passes/review_pr.rs +++ b/services/worker/src/passes/review_pr.rs @@ -5927,6 +5927,62 @@ async fn own_loop_hold( ) } +/// Pure: the review the merge gate queues for the head its own base merge +/// made. It is a push's review of that head (`review_scope::TRIGGER_PUSH`), +/// so an earlier approval is carried with the review it rests on when the +/// PR's own change is the one approved (`review_scope::approved_base`), and +/// only the PR's own change since is reviewed otherwise. Keyed like the +/// gateway's push review of the same head (`head#`), so a labelled PR +/// whose push the gateway also queued is reviewed once. +pub(crate) fn base_merge_review( + team_id: &str, + installation_id: u64, + (owner, repo, pr_number): (&str, &str, u64), + head: &str, + label: &str, +) -> ReviewMessage { + ReviewMessage { + team_id: team_id.to_string(), + installation_id, + repo_owner: owner.to_string(), + repo_name: repo.to_string(), + pr_number, + head_sha: head.to_string(), + label: label.to_string(), + trigger: super::review_scope::TRIGGER_PUSH.to_string(), + dedup_key: format!("head#{head}"), + ..Default::default() + } +} + +/// After the merge gate merged the base into a PR's branch, it queues +/// CoderHelm's review of the new head itself, whether or not the PR carries +/// the review label: the gate never waits on a review it caused. False when +/// review is off for the repo or the job couldn't be sent. +pub(crate) async fn review_after_base_merge( + state: &WorkerState, + team_id: &str, + installation_id: u64, + pr: (&str, &str, u64), + head: &str, +) -> bool { + let Some(label) = enabled_trigger_label(state, team_id, pr.0, pr.1).await else { + return false; + }; + let msg = base_merge_review(team_id, installation_id, pr, head, &label); + // A moment for GitHub to settle the new head's compare. + let delay = 30; + if !enqueue_review(state, &msg, delay).await { + return false; + } + note_queued(state, &msg, delay).await; + info!( + pr = pr.2, + head, "The merge gate merged the base in — review of the new head queued" + ); + true +} + /// The review a run asks for when it hands its PR over without the PR /// reviewer's verdict on its last head: CoderHelm's own pushes during the /// run weren't reviewed (`own_loop_hold`). The job reads the PR's head when