Skip to content

Commit

Permalink
Merge pull request #97 from Kobzol/branch-protection-tests
Browse files Browse the repository at this point in the history
Add branch protection tests
  • Loading branch information
marcoieni authored Dec 16, 2024
2 parents f576797 + 06103d8 commit fd604cb
Show file tree
Hide file tree
Showing 4 changed files with 349 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/github/api/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub(crate) trait GithubRead {
fn repo_collaborators(&self, org: &str, repo: &str) -> anyhow::Result<Vec<RepoUser>>;

/// Get branch_protections
/// Returns a map branch pattern -> (protection ID, protection data)
fn branch_protections(
&self,
org: &str,
Expand Down
2 changes: 1 addition & 1 deletion src/github/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ pub fn convert_permission(p: &rust_team_data::v1::RepoPermission) -> RepoPermiss
}
}

fn construct_branch_protection(
pub fn construct_branch_protection(
expected_repo: &rust_team_data::v1::Repo,
branch_protection: &rust_team_data::v1::BranchProtection,
) -> api::BranchProtection {
Expand Down
264 changes: 260 additions & 4 deletions src/github/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::github::tests::test_utils::{DataModel, RepoData, TeamData};
use rust_team_data::v1::RepoPermission;
use crate::github::tests::test_utils::{BranchProtectionBuilder, DataModel, RepoData, TeamData};
use rust_team_data::v1::{BranchProtectionMode, RepoPermission};

mod test_utils;

Expand Down Expand Up @@ -307,7 +307,13 @@ fn repo_create() {
RepoData::new("repo1")
.description("foo".to_string())
.member("user1", RepoPermission::Write)
.team("team1", RepoPermission::Triage),
.team("team1", RepoPermission::Triage)
.branch_protections(vec![BranchProtectionBuilder::pr_required(
"main",
&["test"],
1,
)
.build()]),
);
let diff = model.diff_repos(gh);
insta::assert_debug_snapshot!(diff, @r#"
Expand Down Expand Up @@ -342,7 +348,22 @@ fn repo_create() {
),
},
],
branch_protections: [],
branch_protections: [
(
"main",
BranchProtection {
pattern: "main",
is_admin_enforced: true,
dismisses_stale_reviews: false,
required_approving_review_count: 1,
required_status_check_contexts: [
"test",
],
push_allowances: [],
requires_approving_reviews: true,
},
),
],
app_installations: [],
},
),
Expand Down Expand Up @@ -726,3 +747,238 @@ fn repo_archive_repo() {
]
"#);
}

#[test]
fn repo_add_branch_protection() {
let mut model = DataModel::default();
model.create_repo(RepoData::new("repo1").team("team1", RepoPermission::Write));

let gh = model.gh_model();
model.get_repo("repo1").branch_protections.extend([
BranchProtectionBuilder::pr_required("master", &["test", "test 2"], 0).build(),
BranchProtectionBuilder::pr_not_required("beta").build(),
]);

let diff = model.diff_repos(gh);
insta::assert_debug_snapshot!(diff, @r#"
[
Update(
UpdateRepoDiff {
org: "rust-lang",
name: "repo1",
repo_node_id: "0",
repo_id: 0,
settings_diff: (
RepoSettings {
description: Some(
"",
),
homepage: None,
archived: false,
auto_merge_enabled: false,
},
RepoSettings {
description: Some(
"",
),
homepage: None,
archived: false,
auto_merge_enabled: false,
},
),
permission_diffs: [],
branch_protection_diffs: [
BranchProtectionDiff {
pattern: "master",
operation: Create(
BranchProtection {
pattern: "master",
is_admin_enforced: true,
dismisses_stale_reviews: false,
required_approving_review_count: 0,
required_status_check_contexts: [
"test",
"test 2",
],
push_allowances: [],
requires_approving_reviews: true,
},
),
},
BranchProtectionDiff {
pattern: "beta",
operation: Create(
BranchProtection {
pattern: "beta",
is_admin_enforced: true,
dismisses_stale_reviews: false,
required_approving_review_count: 0,
required_status_check_contexts: [],
push_allowances: [],
requires_approving_reviews: false,
},
),
},
],
app_installation_diffs: [],
},
),
]
"#);
}

#[test]
fn repo_update_branch_protection() {
let mut model = DataModel::default();
model.create_repo(
RepoData::new("repo1")
.team("team1", RepoPermission::Write)
.branch_protections(vec![BranchProtectionBuilder::pr_required(
"master",
&["test"],
1,
)
.build()]),
);

let gh = model.gh_model();
let protection = model
.get_repo("repo1")
.branch_protections
.last_mut()
.unwrap();
match &mut protection.mode {
BranchProtectionMode::PrRequired {
ci_checks,
required_approvals,
} => {
ci_checks.push("Test".to_string());
*required_approvals = 0;
}
BranchProtectionMode::PrNotRequired => unreachable!(),
}
protection.dismiss_stale_review = true;

let diff = model.diff_repos(gh);
insta::assert_debug_snapshot!(diff, @r#"
[
Update(
UpdateRepoDiff {
org: "rust-lang",
name: "repo1",
repo_node_id: "0",
repo_id: 0,
settings_diff: (
RepoSettings {
description: Some(
"",
),
homepage: None,
archived: false,
auto_merge_enabled: false,
},
RepoSettings {
description: Some(
"",
),
homepage: None,
archived: false,
auto_merge_enabled: false,
},
),
permission_diffs: [],
branch_protection_diffs: [
BranchProtectionDiff {
pattern: "master",
operation: Update(
"0",
BranchProtection {
pattern: "master",
is_admin_enforced: true,
dismisses_stale_reviews: false,
required_approving_review_count: 1,
required_status_check_contexts: [
"test",
],
push_allowances: [],
requires_approving_reviews: true,
},
BranchProtection {
pattern: "master",
is_admin_enforced: true,
dismisses_stale_reviews: true,
required_approving_review_count: 0,
required_status_check_contexts: [
"test",
"Test",
],
push_allowances: [],
requires_approving_reviews: true,
},
),
},
],
app_installation_diffs: [],
},
),
]
"#);
}

#[test]
fn repo_remove_branch_protection() {
let mut model = DataModel::default();
model.create_repo(
RepoData::new("repo1")
.team("team1", RepoPermission::Write)
.branch_protections(vec![
BranchProtectionBuilder::pr_required("main", &["test"], 1).build(),
BranchProtectionBuilder::pr_required("stable", &["test"], 0).build(),
]),
);

let gh = model.gh_model();
model.get_repo("repo1").branch_protections.pop().unwrap();

let diff = model.diff_repos(gh);
insta::assert_debug_snapshot!(diff, @r#"
[
Update(
UpdateRepoDiff {
org: "rust-lang",
name: "repo1",
repo_node_id: "0",
repo_id: 0,
settings_diff: (
RepoSettings {
description: Some(
"",
),
homepage: None,
archived: false,
auto_merge_enabled: false,
},
RepoSettings {
description: Some(
"",
),
homepage: None,
archived: false,
auto_merge_enabled: false,
},
),
permission_diffs: [],
branch_protection_diffs: [
BranchProtectionDiff {
pattern: "stable",
operation: Delete(
"1",
),
},
],
app_installation_diffs: [],
},
),
]
"#);
}
Loading

0 comments on commit fd604cb

Please sign in to comment.