Skip to content

Commit

Permalink
Acknowledge network issues on GitHub (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
ambv authored Jan 16, 2025
1 parent 962ba5c commit a0d1a14
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 17 deletions.
56 changes: 40 additions & 16 deletions cherry_picker/cherry_picker.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import click
import requests
import stamina
from gidgethub import sansio

from . import __version__
Expand Down Expand Up @@ -55,6 +56,7 @@
PUSHING_TO_REMOTE_FAILED
PR_CREATING
PR_CREATING_FAILED
PR_OPENING
REMOVING_BACKPORT_BRANCH
Expand Down Expand Up @@ -95,6 +97,10 @@ class InvalidRepoException(Exception):
pass


class GitHubException(Exception):
pass


class CherryPicker:
ALLOWED_STATES = WORKFLOW_STATES.BACKPORT_PAUSED, WORKFLOW_STATES.UNSET
"""The list of states expected at the start of the app."""
Expand Down Expand Up @@ -430,16 +436,21 @@ def push_to_remote(self, base_branch, head_branch, commit_message=""):
gh_auth = os.getenv("GH_AUTH")
if gh_auth:
set_state(WORKFLOW_STATES.PR_CREATING)
self.create_gh_pr(
base_branch,
head_branch,
commit_message=commit_message,
gh_auth=gh_auth,
)
try:
self.create_gh_pr(
base_branch,
head_branch,
commit_message=commit_message,
gh_auth=gh_auth,
)
except GitHubException:
set_state(WORKFLOW_STATES.PR_CREATING_FAILED)
raise
else:
set_state(WORKFLOW_STATES.PR_OPENING)
self.open_pr(self.get_pr_url(base_branch, head_branch))

@stamina.retry(on=GitHubException, timeout=120)
def create_gh_pr(self, base_branch, head_branch, *, commit_message, gh_auth):
"""
Create PR in GitHub
Expand All @@ -458,14 +469,22 @@ def create_gh_pr(self, base_branch, head_branch, *, commit_message, gh_auth):
"draft": self.config["draft_pr"],
}
url = CREATE_PR_URL_TEMPLATE.format(config=self.config)
response = requests.post(url, headers=request_headers, json=data, timeout=10)
if response.status_code == requests.codes.created:
response_data = response.json()
click.echo(f"Backport PR created at {response_data['html_url']}")
self.pr_number = response_data["number"]
try:
response = requests.post(
url, headers=request_headers, json=data, timeout=30
)
except requests.exceptions.RequestException as req_exc:
raise GitHubException(f"Creating PR on GitHub failed: {req_exc}")
else:
click.echo(response.status_code)
click.echo(response.text)
sc = response.status_code
txt = response.text
if sc != requests.codes.created:
raise GitHubException(
f"Unexpected response ({sc}) when creating PR on GitHub: {txt}"
)
response_data = response.json()
click.echo(f"Backport PR created at {response_data['html_url']}")
self.pr_number = response_data["number"]

def open_pr(self, url):
"""
Expand Down Expand Up @@ -543,9 +562,14 @@ def backport(self):
raise
else:
if self.push:
self.push_to_remote(
maint_branch, cherry_pick_branch, commit_message
)
try:
self.push_to_remote(
maint_branch, cherry_pick_branch, commit_message
)
except GitHubException:
click.echo(self.get_exit_message(maint_branch))
self.set_paused_state()
raise
if not self.is_mirror():
self.cleanup_branch(cherry_pick_branch)
else:
Expand Down
2 changes: 1 addition & 1 deletion cherry_picker/test_cherry_picker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1386,5 +1386,5 @@ def test_create_gh_pr_draft_states(
"maintainer_can_modify": True,
"draft": draft_pr,
},
timeout=10,
timeout=30,
)
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ dependencies = [
"click>=6",
"gidgethub",
"requests",
"stamina",
"tomli>=1.1; python_version<'3.11'",
]
optional-dependencies.dev = [
Expand Down

0 comments on commit a0d1a14

Please sign in to comment.