forked from rancher/webhook
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
name: Backport PR | ||
on: | ||
workflow_dispatch: | ||
inputs: | ||
pull_request: | ||
description: "Pull request number (eg: 123) or link to PR (eg: https://github.com/rancher/webhook/pull/123) " | ||
required: true | ||
cherry_pick: | ||
description: "If true, the squashed commit from the PR will be cherry-picked" | ||
type: boolean | ||
required: true | ||
default: "true" | ||
|
||
permissions: | ||
contents: read | ||
|
||
env: | ||
PULL_REQUEST: ${{ github.event.inputs.pull_request }} | ||
CHERRY_PICK: ${{ github.event.inputs.cherry_pick }} | ||
REPO: ${{ github.repository }} | ||
|
||
jobs: | ||
backport-pr: | ||
runs-on: ubuntu-latest | ||
permissions: | ||
contents: write | ||
pull-requests: write | ||
steps: | ||
- uses: actions/checkout@v4 | ||
with: | ||
path: webhook | ||
# depth: 0 | ||
|
||
- name: Run backport script | ||
env: | ||
GH_TOKEN: ${{ github.token }} | ||
run: | | ||
ref=$(echo "$GITHUB_REF" | sed "s|refs/heads/||") | ||
cd webhook | ||
git config --global user.email "webhook@example.com" | ||
git config --global user.name "tomleb webhook Bot" | ||
./.github/workflows/scripts/backport.sh "$PULL_REQUEST" "$ref" "$CHERRY_PICK" "$REPO" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#!/bin/sh | ||
|
||
set -e | ||
|
||
PULL_REQUEST=$1 | ||
TARGET_BRANCH=$2 | ||
CHERRY_PICK=$3 | ||
REPO=$4 | ||
|
||
if [ -z "$PULL_REQUEST" ] || [ -z "$TARGET_BRANCH" ] || [ -z "$CHERRY_PICK" ] || [ -z "$REPO" ]; then | ||
echo "Usage: $0 <PR number> <target branch> <true|false> <owner/repo>" 1>&2 | ||
exit 1 | ||
fi | ||
|
||
GITHUB_TRIGGERING_ACTOR=${GITHUB_TRIGGERING_ACTOR:-} | ||
|
||
repo_name=$(echo "$REPO" | cut -d/ -f2) | ||
repo_owner=$(echo "$REPO" | cut -d/ -f1) | ||
|
||
target_slug=$(echo "$TARGET_BRANCH" | sed "s|/|-|") | ||
branch_name="backport-$PULL_REQUEST-$target_slug-$$" | ||
|
||
pr_link=https://github.com/$REPO/pull/$PULL_REQUEST | ||
pr_number=$PULL_REQUEST | ||
|
||
# Separate calls because otherwise it was creating trouble.. can probably be fixed | ||
state=$(gh pr view "$pr_number" --json state --jq '.state') | ||
old_title=$(gh pr view "$pr_number" --json title --jq '.title') | ||
old_body=$(gh pr view "$pr_number" --json body --jq '.body') | ||
|
||
if [ $state != "MERGED" ]; then | ||
echo "PR $pr_number ($pr_link) not yet merged, cannot backport" 1>&2 | ||
exit 1 | ||
fi | ||
|
||
git fetch origin "$TARGET_BRANCH:$branch_name" | ||
git switch "$branch_name" | ||
|
||
committed_something="" | ||
|
||
if [ "$CHERRY_PICK" = "true" ]; then | ||
git fetch origin "pull/$pr_number/head:to-cherry-pick" | ||
commit=$(git rev-parse to-cherry-pick) | ||
if git cherry-pick --allow-empty "$commit"; then | ||
committed_something="true" | ||
else | ||
echo "Cherry-pick failed, skipping" 1>&2 | ||
git cherry-pick --abort | ||
fi | ||
fi | ||
|
||
if [ -z "$committed_something" ]; then | ||
# Github won't allow us to create a PR without any changes so we're making an empty commit here | ||
git commit --allow-empty -m "Please amend this commit" | ||
fi | ||
|
||
git push -u origin "$branch_name" | ||
|
||
generated_by="" | ||
if [ -n "$GITHUB_TRIGGERING_ACTOR" ]; then | ||
generated_by=$(cat <<EOF | ||
The workflow was triggered by @$GITHUB_TRIGGERING_ACTOR. | ||
EOF | ||
) | ||
fi | ||
|
||
title=$(echo "[$TARGET_BRANCH] $old_title") | ||
body=$(cat <<EOF | ||
**Backport** | ||
Backport of $pr_link | ||
You can make changes to this PR with the following command: | ||
\`\`\` | ||
git clone https://github.com/$REPO | ||
cd $repo_name | ||
git switch $branch_name | ||
\`\`\` | ||
$generated_by | ||
--- | ||
$old_body | ||
EOF | ||
) | ||
|
||
gh pr create \ | ||
--title "$title" \ | ||
--body "$body" \ | ||
--repo "$REPO" \ | ||
--head "$repo_owner:$branch_name" \ | ||
--base "$TARGET_BRANCH" \ | ||
--draft |