From 054ba6d2dcb4f174d22313ea976b157cb217912e Mon Sep 17 00:00:00 2001 From: gold-silver-copper <157451540+gold-silver-copper@users.noreply.github.com> Date: Tue, 10 Nov 2026 20:38:44 -0800 Subject: [PATCH] feat: merge_queue, a merge queue rule in the Require CI ruleset A repo's merge_queue adds a merge_queue rule to Require CI, so the queue's groups must pass required_checks. Only merge_method is required; the other fields default to GitHub's. sync writes it, sync-from-org and diff read it back; a queue a config can't name is refused. --- README.md | 7 +- docs/changelog.md | 1 + docs/usage.md | 2 + src/config.rs | 111 +++++++++++++++++++++ src/github.rs | 249 +++++++++++++++++++++++++++++++++++++++++----- 5 files changed, 344 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index efc44f4..14d1a2c 100644 --- a/README.md +++ b/README.md @@ -176,7 +176,7 @@ Every command refuses a config with a key it doesn't know before it contacts Git ```text config.yaml: repos[3].settings (repo old-tool): unknown setting `archive`. Did you mean `archived`? ... config.yaml: repos[3].settings (repo old-tool): `default_repository_permission` is not a repository setting: it would be sent to PATCH /orgs/{org}, which is not the repo. ... -config.yaml: repos[3].archived: unknown field `archived`, expected one of `name`, `settings`, `visibility`, `use_default_webhook`, `webhook`, `branch_protections`, `required_checks`, `required_checks_bypass` +config.yaml: repos[3].archived: unknown field `archived`, expected one of `name`, `settings`, `visibility`, `use_default_webhook`, `webhook`, `branch_protections`, `required_checks`, `required_checks_bypass`, `merge_queue` ``` **Example `config.yaml`:** @@ -251,6 +251,8 @@ repos: required_checks_bypass: # optional: who may merge past the checks, and how - team: core-team mode: pull_request # only by merging a PR; `always` also allows direct pushes + merge_queue: # optional: PRs merge through a queue whose groups must pass required_checks + merge_method: squash # required: merge, squash or rebase ``` - `sync` keeps them in the repo's own ruleset named `Require CI`: active, on the default branch (`~DEFAULT_BRANCH`), not strict (a PR needn't be up to date with the branch), one `required_status_checks` rule. It creates the ruleset if the repo has none by that name and rewrites it when its checks (in any order) or any of those properties differ. An existing `Require CI` ruleset is adopted by name. @@ -259,7 +261,8 @@ repos: - A name listed twice is refused when the config is loaded. A `Require CI` ruleset that requires one check twice (from different apps) is refused like one with another rule: a config can't say which pin to keep. - gh-config owns every rule in `Require CI`: if it holds any other rule, `sync` stops with an error naming the repo and the rule instead of dropping it. Keep other rules in other rulesets. - `required_checks_bypass` is the ruleset's whole bypass list: `sync` makes it exactly these teams (by slug), so leaving it out removes every bypass, including the org's admins, apps and roles a ruleset can name but a config can't. `mode` is `pull_request` (merging a PR may skip the checks; a direct push may not) or `always`. A team that isn't in the config's `teams` (when it lists any), a team named twice, an unknown mode, or a bypass list without `required_checks` is refused when the config is loaded; `sync` creates the config's `teams` before it touches any repo, so a bypass list may name a team the same config adds. A team that neither the org nor the config's `teams` has is refused before `sync` writes anything. -- `sync --dry-run` names the checks an update would stop requiring, who may bypass them after it and who no longer may. +- `merge_queue` adds a `merge_queue` rule to `Require CI`, so the queue's groups must pass `required_checks`. Only `merge_method` is required; the rest default to GitHub's defaults for a new queue: `grouping: allgreen` (or `headgreen`), `max_entries_to_build: 5`, `min_entries_to_merge: 1`, `max_entries_to_merge: 5`, `min_entries_to_merge_wait_minutes: 5`, `check_response_timeout_minutes: 60`. Leaving it out removes a queue the ruleset has. It needs `required_checks`. A workflow whose job is a required check must also run on `merge_group`, or queued PRs wait for it until they time out. +- `sync --dry-run` names the checks an update would stop requiring, who may bypass them after it and who no longer may, and any merge queue change. - A repo with no `required_checks` is left alone: `sync` doesn't read or delete its rulesets. To stop requiring checks, delete the ruleset on GitHub and the key. - `sync-from-org` records the checks of each repo's `Require CI` ruleset, in the file's order when the file names the same ones; `diff` compares them as a set. It costs one rulesets request per repo, plus one when `Require CI` exists. A ruleset that can't be read keeps what the file has, and `diff` names the repo and exits 1. It records the bypass list too, reading team ids as slugs (one teams request in all). GitHub shows a ruleset's bypass list only to a token that can administer the repo; when it doesn't, or the list holds anything other than the org's teams, the file's list is kept and `diff` names the repo and exits 1. A ruleset that requires no checks is recorded without them and without a bypass list (a config can't hold a bypass list alone), and `sync-from-org` never writes a config that wouldn't load: it stops and leaves the file as it is. Properties other than the checks and the bypass list aren't recorded, so `diff` shows no change when only those differ; `sync` still corrects them. diff --git a/docs/changelog.md b/docs/changelog.md index 5b732bc..af33948 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file. - `sync` creates repos the config lists and GitHub doesn't have, with their `visibility`. - `sync-from-org` and `diff` record `allow_auto_merge` and private repos' Actions `access_level`, so the drift check keeps them; `sync` reads `access_level` before writing it. - New repo field `required_checks`, kept in the repo ruleset `Require CI`. +- New repo field `merge_queue`: a merge queue rule in `Require CI`, whose groups must pass `required_checks`. `sync` writes it, `sync-from-org` and `diff` read it back. - New repo field `required_checks_bypass`: the teams, and only those, that may bypass `Require CI` (`pull_request` or `always`). `sync` writes it, `sync-from-org` and `diff` read it back. ## [v1.3.2] - 2026-11-09 diff --git a/docs/usage.md b/docs/usage.md index 4b34ef9..a8cfed1 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -56,6 +56,8 @@ repos: # (list) Repository configurations required_checks_bypass: # (list, optional) The only teams that may merge past required_checks - team: core-team # (string) Team slug mode: pull_request # (string) pull_request (by merging a PR) or always (also direct pushes) + merge_queue: # (object, optional) PRs merge through a queue; needs required_checks + merge_method: squash # (string) merge, squash or rebase; the other fields default to GitHub's webhook: # (object, optional) This repo's own webhook url: "http://example.com" # (string) Webhook endpoint URL content_type: "json" # (string) Payload content type diff --git a/src/config.rs b/src/config.rs index 073b148..c64207b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -92,6 +92,80 @@ pub struct Repo { /// bypass list exactly this, so leaving it out removes every bypass. Needs `required_checks`. #[serde(default, skip_serializing_if = "Vec::is_empty")] pub required_checks_bypass: Vec, + /// A merge queue on the default branch, kept in the `Require CI` ruleset: a PR merges only through the + /// queue, whose groups must pass `required_checks`. Needs `required_checks`. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub merge_queue: Option, +} + +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)] +#[serde(deny_unknown_fields)] +/// GitHub's merge queue rule. Only `merge_method` must be given; the rest default to GitHub's own defaults +/// for a new queue. +pub struct MergeQueue { + pub merge_method: MergeMethod, + /// `allgreen`: every PR in a group must pass; `headgreen`: only the group's head. + #[serde(default)] + pub grouping: MergeGrouping, + /// How many queued PRs are built at once. + #[serde(default = "five")] + pub max_entries_to_build: u32, + /// The smallest group merged once `min_entries_to_merge_wait_minutes` have passed. + #[serde(default = "one")] + pub min_entries_to_merge: u32, + /// The largest group merged at once. + #[serde(default = "five")] + pub max_entries_to_merge: u32, + #[serde(default = "five")] + pub min_entries_to_merge_wait_minutes: u32, + /// How long a group waits for its required checks before it fails. + #[serde(default = "sixty")] + pub check_response_timeout_minutes: u32, +} + +fn one() -> u32 { + 1 +} +fn five() -> u32 { + 5 +} +fn sixty() -> u32 { + 60 +} + +#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq)] +#[serde(rename_all = "snake_case")] +pub enum MergeMethod { + Merge, + Squash, + Rebase, +} + +#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq, Default)] +#[serde(rename_all = "lowercase")] +pub enum MergeGrouping { + #[default] + Allgreen, + Headgreen, +} + +impl MergeQueue { + /// What's wrong with the numbers, if anything. GitHub's limits: 0-100 entries, waits 0-360 minutes, + /// a check timeout of 1-360 minutes. + pub fn problem(&self) -> Option { + let entries = [self.max_entries_to_build, self.min_entries_to_merge, self.max_entries_to_merge]; + if entries.iter().any(|n| *n > 100) { + Some("entry counts must be 0-100".into()) + } else if self.min_entries_to_merge > self.max_entries_to_merge { + Some("min_entries_to_merge is above max_entries_to_merge".into()) + } else if self.min_entries_to_merge_wait_minutes > 360 { + Some("min_entries_to_merge_wait_minutes must be 0-360".into()) + } else if !(1..=360).contains(&self.check_response_timeout_minutes) { + Some("check_response_timeout_minutes must be 1-360".into()) + } else { + None + } + } } #[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, PartialOrd, Ord)] @@ -241,6 +315,14 @@ impl Config { let unknown = bypass.iter().find(|b| !config.teams.is_empty() && !config.teams.iter().any(|t| t.name == b.team)); unknown.map(|b| format!("team `{}` isn't one of the config's `teams`.", b.team)) }; + let queue_problem = match &repo.merge_queue { + Some(_) if checks.is_empty() => Some("a merge queue needs `required_checks`: its groups must pass them, and without them `sync` leaves the repo's rulesets alone.".to_string()), + Some(queue) => queue.problem(), + None => None, + }; + if let Some(problem) = queue_problem { + return Err(crate::error::AppError::Config(format!("{main_path}: repos[{i}].merge_queue (repo {}): {problem}", repo.name))); + } if let Some(problem) = bypass_problem { return Err(crate::error::AppError::Config(format!( "{main_path}: repos[{i}].required_checks_bypass (repo {}): {problem}", @@ -429,6 +511,35 @@ allow_force_pushes: false assert!(err.contains("required_checks") && err.contains("`A` is listed twice"), "{err}"); } + #[test] + fn a_merge_queue_needs_checks_a_known_method_and_sane_numbers() { + let load = |text: &str| { + let mut file = tempfile::NamedTempFile::new().unwrap(); + std::io::Write::write_all(&mut file, text.as_bytes()).unwrap(); + Config::from_file_with_defaults(file.path().to_str().unwrap(), None).map_err(|e| e.to_string()) + }; + let ok = load("org: o\nrepos:\n- name: r\n required_checks: [CI gate]\n merge_queue: {merge_method: squash}\n").unwrap(); + let queue = ok.repos[0].merge_queue.clone().unwrap(); + assert_eq!((queue.merge_method, queue.grouping), (MergeMethod::Squash, MergeGrouping::Allgreen)); + assert_eq!( + (queue.max_entries_to_build, queue.min_entries_to_merge, queue.max_entries_to_merge, queue.min_entries_to_merge_wait_minutes, queue.check_response_timeout_minutes), + (5, 1, 5, 5, 60), + "GitHub's defaults for a new queue" + ); + for (queue, expected) in [ + ("{merge_method: fast_forward}", "unknown variant `fast_forward`"), + ("{grouping: allgreen}", "missing field `merge_method`"), + ("{merge_method: squash, max_entries: 3}", "unknown field `max_entries`"), + ("{merge_method: squash, min_entries_to_merge: 6}", "min_entries_to_merge is above max_entries_to_merge"), + ("{merge_method: squash, check_response_timeout_minutes: 0}", "check_response_timeout_minutes must be 1-360"), + ] { + let err = load(&format!("org: o\nrepos:\n- name: r\n required_checks: [CI gate]\n merge_queue: {queue}\n")).unwrap_err(); + assert!(err.contains("merge_queue") && err.contains(expected), "{queue}: {err}"); + } + let err = load("org: o\nrepos:\n- name: r\n merge_queue: {merge_method: squash}\n").unwrap_err(); + assert!(err.contains("needs `required_checks`"), "{err}"); + } + #[test] fn a_bypass_list_names_known_teams_once_with_a_known_mode_and_needs_checks() { let load = |text: &str| { diff --git a/src/github.rs b/src/github.rs index 64b6042..3326979 100644 --- a/src/github.rs +++ b/src/github.rs @@ -9,7 +9,7 @@ Public APIs are documented for maintainability. Internal response structs are used for deserialization. */ -use crate::config::{Assignment, BypassMode, CheckBypass, Repo, RepoSettings, Team, User, WebhookConfig, Config}; +use crate::config::{Assignment, BypassMode, CheckBypass, MergeGrouping, MergeMethod, MergeQueue, Repo, RepoSettings, Team, User, WebhookConfig, Config}; use crate::error::{AppError, AppResult}; use colored::*; use log::{debug, info, error}; @@ -32,6 +32,55 @@ const ACCESS_LEVEL: &str = "access_level"; /// The repo ruleset `required_checks` is kept in. gh-config owns every rule in it. const REQUIRE_CI: &str = "Require CI"; const REQUIRED_STATUS_CHECKS: &str = "required_status_checks"; +const MERGE_QUEUE: &str = "merge_queue"; + +/// The `merge_queue` rule's parameters, as GitHub takes them. +fn merge_queue_parameters(queue: &MergeQueue) -> serde_json::Value { + let method = match queue.merge_method { + MergeMethod::Merge => "MERGE", + MergeMethod::Squash => "SQUASH", + MergeMethod::Rebase => "REBASE", + }; + let grouping = match queue.grouping { + MergeGrouping::Allgreen => "ALLGREEN", + MergeGrouping::Headgreen => "HEADGREEN", + }; + json!({ + "merge_method": method, + "grouping_strategy": grouping, + "max_entries_to_build": queue.max_entries_to_build, + "min_entries_to_merge": queue.min_entries_to_merge, + "max_entries_to_merge": queue.max_entries_to_merge, + "min_entries_to_merge_wait_minutes": queue.min_entries_to_merge_wait_minutes, + "check_response_timeout_minutes": queue.check_response_timeout_minutes, + }) +} + +/// A `merge_queue` rule's parameters as a config entry; a value a config can't name is refused. +fn parse_merge_queue(repo: &str, params: &serde_json::Value) -> AppResult { + let refused = |what: &str| AppError::GitHubApi(format!("Repo {repo}: its `{REQUIRE_CI}` merge queue has {what}, which a config can't name")); + let number = |key: &str| params[key].as_u64().and_then(|n| u32::try_from(n).ok()).ok_or_else(|| refused(&format!("no {key}"))); + let merge_method = match params["merge_method"].as_str() { + Some("MERGE") => MergeMethod::Merge, + Some("SQUASH") => MergeMethod::Squash, + Some("REBASE") => MergeMethod::Rebase, + other => return Err(refused(&format!("merge_method {other:?}"))), + }; + let grouping = match params["grouping_strategy"].as_str() { + Some("ALLGREEN") => MergeGrouping::Allgreen, + Some("HEADGREEN") => MergeGrouping::Headgreen, + other => return Err(refused(&format!("grouping_strategy {other:?}"))), + }; + Ok(MergeQueue { + merge_method, + grouping, + max_entries_to_build: number("max_entries_to_build")?, + min_entries_to_merge: number("min_entries_to_merge")?, + max_entries_to_merge: number("max_entries_to_merge")?, + min_entries_to_merge_wait_minutes: number("min_entries_to_merge_wait_minutes")?, + check_response_timeout_minutes: number("check_response_timeout_minutes")?, + }) +} /// Settings a generated config writes only when they aren't GitHub's default, with that default. `diff` gives /// GitHub's side the default of each one a config names, so writing the default out is no difference. @@ -117,12 +166,31 @@ fn bypass_change(bypass: &[BypassActor], held: Option<&[BypassActor]>, exists: b format!("bypass: {allowed}{dropped}") } +/// The merge-queue part of a dry run's line, empty when it doesn't change. +fn queue_change(wanted: Option<&MergeQueue>, held: Option<&MergeQueue>) -> String { + let named = |q: &MergeQueue| { + format!( + "{:?} merge, {:?} groups of {}-{} (waiting {} min), {} built at once, checks time out after {} min", + q.merge_method, q.grouping, q.min_entries_to_merge, q.max_entries_to_merge, q.min_entries_to_merge_wait_minutes, + q.max_entries_to_build, q.check_response_timeout_minutes + ) + .to_lowercase() + }; + match (wanted, held) { + (a, b) if a == b => String::new(), + (Some(q), _) => format!("; merge queue: {}", named(q)), + (None, Some(_)) => "; merge queue removed".to_string(), + (None, None) => String::new(), + } +} + /// A repo's `Require CI` ruleset as GitHub holds it. struct RequireCi { id: u64, checks: Vec, /// Sorted. `None` when GitHub didn't say: it shows a ruleset's bypass list only to tokens that can administer the repo. bypass: Option>, + merge_queue: Option, /// Whether everything but the checks is what `sync` writes: active, on the default branch, not strict. standard: bool, } @@ -133,8 +201,9 @@ impl RequireCi { } } -/// The `Require CI` ruleset `sync` writes for `checks`, which only `bypass` may merge past. -fn require_ci_body(checks: &[RequiredCheck], bypass: &[BypassActor]) -> serde_json::Value { +/// The `Require CI` ruleset `sync` writes for `checks`, which only `bypass` may merge past, with `merge_queue` +/// when there is one. +fn require_ci_body(checks: &[RequiredCheck], bypass: &[BypassActor], merge_queue: Option<&MergeQueue>) -> serde_json::Value { let bypass: Vec<_> = bypass .iter() .map(|a| match a.actor_id { @@ -149,33 +218,44 @@ fn require_ci_body(checks: &[RequiredCheck], bypass: &[BypassActor]) -> serde_js None => json!({ "context": c.context }), }) .collect(); + let mut rules = vec![json!({ + "type": REQUIRED_STATUS_CHECKS, + "parameters": { + "required_status_checks": checks, + "strict_required_status_checks_policy": false, + "do_not_enforce_on_create": false, + }, + })]; + if let Some(queue) = merge_queue { + rules.push(json!({ "type": MERGE_QUEUE, "parameters": merge_queue_parameters(queue) })); + } json!({ "name": REQUIRE_CI, "target": "branch", "enforcement": "active", "bypass_actors": bypass, "conditions": { "ref_name": { "include": ["~DEFAULT_BRANCH"], "exclude": [] } }, - "rules": [{ - "type": REQUIRED_STATUS_CHECKS, - "parameters": { - "required_status_checks": checks, - "strict_required_status_checks_policy": false, - "do_not_enforce_on_create": false, - }, - }], + "rules": rules, }) } -/// Reads a `Require CI` ruleset. A rule other than required status checks is refused: `sync` would drop it. +/// Reads a `Require CI` ruleset. A rule other than required status checks and the merge queue is refused: +/// `sync` would drop it. fn parse_require_ci(repo: &str, ruleset: &serde_json::Value) -> AppResult { let id = ruleset["id"].as_u64().ok_or_else(|| AppError::GitHubApi(format!("Repo {repo}: the `{REQUIRE_CI}` ruleset has no id")))?; let rules = ruleset["rules"].as_array().map(Vec::as_slice).unwrap_or_default(); - if let Some(other) = rules.iter().map(|r| r["type"].as_str().unwrap_or("?")).find(|t| *t != REQUIRED_STATUS_CHECKS) { + if let Some(other) = rules.iter().map(|r| r["type"].as_str().unwrap_or("?")).find(|t| *t != REQUIRED_STATUS_CHECKS && *t != MERGE_QUEUE) { return Err(AppError::GitHubApi(format!( "Repo {repo}: its `{REQUIRE_CI}` ruleset also holds a `{other}` rule. gh-config owns that ruleset and would drop the rule: move the rule to another ruleset first." ))); } - let params = rules.first().map(|r| &r["parameters"]); + let of_type = |t: &str| rules.iter().filter(|r| r["type"] == t).collect::>(); + let (check_rules, queue_rules) = (of_type(REQUIRED_STATUS_CHECKS), of_type(MERGE_QUEUE)); + if check_rules.len() > 1 || queue_rules.len() > 1 { + return Err(AppError::GitHubApi(format!("Repo {repo}: its `{REQUIRE_CI}` ruleset holds a rule type twice, which a config can't name"))); + } + let merge_queue = queue_rules.first().map(|r| parse_merge_queue(repo, &r["parameters"])).transpose()?; + let params = check_rules.first().map(|r| &r["parameters"]); let checks = params .and_then(|p| p["required_status_checks"].as_array()) .map(|checks| { @@ -195,7 +275,7 @@ fn parse_require_ci(repo: &str, ruleset: &serde_json::Value) -> AppResult AppResult return Ok(()), + Some(current) + if current.standard + && same_checks(¤t.contexts(), checks) + && current.bypass.as_ref() == Some(&bypass) + && current.merge_queue == repo.merge_queue => + { + return Ok(()) + } Some(current) => (Method::PUT, format!("{}/repos/{}/{}/rulesets/{}", self.api_url, self.org, repo.name, current.id)), None => (Method::POST, format!("{}/repos/{}/{}/rulesets", self.api_url, self.org, repo.name)), }; @@ -547,10 +635,11 @@ impl GitHubClient { let removed: Vec<_> = held.iter().map(|c| c.context.as_str()).filter(|c| !checks.iter().any(|n| n == c)).collect(); let removed = if removed.is_empty() { String::new() } else { format!("; no longer requiring: {}", removed.join(", ")) }; let bypass = bypass_change(&bypass, held_bypass.as_deref(), method == Method::PUT, teams); - println!("[Dry Run] Would {verb} ruleset `{REQUIRE_CI}` on {}/{} requiring: {}{removed}; {bypass}", self.org, repo.name, checks.join(", ")); + let queue = queue_change(repo.merge_queue.as_ref(), held_queue.as_ref()); + println!("[Dry Run] Would {verb} ruleset `{REQUIRE_CI}` on {}/{} requiring: {}{removed}; {bypass}{queue}", self.org, repo.name, checks.join(", ")); return Ok(()); } - self.client.request(method, &url, "application/vnd.github+json", Some(require_ci_body(&desired, &bypass))).await?; + self.client.request(method, &url, "application/vnd.github+json", Some(require_ci_body(&desired, &bypass, repo.merge_queue.as_ref()))).await?; info!("Ruleset `{REQUIRE_CI}` on {}/{} now requires: {}; bypass: {}", self.org, repo.name, checks.join(", "), bypass.len()); Ok(()) } @@ -1095,6 +1184,13 @@ impl GitHubClient { yaml_content.push_str(&format!(" - {}\n", serde_yaml::to_string(check)?.trim())); } } + if let Some(queue) = &repo.merge_queue { + yaml_content.push_str(" merge_queue:\n"); + let fields = serde_yaml::to_string(queue)?; + for line in fields.lines() { + yaml_content.push_str(&format!(" {line}\n")); + } + } if !repo.required_checks_bypass.is_empty() { yaml_content.push_str(" required_checks_bypass:\n"); for bypass in &repo.required_checks_bypass { @@ -1236,7 +1332,8 @@ impl GitHubClient { }; let file_checks = in_file.map(|r| r.required_checks.clone()).unwrap_or_default(); let file_bypass = in_file.map(|r| r.required_checks_bypass.clone()).unwrap_or_default(); - let (required_checks, required_checks_bypass) = match self.read_require_ci(&name).await { + let file_queue = in_file.and_then(|r| r.merge_queue.clone()); + let (required_checks, required_checks_bypass, merge_queue) = match self.read_require_ci(&name).await { Ok(found) => { // The file's order when it names the same checks, so regenerating doesn't reorder them. let checks = match found.as_ref().map(RequireCi::contexts).unwrap_or_default() { @@ -1265,12 +1362,13 @@ impl GitHubClient { // A config without checks leaves the ruleset alone, so a bypass list there would mean nothing: // the loader refuses one. let bypass = if checks.is_empty() { Vec::new() } else { bypass }; - (checks, bypass) + let queue = found.as_ref().and_then(|r| r.merge_queue.clone()).filter(|_| !checks.is_empty()); + (checks, bypass, queue) } Err(e) => { error!("Failed to read the `{}` ruleset of repo {}: {}. Its required checks stay as the file has them.", REQUIRE_CI, name, e); unread.push(format!("{name} (required checks)")); - (file_checks, file_bypass) + (file_checks, file_bypass, file_queue) } }; @@ -1283,6 +1381,7 @@ impl GitHubClient { branch_protections: vec![], // Add logic to fetch these if needed required_checks, required_checks_bypass, + merge_queue, }); } @@ -1733,7 +1832,7 @@ mod tests { /// A `Require CI` ruleset with id `id` requiring `checks`, as `sync` writes it. fn require_ci(id: u64, checks: &[&str]) -> serde_json::Value { - let mut ruleset = require_ci_body(&unpinned(checks), &[]); + let mut ruleset = require_ci_body(&unpinned(checks), &[], None); ruleset["id"] = json!(id); ruleset } @@ -2286,7 +2385,7 @@ assignments: client(&server).sync(path, true).await.expect("dry run"); assert!(writes.lock().expect("lock").is_empty(), "a dry run wrote to GitHub"); client(&server).sync(path, false).await.expect("sync"); - let body = |checks: &[&str]| require_ci_body(&unpinned(checks), &[]).to_string(); + let body = |checks: &[&str]| require_ci_body(&unpinned(checks), &[], None).to_string(); assert_eq!( *writes.lock().expect("lock"), [format!("POST /repos/o/fresh/rulesets {}", body(&["A", "Clippy & Docs"])), format!("PUT /repos/o/adopt/rulesets/7 {}", body(&["A", "B"]))] @@ -2632,6 +2731,108 @@ assignments: assert!(repo.required_checks.is_empty() && repo.required_checks_bypass.is_empty(), "{repo:?}"); } + fn squash_queue() -> MergeQueue { + MergeQueue { + merge_method: MergeMethod::Squash, + grouping: MergeGrouping::Allgreen, + max_entries_to_build: 5, + min_entries_to_merge: 1, + max_entries_to_merge: 5, + min_entries_to_merge_wait_minutes: 5, + check_response_timeout_minutes: 60, + } + } + + #[tokio::test] + async fn a_merge_queue_is_written_into_require_ci_after_its_checks() { + let mut server = mockito::Server::new_async().await; + let _mocks = mock_org_with_teams(&mut server, &[], &[("r", json!({}))], |_| json!([]), |_| Some(vec![])).await; + let writes = std::sync::Arc::new(std::sync::Mutex::new(Vec::::new())); + let _post = record(&mut server, &writes, "POST", "/repos/o/r/rulesets").await; + let file = write_file("org: o\nrepos:\n- name: r\n required_checks: [CI gate]\n merge_queue: {merge_method: squash}\n"); + client(&server).sync(file.path().to_str().expect("utf-8 path"), false).await.expect("sync"); + let writes = writes.lock().expect("lock"); + let [post] = writes.as_slice() else { panic!("{writes:?}") }; + let sent: serde_json::Value = serde_json::from_str(post.trim_start_matches("POST /repos/o/r/rulesets ")).expect("json body"); + assert_eq!(sent["rules"][0]["type"], "required_status_checks"); + assert_eq!(sent["rules"][1], json!({"type": "merge_queue", "parameters": { + "merge_method": "SQUASH", "grouping_strategy": "ALLGREEN", "max_entries_to_build": 5, "min_entries_to_merge": 1, + "max_entries_to_merge": 5, "min_entries_to_merge_wait_minutes": 5, "check_response_timeout_minutes": 60}})); + } + + #[tokio::test] + async fn a_merge_queue_round_trips_and_a_matching_one_is_not_rewritten() { + let mut server = mockito::Server::new_async().await; + let mut ruleset = require_a_with(5, Some(json!([]))); + let mut queue = squash_queue(); + queue.grouping = MergeGrouping::Headgreen; + queue.max_entries_to_merge = 3; + ruleset["rules"].as_array_mut().expect("rules").push(json!({"type": "merge_queue", "parameters": merge_queue_parameters(&queue)})); + let _mocks = mock_org_with_teams(&mut server, &[], &[("r", json!({}))], |_| json!([]), |_| Some(vec![ruleset.clone()])).await; + let writes = std::sync::Arc::new(std::sync::Mutex::new(Vec::::new())); + let _put = record(&mut server, &writes, "PUT", "/repos/o/r/rulesets/5").await; + let file = write_file("org: o\nrepos:\n- name: r\n required_checks: [A]\n"); + let path = file.path().to_str().expect("utf-8 path"); + client(&server).generate_config_and_write(path, false).await.expect("export"); + let yaml = std::fs::read_to_string(path).expect("read export"); + assert!(repo_block(&yaml, "r").contains(" merge_queue:\n merge_method: squash\n grouping: headgreen\n"), "{yaml}"); + let exported = Config::from_file_with_defaults(path, None).expect("the export loads"); + assert_eq!(exported.repos.iter().find(|r| r.name == "r").and_then(|r| r.merge_queue.clone()), Some(queue)); + assert!(!client(&server).diff(path).await.expect("diff"), "the exported config doesn't match what it was read from"); + client(&server).sync(path, false).await.expect("sync"); + assert!(writes.lock().expect("lock").is_empty(), "a matching ruleset was rewritten"); + } + + #[tokio::test] + async fn a_merge_queue_the_config_drops_or_changes_is_rewritten_and_the_checks_kept() { + let mut server = mockito::Server::new_async().await; + let mut ruleset = require_a_with(5, Some(json!([]))); + ruleset["rules"].as_array_mut().expect("rules").push(json!({"type": "merge_queue", "parameters": merge_queue_parameters(&squash_queue())})); + let _mocks = mock_org_with_teams(&mut server, &[], &[("r", json!({}))], |_| json!([]), |_| Some(vec![ruleset.clone()])).await; + let writes = std::sync::Arc::new(std::sync::Mutex::new(Vec::::new())); + let _put = record(&mut server, &writes, "PUT", "/repos/o/r/rulesets/5").await; + for (config, queue_rules) in [ + ("org: o\nrepos:\n- name: r\n required_checks: [A]\n", 0), + ("org: o\nrepos:\n- name: r\n required_checks: [A]\n merge_queue: {merge_method: merge}\n", 1), + ] { + writes.lock().expect("lock").clear(); + let file = write_file(config); + let path = file.path().to_str().expect("utf-8 path"); + assert!(client(&server).diff(path).await.expect("diff"), "a different queue was called a match: {config}"); + client(&server).sync(path, false).await.expect("sync"); + let writes = writes.lock().expect("lock"); + let [put] = writes.as_slice() else { panic!("{writes:?}") }; + let sent: serde_json::Value = serde_json::from_str(put.trim_start_matches("PUT /repos/o/r/rulesets/5 ")).expect("json body"); + let rules = sent["rules"].as_array().expect("rules"); + assert_eq!(rules[0], ruleset["rules"][0], "the checks changed"); + assert_eq!(rules.len(), 1 + queue_rules, "{sent}"); + if queue_rules == 1 { + assert_eq!(rules[1]["parameters"]["merge_method"], "MERGE"); + } + } + } + + #[test] + fn a_dry_run_names_the_merge_queue_change() { + let queue = squash_queue(); + assert_eq!(queue_change(Some(&queue), Some(&queue)), ""); + assert_eq!( + queue_change(Some(&queue), None), + "; merge queue: squash merge, allgreen groups of 1-5 (waiting 5 min), 5 built at once, checks time out after 60 min" + ); + assert_eq!(queue_change(None, Some(&queue)), "; merge queue removed"); + } + + #[test] + fn a_merge_queue_a_config_cant_name_is_refused() { + let mut ruleset = require_a_with(5, Some(json!([]))); + let mut params = merge_queue_parameters(&squash_queue()); + params["merge_method"] = json!("FAST_FORWARD"); + ruleset["rules"].as_array_mut().expect("rules").push(json!({"type": "merge_queue", "parameters": params})); + let err = parse_require_ci("r", &ruleset).err().expect("accepted").to_string(); + assert!(err.contains("merge_method") && err.contains("FAST_FORWARD"), "{err}"); + } + #[tokio::test] async fn required_checks_that_cant_be_read_keep_the_files_and_are_never_a_clean_diff() { let mut server = mockito::Server::new_async().await;