|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Usage: source ./check_commit_metadata.sh <from revision> <to revision> |
| 4 | +# Usage: ./check_commit_metadata.sh <from revision> <to revision> |
| 5 | +# |
| 6 | +# Example: ./check_commit_metadata.sh v0.13.1 971e3678 |
| 7 | +# |
| 8 | +# When sourced, this script will populate the COMMIT_METADATA_* variables |
| 9 | +# with the commit metadata for each commit in the revision range. |
| 10 | +# |
| 11 | +# Because this script does some expensive lookups via the GitHub API, its |
| 12 | +# results will be cached in the environment and restored if this script is |
| 13 | +# sourced a second time with the same arguments. |
| 14 | + |
| 15 | +set -euo pipefail |
| 16 | +# shellcheck source=scripts/lib.sh |
| 17 | +source "$(dirname "${BASH_SOURCE[0]}")/../lib.sh" |
| 18 | + |
| 19 | +from_ref=${1:-} |
| 20 | +to_ref=${2:-} |
| 21 | + |
| 22 | +if [[ -z $from_ref ]]; then |
| 23 | + error "No from_ref specified" |
| 24 | +fi |
| 25 | +if [[ -z $to_ref ]]; then |
| 26 | + error "No to_ref specified" |
| 27 | +fi |
| 28 | + |
| 29 | +range="$from_ref..$to_ref" |
| 30 | + |
| 31 | +# Check dependencies. |
| 32 | +dependencies gh |
| 33 | + |
| 34 | +COMMIT_METADATA_BREAKING=0 |
| 35 | +declare -A COMMIT_METADATA_TITLE COMMIT_METADATA_CATEGORY |
| 36 | + |
| 37 | +main() { |
| 38 | + # Match a commit prefix pattern, e.g. feat: or feat(site):. |
| 39 | + prefix_pattern="^([a-z]+)(\([a-z]*\))?:" |
| 40 | + |
| 41 | + # If a commit contains this title prefix or the source PR contains the |
| 42 | + # label, patch releases will not be allowed. |
| 43 | + # This regex matches both `feat!:` and `feat(site)!:`. |
| 44 | + breaking_title="^[a-z]+(\([a-z]*\))?!:" |
| 45 | + breaking_label=release/breaking |
| 46 | + breaking_category=breaking |
| 47 | + |
| 48 | + # Get abbreviated and full commit hashes and titles for each commit. |
| 49 | + mapfile -t commits < <(git log --no-merges --pretty=format:"%h %H %s" "$range") |
| 50 | + |
| 51 | + # If this is a tag, use rev-list to find the commit it points to. |
| 52 | + from_commit=$(git rev-list -n 1 "$from_ref") |
| 53 | + # Get the committer date of the commit so that we can list PRs merged. |
| 54 | + from_commit_date=$(git show --no-patch --date=short --format=%cd "$from_commit") |
| 55 | + |
| 56 | + # Get the labels for all PRs merged since the last release, this is |
| 57 | + # inexact based on date, so a few PRs part of the previous release may |
| 58 | + # be included. |
| 59 | + # |
| 60 | + # Example output: |
| 61 | + # |
| 62 | + # 27386d49d08455b6f8fbf2c18f38244d03fda892 label:security |
| 63 | + # d9f2aaf3b430d8b6f3d5f24032ed6357adaab1f1 |
| 64 | + # fd54512858c906e66f04b0744d8715c2e0de97e6 label:stale label:enhancement |
| 65 | + mapfile -t pr_labels_raw < <( |
| 66 | + gh pr list \ |
| 67 | + --base main \ |
| 68 | + --state merged \ |
| 69 | + --limit 10000 \ |
| 70 | + --search "merged:>=$from_commit_date" \ |
| 71 | + --json mergeCommit,labels \ |
| 72 | + --jq '.[] | .mergeCommit.oid + " " + (["label:" + .labels[].name] | join(" "))' |
| 73 | + ) |
| 74 | + declare -A labels |
| 75 | + for entry in "${pr_labels_raw[@]}"; do |
| 76 | + commit_sha_long=${entry%% *} |
| 77 | + all_labels=${entry#* } |
| 78 | + labels[$commit_sha_long]=$all_labels |
| 79 | + done |
| 80 | + |
| 81 | + for commit in "${commits[@]}"; do |
| 82 | + mapfile -d ' ' -t parts <<<"$commit" |
| 83 | + commit_sha_short=${parts[0]} |
| 84 | + commit_sha_long=${parts[1]} |
| 85 | + commit_prefix=${parts[2]} |
| 86 | + |
| 87 | + # Safety-check, guarantee all commits had their metadata fetched. |
| 88 | + if [[ ! -v labels[$commit_sha_long] ]]; then |
| 89 | + error "Metadata missing for commit $commit_sha_short" |
| 90 | + fi |
| 91 | + |
| 92 | + # Store the commit title for later use. |
| 93 | + title=${parts[*]:2} |
| 94 | + title=${title%$'\n'} |
| 95 | + COMMIT_METADATA_TITLE[$commit_sha_short]=$title |
| 96 | + |
| 97 | + # First, check the title for breaking changes. This avoids doing a |
| 98 | + # GH API request if there's a match. |
| 99 | + if [[ $commit_prefix =~ $breaking_title ]] || [[ ${labels[$commit_sha_long]} = *"label:$breaking_label"* ]]; then |
| 100 | + COMMIT_METADATA_CATEGORY[$commit_sha_short]=$breaking_category |
| 101 | + COMMIT_METADATA_BREAKING=1 |
| 102 | + continue |
| 103 | + fi |
| 104 | + |
| 105 | + if [[ $commit_prefix =~ $prefix_pattern ]]; then |
| 106 | + commit_prefix=${BASH_REMATCH[1]} |
| 107 | + fi |
| 108 | + case $commit_prefix in |
| 109 | + feat | fix) |
| 110 | + COMMIT_METADATA_CATEGORY[$commit_sha_short]=$commit_prefix |
| 111 | + ;; |
| 112 | + *) |
| 113 | + COMMIT_METADATA_CATEGORY[$commit_sha_short]=other |
| 114 | + ;; |
| 115 | + esac |
| 116 | + done |
| 117 | +} |
| 118 | + |
| 119 | +declare_print_commit_metadata() { |
| 120 | + declare -p COMMIT_METADATA_BREAKING COMMIT_METADATA_TITLE COMMIT_METADATA_CATEGORY |
| 121 | +} |
| 122 | + |
| 123 | +export_commit_metadata() { |
| 124 | + _COMMIT_METADATA_CACHE="${range}:$(declare_print_commit_metadata)" |
| 125 | + export _COMMIT_METADATA_CACHE COMMIT_METADATA_BREAKING COMMIT_METADATA_TITLE COMMIT_METADATA_CATEGORY |
| 126 | +} |
| 127 | + |
| 128 | +# _COMMIT_METADATA_CACHE is used to cache the results of this script in |
| 129 | +# the environment because bash arrays are not passed on to subscripts. |
| 130 | +if [[ ${_COMMIT_METADATA_CACHE:-} == "${range}:"* ]]; then |
| 131 | + eval "${_COMMIT_METADATA_CACHE#*:}" |
| 132 | +else |
| 133 | + main |
| 134 | +fi |
| 135 | + |
| 136 | +export_commit_metadata |
| 137 | + |
| 138 | +# Make it easier to debug this script by printing the associative array |
| 139 | +# when it's not sourced. |
| 140 | +if ! issourced; then |
| 141 | + declare_print_commit_metadata |
| 142 | +fi |
0 commit comments