Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit bf2fea8

Browse files
author
David Robertson
authored
Add sanity checks to the release script (#12556)
Check we're on the right branch before tagging, and on the right tag before uploading * Abort if we're on the wrong branch * Check we have the right tag checked out * Clarify that `publish` only releases to GitHub
1 parent ae7858f commit bf2fea8

File tree

2 files changed

+38
-26
lines changed

2 files changed

+38
-26
lines changed

changelog.d/12556.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Release script: confirm the commit to be tagged before tagging.

scripts-dev/release.py

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,7 @@ def prepare() -> None:
8989
"""
9090

9191
# Make sure we're in a git repo.
92-
try:
93-
repo = git.Repo()
94-
except git.InvalidGitRepositoryError:
95-
raise click.ClickException("Not in Synapse repo.")
96-
97-
if repo.is_dirty():
98-
raise click.ClickException("Uncommitted changes exist.")
92+
repo = get_repo_and_check_clean_checkout()
9993

10094
click.secho("Updating git repo...")
10195
repo.remote().fetch()
@@ -171,9 +165,7 @@ def prepare() -> None:
171165
assert not parsed_new_version.is_devrelease
172166
assert not parsed_new_version.is_postrelease
173167

174-
release_branch_name = (
175-
f"release-v{parsed_new_version.major}.{parsed_new_version.minor}"
176-
)
168+
release_branch_name = get_release_branch_name(parsed_new_version)
177169
release_branch = find_ref(repo, release_branch_name)
178170
if release_branch:
179171
if release_branch.is_remote():
@@ -274,13 +266,7 @@ def tag(gh_token: Optional[str]) -> None:
274266
"""Tags the release and generates a draft GitHub release"""
275267

276268
# Make sure we're in a git repo.
277-
try:
278-
repo = git.Repo()
279-
except git.InvalidGitRepositoryError:
280-
raise click.ClickException("Not in Synapse repo.")
281-
282-
if repo.is_dirty():
283-
raise click.ClickException("Uncommitted changes exist.")
269+
repo = get_repo_and_check_clean_checkout()
284270

285271
click.secho("Updating git repo...")
286272
repo.remote().fetch()
@@ -293,6 +279,15 @@ def tag(gh_token: Optional[str]) -> None:
293279
if tag_name in repo.tags:
294280
raise click.ClickException(f"Tag {tag_name} already exists!\n")
295281

282+
# Check we're on the right release branch
283+
release_branch = get_release_branch_name(current_version)
284+
if repo.active_branch.name != release_branch:
285+
click.echo(
286+
f"Need to be on the release branch ({release_branch}) before tagging. "
287+
f"Currently on ({repo.active_branch.name})."
288+
)
289+
click.get_current_context().abort()
290+
296291
# Get the appropriate changelogs and tag.
297292
changes = get_changes_for_version(current_version)
298293

@@ -358,21 +353,15 @@ def tag(gh_token: Optional[str]) -> None:
358353
@cli.command()
359354
@click.option("--gh-token", envvar=["GH_TOKEN", "GITHUB_TOKEN"], required=True)
360355
def publish(gh_token: str) -> None:
361-
"""Publish release."""
356+
"""Publish release on GitHub."""
362357

363358
# Make sure we're in a git repo.
364-
try:
365-
repo = git.Repo()
366-
except git.InvalidGitRepositoryError:
367-
raise click.ClickException("Not in Synapse repo.")
368-
369-
if repo.is_dirty():
370-
raise click.ClickException("Uncommitted changes exist.")
359+
get_repo_and_check_clean_checkout()
371360

372361
current_version = get_package_version()
373362
tag_name = f"v{current_version}"
374363

375-
if not click.confirm(f"Publish {tag_name}?", default=True):
364+
if not click.confirm(f"Publish release {tag_name} on GitHub?", default=True):
376365
return
377366

378367
# Publish the draft release
@@ -406,6 +395,13 @@ def upload() -> None:
406395
current_version = get_package_version()
407396
tag_name = f"v{current_version}"
408397

398+
# Check we have the right tag checked out.
399+
repo = get_repo_and_check_clean_checkout()
400+
tag = repo.tag(f"refs/tags/{tag_name}")
401+
if repo.head.commit != tag.commit:
402+
click.echo("Tag {tag_name} (tag.commit) is not currently checked out!")
403+
click.get_current_context().abort()
404+
409405
pypi_asset_names = [
410406
f"matrix_synapse-{current_version}-py3-none-any.whl",
411407
f"matrix-synapse-{current_version}.tar.gz",
@@ -469,6 +465,21 @@ def get_package_version() -> version.Version:
469465
return version.Version(version_string)
470466

471467

468+
def get_release_branch_name(version_number: version.Version) -> str:
469+
return f"release-v{version_number.major}.{version_number.minor}"
470+
471+
472+
def get_repo_and_check_clean_checkout() -> git.Repo:
473+
"""Get the project repo and check it's not got any uncommitted changes."""
474+
try:
475+
repo = git.Repo()
476+
except git.InvalidGitRepositoryError:
477+
raise click.ClickException("Not in Synapse repo.")
478+
if repo.is_dirty():
479+
raise click.ClickException("Uncommitted changes exist.")
480+
return repo
481+
482+
472483
def find_ref(repo: git.Repo, ref_name: str) -> Optional[git.HEAD]:
473484
"""Find the branch/ref, looking first locally then in the remote."""
474485
if ref_name in repo.references:

0 commit comments

Comments
 (0)