Skip to content

Commit

Permalink
Read branch protection push allowances from rust_team_data
Browse files Browse the repository at this point in the history
  • Loading branch information
Kobzol committed Jan 23, 2024
1 parent a07d977 commit 9448c90
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
8 changes: 4 additions & 4 deletions src/github/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ where

/// An object with a `login` field
#[derive(Deserialize, Debug, Clone, PartialEq, Eq)]
struct Login {
login: String,
pub(crate) struct Login {
pub(crate) login: String,
}

#[derive(serde::Serialize, serde::Deserialize, Debug, Eq, PartialEq, Copy, Clone)]
Expand Down Expand Up @@ -365,8 +365,8 @@ pub(crate) struct UserPushAllowanceActor {
/// Team that can be allowed to push to a branch in a repo
#[derive(Clone, Deserialize, Debug, PartialEq, Eq)]
pub(crate) struct TeamPushAllowanceActor {
organization: Login,
name: String,
pub(crate) organization: Login,
pub(crate) name: String,
}

pub(crate) enum BranchProtectionOp {
Expand Down
32 changes: 21 additions & 11 deletions src/github/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod api;
mod tests;

use self::api::{BranchProtectionOp, TeamPrivacy, TeamRole};
use crate::github::api::{GithubRead, RepoPermission};
use crate::github::api::{GithubRead, Login, RepoPermission};
use log::debug;
use rust_team_data::v1::Bot;
use std::collections::{HashMap, HashSet};
Expand Down Expand Up @@ -454,23 +454,33 @@ fn construct_branch_protection(
expected_repo: &rust_team_data::v1::Repo,
branch_protection: &rust_team_data::v1::BranchProtection,
) -> api::BranchProtection {
let required_approving_review_count: u8 = if expected_repo.bots.contains(&Bot::Bors) {
let uses_bors = expected_repo.bots.contains(&Bot::Bors);
let required_approving_review_count: u8 = if uses_bors {
0
} else {
branch_protection
.required_approvals
.try_into()
.expect("Too large required approval count")
};
let push_allowances = expected_repo
.bots
.contains(&Bot::Bors)
.then(|| {
vec![api::PushAllowanceActor::User(api::UserPushAllowanceActor {
login: "bors".to_owned(),
})]
})
.unwrap_or_default();
let push_allowances = if uses_bors {
vec![api::PushAllowanceActor::User(api::UserPushAllowanceActor {
login: "bors".to_owned(),
})]
} else {
branch_protection
.allowed_merge_teams
.iter()
.map(|team| {
api::PushAllowanceActor::Team(api::TeamPushAllowanceActor {
organization: Login {
login: expected_repo.org.clone(),
},
name: team.to_string(),
})
})
.collect()
};
api::BranchProtection {
pattern: branch_protection.pattern.clone(),
is_admin_enforced: true,
Expand Down
1 change: 1 addition & 0 deletions src/github/tests/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ impl TeamData {
alumni: vec![],
github: (!gh_teams.is_empty()).then(|| TeamGitHub { teams: gh_teams }),
website_data: None,
roles: vec![],
discord: vec![],
}
}
Expand Down

0 comments on commit 9448c90

Please sign in to comment.