Skip to content

Commit e25b228

Browse files
committed
feat: add logic for rolling back release creation
1 parent 35b918b commit e25b228

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

{{cookiecutter.project_name}}/scripts/setup-release.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,26 @@ def get_parser() -> argparse.ArgumentParser:
3939
def setup_release(increment: Optional[str] = None) -> None:
4040
"""Prepares a release of the {{cookiecutter.project_name}} package.
4141
42-
Sets up a release branch from the branch develop, bumps the version, and creates a release commit. Does not tag the
43-
release or push any changes.
42+
Will try to create the release and push, however will return to pre-existing state on error.
4443
"""
4544
check_dependencies(path=REPO_FOLDER, dependencies=["git"])
4645
require_clean_and_up_to_date_repo()
4746

4847
current_version: str = get_package_version()
4948
new_version: str = get_bumped_package_version(increment=increment)
49+
try:
50+
_setup_release(increment=increment, current_version=current_version, new_version=new_version)
51+
except Exception as error:
52+
_rollback_release(version=new_version)
53+
raise error
54+
55+
56+
def _setup_release(increment: str, current_version: str, new_version: str) -> None:
57+
"""Prepares a release of the {{cookiecutter.project_name}} package.
58+
59+
Sets up a release branch from the branch develop, bumps the version, and creates a release commit. Does not tag the
60+
release or push any changes.
61+
"""
5062
create_release_branch(new_version=new_version)
5163
bump_version(increment=increment)
5264

@@ -60,5 +72,17 @@ def setup_release(increment: Optional[str] = None) -> None:
6072
subprocess.run(command, cwd=REPO_FOLDER, capture_output=True, check=True)
6173

6274

75+
def _rollback_release(version: str) -> None:
76+
"""Rolls back to the pre-existing state on error."""
77+
commands: list[list[str]] = [
78+
["git", "checkout", "develop"],
79+
["git", "checkout", "."],
80+
["git", "branch", "-D", f"release/{version}"]
81+
]
82+
83+
for command in commands:
84+
subprocess.run(command, cwd=REPO_FOLDER, check=True)
85+
86+
6387
if __name__ == "__main__":
6488
main()

0 commit comments

Comments
 (0)