|
| 1 | +#!/bin/bash |
| 2 | +# Copyright (c) 2026 Bart van de Lint |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +set -euo pipefail |
| 6 | + |
| 7 | +_dry_run=false |
| 8 | +_assume_yes=false |
| 9 | +_sha="" |
| 10 | + |
| 11 | +print_usage() { |
| 12 | + echo "Usage:" |
| 13 | + echo " ./bin/release Register the current HEAD with JuliaRegistrator" |
| 14 | + echo " ./bin/release --dry-run Print the version, commit and release notes, then exit" |
| 15 | + echo " ./bin/release --yes Skip the confirmation prompt" |
| 16 | + echo " ./bin/release --sha SHA Register a specific commit instead of HEAD" |
| 17 | + echo " ./bin/release -h" |
| 18 | + echo |
| 19 | + echo "Extracts the top section of CHANGELOG.md as release notes and posts a" |
| 20 | + echo "'@JuliaRegistrator register' comment on the commit via the GitHub API." |
| 21 | + echo "The commit must already be pushed to the remote." |
| 22 | +} |
| 23 | + |
| 24 | +while [[ $# -gt 0 ]]; do |
| 25 | + case "$1" in |
| 26 | + -h|--help) print_usage; exit 0 ;; |
| 27 | + -n|--dry-run) _dry_run=true; shift ;; |
| 28 | + -y|--yes) _assume_yes=true; shift ;; |
| 29 | + --sha) _sha="${2:?--sha needs an argument}"; shift 2 ;; |
| 30 | + *) echo "Unknown option: $1" >&2; print_usage >&2; exit 1 ;; |
| 31 | + esac |
| 32 | +done |
| 33 | + |
| 34 | +if [[ $(basename "$(pwd)") == "bin" ]]; then |
| 35 | + cd .. |
| 36 | +fi |
| 37 | + |
| 38 | +for cmd in git gh awk; do |
| 39 | + if ! command -v "$cmd" >/dev/null 2>&1; then |
| 40 | + echo "Required command not found: $cmd" >&2 |
| 41 | + exit 1 |
| 42 | + fi |
| 43 | +done |
| 44 | + |
| 45 | +if ! gh auth status >/dev/null 2>&1; then |
| 46 | + echo "gh is not authenticated. Run 'gh auth login' first." >&2 |
| 47 | + exit 1 |
| 48 | +fi |
| 49 | + |
| 50 | +_version=$(awk -F'"' '/^version[[:space:]]*=/ {print $2; exit}' Project.toml) |
| 51 | +if [[ -z "$_version" ]]; then |
| 52 | + echo "Could not read version from Project.toml" >&2 |
| 53 | + exit 1 |
| 54 | +fi |
| 55 | + |
| 56 | +_changelog_version=$(awk '/^## / { |
| 57 | + for (i = 1; i <= NF; i++) if ($i ~ /^v[0-9]+\.[0-9]+\.[0-9]+$/) { print $i; exit } |
| 58 | +}' CHANGELOG.md) |
| 59 | +if [[ "$_changelog_version" != "v$_version" ]]; then |
| 60 | + echo "Version mismatch: Project.toml is $_version but CHANGELOG top is" \ |
| 61 | + "$_changelog_version (expected v$_version)." >&2 |
| 62 | + echo "Update CHANGELOG.md so its first '## ' header matches the package version." >&2 |
| 63 | + exit 1 |
| 64 | +fi |
| 65 | + |
| 66 | +_notes=$(awk ' |
| 67 | + /^## / { n++; if (n == 1) next } |
| 68 | + n == 1 { print } |
| 69 | +' CHANGELOG.md | awk ' |
| 70 | + { lines[NR] = $0 } |
| 71 | + END { |
| 72 | + start = 1; while (start <= NR && lines[start] ~ /^[[:space:]]*$/) start++ |
| 73 | + end = NR; while (end >= start && lines[end] ~ /^[[:space:]]*$/) end-- |
| 74 | + for (i = start; i <= end; i++) print lines[i] |
| 75 | + } |
| 76 | +') |
| 77 | + |
| 78 | +if [[ -z "$_notes" ]]; then |
| 79 | + echo "No release notes found under the '## v$_version' section." >&2 |
| 80 | + exit 1 |
| 81 | +fi |
| 82 | + |
| 83 | +if [[ -z "$_sha" ]]; then |
| 84 | + _sha=$(git rev-parse HEAD) |
| 85 | +fi |
| 86 | + |
| 87 | +if [[ -n "$(git status --porcelain)" ]]; then |
| 88 | + echo "Warning: working tree has uncommitted changes." >&2 |
| 89 | +fi |
| 90 | + |
| 91 | +_repo=$(gh repo view --json nameWithOwner -q .nameWithOwner) |
| 92 | + |
| 93 | +if ! gh api "repos/$_repo/commits/$_sha" >/dev/null 2>&1; then |
| 94 | + echo "Commit $_sha was not found on $_repo. Push it first:" >&2 |
| 95 | + echo " git push" >&2 |
| 96 | + exit 1 |
| 97 | +fi |
| 98 | + |
| 99 | +_body=$(printf '@JuliaRegistrator register\n\nRelease notes:\n\n%s\n' "$_notes") |
| 100 | + |
| 101 | +echo "Repository: $_repo" |
| 102 | +echo "Version: v$_version" |
| 103 | +echo "Commit: $_sha" |
| 104 | +echo |
| 105 | +echo "Release notes:" |
| 106 | +echo "--------------------------------------------------------------------------" |
| 107 | +echo "$_notes" |
| 108 | +echo "--------------------------------------------------------------------------" |
| 109 | + |
| 110 | +if [[ "$_dry_run" == true ]]; then |
| 111 | + echo |
| 112 | + echo "Dry run: no comment posted." |
| 113 | + exit 0 |
| 114 | +fi |
| 115 | + |
| 116 | +if [[ "$_assume_yes" != true ]]; then |
| 117 | + echo |
| 118 | + read -r -p "Post this comment and trigger JuliaRegistrator? [y/N] " _reply |
| 119 | + if [[ ! "$_reply" =~ ^[Yy]$ ]]; then |
| 120 | + echo "Aborted." |
| 121 | + exit 1 |
| 122 | + fi |
| 123 | +fi |
| 124 | + |
| 125 | +_url=$(gh api "repos/$_repo/commits/$_sha/comments" -f body="$_body" -q .html_url) |
| 126 | + |
| 127 | +echo |
| 128 | +echo "Posted: $_url" |
| 129 | +echo "JuliaRegistrator will open a PR to the General registry shortly." |
0 commit comments