Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[backport-gha] Testing GHA #11

Draft
wants to merge 2 commits into
base: backport-gha
Choose a base branch
from
Draft
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
42 changes: 42 additions & 0 deletions .github/workflows/backport.yaml
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"
95 changes: 95 additions & 0 deletions .github/workflows/scripts/backport.sh
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
Empty file added hi
Empty file.