From 01767bc680a346d12b1a92479fe52ea22ad6f696 Mon Sep 17 00:00:00 2001 From: Dan Rollo Date: Tue, 22 Oct 2024 08:30:35 -0400 Subject: [PATCH] add test case for no branch protection (#6) * add test case for no branch protection --- github_standards/standards.py | 2 +- github_standards/test/test_standards.py | 30 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/github_standards/standards.py b/github_standards/standards.py index baf9b5f..96c3a8b 100644 --- a/github_standards/standards.py +++ b/github_standards/standards.py @@ -58,7 +58,7 @@ def check_and_apply_standard_properties_to_branch(repo, branch: Branch, do_actua branch_protection: Optional[BranchProtection] = None try: branch_protection = branch.get_protection() - except GithubException as e: + except GithubException: # GH returns a 404 when Branch Protection is not yet enabled for the branch in question print(f' Branch {branch} is not protected in {repo.name}') diff --git a/github_standards/test/test_standards.py b/github_standards/test/test_standards.py index 743af89..4b4b22f 100644 --- a/github_standards/test/test_standards.py +++ b/github_standards/test/test_standards.py @@ -19,6 +19,7 @@ from unittest.mock import MagicMock +from github import GithubException from github.Branch import Branch from github.BranchProtection import BranchProtection from github.Repository import Repository @@ -163,6 +164,35 @@ def test_props_in_spec_branch_makes_no_change(self): self.assertEqual(result, "") + def test_props_out_of_spec_branch_makes_a_change_no_branch_protection(self): + repo = self.create_mock_repo() + + requester = self.create_mock_requester() + # noinspection PyTypeChecker + branch = Branch(requester='', headers='', attributes={}, completed='') + branch.get_protection = MagicMock() + ghe = GithubException(status=404, data=None) + branch.get_protection.side_effect = ghe + + branch.edit_protection = MagicMock() + + branch.get_required_pull_request_reviews = MagicMock() + # noinspection PyTypeChecker + rprr = RequiredPullRequestReviews(requester=requester, headers='', attributes={"url": MOCK_REPO_URL, + 'require_code_owner_reviews': True, + 'required_approving_review_count': 1}, + completed='') + branch.get_required_pull_request_reviews.return_value = rprr + + branch.get_required_signatures = MagicMock() + branch.get_required_signatures.return_value = True + + result = standards.check_and_apply_standard_properties_to_branch(repo, branch, True) + + self.assertEqual(result, "") + branch.edit_protection.assert_called_once_with(allow_deletions=False, + allow_force_pushes=False) + def test_props_out_of_spec_branch_makes_a_change(self): repo = self.create_mock_repo()