Skip to content
Myr

release-notes.sh

This file

Path

release-notes.sh

153 lines · 5.8 KiB · Shell

Last changed by

[meta] add `release-notes.sh`, the release notes generatorJordan Harbandbefore adoptiondf7a936

History of this file

Take it

[meta] add `release-notes.sh`, the release notes generatorJordan Harbandbefore adoptiondf7a936

lines 1–153why, one step back
1#!/bin/sh
23# Generates release notes for an nvm release, in the format used in the4# annotated release tags and version bump commits.5#6# Usage: ./release-notes.sh <new-version> [<prev-ref>] [<end-ref>]7# <new-version> the version being released, e.g. `v0.40.7` (the leading `v` is optional)8# <prev-ref> list commits since this ref; defaults to the most recent `vX.Y.Z` tag9# <end-ref> list commits up to this ref; defaults to HEAD10#11# Commits from <prev-ref> to <end-ref> are grouped into sections by the leading12# `[bracket]` tag of their subject line, in `git log` order (newest first).13# Category tags ([New], [Fix], ...) are stripped from the bullet text;14# context tags ([readme], [actions], [meta], ...) are kept.15# Commits without a recognized tag land under "Misc", with a warning on stderr.16#17# Each bullet gets a ` (#123)` reference to the pull request that introduced18# the commit, looked up via `gh` from GitHub's commit->PR association, unless19# the subject already ends with one. Set NVM_RELEASE_NOTES_SKIP_PRS to a20# nonempty value to skip the lookup (references will be omitted).2122NEW_VERSION="${1-}"23PREV_REF="${2-}"24END_REF="${3:-HEAD}"2526if [ -z "${NEW_VERSION}" ]; then27 printf >&2 'Usage: %s <new-version> [<prev-ref>] [<end-ref>]\n' "$0"28 exit 229fi30case "${NEW_VERSION}" in31 v*) ;;32 *) NEW_VERSION="v${NEW_VERSION}" ;;33esac3435if [ -z "${PREV_REF}" ]; then36 PREV_REF="$(git describe --abbrev=0 --tags --match 'v[0-9]*.[0-9]*.[0-9]*')" || {37 printf >&2 'release-notes: failed to determine the previous version tag.\n'38 exit 139 }40fi41for REF in "${PREV_REF}" "${END_REF}"; do42 git rev-parse --quiet --verify "${REF}^{commit}" >/dev/null || {43 # shellcheck disable=SC201644 printf >&2 'release-notes: `%s` is not a valid ref.\n' "${REF}"45 exit 146 }47done4849LOOKUP_PRS=150if [ -n "${NVM_RELEASE_NOTES_SKIP_PRS-}" ]; then51 LOOKUP_PRS=052elif ! command -v gh >/dev/null 2>&1; then53 # shellcheck disable=SC201654 printf >&2 'release-notes: `gh` is not available; PR references will be omitted.\n'55 LOOKUP_PRS=056fi5758# the repo to look up commit->PR associations in, e.g. `nvm-sh/nvm`59REPO_SLUG="${GH_REPO-}"60if [ -z "${REPO_SLUG}" ]; then61 ORIGIN_URL="$(git remote get-url origin 2>/dev/null)" || ORIGIN_URL=''62 case "${ORIGIN_URL}" in63 *github.com[:/]*) REPO_SLUG="$(printf '%s' "${ORIGIN_URL}" | sed 's#^.*github\.com[:/]##;s#\.git$##')" ;;64 esac65fi66: "${REPO_SLUG:=nvm-sh/nvm}"6768TAB="$(printf '\t')"6970COMMITS_FILE="$(mktemp)" || exit 171GH_ERR_FILE="$(mktemp)" || exit 172trap 'rm -f "${COMMITS_FILE}" "${GH_ERR_FILE}"' EXIT7374# each line: <section key> TAB <sha> TAB <bullet text>, in git log order75git log --no-merges --format='%H %s' "${PREV_REF}..${END_REF}" | while IFS=' ' read -r SHA SUBJECT; do76 LOWERED="$(printf '%s' "${SUBJECT}" | tr '[:upper:]' '[:lower:]')"77 STRIPPED="${SUBJECT#\[*\] }"78 case "${LOWERED}" in79 v[0-9]*.[0-9]*.[0-9]*)80 case "${SUBJECT}" in81 *' '*) # not a bump commit, just a subject that starts with a version82 printf >&2 'release-notes: no category prefix, filing under Misc: %s\n' "${SUBJECT}"83 KEY='misc' TEXT="${SUBJECT}"84 ;;85 *) continue ;; # a version bump commit86 esac87 ;;88 '[new] '*) KEY='new' TEXT="${STRIPPED}" ;;89 '[fix] '* | '[patch] '*) KEY='fix' TEXT="${STRIPPED}" ;;90 '[refactor] '*) KEY='refactor' TEXT="${STRIPPED}" ;;91 '[shellcheck] '*) KEY='refactor' TEXT="${SUBJECT}" ;;92 '[robustness] '*) KEY='robustness' TEXT="${STRIPPED}" ;;93 '[perf] '* | '[performance] '*) KEY='perf' TEXT="${STRIPPED}" ;;94 '[dockerfile] '*) KEY='dockerfile' TEXT="${STRIPPED}" ;;95 '[docs] '* | '[doc] '*) KEY='docs' TEXT="${STRIPPED}" ;;96 '[readme] '*) KEY='docs' TEXT="${SUBJECT}" ;;97 '[meta] '* | '[security] '* | '[dev deps] '*)98 KEY='misc' TEXT="${SUBJECT}" ;;99 '[tests] '* | '[test] '*) KEY='tests' TEXT="${STRIPPED}" ;;100 '[actions] '* | '[debug] '*) KEY='tests' TEXT="${SUBJECT}" ;;101 *)102 printf >&2 'release-notes: no category prefix, filing under Misc: %s\n' "${SUBJECT}"103 KEY='misc' TEXT="${SUBJECT}"104 ;;105 esac106 printf '%s\t%s\t%s\n' "${KEY}" "${SHA}" "${TEXT}"107done > "${COMMITS_FILE}"108109if ! [ -s "${COMMITS_FILE}" ]; then110 printf >&2 'release-notes: no commits found in %s..HEAD.\n' "${PREV_REF}"111fi112113print_section() {114 if ! grep -q "^$1${TAB}" "${COMMITS_FILE}"; then115 return 0116 fi117 printf '\n%s\n\n' "$2"118 grep "^$1${TAB}" "${COMMITS_FILE}" | while IFS="${TAB}" read -r _KEY SHA TEXT; do119 REF=''120 case "${TEXT}" in121 *'(#'[0-9]*')') ;; # the subject already carries a reference122 *)123 if [ "${LOOKUP_PRS}" = 1 ]; then124 PR="$(gh api "repos/${REPO_SLUG}/commits/${SHA}/pulls" --jq '.[0].number // empty' 2>"${GH_ERR_FILE}")" || {125 if grep -q 'No commit found' "${GH_ERR_FILE}"; then126 PR='' # not pushed to GitHub yet, so it can not have come from a PR127 else128 printf >&2 'release-notes: failed to look up the PR for commit %s:\n' "${SHA}"129 cat >&2 "${GH_ERR_FILE}"130 exit 1131 fi132 }133 case "${PR}" in134 '' | *[!0-9]*) ;;135 *) REF=" (#${PR})" ;;136 esac137 fi138 ;;139 esac140 printf ' - %s%s\n' "${TEXT}" "${REF}"141 done || exit 1142}143144printf '%s\n' "${NEW_VERSION}"145print_section 'new' 'New Stuff' &&146print_section 'fix' 'Bug Fixes' &&147print_section 'refactor' 'Refactors' &&148print_section 'robustness' 'Robustness' &&149print_section 'perf' 'Performance' &&150print_section 'dockerfile' 'Dockerfile' &&151print_section 'docs' 'Docs' &&152print_section 'misc' 'Misc' &&153print_section 'tests' 'Tests'