Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
028663d
Extend hotfix label automation to cover issues
mdaigle Sep 23, 2026
5a238d0
fix(cherry-pick-to-release): gh api --jq doesn't support jq's --arg
mdaigle Sep 23, 2026
a2f424e
Address review feedback: cross-repo issue filtering, duplicate-guard …
mdaigle Sep 23, 2026
3661b11
hotfix-label-issue.yml: serialize runs per issue+label to close a che…
mdaigle Sep 23, 2026
12e25f7
sync-hotfix-label-to-pr.sh: don't swallow gh issue view failures
mdaigle Sep 23, 2026
c69ee76
Add hotfix label automation diagram doc
mdaigle Sep 23, 2026
dcfabf6
Link every closing issue's backport, not just the first
mdaigle Sep 23, 2026
4f6de72
Refine hotfix automation diagram
mdaigle Sep 23, 2026
b68b931
Move hotfix automation doc out of .github/workflows
mdaigle Sep 23, 2026
5f9a299
Fix cut-off/wrapped labels in hotfix automation diagram
mdaigle Sep 23, 2026
524cf81
Remove hotfix automation diagram doc
mdaigle Sep 23, 2026
a64d78a
Require [VERSION] title prefix when matching backport sub-issue
mdaigle Sep 23, 2026
d88aa47
Explicitly close backport issues on release-branch PR merge
mdaigle Sep 23, 2026
6de6dba
Fix duplicate-detection false positive, PR label permissions, and doc…
mdaigle Sep 23, 2026
052b8f0
Fix backport-issue closing to use an explicit marker, not closingIssu…
mdaigle Sep 23, 2026
447f2b6
Clarify comments: close-backport-issue job scope and duplicate-guard …
mdaigle Sep 23, 2026
ce8ba9f
Fix cherry-pick automation for retroactively-labeled, already-merged PRs
mdaigle Sep 23, 2026
f4d58fb
Fix PR-title injection risk and backport-issue lookup race
mdaigle Sep 23, 2026
12f9c6a
Restrict close-backport-issue.yml to same-repo automation branches
mdaigle Sep 23, 2026
fb6322f
Serialize cherry-pick-hotfix.yml runs per PR number
mdaigle Sep 23, 2026
6530110
Merge main into hotfix-label automation branch
mdaigle Oct 5, 2026
c12bbfa
Address hotfix automation review findings
mdaigle Oct 5, 2026
2996890
Pipe issue PR lookup through jq
mdaigle Oct 5, 2026
cfd56ec
Fix backport PR merge state lookup
mdaigle Oct 23, 2026
6322a86
Fix merged PR dispatch reconciliation
mdaigle Oct 23, 2026
b6425ed
Check out dispatch helper before use
mdaigle Oct 23, 2026
e4ffac4
Recover backport issues from CLI bot identity
mdaigle Oct 23, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 148 additions & 1 deletion .github/scripts/cherry-pick-to-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@
# 5. Milestone lookup is best-effort. If the milestone doesn't exist yet
# the PR is created without one and a warning note is added to the body.
#
# 6. Backport issue lookup is also best-effort: if the original PR's
# closing keywords reference an issue, and that issue has a sub-issue
# milestoned VERSION (the backport issue created by
# create-backport-issue.sh when the issue was labeled
# "Hotfix <version>"), a human-readable "Fixes #<backport-issue>" line
# is appended to the cherry-pick PR body for visibility. Because the
# cherry-pick PR targets a release branch (not the default branch),
# GitHub does not parse closing keywords into 'closingIssuesReferences'
# for it at all (that's populated only for keywords on default-branch
# PRs, or issues manually linked via the PR's "Development" sidebar), so
# the "Fixes #N" text alone would never let close-backport-issue.yml
# find the issue to close. A machine-readable
# "<!-- backport-issue-numbers: N N ... -->" marker is also embedded in
# the body; close-backport-issue.sh reads that marker directly instead
# of relying on 'closingIssuesReferences'.
#
# REQUIRED ENVIRONMENT VARIABLES
# ------------------------------
# VERSION Full hotfix version, e.g. "7.0.1".
Expand Down Expand Up @@ -154,6 +170,134 @@ lookup_milestone() {
fi
}

