@@ -23,8 +23,7 @@ def update_branch_protection(repo: Repository, branch: str, protection_config: P
23
23
# Until pygithub supports this, we need to do it manually
24
24
def edit_protection ( # nosec
25
25
branch ,
26
- strict = NotSet ,
27
- contexts = NotSet ,
26
+ required_status_checks = NotSet ,
28
27
enforce_admins = NotSet ,
29
28
dismissal_users = NotSet ,
30
29
dismissal_teams = NotSet ,
@@ -40,9 +39,8 @@ def edit_protection( # nosec
40
39
required_conversation_resolution = NotSet ,
41
40
): # nosec
42
41
"""
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
46
44
:enforce_admins: bool
47
45
:dismissal_users: list of strings
48
46
:dismissal_teams: list of strings
@@ -55,8 +53,7 @@ def edit_protection( # nosec
55
53
be submitted. Take care to pass both as arguments even if only one is
56
54
changing. Use edit_required_status_checks() to avoid this.
57
55
"""
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
60
57
assert enforce_admins is NotSet or isinstance (enforce_admins , bool ), enforce_admins
61
58
assert dismissal_users is NotSet or all (
62
59
isinstance (element , str ) for element in dismissal_users
@@ -73,17 +70,13 @@ def edit_protection( # nosec
73
70
), required_approving_review_count
74
71
75
72
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 :
81
76
post_parameters ["required_status_checks" ] = {
82
- "strict" : strict ,
83
- "contexts" : contexts ,
77
+ "strict" : false ,
78
+ "contexts" : [] ,
84
79
}
85
- else :
86
- post_parameters ["required_status_checks" ] = None
87
80
88
81
if enforce_admins is not NotSet :
89
82
post_parameters ["enforce_admins" ] = enforce_admins
@@ -116,6 +109,7 @@ def edit_protection( # nosec
116
109
post_parameters ["required_pull_request_reviews" ]["dismissal_restrictions" ]["teams" ] = dismissal_teams
117
110
else :
118
111
post_parameters ["required_pull_request_reviews" ] = None
112
+
119
113
if user_push_restrictions is not NotSet or team_push_restrictions is not NotSet :
120
114
if user_push_restrictions is NotSet :
121
115
user_push_restrictions = []
@@ -208,6 +202,7 @@ def edit_protection( # nosec
208
202
status_check_kwargs ,
209
203
transform_key = "contexts" ,
210
204
)
205
+ extra_kwargs ["required_status_checks" ] = status_check_kwargs
211
206
212
207
# these are not handled by edit_protection, so we have to use the custom api
213
208
attr_to_kwarg (
@@ -230,12 +225,13 @@ def edit_protection( # nosec
230
225
edit_protection (branch = this_branch , ** kwargs , ** extra_kwargs )
231
226
except GithubException as exc :
232
227
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']}")
239
235
240
236
# signed commits has its own method
241
237
if protection_config .require_signed_commits is not None :
0 commit comments