Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unable to update branch_protections for github repos #1112

Open
ecatmur opened this issue Oct 6, 2022 · 1 comment
Open

unable to update branch_protections for github repos #1112

ecatmur opened this issue Oct 6, 2022 · 1 comment

Comments

@ecatmur
Copy link

ecatmur commented Oct 6, 2022

integrations/terraform-provider-github#1147
google/go-github#2274

Apparently github changed API to deprecate "contexts" and replace it with "checks".

image

However they return both in query and error on update if both are present (nb. anonymized):

Traceback (most recent call last):
  File "main.py", line 64, in <module>
    gh_branch.protection().update(enforce_admins=False)
  File "env/lib/python3.10/site-packages/github3/decorators.py", line 24, in auth_wrapper
    return func(self, *args, **kwargs)
  File "env/lib/python3.10/site-packages/github3/repos/branch.py", line 465, in update
    json = self._json(self._put(self._api, json=edit), 200)
  File "env/lib/python3.10/site-packages/github3/models.py", line 161, in _json
    raise exceptions.error_for(response)
github3.exceptions.UnprocessableEntity: 422 Invalid request.

No subschema in "anyOf" matched.
More than one subschema in "oneOf" matched.
Not all subschemas of "allOf" matched.
For 'anyOf/1', {"url"=>"https://.../branches/master/protection/required_status_checks", "strict"=>true, "contexts"=>["my ci check"], "contexts_url"=>"https://.../branches/master/protection/required_status_checks/contexts", "checks"=>[{"context"=>"my ci check", "app_id"=>nil}]} is not a null.

Our workaround is when making updates to other parameters, to pass in the retrieved required_status_checks with the extra "checks" field stripped, e.g. (yes we should do this with as_dict(), I was in a rush):

    - gh_branch.protection().update(enforce_admins=False)
    + checks = gh_branch.protection().required_status_checks.contexts()
    + gh_branch.protection().update(enforce_admins=False, required_status_checks={"strict": True, "contexts": checks})
@sigmavirus24
Copy link
Owner

Would be happy to review a pull request fixing this. Would be happier if the backwards compatibility of GitHub's API wasn't a blatant lie

ecatmur added a commit to ecatmur/github3.py that referenced this issue Oct 7, 2022
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants