Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
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`
```

**Example `config.yaml`:**
Expand Down Expand Up @@ -248,16 +248,20 @@ repos:
repos:
- name: service
required_checks: [Format, Unit Tests, "Clippy & Docs"]
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
```

- `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.
- A check the ruleset already pins to an app (`integration_id`, e.g. GitHub Actions) keeps that pin when `sync` rewrites the ruleset; a new check gets none. The pin isn't part of the config or the drift check.
- `allow_auto_merge` is written after the repo's `Require CI` ruleset, so if the ruleset write fails, auto-merge isn't left on without the checks it waits for.
- 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.
- `sync --dry-run` names the checks an update would stop requiring.
- `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.
- 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. Properties other than the checks aren't recorded, so `diff` shows no change when only those differ; `sync` still corrects them.
- `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.

### Webhooks

Expand Down
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `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

Expand Down
3 changes: 3 additions & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ repos: # (list) Repository configurations
visibility: private # (string, optional) "public" or "private"; required for sync to create a missing repo
required_checks: # (list, optional) Checks a PR to the default branch must pass (ruleset `Require CI`)
- Unit Tests
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)
webhook: # (object, optional) This repo's own webhook
url: "http://example.com" # (string) Webhook endpoint URL
content_type: "json" # (string) Payload content type
Expand Down
79 changes: 79 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,38 @@ pub struct Repo {
/// `Require CI` ruleset. Empty: `sync` leaves the repo's rulesets alone.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub required_checks: Vec<String>,
/// The teams that may merge past `required_checks`, and how. Nobody else can: `sync` makes the ruleset's
/// 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<CheckBypass>,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[serde(deny_unknown_fields)]
/// A team that may bypass a repo's `required_checks`.
pub struct CheckBypass {
/// The team's slug, as in `teams`.
pub team: String,
pub mode: BypassMode,
}

#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[serde(rename_all = "snake_case")]
/// GitHub's ruleset `bypass_mode`.
pub enum BypassMode {
/// Only by merging a PR; a direct push still has to pass the checks.
PullRequest,
/// By merging a PR or by pushing directly.
Always,
}

impl BypassMode {
pub fn as_str(self) -> &'static str {
match self {
BypassMode::PullRequest => "pull_request",
BypassMode::Always => "always",
}
}
}

#[derive(Debug, Serialize, Deserialize, Clone)] // Added Clone
Expand Down Expand Up @@ -178,6 +210,15 @@ impl Config {
}
}

Self::from_value(main_config, main_path)
}

/// Reads a config from YAML text; `main_path` names it in errors. Checked like a config file.
pub fn from_yaml_str(text: &str, main_path: &str) -> crate::error::AppResult<Self> {
Self::from_value(serde_yaml::from_str(text).map_err(crate::error::AppError::Serialization)?, main_path)
}

fn from_value(main_config: Value, main_path: &str) -> crate::error::AppResult<Self> {
// Unknown or misplaced keys are refused, naming the file, where the key is and the keys allowed there.
let config: Config = serde_path_to_error::deserialize(main_config)
.map_err(|e| crate::error::AppError::Config(format!("{main_path}: {}: {}", e.path(), e.inner())))?;
Expand All @@ -191,6 +232,21 @@ impl Config {
repo.name
)));
}
let bypass = &repo.required_checks_bypass;
let bypass_problem = if !bypass.is_empty() && checks.is_empty() {
Some("a bypass list needs `required_checks`: without them `sync` leaves the repo's rulesets alone.".to_string())
} else if let Some(dup) = bypass.iter().enumerate().find_map(|(j, b)| bypass.iter().take(j).any(|x| x.team == b.team).then_some(&b.team)) {
Some(format!("team `{dup}` is listed twice."))
} else {
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))
};
if let Some(problem) = bypass_problem {
return Err(crate::error::AppError::Config(format!(
"{main_path}: repos[{i}].required_checks_bypass (repo {}): {problem}",
repo.name
)));
}
if repo.use_default_webhook && (repo.webhook.is_some() || config.default_webhook.is_none()) {
return Err(crate::error::AppError::Config(format!(
"{main_path}: repos[{i}] (repo {}): `use_default_webhook` needs a top-level `default_webhook` and no `webhook` of its own.",
Expand Down Expand Up @@ -373,6 +429,29 @@ allow_force_pushes: false
assert!(err.contains("required_checks") && err.contains("`A` is listed twice"), "{err}");
}

#[test]
fn a_bypass_list_names_known_teams_once_with_a_known_mode_and_needs_checks() {
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 teams = "teams:\n- name: core\n members: []\n";
let ok = load(&format!("org: o\nrepos:\n- name: r\n required_checks: [A]\n required_checks_bypass:\n - team: core\n mode: pull_request\n{teams}")).unwrap();
assert_eq!(ok.repos[0].required_checks_bypass, [CheckBypass { team: "core".into(), mode: BypassMode::PullRequest }]);
for (bypass, expected) in [
("[{team: core, mode: sometimes}]", "unknown variant `sometimes`"),
("[{team: core, mode: always, why: x}]", "unknown field `why`"),
("[{team: core, mode: always}, {team: core, mode: pull_request}]", "team `core` is listed twice"),
("[{team: ghosts, mode: always}]", "team `ghosts` isn't one of the config's `teams`"),
] {
let err = load(&format!("org: o\nrepos:\n- name: r\n required_checks: [A]\n required_checks_bypass: {bypass}\n{teams}")).unwrap_err();
assert!(err.contains("required_checks_bypass") && err.contains(expected), "{bypass}: {err}");
}
let err = load(&format!("org: o\nrepos:\n- name: r\n required_checks_bypass: [{{team: core, mode: always}}]\n{teams}")).unwrap_err();
assert!(err.contains("needs `required_checks`"), "{err}");
}

#[test]
fn test_config_with_branch_protections() {
let yaml = r#"
Expand Down
Loading
Loading