|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Generate HashiCorp-style release notes from commits since the previous tag. |
| 3 | +# Usage: generate-release-notes.sh NEW_TAG [PREV_TAG] |
| 4 | +# Output: markdown suitable for CHANGELOG.md and GitHub release notes. |
| 5 | +set -euo pipefail |
| 6 | + |
| 7 | +NEW_TAG="${1:?new tag required (e.g. v2.12.0)}" |
| 8 | +PREV_TAG="${2:-}" |
| 9 | + |
| 10 | +if [[ -z "$PREV_TAG" ]]; then |
| 11 | + PREV_TAG="$(git describe --tags --abbrev=0 "${NEW_TAG}^" 2>/dev/null || true)" |
| 12 | +fi |
| 13 | + |
| 14 | +VERSION="${NEW_TAG#v}" |
| 15 | +REPO="${GITHUB_REPOSITORY:-devops-rob/terraform-provider-terracurl}" |
| 16 | + |
| 17 | +if [[ -n "$PREV_TAG" ]]; then |
| 18 | + RANGE="${PREV_TAG}..${NEW_TAG}" |
| 19 | + COMPARE_URL="https://github.com/${REPO}/compare/${PREV_TAG}...${NEW_TAG}" |
| 20 | +else |
| 21 | + RANGE="${NEW_TAG}" |
| 22 | + COMPARE_URL="" |
| 23 | +fi |
| 24 | + |
| 25 | +should_skip() { |
| 26 | + local subject="$1" |
| 27 | + local lower="${subject,,}" |
| 28 | + |
| 29 | + if [[ "$lower" =~ ^merge[[:space:]] ]]; then |
| 30 | + return 0 |
| 31 | + fi |
| 32 | + if [[ "$lower" =~ ^chore: ]]; then |
| 33 | + return 0 |
| 34 | + fi |
| 35 | + if [[ "$lower" =~ fix[[:space:]]gofmt ]]; then |
| 36 | + return 0 |
| 37 | + fi |
| 38 | + if [[ "$lower" =~ fix[[:space:]]linter ]]; then |
| 39 | + return 0 |
| 40 | + fi |
| 41 | + if [[ "$lower" =~ fix[[:space:]]ci[[:space:]]linter ]]; then |
| 42 | + return 0 |
| 43 | + fi |
| 44 | + if [[ "$lower" =~ ^align[[:space:]] ]]; then |
| 45 | + return 0 |
| 46 | + fi |
| 47 | + |
| 48 | + return 1 |
| 49 | +} |
| 50 | + |
| 51 | +commit_group() { |
| 52 | + local subject="$1" |
| 53 | + local lower="${subject,,}" |
| 54 | + |
| 55 | + if should_skip "$subject"; then |
| 56 | + echo "skip" |
| 57 | + return |
| 58 | + fi |
| 59 | + |
| 60 | + if [[ "$lower" =~ ^add[[:space:]] ]] || \ |
| 61 | + [[ "$lower" =~ ^feat: ]] || \ |
| 62 | + [[ "$lower" =~ ^feat[[:space:]] ]] || \ |
| 63 | + [[ "$lower" =~ ^enhancement: ]] || \ |
| 64 | + [[ "$subject" == *"(Closes #"* ]] || \ |
| 65 | + [[ "$subject" == *"(closes #"* ]]; then |
| 66 | + echo "enhancements" |
| 67 | + return |
| 68 | + fi |
| 69 | + |
| 70 | + if [[ "$lower" =~ ^fix[[:space:]] ]] || \ |
| 71 | + [[ "$lower" =~ ^fix: ]] || \ |
| 72 | + [[ "$lower" =~ ^bug: ]] || \ |
| 73 | + [[ "$lower" =~ ^patch: ]]; then |
| 74 | + echo "bug_fixes" |
| 75 | + return |
| 76 | + fi |
| 77 | + |
| 78 | + echo "other" |
| 79 | +} |
| 80 | + |
| 81 | +enhancement_lines=() |
| 82 | +bug_fix_lines=() |
| 83 | +other_lines=() |
| 84 | + |
| 85 | +while IFS= read -r subject; do |
| 86 | + [[ -z "$subject" ]] && continue |
| 87 | + case "$(commit_group "$subject")" in |
| 88 | + enhancements) enhancement_lines+=("$subject") ;; |
| 89 | + bug_fixes) bug_fix_lines+=("$subject") ;; |
| 90 | + other) other_lines+=("$subject") ;; |
| 91 | + esac |
| 92 | +done < <(git log --format=%s "$RANGE" 2>/dev/null || true) |
| 93 | + |
| 94 | +print_section() { |
| 95 | + local title="$1" |
| 96 | + shift |
| 97 | + if [[ $# -eq 0 ]]; then |
| 98 | + return |
| 99 | + fi |
| 100 | + printf '%s:\n\n' "$title" |
| 101 | + for line in "$@"; do |
| 102 | + printf -- '- %s\n' "$line" |
| 103 | + done |
| 104 | + printf '\n' |
| 105 | +} |
| 106 | + |
| 107 | +printf '# %s\n\n' "$NEW_TAG" |
| 108 | +printf 'Terraform provider for HTTP requests via Terraform. Install from the [Terraform Registry](https://registry.terraform.io/providers/devops-rob/terracurl/latest).\n\n' |
| 109 | + |
| 110 | +if [[ -n "$COMPARE_URL" ]]; then |
| 111 | + printf '**Full changelog:** [%s...%s](%s)\n\n' "$PREV_TAG" "$NEW_TAG" "$COMPARE_URL" |
| 112 | +fi |
| 113 | + |
| 114 | +printf '## %s\n\n' "$VERSION" |
| 115 | + |
| 116 | +print_section "ENHANCEMENTS" "${enhancement_lines[@]}" |
| 117 | +print_section "BUG FIXES" "${bug_fix_lines[@]}" |
| 118 | +print_section "OTHER" "${other_lines[@]}" |
| 119 | + |
| 120 | +if [[ ${#enhancement_lines[@]} -eq 0 && ${#bug_fix_lines[@]} -eq 0 && ${#other_lines[@]} -eq 0 ]]; then |
| 121 | + printf '_No categorized commits in range %s._\n' "$RANGE" |
| 122 | +fi |
0 commit comments