sigmavirus24 / github3.py

Hi, I'm a library for interacting with GItHub's REST API in a convenient and ergonomic way. I work on Python 3.6+.
https://github3.readthedocs.io
BSD 3-Clause "New" or "Revised" License
1.21k stars 404 forks source link

Setting protection is broken with existing "restrict who can push" setup #1131

Open ccotter opened 1 year ago

ccotter commented 1 year ago

With v3.2.0, when a branch protection is setup on a branch with "Restrict who can push to matching branches" enabled with one or more users or team, and no other protections are enabled, then when attempting to add more protections, the github3 fails with

protection.update(required_pull_request_reviews={ ... })
  File "/home/ccotter/git/policy_bot/src/policy_bot.py", line 225, in validate_branch
    protection.update(required_pull_request_reviews={}, restrictions=restrictions)
  File "/home/ccotter/git/policy_bot/venv/lib/python3.10/site-packages/github3/decorators.py", line 24, in auth_wrapper
    return func(self, *args, **kwargs)
  File "/home/ccotter/git/policy_bot/venv/lib/python3.10/site-packages/github3/repos/branch.py", line 469, in update
    json = self._json(self._put(self._api, json=edit), 200)
  File "/home/ccotter/git/policy_bot/venv/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.

(Note the line number from the github3 code are a bit off as I added some prints)

While github3 is creating the PUT HTTP request, it ends up inserting the the entire restrictions value from the original request that retrieved the restrictions, and the PUT request ends up containing an invalid payload. To fix, I think https://github.com/sigmavirus24/github3.py/blob/3.2.0/src/github3/repos/branch.py#L460 needs to extract out only the login name of each user, and the team name of each time. If I get a chance, I can submit a fix for this.