Skip to content

Commit

Permalink
refactor: check API response globally (#511)
Browse files Browse the repository at this point in the history
  • Loading branch information
sileht authored Oct 29, 2024
1 parent 8cd68b6 commit f2a95ea
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions mergify_cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import argparse
import asyncio
import contextlib
import dataclasses
import importlib.metadata
import os
Expand Down Expand Up @@ -46,15 +45,15 @@
TMP_STACK_BRANCH = "mergify-cli-tmp"


def check_for_status(response: httpx.Response) -> None:
async def check_for_status(response: httpx.Response) -> None:
if response.status_code < 400:
return

if response.status_code < 500:
await response.aread()
data = response.json()
console.print(f"url: {response.request.url}", style="red")
with contextlib.suppress(httpx.RequestNotRead):
console.print(f"data: {response.request.content.decode()}", style="red")
console.print(f"data: {response.request.content.decode()}", style="red")
console.print(
f"HTTPError {response.status_code}: {data['message']}",
style="red",
Expand Down Expand Up @@ -154,7 +153,6 @@ async def get_change_id_and_pull_from_github(
branch = ref["ref"][len("refs/heads/") :]
changeid = ChangeId(branch[len(stack_prefix) + 1 :])
r = await client.get("pulls", params={"head": f"{user}:{branch}", "state": "all"})
check_for_status(r)
pulls = typing.cast(list[github_types.PullRequest], r.json())
opened_pulls = [pull for pull in pulls if pull["state"] == "open"]
merged_pulls = [pull for pull in pulls if pull["merged_at"] is not None]
Expand All @@ -172,7 +170,6 @@ async def get_change_id_and_pull_from_github(

if pull["body"] is None or "merged_at" not in pull:
r = await client.get(f"pulls/{pull['number']}")
check_for_status(r)
pull = typing.cast(github_types.PullRequest, r.json())
return changeid, pull

Expand Down Expand Up @@ -403,8 +400,6 @@ async def create_or_update_comments(
new_body = stack_comment.body(pull)

r = await client.get(f"issues/{pull['number']}/comments")
check_for_status(r)

comments = typing.cast(list[github_types.Comment], r.json())
for comment in comments:
if StackComment.is_stack_comment(comment):
Expand Down Expand Up @@ -498,7 +493,6 @@ async def create_or_update_stack( # noqa: PLR0913,PLR0917
)

r = await client.patch(f"pulls/{change.pull['number']}", json=pull_changes)
check_for_status(r)
return change.pull

elif change.action == "create":
Expand All @@ -515,7 +509,6 @@ async def create_or_update_stack( # noqa: PLR0913,PLR0917
"base": change.base_branch,
},
)
check_for_status(r)
return typing.cast(github_types.PullRequest, r.json())

msg = f"Unhandled action: {change.action}"
Expand All @@ -527,8 +520,7 @@ async def delete_stack(
stack_prefix: str,
change: OrphanChange,
) -> None:
r = await client.delete(f"git/refs/heads/{stack_prefix}/{change.id}")
check_for_status(r)
await client.delete(f"git/refs/heads/{stack_prefix}/{change.id}")
console.log(get_log_from_orphan_change(change, dry_run=False))


Expand Down Expand Up @@ -627,7 +619,6 @@ async def get_remote_changes(
) -> RemoteChanges:
known_changeids = RemoteChanges({})
r = await client.get(f"git/matching-refs/heads/{stack_prefix}/")
check_for_status(r)
refs = typing.cast(list[github_types.GitRef], r.json())

tasks = [
Expand All @@ -647,7 +638,7 @@ async def get_remote_changes(

# TODO(charly): fix code to conform to linter (number of arguments, local
# variables, statements, positional arguments, branches)
async def stack_push( # noqa: PLR0913, PLR0915, PLR0917, PLR0912
async def stack_push( # noqa: PLR0913, PLR0915, PLR0917
github_server: str,
token: str,
skip_rebase: bool,
Expand Down Expand Up @@ -699,10 +690,13 @@ async def stack_push( # noqa: PLR0913, PLR0915, PLR0917, PLR0912
)
sys.exit(1)

event_hooks: typing.Mapping[str, list[typing.Callable[..., typing.Any]]] = {
"request": [],
"response": [check_for_status],
}
if DEBUG:
event_hooks = {"request": [log_httpx_request], "response": [log_httpx_response]}
else:
event_hooks = {}
event_hooks["request"].insert(0, log_httpx_request)
event_hooks["response"].insert(0, log_httpx_response)

async with httpx.AsyncClient(
base_url=f"{github_server}/repos/{user}/{repo}/",
Expand All @@ -711,7 +705,7 @@ async def stack_push( # noqa: PLR0913, PLR0915, PLR0917, PLR0912
"User-Agent": f"mergify_cli/{VERSION}",
"Authorization": f"token {token}",
},
event_hooks=event_hooks, # type: ignore[arg-type]
event_hooks=event_hooks,
follow_redirects=True,
timeout=5.0,
) as client:
Expand Down

0 comments on commit f2a95ea

Please sign in to comment.