Skip to content

Commit 33090f5

Browse files
author
shiro
authored
fix(branch_protections.py): Working Status Check Reqs (#42)
Status Check requirements now properly apply to branch protection policy
1 parent 441b8e4 commit 33090f5

File tree

1 file changed

+18
-22
lines changed

1 file changed

+18
-22
lines changed

repo_manager/github/branch_protections.py

+18-22
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ def update_branch_protection(repo: Repository, branch: str, protection_config: P
2323
# Until pygithub supports this, we need to do it manually
2424
def edit_protection( # nosec
2525
branch,
26-
strict=NotSet,
27-
contexts=NotSet,
26+
required_status_checks=NotSet,
2827
enforce_admins=NotSet,
2928
dismissal_users=NotSet,
3029
dismissal_teams=NotSet,
@@ -40,9 +39,8 @@ def edit_protection( # nosec
4039
required_conversation_resolution=NotSet,
4140
): # nosec
4241
"""
43-
:calls: `PUT /repos/{owner}/{repo}/branches/{branch}/protection <https://docs.github.com/en/rest/reference/repos#get-branch-protection>`_
44-
:strict: bool
45-
:contexts: list of strings
42+
:calls: `PUT /repos/{owner}/{repo}/branches/{branch}/protection <https://docs.github.com/en/rest/branches/branch-protection?apiVersion=2022-11-28#update-branch-protection>`_
43+
:required_status_checks: dict
4644
:enforce_admins: bool
4745
:dismissal_users: list of strings
4846
:dismissal_teams: list of strings
@@ -55,8 +53,7 @@ def edit_protection( # nosec
5553
be submitted. Take care to pass both as arguments even if only one is
5654
changing. Use edit_required_status_checks() to avoid this.
5755
"""
58-
assert strict is NotSet or isinstance(strict, bool), strict
59-
assert contexts is NotSet or all(isinstance(element, str) for element in contexts), contexts
56+
assert required_status_checks is NotSet or isinstance(required_status_checks, dict), required_status_checks
6057
assert enforce_admins is NotSet or isinstance(enforce_admins, bool), enforce_admins
6158
assert dismissal_users is NotSet or all(
6259
isinstance(element, str) for element in dismissal_users
@@ -73,17 +70,13 @@ def edit_protection( # nosec
7370
), required_approving_review_count
7471

7572
post_parameters = {}
76-
if strict is not NotSet or contexts is not NotSet:
77-
if strict is NotSet:
78-
strict = False
79-
if contexts is NotSet:
80-
contexts = []
73+
if required_status_checks is not NotSet:
74+
post_parameters["required_status_checks"] = required_status_checks
75+
else:
8176
post_parameters["required_status_checks"] = {
82-
"strict": strict,
83-
"contexts": contexts,
77+
"strict": false,
78+
"contexts": [],
8479
}
85-
else:
86-
post_parameters["required_status_checks"] = None
8780

8881
if enforce_admins is not NotSet:
8982
post_parameters["enforce_admins"] = enforce_admins
@@ -116,6 +109,7 @@ def edit_protection( # nosec
116109
post_parameters["required_pull_request_reviews"]["dismissal_restrictions"]["teams"] = dismissal_teams
117110
else:
118111
post_parameters["required_pull_request_reviews"] = None
112+
119113
if user_push_restrictions is not NotSet or team_push_restrictions is not NotSet:
120114
if user_push_restrictions is NotSet:
121115
user_push_restrictions = []
@@ -208,6 +202,7 @@ def edit_protection( # nosec
208202
status_check_kwargs,
209203
transform_key="contexts",
210204
)
205+
extra_kwargs["required_status_checks"] = status_check_kwargs
211206

212207
# these are not handled by edit_protection, so we have to use the custom api
213208
attr_to_kwarg(
@@ -230,12 +225,13 @@ def edit_protection( # nosec
230225
edit_protection(branch=this_branch, **kwargs, **extra_kwargs)
231226
except GithubException as exc:
232227
raise ValueError(f"{exc.data['message']} {exc.data['documentation_url']}")
233-
234-
if status_check_kwargs != {}:
235-
try:
236-
this_branch.edit_required_status_checks(**status_check_kwargs)
237-
except GithubException as exc:
238-
raise ValueError(f"{exc.data['message']} {exc.data['documentation_url']}")
228+
# This errors out because the underlying method does a UPDATE instead of a POST as stated by GitHub documentation
229+
# was able to fix this issue by adding the additional key to kwargs above; signed commits could maybe be done too..
230+
# if status_check_kwargs != {}:
231+
# try:
232+
# this_branch.edit_required_status_checks(**status_check_kwargs)
233+
# except GithubException as exc:
234+
# raise ValueError(f"{exc.data['message']} {exc.data['documentation_url']}")
239235

240236
# signed commits has its own method
241237
if protection_config.require_signed_commits is not None:

0 commit comments

Comments
 (0)