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

Add branch protection support for required_status_checks.checks object #2884

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

treee111
Copy link
Contributor

@treee111 treee111 commented Jan 26, 2024

as outlined in #2873 (comment):

Looking at the docs I found that argument required_status_checks.contexts is deprecated in the API and replaced by required_status_checks.checks.

I've done:

  • adds checks as another argument
  • checks should be of type List[Union[str, Tuple[str, int]]] so that this allows for checks=["check1", ("check2", -1), ("check3", 123456)]
  • if only context is given, it's values should be used to populate required_status_checks.checks.context, rather than required_status_checks.contexts
  • if both checks and context are given, context should be ignored

one thing I am unsure about is how to check for checks, this commented-out section is not working...
https://github.com/treee111/PyGithub/blob/cce1d1de9795a07e2134ae1e780d6b922bd59d96/github/Branch.py#L349

I created the replay data manually because the command to create them automatically did not work for me.

closes #2157

@treee111
Copy link
Contributor Author

Hi @EnricoMi could you please have a look at the coding if this is the right direction and what needs to be adjusted. Thanks!

@codecov-commenter
Copy link

codecov-commenter commented Jan 26, 2024

Codecov Report

Attention: 9 lines in your changes are missing coverage. Please review.

Comparison is base (7e7653f) 96.70% compared to head (8e9f422) 96.64%.

❗ Current head 8e9f422 differs from pull request most recent head f9f04cb. Consider uploading reports for the commit f9f04cb to get more accurate results

Files Patch % Lines
github/Branch.py 64.00% 9 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2884      +/-   ##
==========================================
- Coverage   96.70%   96.64%   -0.07%     
==========================================
  Files         147      147              
  Lines       14885    14887       +2     
==========================================
- Hits        14395    14388       -7     
- Misses        490      499       +9     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

github/Branch.py Outdated
) -> RequiredStatusChecks:
"""
:calls: `PATCH /repos/{owner}/{repo}/branches/{branch}/protection/required_status_checks <https://docs.github.com/en/rest/reference/repos#branches>`_
"""
assert is_optional(strict, bool), strict
assert is_optional_list(contexts, str), contexts
# assert is_optional_list(checks, Union[str, Tuple[str, int]]), checks
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# assert is_optional_list(checks, Union[str, Tuple[str, int]]), checks
assert is_optional_list(checks, (str, tuple)), checks
if checks is not None:
assert all(not isinstance(check, tuple) or list(map(type, check)) == [str, int] for check in checks), checks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, have done the if checks is not None: in another way because that way it didn't worked for None. Is this OK?

github/Branch.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@EnricoMi EnricoMi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally right direction, just minor style comments.

Comment on lines +189 to +199
elif is_defined(checks):
post_parameters["checks"] = []
for check in checks:
if isinstance(check, tuple):
context, app_id = check
post_parameters["checks"].append({"context": context, "app_id": app_id})
else:
post_parameters["checks"].append({"context": check})
elif is_defined(contexts):
post_parameters["checks"] = [{"context": context} for context in contexts]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you want to populate list checks here, as used below to set post_parameters["required_status_checks"].

{"checks": [{"context": "check1"}, {"context": "check2", "app_id": -1}, {"context": "check3", "app_id": 123456}]}
200
[('status', '200 OK'), ('x-ratelimit-remaining', '4994'), ('content-length', '0'), ('server', 'nginx/1.0.13'), ('connection', 'keep-alive'), ('x-ratelimit-limit', '5000'), ('etag', '"81ba94a48ad867b26d48c023ac584f43"'), ('date', 'Mon, 07 May 2018 13:42:41 GMT'), ('content-type', 'application/json; charset=utf-8')]
''
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like this is not correct

Copy link
Contributor Author

@treee111 treee111 Jan 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how to create the file automatically? This does not work for me in macOS
pytest -k Branch.testEditRequiredStatusChecksChecks --record --auth_with_token

(pygithub-dev) <user>@<computer> PyGithub % pytest -k Branch.testEditRequiredStatusChecksChecks --record --auth_with_token
=============================================================================================== test session starts ===============================================================================================
platform darwin -- Python 3.10.13, pytest-7.4.4, pluggy-1.3.0
rootdir: /Users/<user>/VSCode/python/PyGithub
configfile: pyproject.toml
plugins: github-actions-annotate-failures-0.2.0, subtests-0.11.0, cov-4.1.0
collected 890 items / 890 deselected / 0 selected                                                                                                                                                                 

============================================================================================= 890 deselected in 0.32s =============================================================================================


post_parameters: dict[str, Any] = NotSet.remove_unset_items({"strict": strict, "contexts": contexts})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please keep the NotSet.remove_unset_items pattern where it exists, preferred over is_defined + post_parameters = ... above.

Copy link
Contributor Author

@treee111 treee111 Jan 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tried to comply with the pattern but didn't found a example in PyGithub which uses kind of calculation of the parameters like I do here and sets post_parameters afterwards.
I dont think 8776ae5 makes it easier to read, maybe you could make it clearer what I have to adjust based on that.

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

Successfully merging this pull request may close these issues.

Add branch protection support for required_status_checks checks object
3 participants