From 9448c900d862876aa3386f26cb7249bef1b66a1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Tue, 23 Jan 2024 12:43:50 +0100 Subject: [PATCH] Read branch protection push allowances from `rust_team_data` --- src/github/api/mod.rs | 8 ++++---- src/github/mod.rs | 32 +++++++++++++++++++++----------- src/github/tests/test_utils.rs | 1 + 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/github/api/mod.rs b/src/github/api/mod.rs index 3def026..fc22085 100644 --- a/src/github/api/mod.rs +++ b/src/github/api/mod.rs @@ -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)] @@ -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 { diff --git a/src/github/mod.rs b/src/github/mod.rs index 9130b3c..fd66d3b 100644 --- a/src/github/mod.rs +++ b/src/github/mod.rs @@ -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}; @@ -454,7 +454,8 @@ 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 @@ -462,15 +463,24 @@ fn construct_branch_protection( .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, diff --git a/src/github/tests/test_utils.rs b/src/github/tests/test_utils.rs index 0b668a4..3f2909a 100644 --- a/src/github/tests/test_utils.rs +++ b/src/github/tests/test_utils.rs @@ -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![], } }