# -- Helper: look up the backport issue ---------------------------------------
# Best-effort, mirroring lookup_milestone. If the original PR's closing
# keywords reference an issue, and that issue has a sub-issue milestoned
# VERSION (the backport issue created by create-backport-issue.sh when the
# issue was labeled "Hotfix <version>"), a "Fixes #<backport-issue>" line is
# appended to the cherry-pick PR body for human visibility, and the issue
# number is also recorded in a "<!-- backport-issue-numbers: ... -->" marker.
# Because this PR targets a release branch rather than the default branch,
# GitHub does not populate 'closingIssuesReferences' for closing keywords in
# its body at all (only default-branch keyword references and manually
# sidebar-linked issues appear there), so the "Fixes #N" text by itself gives
# close-backport-issue.yml nothing to find. The marker is the only reliable
# record of which issue(s) to close once this PR merges.
# If nothing matches, this is silently skipped — it's a convenience, not a
# requirement.
# create-backport-issue.sh (creates that sub-issue) and this cherry-pick can
# run concurrently, both triggered by the same "Hotfix <version>" label event
# — most directly when sync-hotfix-label-from-issue.sh dispatches
# cherry-pick-hotfix.yml in "reconcile" mode for an already-merged PR at the
# same time hotfix-label-issue.yml is creating the backport issue. To avoid
# a false "nothing to find" from that race, a missing sub-issue is retried a
# few times (with a short delay) as long as the parent issue still carries
# the label that would have triggered its creation.
lookup_backport_issue() {
local version="$1"
BACKPORT_ISSUE_NOTE=""
local backport_issue_numbers=""

# closingIssuesReferences can include issues from other repositories (e.g.
# "Fixes owner/other#123"). Only consider references in this repository —
# a bare '.number' would otherwise let a cross-repo reference collide with
# an unrelated local issue of the same number. 'gh ... --jq' takes a single
# query string (no jq '--arg' passthrough — see note above), so the repo
# is escaped and interpolated directly.
local repo_escaped
repo_escaped=$(printf '%s' "${GITHUB_REPOSITORY}" | sed 's/["\\]/\\&/g')

local closing_issues
closing_issues=$(gh pr view "${PR_NUMBER}" --repo "${GITHUB_REPOSITORY}" \
--json closingIssuesReferences \
--jq ".closingIssuesReferences[] | select((.repository.owner.login + \"/\" + .repository.name) == \"${repo_escaped}\") | .number" \
2>/dev/null || true)

if [[ -z "${closing_issues}" ]]; then
return
fi

# 'gh api --jq' takes a single query string and does not support jq's
# '--arg' for safe variable injection, so the version is escaped and
# interpolated directly into the filter expression.
local version_escaped
version_escaped=$(printf '%s' "${version}" | sed 's/["\\]/\\&/g')

# A single PR can close multiple Hotfix-labeled parent issues for the same
# release (e.g. two related bugs fixed together); accumulate a "Fixes" line
# for every parent that has a matching backport issue instead of stopping
# at the first match, so the cherry-pick PR closes all of them.
#
# A parent issue's sub-issues can include unrelated children milestoned to
# the same release (e.g. a separate follow-up task), so matching on
# milestone alone can pick the wrong one. create-backport-issue.sh always
# titles the backport issue "[VERSION] <parent title>", so also require
# that deterministic prefix to identify the right sub-issue.
# create-backport-issue.sh (triggered by the same "Hotfix <version>" label
# on the parent issue) runs concurrently with this cherry-pick, not before
# it — there's no ordering guarantee between the two. If the parent issue
# currently carries the label but its backport sub-issue hasn't shown up
# yet, retry briefly rather than silently missing it on the first look.
# A handful of short retries comfortably covers that workflow's runtime
# without meaningfully delaying the (far more common) case where no
# backport issue is expected at all.
local -r backport_lookup_max_attempts=6
local -r backport_lookup_retry_delay_seconds=10

local issue_number backport_number match_count attempt issue_labels
while IFS= read -r issue_number; do
[[ -z "${issue_number}" ]] && continue

for (( attempt = 1; attempt <= backport_lookup_max_attempts; attempt++ )); do
backport_number=$(gh api "repos/${GITHUB_REPOSITORY}/issues/${issue_number}/sub_issues" \
--paginate --jq ".[] | select(.milestone.title == \"${version_escaped}\" and (.title | startswith(\"[${version_escaped}] \"))) | .number" \
2>/dev/null || true)
match_count=$(grep -c . <<< "${backport_number}" || true)
backport_number=$(head -n1 <<< "${backport_number}")

if [[ -n "${backport_number}" ]]; then
break
fi

# Nothing found yet. Only worth retrying if the parent issue still
# carries the label that would have triggered create-backport-issue.sh
# in the first place — otherwise no backport issue is expected and
# retrying would just waste time.
issue_labels=$(gh api "repos/${GITHUB_REPOSITORY}/issues/${issue_number}" \
--jq '.labels[].name' 2>/dev/null || true)
if ! grep -qx "Hotfix ${version}" <<< "${issue_labels}"; then
break
fi

if [[ "${attempt}" -lt "${backport_lookup_max_attempts}" ]]; then
echo "No backport issue found yet for #${issue_number} (milestone '${version}')," \
"but the 'Hotfix ${version}' label is present; retrying" \
"(attempt ${attempt}/${backport_lookup_max_attempts})..."
sleep "${backport_lookup_retry_delay_seconds}"
fi
done

if [[ "${match_count}" -gt 1 ]]; then
echo "::warning::Issue #${issue_number} has multiple sub-issues titled" \
"'[${version}] ...' milestoned '${version}'; using the first (#${backport_number})."
fi

if [[ -n "${backport_number}" ]]; then
echo "Found backport issue #${backport_number} (sub-issue of #${issue_number}, milestone '${version}')."
BACKPORT_ISSUE_NOTE+=$'\n\nFixes #'"${backport_number}"
Comment thread
mdaigle marked this conversation as resolved.
Resolved
backport_issue_numbers+="${backport_number} "
fi
done <<< "${closing_issues}"

# Prepend the machine-readable marker (if any backport issues were found)
# so close-backport-issue.sh can recover the issue numbers without relying
# on GitHub's closing-keyword parsing, which doesn't run for non-default-
# branch PRs. Kept on its own HTML-comment line so it never renders.
if [[ -n "${backport_issue_numbers}" ]]; then
BACKPORT_ISSUE_NOTE=$'\n\n<!-- backport-issue-numbers: '"${backport_issue_numbers}"'-->'"${BACKPORT_ISSUE_NOTE}"
fi
}

