Skip to content

Commit 012c44a

Browse files
authored
Merge pull request #1576 from Kobzol/merge-bot
Add `merge-bots` configuration per branch protection
2 parents d1f57e2 + a5231d3 commit 012c44a

File tree

8 files changed

+58
-14
lines changed

8 files changed

+58
-14
lines changed

docs/toml-schema.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,4 +352,12 @@ required-approvals = 1
352352
# can push/merge to the branch.
353353
# (optional)
354354
allowed-merge-teams = ["awesome-team"]
355+
# Determines the merge queue bot(s) that manage pushes to this branch.
356+
# When a bot manages the queue, some other options, like
357+
# `required-approvals` and `pr-required` options are not valid.
358+
#
359+
# Currently, only the "homu" option is supported.
360+
# When "homu" is used, "bors" has to be in the `bots` array.
361+
# (optional)
362+
merge-bots = ["homu"]
355363
```

rust_team_data/src/v1.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,19 @@ pub enum BranchProtectionMode {
219219
PrNotRequired,
220220
}
221221

222+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
223+
#[serde(rename_all = "snake_case")]
224+
pub enum MergeBot {
225+
Homu,
226+
}
227+
222228
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
223229
pub struct BranchProtection {
224230
pub pattern: String,
225231
pub dismiss_stale_review: bool,
226232
pub mode: BranchProtectionMode,
227233
pub allowed_merge_teams: Vec<String>,
234+
pub merge_bots: Vec<MergeBot>,
228235
}
229236

230237
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]

src/schema.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,12 @@ pub(crate) enum RepoPermission {
787787
Admin,
788788
}
789789

790+
#[derive(serde_derive::Deserialize, Debug, PartialEq, Eq)]
791+
#[serde(rename_all = "kebab-case")]
792+
pub(crate) enum MergeBot {
793+
Homu,
794+
}
795+
790796
#[derive(serde_derive::Deserialize, Debug)]
791797
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
792798
pub(crate) struct BranchProtection {
@@ -801,4 +807,6 @@ pub(crate) struct BranchProtection {
801807
pub pr_required: bool,
802808
#[serde(default)]
803809
pub allowed_merge_teams: Vec<String>,
810+
#[serde(default)]
811+
pub merge_bots: Vec<MergeBot>,
804812
}

src/static_api.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::data::Data;
2-
use crate::schema::{Bot, Email, Permissions, RepoPermission, TeamKind, ZulipGroupMember};
2+
use crate::schema::{
3+
Bot, Email, MergeBot, Permissions, RepoPermission, TeamKind, ZulipGroupMember,
4+
};
35
use anyhow::{ensure, Context as _, Error};
46
use indexmap::IndexMap;
57
use log::info;
@@ -59,6 +61,13 @@ impl<'a> Generator<'a> {
5961
BranchProtectionMode::PrNotRequired
6062
},
6163
allowed_merge_teams: b.allowed_merge_teams.clone(),
64+
merge_bots: b
65+
.merge_bots
66+
.iter()
67+
.map(|bot| match bot {
68+
MergeBot::Homu => v1::MergeBot::Homu,
69+
})
70+
.collect(),
6271
})
6372
.collect();
6473
let managed_by_bors = r.bots.contains(&Bot::Bors);

src/validate.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::data::Data;
22
use crate::github::GitHubApi;
3-
use crate::schema::{Bot, Email, Permissions, Team, TeamKind, TeamPeople, ZulipGroupMember};
3+
use crate::schema::{
4+
Bot, Email, MergeBot, Permissions, Team, TeamKind, TeamPeople, ZulipGroupMember,
5+
};
46
use crate::zulip::ZulipApi;
57
use anyhow::{bail, Error};
68
use log::{error, warn};
@@ -824,7 +826,8 @@ fn validate_branch_protections(data: &Data, errors: &mut Vec<String>) {
824826
let github_teams = data.github_teams();
825827

826828
wrapper(data.repos(), errors, |repo, _| {
827-
let bors_used = repo.bots.iter().any(|b| matches!(b, Bot::Bors));
829+
let homu_configured = repo.bots.iter().any(|b| matches!(b, Bot::Bors));
830+
828831
for protection in &repo.branch_protections {
829832
for team in &protection.allowed_merge_teams {
830833
let key = (repo.org.clone(), team.clone());
@@ -858,19 +861,24 @@ but that team does not seem to exist"#,
858861
}
859862
}
860863

861-
if bors_used {
862-
if protection.required_approvals.is_some() {
864+
let managed_by_homu = protection.merge_bots.contains(&MergeBot::Homu);
865+
if managed_by_homu {
866+
if !homu_configured {
863867
bail!(
864-
r#"repo '{}' uses bors and its branch protection for {} uses the `required-approvals` attribute;
865-
please remove the attribute when using bors"#,
868+
r#"repo '{}' uses homu to manage a branch protection for '{}', but homu is not enabled. Add "bors" to the `bots` array"#,
866869
repo.name,
867870
protection.pattern,
868871
);
869872
}
870-
if !protection.allowed_merge_teams.is_empty() {
873+
if protection.required_approvals.is_some()
874+
|| protection.dismiss_stale_review
875+
|| !protection.pr_required
876+
|| !protection.allowed_merge_teams.is_empty()
877+
{
871878
bail!(
872-
r#"repo '{}' uses bors and its branch protection for {} uses the `allowed-merge-teams` attribute;
873-
please remove the attribute when using bors"#,
879+
r#"repo '{}' uses the homu merge bot, but its branch protection for {} uses invalid
880+
attributes (`required-approvals`, `dismiss-stale-review`, `pr-required` or `allowed-merge-teams`).
881+
Please remove the attributes when using bors"#,
874882
repo.name,
875883
protection.pattern,
876884
);

tests/static-api/_expected/v1/repos.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
"required_approvals": 1
2626
}
2727
},
28-
"allowed_merge_teams": []
28+
"allowed_merge_teams": [],
29+
"merge_bots": []
2930
}
3031
],
3132
"archived": true,
@@ -59,7 +60,8 @@
5960
},
6061
"allowed_merge_teams": [
6162
"foo"
62-
]
63+
],
64+
"merge_bots": []
6365
}
6466
],
6567
"archived": false,

tests/static-api/_expected/v1/repos/archived_repo.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"required_approvals": 1
2424
}
2525
},
26-
"allowed_merge_teams": []
26+
"allowed_merge_teams": [],
27+
"merge_bots": []
2728
}
2829
],
2930
"archived": true,

tests/static-api/_expected/v1/repos/some_repo.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
},
2626
"allowed_merge_teams": [
2727
"foo"
28-
]
28+
],
29+
"merge_bots": []
2930
}
3031
],
3132
"archived": false,

0 commit comments

Comments
 (0)