Skip to content

Commit 8cb0a68

Browse files
committed
Fix up hook handling
1 parent ee045d1 commit 8cb0a68

File tree

9 files changed

+177
-115
lines changed

9 files changed

+177
-115
lines changed

.github/actions/check-release/action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,4 @@ runs:
4747
python -m jupyter_releaser.actions.draft_release
4848
4949
# Publish Assets
50-
jupyter-releaser publish-assets --use-checkout-dir
50+
jupyter-releaser publish-assets

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ You can also define hooks to run before and after
140140
commands in a `hooks` section. Hooks can be a shell command to run or
141141
a list of shell commands, and are specified to run `before-` or `after-`
142142
a command.
143-
Note: the only invalid hook name is `before-prep-git`, since a checkout of the target repository is not yet available at that point.
143+
Note: the only unusable hook names are `before-prep-git` and `before-extract-release`, since a checkout of the target repository is not yet available at that point.
144144

145145
### Configuration File Priority
146146

jupyter_releaser/cli.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ def invoke(self, ctx):
100100
# Handle after hooks
101101

102102
# Re-read config if we just did a git checkout
103-
if cmd_name == "prep-git":
103+
if cmd_name in ["prep-git", "extract-release"]:
104+
os.chdir(util.CHECKOUT_NAME)
104105
config = util.read_config()
105106
hooks = config.get("hooks", {})
106107

@@ -456,6 +457,7 @@ def draft_release(
456457
@main.command()
457458
@add_options(auth_options)
458459
@click.argument("release-url", nargs=1)
460+
@use_checkout_dir()
459461
def delete_release(auth, release_url):
460462
"""Delete a draft GitHub release by url to the release page"""
461463
lib.delete_release(auth, release_url)
@@ -487,18 +489,17 @@ def extract_release(auth, dist_dir, dry_run, release_url, npm_install_options):
487489
envvar="TWINE_COMMAND",
488490
default="twine upload",
489491
)
490-
@click.option("--use-checkout-dir", help="Use the checkout directory", is_flag=True)
491492
@add_options(dry_run_options)
492-
def publish_assets(dist_dir, npm_token, npm_cmd, twine_cmd, dry_run, use_checkout_dir):
493+
@use_checkout_dir()
494+
def publish_assets(dist_dir, npm_token, npm_cmd, twine_cmd, dry_run):
493495
"""Publish release asset(s)"""
494-
lib.publish_assets(
495-
dist_dir, npm_token, npm_cmd, twine_cmd, dry_run, use_checkout_dir
496-
)
496+
lib.publish_assets(dist_dir, npm_token, npm_cmd, twine_cmd, dry_run)
497497

498498

499499
@main.command()
500500
@add_options(auth_options)
501501
@click.argument("release-url", nargs=1)
502+
@use_checkout_dir()
502503
def publish_release(auth, release_url):
503504
"""Publish GitHub release"""
504505
lib.publish_release(auth, release_url)
@@ -510,14 +511,14 @@ def publish_release(auth, release_url):
510511
@add_options(username_options)
511512
@add_options(changelog_path_options)
512513
@add_options(dry_run_options)
513-
@add_options(git_url_options)
514514
@click.argument("release-url")
515+
@use_checkout_dir()
515516
def forwardport_changelog(
516-
auth, ref, branch, repo, username, changelog_path, dry_run, git_url, release_url
517+
auth, ref, branch, repo, username, changelog_path, dry_run, release_url
517518
):
518519
"""Forwardport Changelog Entries to the Default Branch"""
519520
lib.forwardport_changelog(
520-
auth, ref, branch, repo, username, changelog_path, dry_run, git_url, release_url
521+
auth, ref, branch, repo, username, changelog_path, dry_run, release_url
521522
)
522523

523524

jupyter_releaser/lib.py

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ def check_links(ignore_glob, ignore_links, cache_file, links_expire):
7777
for f in files:
7878
file_cmd = cmd + f' "{f}"'
7979
try:
80-
util.run(file_cmd)
80+
util.run(file_cmd, shell=False)
8181
except Exception as e:
8282
# Return code 5 means no tests were run (no links found)
8383
if e.returncode != 5:
84-
util.run(file_cmd + " --lf")
84+
util.run(file_cmd + " --lf", shell=False)
8585

8686

8787
def draft_changelog(version_spec, branch, repo, since, auth, changelog_path, dry_run):
@@ -272,8 +272,12 @@ def extract_release(auth, dist_dir, dry_run, release_url, npm_install_options):
272272
owner, repo = match["owner"], match["repo"]
273273
gh = GhApi(owner=owner, repo=repo, token=auth)
274274
release = util.release_for_url(gh, release_url)
275+
branch = release.target_commitish
275276
assets = release.assets
276277

278+
# Prepare a git checkout
279+
prep_git(None, branch, f"{owner}/{repo}", auth, None, None)
280+
277281
# Clean the dist folder
278282
dist = Path(dist_dir)
279283
if dist.exists():
@@ -303,7 +307,6 @@ def extract_release(auth, dist_dir, dry_run, release_url, npm_install_options):
303307
if dry_run:
304308
return
305309

306-
branch = release.target_commitish
307310
tag_name = release.tag_name
308311

309312
sha = None
@@ -313,17 +316,10 @@ def extract_release(auth, dist_dir, dry_run, release_url, npm_install_options):
313316
if sha is None:
314317
raise ValueError("Could not find tag")
315318

