-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add branch protection call to client #28
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
871d7fd
add branch protection call to client
jbristow 8bd4269
chore: update deps and lint
jbristow 23db2f8
chore: update to next version
jbristow b03479f
chore: add tests
jbristow 8b11b58
chore: move freezegun to dev deps
jbristow b523349
chore: add to codeowners
jbristow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -617,3 +617,38 @@ async def fetch_teams_for_repo(self, *, owner_login: str, repo_name: str): | |
| yield team | ||
| except httpx.HTTPError as e: | ||
| _fetch_problem(f"teams for repo {owner_login}/{repo_name}", e) | ||
|
|
||
| async def fetch_branch_protection( | ||
| self, | ||
| *, | ||
| owner_login: str, | ||
| repo_name: str, | ||
| branch: str, | ||
| ) -> types.BranchProtection | None: | ||
| """Fetches the branch protection for a given branch. | ||
|
|
||
| https://docs.github.com/en/[email protected]/rest/branches/branch-protection?apiVersion=2022-11-28#get-branch-protection | ||
| """ | ||
|
|
||
| try: | ||
| return await self._get_item( | ||
| f"repos/{owner_login}/{repo_name}/branches/{branch}/protection" | ||
| ) | ||
| except httpx.HTTPError as e: | ||
| match e: | ||
| case httpx.HTTPStatusError(response=response) if ( | ||
| response.status_code == 404 | ||
| ): | ||
| logger.info( | ||
| "Branch protection not found for branch %s on repo %s/%s", | ||
| branch, | ||
| owner_login, | ||
| repo_name, | ||
| ) | ||
| case _: | ||
| _fetch_problem( | ||
| f"branch protection for branch {branch} on " | ||
| f"repo {owner_login}/{repo_name}", | ||
| e, | ||
| ) | ||
| return None | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| [tool.poetry] | ||
| name = "nodestream-plugin-github" | ||
| version = "0.14.3" | ||
| version = "0.14.4" | ||
| description = "" | ||
| authors = [ | ||
| "Jon Bristow <[email protected]>", | ||
|
|
@@ -20,14 +20,15 @@ httpx = ">=0.27,<0.28" | |
| freezegun = "^1.5.5" | ||
|
|
||
| [tool.poetry.group.dev.dependencies] | ||
| ruff = "^0.13.0" | ||
| black = "^25.1.0" | ||
| isort = "^6.0.0" | ||
| ruff = "^0.14.2" | ||
| black = "^25.9.0" | ||
| isort = "^7.0.0" | ||
| pytest = "^8.4.1" | ||
| pytest-asyncio = "^1.1.0" | ||
| pytest-asyncio = "^1.2.0" | ||
| pytest-httpx = "^0.34.0" | ||
| pytest-cov = "^7.0.0" | ||
| pytest-github-actions-annotate-failures = "^0.3.0" | ||
| pytest-mock = "^3.15.1" | ||
|
|
||
| [build-system] | ||
| requires = ["poetry-core"] | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| # noinspection PyProtectedMember | ||
| from typing import Any, Optional | ||
| from typing import Any | ||
|
|
||
| from pytest_httpx import HTTPXMock | ||
|
|
||
|
|
@@ -192,7 +192,7 @@ def get_repos_for_user( | |
| **kwargs, | ||
| ) | ||
|
|
||
| def get_enterprise_audit_logs(self, *, search_phrase: Optional[str], **kwargs: Any): | ||
| def get_enterprise_audit_logs(self, *, search_phrase: str | None, **kwargs: Any): | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. new ruff did this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Much better |
||
| url = f"{self.base_url}/enterprises/test-enterprise/audit-log" | ||
| url += f"?per_page={self.per_page}" | ||
| if search_phrase: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| import httpx | ||
| import pytest | ||
| from pytest_httpx import HTTPXMock | ||
| from pytest_mock import MockerFixture | ||
|
|
||
| from nodestream_github.client import GithubRestApiClient | ||
| from nodestream_github.client import githubclient as githubclient | ||
| from tests.mocks.githubrest import DEFAULT_BASE_URL, DEFAULT_HOSTNAME | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_fetch_branch_protection(httpx_mock: HTTPXMock): | ||
| client = GithubRestApiClient( | ||
| auth_token="test-auth-token", | ||
| github_hostname=DEFAULT_HOSTNAME, | ||
| user_agent="test-user-agent", | ||
| ) | ||
|
|
||
| httpx_mock.add_response( | ||
| url=f"{DEFAULT_BASE_URL}/repos/octocat/Hello-World/branches/main/protection", | ||
| json={"enabled": True}, | ||
| ) | ||
|
|
||
| result = await client.fetch_branch_protection( | ||
| owner_login="octocat", | ||
| repo_name="Hello-World", | ||
| branch="main", | ||
| ) | ||
|
|
||
| assert result == {"enabled": True} | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_fetch_branch_protection_404( | ||
| httpx_mock: HTTPXMock, mocker: MockerFixture | ||
| ): | ||
| client = GithubRestApiClient( | ||
| auth_token="test-auth-token", | ||
| github_hostname=DEFAULT_HOSTNAME, | ||
| user_agent="test-user-agent", | ||
| ) | ||
|
|
||
| httpx_mock.add_response( | ||
| url=f"{DEFAULT_BASE_URL}/repos/octocat/Hello-World/branches/main/protection", | ||
| status_code=httpx.codes.NOT_FOUND, | ||
| json={ | ||
| "documentation_url": "https://docs.github.com/[email protected]/rest", | ||
| "message": "Not Found", | ||
| }, | ||
| ) | ||
| log_info = mocker.spy(githubclient.logger, "info") | ||
|
|
||
| result = await client.fetch_branch_protection( | ||
| owner_login="octocat", | ||
| repo_name="Hello-World", | ||
| branch="main", | ||
| ) | ||
|
|
||
| assert result is None | ||
| log_info.assert_called_once_with( | ||
| "Branch protection not found for branch %s on repo %s/%s", | ||
| "main", | ||
| "octocat", | ||
| "Hello-World", | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_fetch_branch_protection_503( | ||
| httpx_mock: HTTPXMock, mocker: MockerFixture | ||
| ): | ||
| client = GithubRestApiClient( | ||
| auth_token="test-auth-token", | ||
| github_hostname=DEFAULT_HOSTNAME, | ||
| user_agent="test-user-agent", | ||
| ) | ||
|
|
||
| httpx_mock.add_response( | ||
| url=f"{DEFAULT_BASE_URL}/repos/octocat/Hello-World/branches/main/protection", | ||
| status_code=httpx.codes.SERVICE_UNAVAILABLE, | ||
| ) | ||
| log_warning = mocker.spy(githubclient.logger, "warning") | ||
|
|
||
| result = await client.fetch_branch_protection( | ||
| owner_login="octocat", | ||
| repo_name="Hello-World", | ||
| branch="main", | ||
| ) | ||
|
|
||
| assert result is None | ||
| log_warning.assert_called_once_with( | ||
| "%s %s - %s%s", | ||
| 503, | ||
| "Service Unavailable", | ||
| "/api/v3/repos/octocat/Hello-World/branches/main/protection", | ||
| "", | ||
| stacklevel=2, | ||
| ) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't
WARNon 404, since that's normal if branch protection is not enabled.