From 51e1c1fd2cbedcb0620f617ffb4b299794ca6bc3 Mon Sep 17 00:00:00 2001 From: Christopher Dignam Date: Fri, 26 Nov 2021 14:25:19 -0500 Subject: [PATCH 1/3] ref(bot): remove dead get_permissions_for_username code --- bot/kodiak/queries/__init__.py | 25 +------------------- bot/kodiak/test_queries.py | 43 ---------------------------------- 2 files changed, 1 insertion(+), 67 deletions(-) diff --git a/bot/kodiak/queries/__init__.py b/bot/kodiak/queries/__init__.py index 8a3020f94..9066a0218 100644 --- a/bot/kodiak/queries/__init__.py +++ b/bot/kodiak/queries/__init__.py @@ -922,30 +922,7 @@ async def get_api_features(self) -> ApiFeatures | None: ) return _api_features_cache - async def get_permissions_for_username(self, username: str) -> Permission: - headers = await get_headers( - session=self.session, installation_id=self.installation_id - ) - async with self.throttler: - res = await self.session.get( - conf.v3_url( - f"/repos/{self.owner}/{self.repo}/collaborators/{username}/permission" - ), - headers=headers, - ) - log = self.log.bind(res=res, username=username) - try: - res.raise_for_status() - except http.HTTPError: - log.warning("get_permissions request_failure", exc_info=True) - return Permission.NONE - - try: - return Permission(res.json()["permission"]) - except (IndexError, TypeError, ValueError): - log.exception("get_permissions parse error") - return Permission.NONE - + async def get_bot_reviews(self, *, reviews: List[PRReviewSchema]) -> List[PRReview]: bot_reviews: List[PRReview] = [] for review in reviews: diff --git a/bot/kodiak/test_queries.py b/bot/kodiak/test_queries.py index 465c29bed..6b3bafed3 100644 --- a/bot/kodiak/test_queries.py +++ b/bot/kodiak/test_queries.py @@ -265,18 +265,6 @@ async def test_get_event_info_blocked( ) ), ) - - async def get_permissions_for_username_patch(username: str) -> Permission: - if username in ("walrus", "ghost"): - return Permission.WRITE - if username in ("kodiak",): - return Permission.ADMIN - raise Exception - - mocker.patch.object( - api_client, "get_permissions_for_username", get_permissions_for_username_patch - ) - res = await api_client.get_event_info(pr_number=100) assert res is not None assert res == block_event @@ -295,19 +283,6 @@ def mock_get_token_for_install(mocker: MockFixture) -> None: ) -@pytest.mark.asyncio -async def test_get_permissions_for_username_missing( - api_client: Client, mocker: MockFixture, mock_get_token_for_install: None -) -> None: - not_found = Response(status_code=404, request=Request(method="", url="")) - mocker.patch( - "kodiak.queries.http.AsyncClient.get", return_value=wrap_future(not_found) - ) - async with api_client as api_client: - res = await api_client.get_permissions_for_username("_invalid_username") - assert res == Permission.NONE - - PERMISSION_OK_READ_USER_RESPONSE = json.dumps( { "permission": "read", @@ -335,24 +310,6 @@ async def test_get_permissions_for_username_missing( ).encode() -@pytest.mark.asyncio -async def test_get_permissions_for_username_read( - api_client: Client, mocker: MockFixture, mock_get_token_for_install: None -) -> None: - response = Response( - status_code=200, - content=PERMISSION_OK_READ_USER_RESPONSE, - request=Request(method="", url=""), - ) - - mocker.patch( - "kodiak.queries.http.AsyncClient.get", return_value=wrap_future(response) - ) - async with api_client as api_client: - res = await api_client.get_permissions_for_username("ghost") - assert res == Permission.READ - - def create_fake_redis_reply(res: Dict[bytes, bytes]) -> Any: class FakeDictReply: @staticmethod From b7bb8090a54e4d97201ea85cc7f6d6d7775ef413 Mon Sep 17 00:00:00 2001 From: Christopher Dignam Date: Fri, 26 Nov 2021 14:35:39 -0500 Subject: [PATCH 2/3] ref(bot): cut dead Permission enum --- bot/kodiak/queries/__init__.py | 18 ++++-------------- bot/kodiak/test_evaluation.py | 3 +-- bot/kodiak/test_queries.py | 3 +-- 3 files changed, 6 insertions(+), 18 deletions(-) diff --git a/bot/kodiak/queries/__init__.py b/bot/kodiak/queries/__init__.py index 9066a0218..2045862ab 100644 --- a/bot/kodiak/queries/__init__.py +++ b/bot/kodiak/queries/__init__.py @@ -505,7 +505,6 @@ class PRReviewAuthorSchema(BaseModel): @dataclass class PRReviewAuthor: login: str - permission: Permission class PRReviewSchema(BaseModel): @@ -555,15 +554,6 @@ class CheckRun(BaseModel): conclusion: Optional[CheckConclusionState] -class Permission(Enum): - """ - https://developer.github.com/v3/repos/collaborators/#review-a-users-permission-level - """ - - ADMIN = "admin" - WRITE = "write" - READ = "read" - NONE = "none" class TokenResponse(BaseModel): @@ -923,7 +913,7 @@ async def get_api_features(self) -> ApiFeatures | None: return _api_features_cache - async def get_bot_reviews(self, *, reviews: List[PRReviewSchema]) -> List[PRReview]: + def get_bot_reviews(self, *, reviews: List[PRReviewSchema]) -> List[PRReview]: bot_reviews: List[PRReview] = [] for review in reviews: if not review.author: @@ -937,7 +927,7 @@ async def get_bot_reviews(self, *, reviews: List[PRReviewSchema]) -> List[PRRevi state=review.state, createdAt=review.createdAt, author=PRReviewAuthor( - login=review.author.login, permission=Permission.WRITE + login=review.author.login ), ) ) @@ -1069,8 +1059,8 @@ async def get_event_info(self, pr_number: int) -> Optional[EventInfoResponse]: repo=repository, ref_name=pr.baseRefName ) - partial_reviews = get_reviews(pr=pull_request) - bot_reviews = await self.get_bot_reviews(reviews=partial_reviews) + all_reviews = get_reviews(pr=pull_request) + bot_reviews = self.get_bot_reviews(reviews=all_reviews) return EventInfoResponse( config=cfg.parsed, config_str=cfg.text, diff --git a/bot/kodiak/test_evaluation.py b/bot/kodiak/test_evaluation.py index b6b532b6a..30df987d3 100644 --- a/bot/kodiak/test_evaluation.py +++ b/bot/kodiak/test_evaluation.py @@ -21,7 +21,6 @@ MergeableState, MergeStateStatus, NodeListPushAllowance, - Permission, PRReview, PRReviewAuthor, PRReviewRequest, @@ -271,7 +270,7 @@ def create_review() -> PRReview: return PRReview( state=PRReviewState.APPROVED, createdAt=datetime(2015, 5, 25), - author=PRReviewAuthor(login="ghost", permission=Permission.WRITE), + author=PRReviewAuthor(login="ghost"), ) diff --git a/bot/kodiak/test_queries.py b/bot/kodiak/test_queries.py index 6b3bafed3..a546ac417 100644 --- a/bot/kodiak/test_queries.py +++ b/bot/kodiak/test_queries.py @@ -21,7 +21,6 @@ MergeableState, MergeStateStatus, NodeListPushAllowance, - Permission, PRReview, PRReviewAuthor, PRReviewRequest, @@ -202,7 +201,7 @@ def block_event() -> EventInfoResponse: PRReview( createdAt=datetime.fromisoformat("2019-05-24T10:21:32+00:00"), state=PRReviewState.APPROVED, - author=PRReviewAuthor(login="kodiakhq", permission=Permission.WRITE), + author=PRReviewAuthor(login="kodiakhq"), ), ], status_contexts=[ From 218484ada3f369663f30ed5591089d4bebb38727 Mon Sep 17 00:00:00 2001 From: Christopher Dignam Date: Fri, 26 Nov 2021 14:36:06 -0500 Subject: [PATCH 3/3] fmt --- bot/kodiak/queries/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/bot/kodiak/queries/__init__.py b/bot/kodiak/queries/__init__.py index 9066a0218..b1b8d6ade 100644 --- a/bot/kodiak/queries/__init__.py +++ b/bot/kodiak/queries/__init__.py @@ -922,7 +922,6 @@ async def get_api_features(self) -> ApiFeatures | None: ) return _api_features_cache - async def get_bot_reviews(self, *, reviews: List[PRReviewSchema]) -> List[PRReview]: bot_reviews: List[PRReview] = [] for review in reviews: