Skip to content

Commit c871cb7

Browse files
committed
Use gh pr list to reduce API requests
1 parent 3e6c295 commit c871cb7

File tree

4 files changed

+61
-27
lines changed

4 files changed

+61
-27
lines changed

scripts/release.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ old_version=${versions[0]}
7373

7474
log "Checking commit metadata for changes since $old_version..."
7575
# shellcheck source=scripts/release/check_commit_metadata.sh
76-
source "$SCRIPT_DIR/release/check_commit_metadata.sh" "$old_version..$ref"
76+
source "$SCRIPT_DIR/release/check_commit_metadata.sh" "$old_version" "$ref"
7777

7878
mapfile -d . -t version_parts <<<"$old_version"
7979
if [[ $minor == 1 ]] || [[ $COMMIT_METADATA_BREAKING == 1 ]]; then

scripts/release/check_commit_metadata.sh

Lines changed: 58 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#!/usr/bin/env bash
22

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>
55
#
6-
# Example: ./check_commit_tags.sh v0.13.1..971e3678
6+
# Example: ./check_commit_metadata.sh v0.13.1..971e3678
77
#
88
# When sourced, this script will populate the COMMIT_METADATA_* variables
99
# with the commit metadata for each commit in the revision range.
@@ -16,11 +16,17 @@ set -euo pipefail
1616
# shellcheck source=scripts/lib.sh
1717
source "$(dirname "${BASH_SOURCE[0]}")/../lib.sh"
1818

19-
range=${1:-}
19+
from_ref=${1:-}
20+
to_ref=${2:-}
2021

21-
if [[ -z $range ]]; then
22-
error "No revision range specified"
22+
if [[ -z $from_ref ]]; then
23+
error "No from_ref specified"
2324
fi
25+
if [[ -z $from_ref ]]; then
26+
error "No to_ref specified"
27+
fi
28+
29+
range="$from_ref..$to_ref"
2430

2531
# Check dependencies.
2632
dependencies gh
@@ -39,31 +45,59 @@ main() {
3945
breaking_label=release/breaking
4046
breaking_category=breaking
4147

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
4380

4481
for commit in "${commits[@]}"; do
4582
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
4891

4992
# Store the commit title for later use.
50-
title=${parts[*]:1}
93+
title=${parts[*]:2}
5194
title=${title%$'\n'}
52-
COMMIT_METADATA_TITLE[$commit_sha]=$title
95+
COMMIT_METADATA_TITLE[$commit_sha_short]=$title
5396

5497
# First, check the title for breaking changes. This avoids doing a
5598
# 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
67101
COMMIT_METADATA_BREAKING=1
68102
continue
69103
fi
@@ -73,10 +107,10 @@ main() {
73107
fi
74108
case $commit_prefix in
75109
feat | fix)
76-
COMMIT_METADATA_CATEGORY[$commit_sha]=$commit_prefix
110+
COMMIT_METADATA_CATEGORY[$commit_sha_short]=$commit_prefix
77111
;;
78112
*)
79-
COMMIT_METADATA_CATEGORY[$commit_sha]=other
113+
COMMIT_METADATA_CATEGORY[$commit_sha_short]=other
80114
;;
81115
esac
82116
done

scripts/release/generate_release_notes.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ if [[ -z $ref ]]; then
5959
fi
6060

6161
# shellcheck source=scripts/release/check_commit_metadata.sh
62-
source "$SCRIPT_DIR/release/check_commit_metadata.sh" "${old_version}..${ref}"
62+
source "$SCRIPT_DIR/release/check_commit_metadata.sh" "${old_version}" "${ref}"
6363

6464
# Sort commits by title prefix, then by date, only return sha at the end.
6565
mapfile -t commits < <(git log --no-merges --pretty=format:"%ct %h %s" "${old_version}..${ref}" | sort -k3,3 -k1,1n | cut -d' ' -f2)

scripts/release/publish.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ if [[ "$dry_run" == 1 ]]; then
107107
fi
108108

109109
# shellcheck source=scripts/release/check_commit_metadata.sh
110-
source "$SCRIPT_DIR/release/check_commit_metadata.sh" "$old_tag..$new_ref"
110+
source "$SCRIPT_DIR/release/check_commit_metadata.sh" "$old_tag" "$new_ref"
111111

112112
# Craft the release notes.
113113
release_notes="$(execrelative ./generate_release_notes.sh --old-version "$old_tag" --new-version "$new_tag" --ref "$new_ref")"

0 commit comments

Comments
 (0)