Skip to content

Commit fa8acb7

Browse files
authored
Handle new GitHub branch_protection API
Fixes sigmavirus24#1112 Since ~Q1 2022, branch_protection has an additional "checks" field which deprecates "contexts": "contexts": list[str] "checks": list[TypedDict("checks", context=str, app_id=int | None)] The GitHub API returns both and then errors if both are present on update. So, when making unrelated changes, check whether both are present and if so remove the "contexts" field, on the assumption that "checks" contains all the same data plus possibly app_id fields that we should not overwrite with nil. Does this look OK? Happy to add polish, tests etc. if it's the right approach.
1 parent 5e72d01 commit fa8acb7

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/github3/repos/branch.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,10 @@ def update(
437437
else None
438438
),
439439
}
440+
if current_status["required_status_checks"] is not None and all(
441+
key in current_status["required_status_checks"] for key in ("contexts", "checks")):
442+
# Prefer the new "checks" field when updating
443+
del current_status["required_status_checks"]["contexts"]
440444
edit = {
441445
"enabled": True,
442446
"enforce_admins": (
@@ -1186,7 +1190,7 @@ def delete_contexts(self, contexts):
11861190
return self._boolean(resp, 204, 404)
11871191

11881192
@decorators.requires_auth
1189-
def update(self, strict=None, contexts=None):
1193+
def update(self, strict=None, contexts=None, checks=None):
11901194
"""Update required status checks for the branch.
11911195
11921196
This requires admin or owner permissions to the repository and
@@ -1201,6 +1205,10 @@ def update(self, strict=None, contexts=None):
12011205
Whether this should be strict protection or not.
12021206
:param list contexts:
12031207
A list of context names that should be required.
1208+
:param list checks:
1209+
A list of dicts describing status checks that should be required.
1210+
Keys are: "context": str, "app_id": int (optional).
1211+
Replaces "contexts".
12041212
:returns:
12051213
A new instance of this class with the updated information
12061214
:rtype:
@@ -1215,8 +1223,12 @@ def update(self, strict=None, contexts=None):
12151223
json = None
12161224
if strict is not None:
12171225
update_data["strict"] = strict
1218-
if contexts is not None:
1226+
if contexts is not None and checks is not None:
1227+
raise ValueError("cannot supply both 'contexts' and 'checks'")
1228+
elif contexts is not None:
12191229
update_data["contexts"] = contexts
1230+
elif checks is not None:
1231+
update_data["checks"] = checks
12201232
if update_data:
12211233
resp = self._patch(self.url, json=update_data)
12221234
json = self._json(resp, 200)

0 commit comments

Comments
 (0)