Skip to content
This repository was archived by the owner on Mar 31, 2025. It is now read-only.

Commit 7d3cf14

Browse files
authored
Merge pull request #76 from Kobzol/pr-required
Implement sync of "PR required" property
2 parents f235bd8 + 09ab992 commit 7d3cf14

File tree

5 files changed

+27
-9
lines changed

5 files changed

+27
-9
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/github/api/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ pub(crate) struct BranchProtection {
338338
pub(crate) required_status_check_contexts: Vec<String>,
339339
#[serde(deserialize_with = "allowances")]
340340
pub(crate) push_allowances: Vec<PushAllowanceActor>,
341+
pub(crate) requires_approving_reviews: bool,
341342
}
342343

343344
fn nullable<'de, D, T>(deserializer: D) -> Result<T, D::Error>

src/github/api/write.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,8 @@ impl GitHubWrite {
409409
dismiss_stale: bool,
410410
review_count: u8,
411411
restricts_pushes: bool,
412+
// Is a PR required to push into this branch?
413+
requires_approving_reviews: bool,
412414
push_actor_ids: &'a [String],
413415
}
414416
let mutation_name = match op {
@@ -424,7 +426,7 @@ impl GitHubWrite {
424426
BranchProtectionOp::UpdateBranchProtection(id) => id,
425427
};
426428
let query = format!("
427-
mutation($id: ID!, $pattern:String!, $contexts: [String!], $dismissStale: Boolean, $reviewCount: Int, $pushActorIds: [ID!], $restrictsPushes: Boolean) {{
429+
mutation($id: ID!, $pattern:String!, $contexts: [String!], $dismissStale: Boolean, $reviewCount: Int, $pushActorIds: [ID!], $restrictsPushes: Boolean, $requiresApprovingReviews: Boolean) {{
428430
{mutation_name}(input: {{
429431
{id_field}: $id,
430432
pattern: $pattern,
@@ -433,7 +435,7 @@ impl GitHubWrite {
433435
isAdminEnforced: true,
434436
requiredApprovingReviewCount: $reviewCount,
435437
dismissesStaleReviews: $dismissStale,
436-
requiresApprovingReviews:true,
438+
requiresApprovingReviews: $requiresApprovingReviews,
437439
restrictsPushes: $restrictsPushes,
438440
pushActorIds: $pushActorIds
439441
}}) {{
@@ -470,6 +472,7 @@ impl GitHubWrite {
470472
// to merge *or* we only allow those in `push_actor_ids`)
471473
restricts_pushes: !push_actor_ids.is_empty(),
472474
push_actor_ids: &push_actor_ids,
475+
requires_approving_reviews: branch_protection.requires_approving_reviews,
473476
},
474477
)?;
475478
}

src/github/mod.rs

+19-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ mod tests;
55
use self::api::{BranchProtectionOp, TeamPrivacy, TeamRole};
66
use crate::github::api::{GithubRead, Login, PushAllowanceActor, RepoPermission, RepoSettings};
77
use log::debug;
8-
use rust_team_data::v1::Bot;
8+
use rust_team_data::v1::{Bot, BranchProtectionMode};
99
use std::collections::{HashMap, HashSet};
1010
use std::fmt::{Display, Formatter, Write};
1111

@@ -606,10 +606,14 @@ fn construct_branch_protection(
606606
let required_approving_review_count: u8 = if uses_bors {
607607
0
608608
} else {
609-
branch_protection
610-
.required_approvals
611-
.try_into()
612-
.expect("Too large required approval count")
609+
match branch_protection.mode {
610+
BranchProtectionMode::PrRequired {
611+
required_approvals, ..
612+
} => required_approvals
613+
.try_into()
614+
.expect("Too large required approval count"),
615+
BranchProtectionMode::PrNotRequired => 0,
616+
}
613617
};
614618
let mut push_allowances: Vec<PushAllowanceActor> = branch_protection
615619
.allowed_merge_teams
@@ -634,8 +638,17 @@ fn construct_branch_protection(
634638
is_admin_enforced: true,
635639
dismisses_stale_reviews: branch_protection.dismiss_stale_review,
636640
required_approving_review_count,
637-
required_status_check_contexts: branch_protection.ci_checks.clone(),
641+
required_status_check_contexts: match &branch_protection.mode {
642+
BranchProtectionMode::PrRequired { ci_checks, .. } => ci_checks.clone(),
643+
BranchProtectionMode::PrNotRequired => {
644+
vec![]
645+
}
646+
},
638647
push_allowances,
648+
requires_approving_reviews: matches!(
649+
branch_protection.mode,
650+
BranchProtectionMode::PrRequired { .. }
651+
),
639652
}
640653
}
641654

src/github/tests/test_utils.rs

+1
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ impl TeamData {
152152
name: name.clone(),
153153
kind,
154154
subteam_of: None,
155+
top_level: None,
155156
members: vec![],
156157
alumni: vec![],
157158
github: (!gh_teams.is_empty()).then_some(TeamGitHub { teams: gh_teams }),

0 commit comments

Comments
 (0)