Skip to content

Commit

Permalink
feat: auto cherry pick support multiple versions (#6894)
Browse files Browse the repository at this point in the history
  • Loading branch information
mintsweet authored Jan 30, 2024
1 parent 81e3149 commit 70165df
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 59 deletions.
7 changes: 6 additions & 1 deletion .github/actions/auto-cherry-pick/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
name: "Auto Cherry Pick"
description: "cherry pick commits from Pull Requests into Release branch"
inputs:
trigger_label_prefix:
description: "The trigger label prefic"
default: "needs-cherrypick-"
required: false
author_email:
description: "The author email address"
default: ${{ github.actor }}@users.noreply.github.com
Expand All @@ -41,7 +45,8 @@ runs:
GH_TOKEN: ${{ github.token }}
ORIGIN_PR_NUMBER: ${{ github.event.pull_request.number }}
ORIGIN_PR_TITLE: ${{ github.event.pull_request.title }}
ORIGIN_PR_LABELS_JSON: ${{ toJson(github.event.pull_request.labels.*.name) }}
ORIGIN_PR_LABELS_JSON: ${{ toJson(github.event.pull_request.labels.*.name) }}
TRIGER_LABEL_PREFIX: ${{ inputs.trigger_label_prefix }}
AUTHOR_EMAIL: ${{ inputs.author_email }}
AUTHOR_NAME: ${{ inputs.author_name }}
ASSIGNEES: ${{ inputs.assignees }}
105 changes: 47 additions & 58 deletions .github/actions/auto-cherry-pick/cherry-pick.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,81 +19,70 @@

set -e

ORIGIN_PR_LABELS=($(echo "$ORIGIN_PR_LABELS_JSON" | jq -r '.[]'))
ORIGIN_PR_LABELS=$(echo "$ORIGIN_PR_LABELS_JSON" | jq -r '.[]')

echo "::group::Origin Info"
echo "Origin PR Number: $ORIGIN_PR_NUMBER"
echo "Origin PR Title: $ORIGIN_PR_TITLE"
echo "Origin PR Labels: $ORIGIN_PR_LABELS"
echo "GitHub SHA: $GITHUB_SHA"
echo "Trigger Label Prefix: $TRIGER_LABEL_PREFIX"
echo "Author Email: $AUTHOR_EMAIL"
echo "Author Name: $AUTHOR_NAME"
echo "Assignees: $ASSIGNEES"
echo "::endgroup::"

TARGET_LABEL_PREFIX="needs-cherrypick-"
TARGET_LABEL=""

for label in "${ORIGIN_PR_LABELS[@]}"; do
if [[ "$label" == "$TARGET_LABEL_PREFIX"* ]]; then
TARGET_LABEL="$label"
break
fi
done

if [ -z "$TARGET_LABEL" ]; then
echo "No need cherry-pick."
exit 0
fi

TARGET_BRANCH="release-${TARGET_LABEL##*-}"
AUTO_CREATE_PR_BRANCH="$TARGET_BRANCH-auto-cherry-pick-$ORIGIN_PR_NUMBER"
AUTO_CHERRY_PICK_LABEL="bot/auto-cherry-pick"
AUTO_CHERRY_PICK_VERSION_LABEL="bot/auto-cherry-pick-for-$TARGET_BRANCH"
AUTO_CHERRY_PICK_FAILED_LABEL="bot/auto-cherry-pick-failed"
AUTO_CHERRY_PICK_COMPLETED_LABEL="bot/auto-cherry-pick-completed"

echo "::group::Generate Info"
echo "Target Branch: $TARGET_BRANCH"
echo "Auto Create PR Branch: $AUTO_CREATE_PR_BRANCH"
echo "Auto Cherry Pick Label: $AUTO_CHERRY_PICK_LABEL"
echo "Auto Cherry Pick Version Label: $AUTO_CHERRY_PICK_VERSION_LABEL"
echo "Auto Cherry Pick Failed Label: $AUTO_CHERRY_PICK_FAILED_LABEL"
echo "Auto Cherry Pick Completed Label: $AUTO_CHERRY_PICK_COMPLETED_LABEL"
echo "::endgroup::"
for label in ${ORIGIN_PR_LABELS[@]}; do
if [[ "$label" == "$TRIGER_LABEL_PREFIX"* ]]; then
TARGET_BRANCH="release-${label##*-}"
AUTO_CREATE_PR_BRANCH="$TARGET_BRANCH-auto-cherry-pick-$ORIGIN_PR_NUMBER"
AUTO_CHERRY_PICK_VERSION_LABEL="bot/auto-cherry-pick-for-$TARGET_BRANCH"

echo "::group::Git Cherry Pick"
git config --global user.email "$AUTHOR_EMAIL"
git config --global user.name "$AUTHOR_NAME"
echo "::group::Generate Info"
echo "Target Branch: $TARGET_BRANCH"
echo "Auto Create PR Branch: $AUTO_CREATE_PR_BRANCH"
echo "Auto Cherry Pick Version Label: $AUTO_CHERRY_PICK_VERSION_LABEL"
echo "::endgroup::"

git remote update
git fetch --all
git checkout -b $AUTO_CREATE_PR_BRANCH origin/$TARGET_BRANCH
git cherry-pick -m 1 $GITHUB_SHA || (
gh pr comment $ORIGIN_PR_NUMBER --body "🤖 The current file has a conflict, and the pr cannot be automatically created."
gh pr edit $ORIGIN_PR_NUMBER --add-label $AUTO_CHERRY_PICK_FAILED_LABEL
exit 1
)
git push origin $AUTO_CREATE_PR_BRANCH
echo "::endgroup::"
echo "::group::Git Cherry Pick"
git config --global user.email "$AUTHOR_EMAIL"
git config --global user.name "$AUTHOR_NAME"

echo "::group::GitHub Auto Create PR"
AUTO_CREATED_PR_LINK=$(gh pr create \
-B $TARGET_BRANCH \
-H $AUTO_CREATE_PR_BRANCH \
-t "cherry-pick #$ORIGIN_PR_NUMBER $ORIGIN_PR_TITLE" \
-b "cherry-pick #$ORIGIN_PR_NUMBER $ORIGIN_PR_TITLE" \
-a $ASSIGNEES)
git remote update
git fetch --all
git checkout -b $AUTO_CREATE_PR_BRANCH origin/$TARGET_BRANCH
git cherry-pick -m 1 $GITHUB_SHA || (
gh pr comment $ORIGIN_PR_NUMBER --body "🤖 The current file has a conflict, and the pr cannot be automatically created."
gh pr edit $ORIGIN_PR_NUMBER --add-label $AUTO_CHERRY_PICK_FAILED_LABEL
exit 1
)
git push origin $AUTO_CREATE_PR_BRANCH
echo "::endgroup::"

gh pr comment $ORIGIN_PR_NUMBER --body "🤖 cherry pick finished successfully 🎉!"
gh pr edit $ORIGIN_PR_NUMBER --add-label $AUTO_CHERRY_PICK_COMPLETED_LABEL || (
gh label create $AUTO_CHERRY_PICK_COMPLETED_LABEL -c "#0E8A16" -d "auto cherry pick completed"
gh pr edit $ORIGIN_PR_NUMBER --add-label $AUTO_CHERRY_PICK_COMPLETED_LABEL
)
echo "::group::GitHub Auto Create PR"
AUTO_CREATED_PR_LINK=$(gh pr create \
-B $TARGET_BRANCH \
-H $AUTO_CREATE_PR_BRANCH \
-t "cherry-pick #$ORIGIN_PR_NUMBER $ORIGIN_PR_TITLE" \
-b "cherry-pick #$ORIGIN_PR_NUMBER $ORIGIN_PR_TITLE" \
-a $ASSIGNEES)

gh pr comment $AUTO_CREATED_PR_LINK --body "🤖 this a auto create pr!cherry picked from #$ORIGIN_PR_NUMBER."
gh pr edit $AUTO_CREATED_PR_LINK --add-label "$AUTO_CHERRY_PICK_LABEL,$AUTO_CHERRY_PICK_VERSION_LABEL" || (
gh label create $AUTO_CHERRY_PICK_VERSION_LABEL -c "#5319E7" -d "auto cherry pick pr for $TARGET_BRANCH"
gh pr edit $AUTO_CREATED_PR_LINK --add-label "$AUTO_CHERRY_PICK_LABEL,$AUTO_CHERRY_PICK_VERSION_LABEL"
)
echo "::endgroup::"
gh pr comment $ORIGIN_PR_NUMBER --body "🤖 Target: #$TARGET_BRANCH cherry pick finished successfully 🎉!"
gh pr edit $ORIGIN_PR_NUMBER --add-label $AUTO_CHERRY_PICK_COMPLETED_LABEL || (
gh label create $AUTO_CHERRY_PICK_COMPLETED_LABEL -c "#0E8A16" -d "auto cherry pick completed"
gh pr edit $ORIGIN_PR_NUMBER --add-label $AUTO_CHERRY_PICK_COMPLETED_LABEL
)

gh pr comment $AUTO_CREATED_PR_LINK --body "🤖 this a auto create pr!cherry picked from #$ORIGIN_PR_NUMBER."
gh pr edit $AUTO_CREATED_PR_LINK --add-label "$AUTO_CHERRY_PICK_LABEL,$AUTO_CHERRY_PICK_VERSION_LABEL" || (
gh label create $AUTO_CHERRY_PICK_LABEL -c "#0E8A16" -d "auto cherry pick pr" -f
gh label create $AUTO_CHERRY_PICK_VERSION_LABEL -c "#5319E7" -d "auto cherry pick pr for $TARGET_BRANCH" -f
gh pr edit $AUTO_CREATED_PR_LINK --add-label "$AUTO_CHERRY_PICK_LABEL,$AUTO_CHERRY_PICK_VERSION_LABEL"
)
echo "::endgroup::"
fi
done

0 comments on commit 70165df

Please sign in to comment.