Skip to content

Commit c3f3d2f

Browse files
committed
fix: deduplicate --help by emitting header comments, add GITHUB_REPOSITORY validation
- Replace duplicated --help heredocs with awk extraction of the header comment block in both scripts, eliminating content drift. - Add GITHUB_REPOSITORY to required env var validation in extract-hotfix-versions.sh (was used with set -u but not validated). - Pass --repo to 'gh pr list' so it works without a .git directory. - Set GITHUB_REPOSITORY in bats test setup() for deterministic runs. - Update test assertions to match uppercase header comment headings.
1 parent dd13252 commit c3f3d2f

4 files changed

Lines changed: 18 additions & 52 deletions

File tree

.github/scripts/cherry-pick-to-release.sh

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -66,29 +66,9 @@ set -euo pipefail
6666

6767
# -- Runtime help -------------------------------------------------------------
6868
if [[ "${1:-}" == "--help" || "${1:-}" == "-h" ]]; then
69-
cat <<'EOF'
70-
Usage: cherry-pick-to-release.sh
71-
72-
Cherry-picks a merge commit into a release branch and opens a PR.
73-
74-
Required environment variables:
75-
VERSION Hotfix version (e.g. "7.0.1")
76-
MERGE_COMMIT_SHA SHA of the merge commit
77-
PR_NUMBER Original PR number
78-
PR_TITLE Original PR title
79-
GH_TOKEN GitHub token
80-
GITHUB_REPOSITORY owner/repo (set by Actions)
81-
82-
Behavior:
83-
- Derives release branch from major.minor (7.0.1 → release/7.0)
84-
- Skips if patch is already applied on the target branch
85-
- Creates a clean PR on success, or a CONFLICTS PR on failure
86-
- Milestone is set if it exists; otherwise a warning is logged
87-
88-
Exit codes:
89-
0 Success (including "already applied" — no PR created)
90-
1 Error (e.g. version parse failure)
91-
EOF
69+
# Print the header comment block (between the license banner and the
70+
# closing banner), stripping the leading '# ' prefix.
71+
awk '/^#{2,}$/ { n++; next } n == 2 { sub(/^# ?/, ""); print }' "$0"
9272
exit 0
9373
fi
9474

.github/scripts/extract-hotfix-versions.sh

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@
2626
#
2727
# REQUIRED ENVIRONMENT VARIABLES
2828
# ------------------------------
29-
# LABELS Comma-separated list of all label names on the PR.
30-
# EVENT_ACTION The GitHub event action: "closed" or "labeled".
31-
# EVENT_LABEL For 'labeled' events, the name of the label that was added.
32-
# Empty or unset for 'closed' events.
33-
# PR_NUMBER The pull request number (used to derive cherry-pick branch names).
34-
# GH_TOKEN GitHub token for API calls (gh CLI auth).
29+
# LABELS Comma-separated list of all label names on the PR.
30+
# EVENT_ACTION The GitHub event action: "closed" or "labeled".
31+
# EVENT_LABEL For 'labeled' events, the name of the label that was added.
32+
# Empty or unset for 'closed' events.
33+
# PR_NUMBER The pull request number (used to derive cherry-pick branch names).
34+
# GH_TOKEN GitHub token for API calls (gh CLI auth).
35+
# GITHUB_REPOSITORY Owner/repo (e.g. "dotnet/SqlClient"). Set automatically by Actions.
3536
#
3637
# OUTPUTS
3738
# -------
@@ -58,33 +59,17 @@ set -euo pipefail
5859

5960
# -- Runtime help -------------------------------------------------------------
6061
if [[ "${1:-}" == "--help" || "${1:-}" == "-h" ]]; then
61-
cat <<'EOF'
62-
Usage: extract-hotfix-versions.sh
63-
64-
Parses "Hotfix X.Y.Z" labels from a GitHub PR and emits a JSON array for
65-
matrix fan-out.
66-
67-
Required environment variables:
68-
LABELS Comma-separated label names on the PR
69-
EVENT_ACTION "closed" or "labeled"
70-
EVENT_LABEL The label just added (labeled events only)
71-
PR_NUMBER PR number
72-
GH_TOKEN GitHub token for gh CLI
73-
74-
Output (written to $GITHUB_OUTPUT):
75-
versions=["7.0.1","8.0.0"]
76-
77-
Exit codes:
78-
0 Success (including "nothing to do" — versions=[])
79-
1 Error (e.g. no valid Hotfix labels on a 'closed' event)
80-
EOF
62+
# Print the header comment block (between the license banner and the
63+
# closing banner), stripping the leading '# ' prefix.
64+
awk '/^#{2,}$/ { n++; next } n == 2 { sub(/^# ?/, ""); print }' "$0"
8165
exit 0
8266
fi
8367

8468
# -- Input validation ---------------------------------------------------------
8569
: "${LABELS:?LABELS environment variable is required}"
8670
: "${EVENT_ACTION:?EVENT_ACTION environment variable is required}"
8771
: "${PR_NUMBER:?PR_NUMBER environment variable is required}"
72+
: "${GITHUB_REPOSITORY:?GITHUB_REPOSITORY environment variable is required}"
8873

8974
# -- 'labeled' event: process only the newly added label ----------------------
9075
if [[ "${EVENT_ACTION}" == "labeled" ]]; then
@@ -114,7 +99,7 @@ if [[ "${EVENT_ACTION}" == "labeled" ]]; then
11499
exit 0
115100
fi
116101

117-
EXISTING_PR=$(gh pr list --head "${CHERRY_PICK_BRANCH}" --state all \
102+
EXISTING_PR=$(gh pr list --repo "${GITHUB_REPOSITORY}" --head "${CHERRY_PICK_BRANCH}" --state all \
118103
--json number --jq 'length')
119104
if [[ "${EXISTING_PR}" -gt 0 ]]; then
120105
echo "A cherry-pick PR from '${CHERRY_PICK_BRANCH}' already exists. Skipping."

.github/scripts/tests/cherry-pick-to-release.bats

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ STUB
7070
run bash "${SCRIPT}" --help
7171
[ "$status" -eq 0 ]
7272
[[ "$output" == *"Cherry-picks a merge commit"* ]]
73-
[[ "$output" == *"Required environment variables"* ]]
73+
[[ "$output" == *"REQUIRED ENVIRONMENT VARIABLES"* ]]
7474
}
7575

7676
@test "prints help text with -h" {

.github/scripts/tests/extract-hotfix-versions.bats

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ setup() {
2828
export EVENT_LABEL=""
2929
export PR_NUMBER="42"
3030
export GH_TOKEN="fake-token"
31+
export GITHUB_REPOSITORY="dotnet/SqlClient"
3132
}
3233

3334
teardown() {
@@ -45,7 +46,7 @@ get_versions() {
4546
run bash "${SCRIPT}" --help
4647
[ "$status" -eq 0 ]
4748
[[ "$output" == *"Parses"* ]]
48-
[[ "$output" == *"Required environment variables"* ]]
49+
[[ "$output" == *"REQUIRED ENVIRONMENT VARIABLES"* ]]
4950
}
5051

5152
@test "prints help text with -h" {

0 commit comments

Comments
 (0)