Skip to content

Commit

Permalink
Implement sync of "PR required" property
Browse files Browse the repository at this point in the history
  • Loading branch information
Kobzol committed Sep 27, 2024
1 parent f235bd8 commit e9d3dcb
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/github/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ pub(crate) struct BranchProtection {
pub(crate) required_status_check_contexts: Vec<String>,
#[serde(deserialize_with = "allowances")]
pub(crate) push_allowances: Vec<PushAllowanceActor>,
pub(crate) requires_approving_reviews: bool,
}

fn nullable<'de, D, T>(deserializer: D) -> Result<T, D::Error>
Expand Down
7 changes: 5 additions & 2 deletions src/github/api/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,8 @@ impl GitHubWrite {
dismiss_stale: bool,
review_count: u8,
restricts_pushes: bool,
// Is a PR required to push into this branch?
requires_approving_reviews: bool,
push_actor_ids: &'a [String],
}
let mutation_name = match op {
Expand All @@ -424,7 +426,7 @@ impl GitHubWrite {
BranchProtectionOp::UpdateBranchProtection(id) => id,
};
let query = format!("
mutation($id: ID!, $pattern:String!, $contexts: [String!], $dismissStale: Boolean, $reviewCount: Int, $pushActorIds: [ID!], $restrictsPushes: Boolean) {{
mutation($id: ID!, $pattern:String!, $contexts: [String!], $dismissStale: Boolean, $reviewCount: Int, $pushActorIds: [ID!], $restrictsPushes: Boolean, $requiresApprovingReviews: Boolean) {{
{mutation_name}(input: {{
{id_field}: $id,
pattern: $pattern,
Expand All @@ -433,7 +435,7 @@ impl GitHubWrite {
isAdminEnforced: true,
requiredApprovingReviewCount: $reviewCount,
dismissesStaleReviews: $dismissStale,
requiresApprovingReviews:true,
requiresApprovingReviews: $requiresApprovingReviews,
restrictsPushes: $restrictsPushes,
pushActorIds: $pushActorIds
}}) {{
Expand Down Expand Up @@ -470,6 +472,7 @@ impl GitHubWrite {
// to merge *or* we only allow those in `push_actor_ids`)
restricts_pushes: !push_actor_ids.is_empty(),
push_actor_ids: &push_actor_ids,
requires_approving_reviews: branch_protection.requires_approving_reviews,
},
)?;
}
Expand Down
25 changes: 19 additions & 6 deletions src/github/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod tests;
use self::api::{BranchProtectionOp, TeamPrivacy, TeamRole};
use crate::github::api::{GithubRead, Login, PushAllowanceActor, RepoPermission, RepoSettings};
use log::debug;
use rust_team_data::v1::Bot;
use rust_team_data::v1::{Bot, BranchProtectionMode};
use std::collections::{HashMap, HashSet};
use std::fmt::{Display, Formatter, Write};

Expand Down Expand Up @@ -606,10 +606,14 @@ fn construct_branch_protection(
let required_approving_review_count: u8 = if uses_bors {
0
} else {
branch_protection
.required_approvals
.try_into()
.expect("Too large required approval count")
match branch_protection.mode {
BranchProtectionMode::PrRequired {
required_approvals, ..
} => required_approvals
.try_into()
.expect("Too large required approval count"),
BranchProtectionMode::PrNotRequired => 0,
}
};
let mut push_allowances: Vec<PushAllowanceActor> = branch_protection
.allowed_merge_teams
Expand All @@ -634,8 +638,17 @@ fn construct_branch_protection(
is_admin_enforced: true,
dismisses_stale_reviews: branch_protection.dismiss_stale_review,
required_approving_review_count,
required_status_check_contexts: branch_protection.ci_checks.clone(),
required_status_check_contexts: match &branch_protection.mode {
BranchProtectionMode::PrRequired { ci_checks, .. } => ci_checks.clone(),
BranchProtectionMode::PrNotRequired => {
vec![]
}
},
push_allowances,
requires_approving_reviews: matches!(
branch_protection.mode,
BranchProtectionMode::PrRequired { .. }
),
}
}

Expand Down

0 comments on commit e9d3dcb

Please sign in to comment.