Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a branch protection option to choose required approval count #1164

Merged
merged 2 commits into from
Jan 6, 2024
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
4 changes: 4 additions & 0 deletions docs/toml-schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,8 @@ ci-checks = ["CI"]
# merging into this branch require another review.
# (optional - default `false`)
dismiss-stale-review = false
# How many approvals are required for a PR to be merged.
# This option is only relevant if bors is not used.
Mark-Simulacrum marked this conversation as resolved.
Show resolved Hide resolved
# (optional - default `1`)
required-approvals = 1
```
1 change: 1 addition & 0 deletions rust_team_data/src/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ pub struct BranchProtection {
pub pattern: String,
pub ci_checks: Vec<String>,
pub dismiss_stale_review: bool,
pub required_approvals: u32,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down
2 changes: 2 additions & 0 deletions src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -733,4 +733,6 @@ pub(crate) struct BranchProtection {
pub ci_checks: Vec<String>,
#[serde(default)]
pub dismiss_stale_review: bool,
#[serde(default)]
pub required_approvals: Option<u32>,
}
1 change: 1 addition & 0 deletions src/static_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ impl<'a> Generator<'a> {
pattern: b.pattern.clone(),
ci_checks: b.ci_checks.clone(),
dismiss_stale_review: b.dismiss_stale_review,
required_approvals: b.required_approvals.unwrap_or(1),
})
.collect();
let repo = v1::Repo {
Expand Down
21 changes: 20 additions & 1 deletion src/validate.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::data::Data;
use crate::github::GitHubApi;
use crate::schema::{Email, Permissions, Team, TeamKind, TeamPeople, ZulipGroupMember};
use crate::schema::{Bot, Email, Permissions, Team, TeamKind, TeamPeople, ZulipGroupMember};
use crate::zulip::ZulipApi;
use anyhow::{bail, Error};
use log::{error, warn};
Expand Down Expand Up @@ -44,6 +44,7 @@ static CHECKS: &[Check<fn(&Data, &mut Vec<String>)>] = checks![
validate_zulip_group_ids,
validate_zulip_group_extra_people,
validate_repos,
validate_branch_protections,
];

#[allow(clippy::type_complexity)]
Expand Down Expand Up @@ -774,6 +775,24 @@ fn validate_repos(data: &Data, errors: &mut Vec<String>) {
});
}

/// Validate that branch protections make sense in combination with used bots.
fn validate_branch_protections(data: &Data, errors: &mut Vec<String>) {
wrapper(data.repos(), errors, |repo, _| {
let bors_used = repo.bots.iter().any(|b| matches!(b, Bot::Bors));
for protection in &repo.branch_protections {
if bors_used && protection.required_approvals.is_some() {
bail!(
r#"repo '{}' uses bors and its branch protection for {} uses the `required-approvals` attribute;
please remove the attribute when using bors"#,
repo.name,
protection.pattern,
);
}
}
Ok(())
})
}

fn wrapper<T, I, F>(iter: I, errors: &mut Vec<String>, mut func: F)
where
I: Iterator<Item = T>,
Expand Down
3 changes: 2 additions & 1 deletion tests/static-api/_expected/v1/repos.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"ci_checks": [
"CI"
],
"dismiss_stale_review": false
"dismiss_stale_review": false,
"required_approvals": 1
}
]
}
Expand Down
3 changes: 2 additions & 1 deletion tests/static-api/_expected/v1/repos/some_repo.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"ci_checks": [
"CI"
],
"dismiss_stale_review": false
"dismiss_stale_review": false,
"required_approvals": 1
}
]
}