# -- Step 4: Attempt the cherry-pick ------------------------------------------
# Options (--mainline) must precede the commit operand.
if git cherry-pick ${MAINLINE_FLAG} "${MERGE_COMMIT_SHA}"; then
Expand All @@ -162,12 +306,13 @@ if git cherry-pick ${MAINLINE_FLAG} "${MERGE_COMMIT_SHA}"; then
git push origin "${CHERRY_PICK_BRANCH}"

lookup_milestone "${VERSION}"
lookup_backport_issue "${VERSION}"

gh pr create \
--base "${TARGET_BRANCH}" \
--head "${CHERRY_PICK_BRANCH}" \
--title "[${VERSION} Cherry-pick] ${PR_TITLE}" \
--body "Cherry-pick of #${PR_NUMBER} (${MERGE_COMMIT_SHA}) into \`${TARGET_BRANCH}\`.${MILESTONE_NOTE}" \
--body "Cherry-pick of #${PR_NUMBER} (${MERGE_COMMIT_SHA}) into \`${TARGET_BRANCH}\`.${MILESTONE_NOTE}${BACKPORT_ISSUE_NOTE}" \
${MILESTONE_ARG}
else
# --- Conflict path ---
Expand All @@ -192,13 +337,15 @@ else
git push origin "${CHERRY_PICK_BRANCH}"

lookup_milestone "${VERSION}"
lookup_backport_issue "${VERSION}"

