Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
72 changes: 66 additions & 6 deletions .github/workflows/back-port.yaml
Original file line number Diff line number Diff line change
@@ -1,22 +1,82 @@
name: Backport
# Runs on the merge commit rather than pull_request_target, which GitHub blocks by default
# in public repositories; the GITHUB_TOKEN of a push to this repo can open the backport PR.
on:
pull_request_target:
types:
- closed
push:
branches:
- main
- release-*

permissions:
contents: read

jobs:
# align with crossplane's choice https://github.com/crossplane/crossplane/blob/master/.github/workflows/backport.yml
open-pr:
runs-on: ubuntu-22.04
if: github.event.pull_request.merged
permissions:
contents: write
pull-requests: write
steps:
- name: Find merged PR
id: pr
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SHA: ${{ github.sha }}
BRANCH: ${{ github.ref_name }}
run: |
# The commit-to-PR association can lag the push by a few seconds. Matching the base
# branch keeps a new release branch, pushed at an already-merged commit, from
# backporting that PR again.
for attempt in 1 2 3; do
pr=$(gh api "repos/$GITHUB_REPOSITORY/commits/$SHA/pulls" |
jq -c --arg sha "$SHA" --arg branch "$BRANCH" \
'[.[] | select(.merge_commit_sha == $sha and .base.ref == $branch)][0] // empty')
[[ -n "$pr" ]] && break
if (( attempt < 3 )); then sleep $((attempt * 5)); fi
done
if [[ -z "$pr" ]]; then
echo "No pull request merged into $BRANCH as $SHA; nothing to backport"
exit 0
fi
number=$(jq -r .number <<<"$pr")
if jq -e '[.labels[].name | select(startswith("backport "))] | length > 0' <<<"$pr" >/dev/null; then
echo "number=$number" >> "$GITHUB_OUTPUT"
else
echo "#$number has no backport label"
fi

- name: Checkout
uses: actions/checkout@v4
if: steps.pr.outputs.number
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8
with:
fetch-depth: 0

# The DCO check needs a sign-off matching the author or committer. A squash merge
# records the author under their GitHub commit email, which can differ from the one
# they sign off with, so the backport is committed under the sign-off identity.
- name: Committer from sign-off
id: committer
if: steps.pr.outputs.number
env:
SHA: ${{ github.sha }}
run: |
re='^([^<>]+) <([^<>[:space:]]+@[^<>[:space:]]+)>[[:space:]]*$'
# The step runs under -eo pipefail; no valid sign-off means the fallback, not a failure.
line=$(git log -1 --format=%B "$SHA" | sed -n 's/^Signed-off-by: //p' | grep -E -m1 "$re" || true)
if [[ "$line" =~ $re ]]; then
echo "name=${BASH_REMATCH[1]}" >> "$GITHUB_OUTPUT"
echo "email=${BASH_REMATCH[2]}" >> "$GITHUB_OUTPUT"
else
echo "::warning::No usable Signed-off-by on the merged commit; committing as github-actions[bot]"
fi

- name: Open Backport PR
uses: korthout/backport-action@v3
if: steps.pr.outputs.number
uses: korthout/backport-action@8560fb503c275d433c56f05a2f64850093baa9f7 # v4.7.0
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
github_workspace: ${{ github.workspace }}
source_pr_number: ${{ steps.pr.outputs.number }}
git_committer_name: ${{ steps.committer.outputs.name || 'github-actions[bot]' }}
git_committer_email: ${{ steps.committer.outputs.email || '41898282+github-actions[bot]@users.noreply.github.com' }}
32 changes: 29 additions & 3 deletions .github/workflows/issue-commands.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ jobs:
backport:
runs-on: ubuntu-22.04
if: github.event.issue.pull_request && contains(github.event.comment.body, '/backport')
permissions:
contents: write
pull-requests: write
issues: write
steps:
- name: Extract Command
id: command
Expand All @@ -42,7 +46,7 @@ jobs:
reaction: "true"
reaction-type: "eyes"
allow-edits: "false"
permission-level: read
permission-level: write
- name: Handle Command
uses: actions/github-script@v7
env:
Expand All @@ -56,7 +60,7 @@ jobs:
label = "backport " + version
}
// Add our backport label.
github.issues.addLabels({
await github.rest.issues.addLabels({
// Every pull request is an issue, but not every issue is a pull request.
issue_number: context.issue.number,
owner: context.repo.owner,
Expand All @@ -68,8 +72,30 @@ jobs:
uses: actions/checkout@v4
with:
fetch-depth: 0
# Same sign-off committer as back-port.yml; the DCO check needs it to match.
- name: Committer from sign-off
id: committer
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR: ${{ github.event.issue.number }}
run: |
sha=$(gh pr view "$PR" -R "$GITHUB_REPOSITORY" --json mergeCommit --jq '.mergeCommit.oid // empty')
re='^([^<>]+) <([^<>[:space:]]+@[^<>[:space:]]+)>[[:space:]]*$'
line=""
if [[ -n "$sha" ]]; then
# The step runs under -eo pipefail; no valid sign-off means the fallback, not a failure.
line=$(git log -1 --format=%B "$sha" | sed -n 's/^Signed-off-by: //p' | grep -E -m1 "$re" || true)
fi
if [[ "$line" =~ $re ]]; then
echo "name=${BASH_REMATCH[1]}" >> "$GITHUB_OUTPUT"
echo "email=${BASH_REMATCH[2]}" >> "$GITHUB_OUTPUT"
else
echo "::warning::No usable Signed-off-by on the merged commit; committing as github-actions[bot]"
fi
- name: Open Backport PR
uses: zeebe-io/backport-action@v0.0.8
uses: korthout/backport-action@8560fb503c275d433c56f05a2f64850093baa9f7 # v4.7.0
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
github_workspace: ${{ github.workspace }}
git_committer_name: ${{ steps.committer.outputs.name || 'github-actions[bot]' }}
git_committer_email: ${{ steps.committer.outputs.email || '41898282+github-actions[bot]@users.noreply.github.com' }}
Loading