From 5f00a1516912ce8992b39cff8c32db6f29c4c6d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Langa?= Date: Wed, 15 Jan 2025 14:02:08 +0100 Subject: [PATCH 1/2] Acknowledge network issues on GitHub --- cherry_picker/cherry_picker.py | 56 ++++++++++++++++++++--------- cherry_picker/test_cherry_picker.py | 2 +- pyproject.toml | 1 + 3 files changed, 42 insertions(+), 17 deletions(-) diff --git a/cherry_picker/cherry_picker.py b/cherry_picker/cherry_picker.py index cfde185..5cc0c6b 100755 --- a/cherry_picker/cherry_picker.py +++ b/cherry_picker/cherry_picker.py @@ -13,6 +13,7 @@ import click import requests +import stamina from gidgethub import sansio from . import __version__ @@ -55,6 +56,7 @@ PUSHING_TO_REMOTE_FAILED PR_CREATING + PR_CREATING_FAILED PR_OPENING REMOVING_BACKPORT_BRANCH @@ -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.""" @@ -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 @@ -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): """ @@ -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: diff --git a/cherry_picker/test_cherry_picker.py b/cherry_picker/test_cherry_picker.py index 713c251..5eefb1f 100644 --- a/cherry_picker/test_cherry_picker.py +++ b/cherry_picker/test_cherry_picker.py @@ -1386,5 +1386,5 @@ def test_create_gh_pr_draft_states( "maintainer_can_modify": True, "draft": draft_pr, }, - timeout=10, + timeout=30, ) diff --git a/pyproject.toml b/pyproject.toml index e675412..d87d2c7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,6 +28,7 @@ dependencies = [ "click>=6", "gidgethub", "requests", + "stamina", "tomli>=1.1; python_version<'3.11'", ] optional-dependencies.dev = [ From 0e3720c5fcd991214997b383cecdce18b5bee731 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 15 Jan 2025 13:07:06 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- cherry_picker/cherry_picker.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cherry_picker/cherry_picker.py b/cherry_picker/cherry_picker.py index 5cc0c6b..52df11d 100755 --- a/cherry_picker/cherry_picker.py +++ b/cherry_picker/cherry_picker.py @@ -568,7 +568,7 @@ def backport(self): ) except GitHubException: click.echo(self.get_exit_message(maint_branch)) - self.set_paused_state() + self.set_paused_state() raise if not self.is_mirror(): self.cleanup_branch(cherry_pick_branch)