You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devlake.apache.org by mi...@apache.org on 2023/08/08 10:05:00 UTC

[incubator-devlake] 01/01: fix: incorrect trigger timing

This is an automated email from the ASF dual-hosted git repository.

mintsweet pushed a commit to branch fix-action-trigger
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git

commit ed0eeb206f40cdbf0a648f2961f612caea92d35d
Author: mintsweet <0x...@gmail.com>
AuthorDate: Tue Aug 8 22:04:47 2023 +1200

    fix: incorrect trigger timing
---
 .github/actions/auto-cherry-pick/action.yml     |  6 +--
 .github/actions/auto-cherry-pick/cherry-pick.sh | 70 +++++++++++++++++--------
 .github/workflows/auto-cherry-pick.yml          |  2 +-
 3 files changed, 51 insertions(+), 27 deletions(-)

diff --git a/.github/actions/auto-cherry-pick/action.yml b/.github/actions/auto-cherry-pick/action.yml
index 9874ae1fd..c5549bca3 100644
--- a/.github/actions/auto-cherry-pick/action.yml
+++ b/.github/actions/auto-cherry-pick/action.yml
@@ -39,9 +39,9 @@ runs:
       shell: bash
       env:
         GH_TOKEN: ${{ github.token }}
-        PR_NUMBER: ${{ github.event.pull_request.number }}
-        PR_TITLE: ${{ github.event.pull_request.title }}
-        LABEL_NAME: ${{ github.event.label.name }}
+        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) }}
         AUTHOR_EMAIL: ${{ inputs.author_email }}
         AUTHOR_NAME: ${{ inputs.author_name }}
         ASSIGNEES: ${{ inputs.assignees }}
diff --git a/.github/actions/auto-cherry-pick/cherry-pick.sh b/.github/actions/auto-cherry-pick/cherry-pick.sh
index 484bd7e37..095fe5965 100644
--- a/.github/actions/auto-cherry-pick/cherry-pick.sh
+++ b/.github/actions/auto-cherry-pick/cherry-pick.sh
@@ -19,23 +19,47 @@
 
 set -e
 
-TARGET_BRANCH="release-${LABEL_NAME##*-}"
-PR_BRANCH="$TARGET_BRANCH-auto-cherry-pick-$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"
+ORIGIN_PR_LABELS=($(echo "$ORIGIN_PR_LABELS_JSON" | jq -r '.[]'))
 
-echo "::group::Basic Info"
-echo "PR Number: $PR_NUMBER"
-echo "PR Title: $PR_TITLE"
-echo "Label: $LABEL_NAME"
+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 "Author Email: $AUTHOR_EMAIL"
 echo "Author Name: $AUTHOR_NAME"
 echo "Assignees: $ASSIGNEES"
+echo "::endgroup::"
+
+TARGET_LABEL_PREFIX="needs-cherry-pick-"
+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 "PR Branch: $PR_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::"
 
 echo "::group::Git Cherry Pick"
@@ -44,30 +68,30 @@ git config --global user.name "$AUTHOR_NAME"
 
 git remote update
 git fetch --all
-git checkout -b $PR_BRANCH origin/$TARGET_BRANCH
-git cherry-pick $GITHUB_SHA || (
-	gh pr comment $PR_NUMBER --body "🤖 The current file has a conflict, and the pr cannot be automatically created."
-	gh pr edit $PR_NUMBEr --add-label $AUTO_CHERRY_PICK_FAILED_LABEL
+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 $PR_BRANCH
+git push origin $AUTO_CREATE_PR_BRANCH
 echo "::endgroup::"
 
 echo "::group::GitHub Auto Create PR"
 AUTO_CREATED_PR_LINK=$(gh pr create \
 	-B $TARGET_BRANCH \
-	-H $PR_BRANCH \
-	-t "cherry-pick #$PR_NUMBER $PR_TITLE" \
-	-b "cherry-pick #$PR_NUMBER $PR_TITLE" \
+	-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 $PR_NUMBER --body "🤖 cherry pick finished successfully 🎉!"
-gh pr edit $PR_NUMBER --add-label $AUTO_CHERRY_PICK_COMPLETED_LABEL || (
+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 $PR_NUMBER --add-label $AUTO_CHERRY_PICK_COMPLETED_LABEL
+	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 #$PR_NUMBER."
+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"
diff --git a/.github/workflows/auto-cherry-pick.yml b/.github/workflows/auto-cherry-pick.yml
index b9957a49a..1b3d34ff9 100644
--- a/.github/workflows/auto-cherry-pick.yml
+++ b/.github/workflows/auto-cherry-pick.yml
@@ -25,7 +25,7 @@ on:
 
 jobs:
   auto-cherry-pick:
-    if: github.event.pull_request.merged == true && startsWith(github.event.label.name, 'needs-cherrypick-')
+    if: github.event.pull_request.merged == true
     runs-on: ubuntu-latest
     steps:
       - uses: actions/checkout@v3