Skip to content

Commit de4f93b

Browse files
authored
Fix handling of ensure sha (#435)
1 parent 0dc959f commit de4f93b

File tree

5 files changed

+33
-22
lines changed

5 files changed

+33
-22
lines changed

jupyter_releaser/actions/populate_release.py

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,7 @@
55
import sys
66

77
from jupyter_releaser.actions.common import run_action, setup
8-
from jupyter_releaser.util import (
9-
actions_output,
10-
ensure_sha,
11-
get_gh_object,
12-
log,
13-
release_for_url,
14-
)
8+
from jupyter_releaser.util import actions_output, get_gh_object, log, release_for_url
159

1610
setup()
1711

@@ -33,11 +27,7 @@
3327
raise RuntimeError("Cannot complete Draft Release, no draft GitHub release url found!")
3428

3529
run_action("jupyter-releaser prep-git")
36-
37-
if not dry_run:
38-
# Ensure the branch sha has not changed.
39-
ensure_sha()
40-
30+
run_action("jupyter-releaser ensure-sha")
4131
run_action("jupyter-releaser bump-version")
4232
run_action("jupyter-releaser extract-changelog")
4333

@@ -48,7 +38,5 @@
4838
run_action("jupyter-releaser build-python")
4939
run_action("jupyter-releaser check-python")
5040
run_action("jupyter-releaser tag-release")
51-
52-
if not dry_run:
53-
ensure_sha()
41+
run_action("jupyter-releaser ensure-sha")
5442
run_action("jupyter-releaser populate-release")

jupyter_releaser/cli.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,17 @@ def publish_release(auth, dry_run, release_url):
643643
lib.publish_release(auth, dry_run, release_url)
644644

645645

646+
@main.command()
647+
@add_options(branch_options)
648+
@add_options(dry_run_options)
649+
@click.option(
650+
"--expected-sha", help="The expected sha of the branch HEAD", envvar="RH_EXPECTED_SHA"
651+
)
652+
@use_checkout_dir()
653+
def ensure_sha(ref, branch, repo, dry_run, expected_sha):
654+
util.ensure_sha(dry_run, expected_sha, branch)
655+
656+
646657
@main.command()
647658
@add_options(auth_options)
648659
@add_options(branch_options)

jupyter_releaser/lib.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def draft_changelog(
9898
version=version,
9999
post_version_spec=post_version_spec,
100100
post_version_message=post_version_message,
101-
current_sha=current_sha,
101+
expected_sha=current_sha,
102102
)
103103
with tempfile.TemporaryDirectory() as d:
104104
metadata_path = Path(d) / util.METADATA_JSON
@@ -225,7 +225,6 @@ def populate_release(
225225
remote_name = util.get_remote_name(dry_run)
226226
remote_url = util.run(f"git config --get remote.{remote_name}.url")
227227
if not os.path.exists(remote_url):
228-
util.ensure_sha()
229228
util.run(f"git push {remote_name} HEAD:{branch} --follow-tags --tags")
230229

231230
# Set the body of the release with the changelog contents.

jupyter_releaser/tests/test_cli.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ def test_list_envvars(runner):
147147
check-imports: RH_CHECK_IMPORTS
148148
dist-dir: RH_DIST_DIR
149149
dry-run: RH_DRY_RUN
150+
expected-sha: RH_EXPECTED_SHA
150151
npm-cmd: RH_NPM_COMMAND
151152
npm-install-options: RH_NPM_INSTALL_OPTIONS
152153
npm-registry: NPM_REGISTRY
@@ -808,3 +809,13 @@ def test_forwardport_changelog_has_new(npm_package, runner, mocker, mock_github,
808809
## 1.0.1
809810
"""
810811
assert expected in text, text
812+
813+
814+
def test_ensure_sha(npm_package, runner, git_prep):
815+
sha = util.run("git rev-parse HEAD", cwd=util.CHECKOUT_NAME)
816+
current = util.run("git branch --show-current", cwd=util.CHECKOUT_NAME)
817+
runner(["ensure-sha", "--branch", current, "--expected-sha", sha])
818+
runner(["ensure-sha", "--branch", current, "--expected-sha", "abc", "--dry-run"])
819+
820+
with pytest.raises(RuntimeError):
821+
runner(["ensure-sha", "--branch", current, "--expected-sha", "abc"])

jupyter_releaser/util.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -614,17 +614,19 @@ def handle_since():
614614
return since
615615

616616

617-
def ensure_sha():
617+
def ensure_sha(dry_run, expected_sha, branch):
618618
"""Ensure the sha of the remote branch matches the expected sha"""
619-
current_sha = os.environ["RH_CURRENT_SHA"]
620-
branch = os.environ["RH_BRANCH"]
621619
log("Ensuring sha...")
622620
remote_name = get_remote_name(False)
623621
run("git remote -v", echo=True)
624622
run(f"git fetch {remote_name} {branch}", echo=True)
625623
sha = run(f"git rev-parse {remote_name}/{branch}", echo=True)
626-
if sha != current_sha:
627-
raise RuntimeError(f"{branch} current sha {sha} is not equal to expected sha {current_sha}")
624+
if sha != expected_sha:
625+
msg = f"{branch} current sha {sha} is not equal to expected sha {expected_sha}"
626+
if dry_run:
627+
log(msg)
628+
else:
629+
raise RuntimeError(msg)
628630

629631

630632
def get_gh_object(dry_run=False, **kwargs):

0 commit comments

Comments
 (0)