1
1
#! /usr/bin/env bash
2
2
3
- # Usage: source ./check_commit_tags .sh <revision range>
4
- # Usage: ./check_commit_tags .sh <revision range>
3
+ # Usage: source ./check_commit_metadata .sh <revision range>
4
+ # Usage: ./check_commit_metadata .sh <revision range>
5
5
#
6
- # Example: ./check_commit_tags .sh v0.13.1..971e3678
6
+ # Example: ./check_commit_metadata .sh v0.13.1..971e3678
7
7
#
8
8
# When sourced, this script will populate the COMMIT_METADATA_* variables
9
9
# with the commit metadata for each commit in the revision range.
@@ -16,11 +16,17 @@ set -euo pipefail
16
16
# shellcheck source=scripts/lib.sh
17
17
source " $( dirname " ${BASH_SOURCE[0]} " ) /../lib.sh"
18
18
19
- range=${1:- }
19
+ from_ref=${1:- }
20
+ to_ref=${2:- }
20
21
21
- if [[ -z $range ]]; then
22
- error " No revision range specified"
22
+ if [[ -z $from_ref ]]; then
23
+ error " No from_ref specified"
23
24
fi
25
+ if [[ -z $from_ref ]]; then
26
+ error " No to_ref specified"
27
+ fi
28
+
29
+ range=" $from_ref ..$to_ref "
24
30
25
31
# Check dependencies.
26
32
dependencies gh
@@ -39,31 +45,59 @@ main() {
39
45
breaking_label=release/breaking
40
46
breaking_category=breaking
41
47
42
- mapfile -t commits < <( git log --no-merges --pretty=format:" %h %s" " $range " )
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
43
80
44
81
for commit in " ${commits[@]} " ; do
45
82
mapfile -d ' ' -t parts <<< " $commit"
46
- commit_sha=${parts[0]}
47
- commit_prefix=${parts[1]}
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
48
91
49
92
# Store the commit title for later use.
50
- title=${parts[*]: 1 }
93
+ title=${parts[*]: 2 }
51
94
title=${title% $' \n ' }
52
- COMMIT_METADATA_TITLE[$commit_sha ]=$title
95
+ COMMIT_METADATA_TITLE[$commit_sha_short ]=$title
53
96
54
97
# First, check the title for breaking changes. This avoids doing a
55
98
# GH API request if there's a match.
56
- if [[ $commit_prefix =~ $breaking_title ]]; then
57
- COMMIT_METADATA_CATEGORY[$commit_sha ]=$breaking_category
58
- COMMIT_METADATA_BREAKING=1
59
- continue
60
- fi
61
-
62
- # Get the labels for the PR associated with this commit.
63
- mapfile -t labels < <( gh api -H " Accept: application/vnd.github+json" " /repos/coder/coder/commits/${commit_sha} /pulls" -q ' .[].labels[].name' )
64
-
65
- if [[ " ${labels[*]} " = * " ${breaking_label} " * ]]; then
66
- COMMIT_METADATA_CATEGORY[$commit_sha ]=$breaking_category
99
+ if [[ $commit_prefix =~ $breaking_title ]] || [[ ${labels[$commit_sha_long]} = * " label:$breaking_label " * ]]; then
100
+ COMMIT_METADATA_CATEGORY[$commit_sha_short ]=$breaking_category
67
101
COMMIT_METADATA_BREAKING=1
68
102
continue
69
103
fi
@@ -73,10 +107,10 @@ main() {
73
107
fi
74
108
case $commit_prefix in
75
109
feat | fix)
76
- COMMIT_METADATA_CATEGORY[$commit_sha ]=$commit_prefix
110
+ COMMIT_METADATA_CATEGORY[$commit_sha_short ]=$commit_prefix
77
111
;;
78
112
* )
79
- COMMIT_METADATA_CATEGORY[$commit_sha ]=other
113
+ COMMIT_METADATA_CATEGORY[$commit_sha_short ]=other
80
114
;;
81
115
esac
82
116
done
0 commit comments