|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +if [ "$#" -ne 1 ]; then |
| 6 | + echo "Provide a single argument with a version of the release draft to use." >&2 |
| 7 | + echo "Usage: $0 <VERSION>" |
| 8 | + exit 1 |
| 9 | +fi |
| 10 | + |
| 11 | +VERSION="$1" |
| 12 | + |
| 13 | +RELEASE_NOTES=$(gh release view "$VERSION" --json body --jq .body) |
| 14 | + |
| 15 | +BREAKING_CHANGES_HEADER="Breaking Changes" |
| 16 | +RELEASE_NOTES_HEADER="Release Notes" |
| 17 | + |
| 18 | +commit_message_re="-\s(.*)\s(\(#[0-9]+\)\s@.*)" |
| 19 | +rls_header_re="^##.*(Features|$BREAKING_CHANGES_HEADER|Bug Fixes|Fixed Vulnerabilities)" |
| 20 | + |
| 21 | +extract_header() { |
| 22 | + local commit="$1" |
| 23 | + local header_name="$2" |
| 24 | + awk " |
| 25 | + /^\s?$/ {next} |
| 26 | + /## $header_name/ {rn=1} |
| 27 | + rn && !/^##/ {print}; |
| 28 | + /##/ && !/## $header_name/ {rn=0}" <<<"$commit" |
| 29 | +} |
| 30 | + |
| 31 | +indent() { |
| 32 | + while IFS= read -r line; do |
| 33 | + printf " %s\n" "${line%"${line##*[![:space:]]}"}" |
| 34 | + done <<<"$1" |
| 35 | +} |
| 36 | + |
| 37 | +new_notes="" |
| 38 | +rls_header="" |
| 39 | +while IFS= read -r line; do |
| 40 | + new_notes+="$line\n" |
| 41 | + if [[ $line == \##* ]]; then |
| 42 | + if ! [[ $line =~ $rls_header_re ]]; then |
| 43 | + rls_header="" |
| 44 | + continue |
| 45 | + fi |
| 46 | + rls_header="${BASH_REMATCH[1]}" |
| 47 | + fi |
| 48 | + if [[ $rls_header == "" ]]; then |
| 49 | + continue |
| 50 | + fi |
| 51 | + if [[ $line != -* ]]; then |
| 52 | + continue |
| 53 | + fi |
| 54 | + if ! [[ $line =~ $commit_message_re ]]; then |
| 55 | + continue |
| 56 | + fi |
| 57 | + commit_msg="${BASH_REMATCH[1]}" |
| 58 | + commit_body=$(git log --grep "$commit_msg" -n1 --pretty="%b") |
| 59 | + |
| 60 | + add_notes() { |
| 61 | + local notes="$1" |
| 62 | + if [[ $notes != "" ]]; then |
| 63 | + new_notes+=$(indent "> $notes") |
| 64 | + new_notes+="\n" |
| 65 | + fi |
| 66 | + } |
| 67 | + |
| 68 | + rn=$(extract_header "$commit_body" "$RELEASE_NOTES_HEADER") |
| 69 | + bc=$(extract_header "$commit_body" "$BREAKING_CHANGES_HEADER") |
| 70 | + |
| 71 | + case $rls_header in |
| 72 | + "$BREAKING_CHANGES_HEADER") add_notes "$bc" ;; |
| 73 | + *) add_notes "$rn" ;; |
| 74 | + esac |
| 75 | + |
| 76 | +done <<<"$RELEASE_NOTES" |
| 77 | + |
| 78 | +echo "Uploading release notes for $VERSION" |
| 79 | +# shellcheck disable=2059 |
| 80 | +printf "$new_notes" | gh release edit "$VERSION" --verify-tag -F - |
0 commit comments