# Build the PR body using printf to avoid quoting pitfalls with embedded
# newlines (mixed $'...' and '...' quoting can leave literal \n in output).
CONFLICT_BODY="$(printf '%s' \
"Cherry-pick of #${PR_NUMBER} (${MERGE_COMMIT_SHA}) into " \
"\`${TARGET_BRANCH}\` **failed due to merge conflicts**." \
"${MILESTONE_NOTE}" \
"${BACKPORT_ISSUE_NOTE}" \
$'\n\nPlease resolve manually:\n```bash\n' \
"git fetch origin" \
$'\n' \
Expand Down
122 changes: 122 additions & 0 deletions .github/scripts/close-backport-issue.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#!/usr/bin/env bash
#################################################################################
# Licensed to the .NET Foundation under one or more agreements. #
# The .NET Foundation licenses this file to you under the MIT license. #
# See the LICENSE file in the project root for more information. #
#################################################################################
#
# close-backport-issue.sh
#
# Explicitly closes the backport issue(s) that a cherry-pick PR (created by
# cherry-pick-to-release.sh) is meant to close, after that PR merges into a
# non-default branch (e.g. a release/X.Y branch).
#
# WHY THIS EXISTS
# ----------------
# GitHub only auto-closes an issue referenced by closing keywords (Fixes,
# Closes, Resolves) when the referencing PR merges into the repository's
# *default* branch — and for PRs targeting any other branch, it doesn't even
# populate 'closingIssuesReferences' for keyword references (only issues
# manually linked via the PR's "Development" sidebar show up there). The
# cherry-pick PRs created by cherry-pick-to-release.sh always target a
# release/X.Y branch, so their "Fixes #<backport issue>" text is purely
# cosmetic and gives the GitHub API nothing to report. This script closes the
# gap by reading the "<!-- backport-issue-numbers: ... -->" marker that
# lookup_backport_issue() (in cherry-pick-to-release.sh) embeds in the PR
# body, and closing each issue named there explicitly. Using an explicit
# marker (rather than any closing keyword found in the PR body) also means
# this only ever closes issues our own automation created and recorded, never
# an issue a human happens to reference with "Fixes #N" in an unrelated,
# manually-authored release-branch PR.
#
# OVERVIEW
# --------
# 1. Read the merged PR's body and extract the issue numbers recorded in
# its "<!-- backport-issue-numbers: ... -->" marker, if any.
#
# 2. For each such issue that is still open, close it with a comment
# linking back to the merged PR.
#
# This is a no-op (and not an error) when the PR has no marker, or when every
# referenced issue is already closed.
#
# REQUIRED ENVIRONMENT VARIABLES
# ------------------------------
# PR_NUMBER The pull request number that just merged.
# GH_TOKEN GitHub token for 'gh' CLI authentication.
# GITHUB_REPOSITORY Owner/repo (e.g. "dotnet/SqlClient"). Set by Actions.
#
# OUTPUTS
# -------
# Closes zero or more issues in this repository.
#
# USAGE
# Called from the close-backport-issue.yml workflow. Can also be run
# locally:
#
# export PR_NUMBER=4750
# export GH_TOKEN="ghp_..."
# export GITHUB_REPOSITORY="dotnet/SqlClient"
# bash .github/scripts/close-backport-issue.sh
#
#################################################################################
set -euo pipefail

# -- Runtime help -------------------------------------------------------------
if [[ "${1:-}" == "--help" || "${1:-}" == "-h" ]]; then
# Print the header comment block (between the license banner and the
# closing banner), stripping the leading '# ' prefix.
awk '/^#{2,}$/ { n++; next } n == 2 { sub(/^# ?/, ""); print }' "$0"
exit 0
fi

# -- Input validation ---------------------------------------------------------
: "${PR_NUMBER:?PR_NUMBER environment variable is required}"
: "${GITHUB_REPOSITORY:?GITHUB_REPOSITORY environment variable is required}"

echo "Pull request: #${PR_NUMBER}"

# -- Step 1: Extract backport issue numbers from the PR body's marker --------
PR_BODY=$(gh pr view "${PR_NUMBER}" --repo "${GITHUB_REPOSITORY}" \
--json body --jq '.body')

# The marker line looks like: <!-- backport-issue-numbers: 123 456 -->
MARKER_LINE=$(grep -o '<!-- *backport-issue-numbers:[0-9 ]*-->' <<< "${PR_BODY}" || true)

if [[ -z "${MARKER_LINE}" ]]; then
echo "No backport-issue-numbers marker found on #${PR_NUMBER}. Nothing to close."
exit 0
fi

CLOSING_ISSUES=$(grep -o '[0-9]\+' <<< "${MARKER_LINE}")

echo "Backport issue numbers: ${CLOSING_ISSUES}"

# -- Step 2: Close each referenced issue that is still open -------------------
CLOSED_ANY=0
while IFS= read -r issue_number; do
[[ -z "${issue_number}" ]] && continue
Comment thread
mdaigle marked this conversation as resolved.
Resolved

# A real API/permission failure here must not be swallowed and mistaken for
# "issue doesn't exist" — surface it loudly instead of silently skipping.
if ! issue_state=$(gh issue view "${issue_number}" --repo "${GITHUB_REPOSITORY}" \
--json state --jq '.state'); then
echo "::error::Failed to look up state for issue #${issue_number}." >&2
exit 1
fi

if [[ "${issue_state}" != "OPEN" ]]; then
echo "Issue #${issue_number} is already $(tr '[:upper:]' '[:lower:]' <<< "${issue_state}"). Skipping."
continue
fi

echo "Closing issue #${issue_number} (referenced by merged PR #${PR_NUMBER})."
gh issue close "${issue_number}" --repo "${GITHUB_REPOSITORY}" \
--reason completed \
--comment "Closed by #${PR_NUMBER}, which merged into a non-default branch. GitHub only auto-closes issues for PRs merged into the default branch, so this was closed explicitly."
CLOSED_ANY=1
done <<< "${CLOSING_ISSUES}"

if [[ "${CLOSED_ANY}" -eq 0 ]]; then
echo "All referenced issues were already closed."
fi
Loading
Loading