316-
# Run a git checkout
317-
# Fetch the branch
318-
# Get the commmit message for the branch
319+
# Get the commmit message for the tag
319320
commit_message = ""
320-
with TemporaryDirectory() as td:
321-
url = gh.repos.get().html_url
322-
util.run(f"git clone {url} local", cwd=td)
323-
checkout = osp.join(td, "local")
324-
if not osp.exists(url):
325-
util.run(f"git fetch origin {branch}", cwd=checkout)
326-
commit_message = util.run(f"git log --format=%B -n 1 {sha}", cwd=checkout)
321+
checkout = osp.join(os.getcwd(), util.CHECKOUT_NAME)
322+
commit_message = util.run(f"git log --format=%B -n 1 {sha}", cwd=checkout)
327323

328324
for asset in assets:
329325
# Check the sha against the published sha
@@ -351,13 +347,8 @@ def parse_release_url(release_url):
351347
return match
352348

353349

354-
def publish_assets(dist_dir, npm_token, npm_cmd, twine_cmd, dry_run, use_checkout_dir):
350+
def publish_assets(dist_dir, npm_token, npm_cmd, twine_cmd, dry_run):
355351
"""Publish release asset(s)"""
356-
if use_checkout_dir:
357-
if not osp.exists(util.CHECKOUT_NAME):
358-
raise ValueError("Please run prep-git first")
359-
os.chdir(util.CHECKOUT_NAME)
360-
361352
if dry_run:
362353
# Start local pypi server with no auth, allowing overwrites,
363354
# in a temporary directory
@@ -424,14 +415,14 @@ def publish_release(auth, release_url):
424415
util.actions_output("release_url", release.html_url)
425416

426417

427-
def prep_git(ref, branch, repo, auth, username, url, install=True):
418+
def prep_git(ref, branch, repo, auth, username, url):
428419
"""Set up git"""
429420
repo = repo or util.get_repo()
430421

431422
user_name = ""
432423
try:
433424
user_name = util.run("git config --global user.email")
434-
except Exception as e:
425+
except Exception:
435426
pass
436427

437428
if not user_name:
@@ -498,42 +489,42 @@ def prep_git(ref, branch, repo, auth, username, url, install=True):
498489
util.run(checkout_cmd)
499490

500491
# Install the package
501-
if install:
502-
# install python package in editable mode with test deps
503-
if util.SETUP_PY.exists():
504-
util.run('pip install -q -e ".[test]"')
492+
# install python package in editable mode with test deps
493+
if util.SETUP_PY.exists():
494+
util.run('pip install -q -e ".[test]"')
505495

506-
# prefer yarn if yarn lock exists
507-
elif util.YARN_LOCK.exists():
508-
if not shutil.which("yarn"):
509-
util.run("npm install -g yarn")
510-
util.run("yarn")
496+
# prefer yarn if yarn lock exists
497+
elif util.YARN_LOCK.exists():
498+
if not shutil.which("yarn"):
499+
util.run("npm install -g yarn")
500+
util.run("yarn")
511501

512-
# npm install otherwise
513-
elif util.PACKAGE_JSON.exists():
514-
util.run("npm install")
502+
# npm install otherwise
503+
elif util.PACKAGE_JSON.exists():
504+
util.run("npm install")
515505

516506
os.chdir(orig_dir)
517507

518508
return branch
519509

520510

521511
def forwardport_changelog(
522-
auth, ref, branch, repo, username, changelog_path, dry_run, git_url, release_url
512+
auth, ref, branch, repo, username, changelog_path, dry_run, release_url
523513
):
524514
"""Forwardport Changelog Entries to the Default Branch"""
525515
# Set up the git repo with the branch
526516
match = parse_release_url(release_url)
527517
gh = GhApi(owner=match["owner"], repo=match["repo"], token=auth)
528518
release = util.release_for_url(gh, release_url)
529519
tag = release.tag_name
520+
source_branch = release.target_commitish
530521

531522
repo = f'{match["owner"]}/{match["repo"]}'
532523

533-
# We want to target the main branch
534-
orig_dir = os.getcwd()
535-
branch = prep_git(None, None, repo, auth, username, git_url, install=False)
536-
os.chdir(util.CHECKOUT_NAME)
524+
# switch to main branch here
525+
branch = branch or util.get_default_branch()
526+
util.run(f"git fetch origin {branch}")
527+
util.run(f"git checkout {branch}")
537528

538529
# Bail if the tag has been merged to the branch
539530
tags = util.run(f"git --no-pager tag --merged {branch}", quiet=True)
@@ -594,5 +585,4 @@ def forwardport_changelog(
594585
)
595586

596587
# Clean up after ourselves
597-
os.chdir(orig_dir)
598-
shutil.rmtree(util.CHECKOUT_NAME)
588+
util.run(f"git checkout {source_branch}")

jupyter_releaser/tests/conftest.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ def git_repo(tmp_path):
5858
readme = tmp_path / "README.md"
5959
readme.write_text("Hello from foo project\n", encoding="utf-8")
6060

61+
config = Path(util.JUPYTER_RELEASER_CONFIG)
62+
config.write_text(testutil.TOML_CONFIG, encoding="utf-8")
63+
6164
run("git add .")
6265
run('git commit -m "foo"')
6366
run(f"git remote add origin {util.normalize_path(tmp_path)}")

0 commit comments

Comments
 (0)