From fe8ba72f9e918ed0fde38a203dbfd52f135e1f40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Tue, 6 Feb 2024 10:10:58 +0100 Subject: [PATCH] Add: Revert local changes if creating a GitHub release fails Improve the create release "workflow" by reverting the changes (tag and version update commit) if creating a GitHub release via the API did fail. --- pontos/release/create.py | 7 ++++++- tests/release/test_create.py | 14 +++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/pontos/release/create.py b/pontos/release/create.py index 3076d572b..2436fb2ea 100644 --- a/pontos/release/create.py +++ b/pontos/release/create.py @@ -13,7 +13,7 @@ from pontos.changelog.conventional_commits import ChangelogBuilder from pontos.errors import PontosError -from pontos.git import Git +from pontos.git import Git, ResetMode from pontos.github.actions.core import ActionIO from pontos.github.api import GitHubAsyncRESTApi from pontos.release.command import AsyncCommand @@ -315,6 +315,11 @@ async def async_run( # type: ignore[override] self.terminal.ok(f"Created release {release_version}") except httpx.HTTPStatusError as e: self.print_error(str(e)) + # revert commit and tag + self.git.delete_tag(git_version) + self.git.push(git_version, delete=True, remote=git_remote_name) + self.git.reset("HEAD^", mode=ResetMode.HARD) + self.git.push(force=True, remote=git_remote_name) return CreateReleaseReturnValue.CREATE_RELEASE_ERROR if next_version is None: diff --git a/tests/release/test_create.py b/tests/release/test_create.py index 4c000192b..6a7597537 100644 --- a/tests/release/test_create.py +++ b/tests/release/test_create.py @@ -15,7 +15,7 @@ from httpx import HTTPStatusError, Request, Response -from pontos.git import ConfigScope, Git, StatusEntry +from pontos.git import ConfigScope, Git, ResetMode, StatusEntry from pontos.github.actions.errors import GitHubActionsError from pontos.release.create import ( CreateReleaseReturnValue, @@ -1653,9 +1653,17 @@ def test_github_create_release_failure( released, CreateReleaseReturnValue.CREATE_RELEASE_ERROR ) - git_instance_mock.push.assert_called_once_with( - follow_tags=True, remote=None + git_instance_mock.push.assert_has_calls( + [ + call(follow_tags=True, remote=None), + call("v0.0.1", delete=True, remote=None), + call(force=True, remote=None), + ] + ) + git_instance_mock.reset.assert_called_once_with( + "HEAD^", mode=ResetMode.HARD ) + git_instance_mock.delete_tag.assert_called_once_with("v0.0.1") git_instance_mock.add.assert_called_once_with(Path("MyProject.conf")) git_instance_mock.commit.assert_called_once_with( "Automatic release to 0.0.1", verify=False, gpg_signing_key="1234"