blob: f58d0a4589444fd42d984a8a87c08a02a3124c94 [file] [log] [blame]
name: Label Cherry Pick
on:
pull_request_target:
types:
- labeled
- closed
jobs:
prepare_branch_list:
runs-on: ubuntu-latest
if: |
github.event.pull_request.merged == true &&
github.event.pull_request.merge_commit_sha != null
outputs:
target_branch: ${{ steps.set-branches.outputs.target_branch }}
steps:
- name: Set Branches
id: set-branches
env:
PR_LABELS: ${{ toJson(github.event.pull_request.labels) }}
EVENT_ACTION: ${{ github.event.action }}
LABEL_NAME: ${{ github.event.label.name }}
BASE_REF: ${{ github.base_ref }}
run: |
if [[ $EVENT_ACTION == 'closed' ]]; then
# Get a list of the labels from the PR.
labels=$(echo "$PR_LABELS" | jq -r '.[].name')
else
# Or get the label that was added on the merged PR.
labels=$LABEL_NAME
fi
branches=("24.lts.1+" "23.lts.1+" "22.lts.1+" "21.lts.1+" "20.lts.1+" "19.lts.1+" "rc_11" "COBALT_9")
filtered_branches=()
for branch in "${branches[@]}"; do
if [[ $branch == $BASE_REF ]]; then
continue
fi
if [[ ${labels[@]} =~ "cp-$branch" ]]; then
filtered_branches+=("$branch")
fi
done
echo "target_branch=$(echo -n "${filtered_branches[@]}" | jq -cRs 'split(" ")')" >> $GITHUB_OUTPUT
cherry_pick:
runs-on: ubuntu-latest
permissions:
issues: write
needs: prepare_branch_list
if: needs.prepare_branch_list.outputs.target_branch != '[]'
strategy:
matrix:
target_branch: ${{ fromJson(needs.prepare_branch_list.outputs.target_branch) }}
env:
ACCESS_TOKEN: ${{ secrets.CHERRY_PICK_TOKEN }}
REPOSITORY: ${{ github.repository }}
GITHUB_REF: ${{ github.ref }}
MERGE_COMMIT_SHA: ${{ github.event.pull_request.merge_commit_sha }}
CHERRY_PICK_BRANCH: cherry-pick-${{ matrix.target_branch }}-${{ github.event.pull_request.number }}
steps:
- name: Checkout repository
uses: kaidokert/checkout@v3.5.999
timeout-minutes: 30
with:
ref: ${{ matrix.target_branch }}
fetch-depth: 0
persist-credentials: false
- name: Setup Git
run: |
git config --global user.name "GitHub Release Automation"
git config --global user.email "github@google.com"
- name: Cherry pick merge commit
id: cherry-pick
continue-on-error: true
run: |
set -x
git fetch origin ${{ matrix.target_branch }}
set +e
# Select the first parent as the mainline tree. This is necessary if
# the commit has multiple parents as the cherry-pick will fail otherwise.
git cherry-pick -x --mainline=1 ${MERGE_COMMIT_SHA}
RES=$?
set -e
if [ ${RES} -ne 0 ]; then
# If the cherry pick failed due to a merge conflict we can
# add the conflicting file and create the commit anyway.
git add .
git cherry-pick --continue
fi
exit ${RES}
- name: Create Pull Request
id: create-pr
continue-on-error: true
uses: peter-evans/create-pull-request@2b011faafdcbc9ceb11414d64d0573f37c774b04 # v4.2.3
with:
token: ${{ secrets.CHERRY_PICK_TOKEN }}
draft: ${{ steps.cherry-pick.outcome == 'failure' }}
base: ${{ matrix.target_branch }}
branch: ${{ env.CHERRY_PICK_BRANCH }}
committer: GitHub Release Automation <github@google.com>
reviewers: ${{ github.event.pull_request.user.login }}
title: "Cherry pick PR #${{ github.event.pull_request.number }}: ${{ github.event.pull_request.title }}"
body: |
Refer to the original PR: https://github.com/${{ github.repository }}/pull/${{ github.event.pull_request.number }}
${{ github.event.pull_request.body }}
- name: Comment on failure
uses: actions/github-script@v6
with:
github-token: ${{ secrets.CHERRY_PICK_TOKEN }}
script: |
if ('${{ steps.create-pr.outputs.pull-request-number }}' == '') {
// Comment on the originating PR if creating a cherry pick PR failed.
github.rest.issues.createComment({
issue_number: context.payload.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: '> [!IMPORTANT]\n> Creating the cherry pick PR failed! Check the log at ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} for details.'
});
} else if ('${{ steps.cherry-pick.outcome }}' == 'failure') {
// Comment on the new PR if the cherry pick failed.
github.rest.issues.createComment({
issue_number: '${{ steps.create-pr.outputs.pull-request-number }}',
owner: context.repo.owner,
repo: context.repo.repo,
body: '> [!IMPORTANT]\n> There were merge conflicts while cherry picking! Check out [${{ env.CHERRY_PICK_BRANCH }}](${{ github.repository }}/tree/${{ env.CHERRY_PICK_BRANCH }}) and fix the conflicts before proceeding. Check the log at ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} for details.'
});
}