|
| 1 | +name: Cherry Pick |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request_target: |
| 5 | + branches: [develop] |
| 6 | + types: [closed, labeled] |
| 7 | + |
| 8 | +permissions: |
| 9 | + contents: write |
| 10 | + pull-requests: write |
| 11 | + issues: write |
| 12 | + |
| 13 | +concurrency: |
| 14 | + group: ${{ github.workflow }}-${{ github.event.pull_request.number }} |
| 15 | + cancel-in-progress: false |
| 16 | + |
| 17 | +jobs: |
| 18 | + cherry-pick: |
| 19 | + if: > |
| 20 | + github.event.pull_request.merged == true && |
| 21 | + ( |
| 22 | + github.event.action == 'labeled' || |
| 23 | + contains(join(github.event.pull_request.labels.*.name, ' '), 'cherry-pick') |
| 24 | + ) |
| 25 | + runs-on: ubuntu-latest |
| 26 | + steps: |
| 27 | + - name: Checkout |
| 28 | + uses: actions/checkout@v6 |
| 29 | + with: |
| 30 | + fetch-depth: 0 |
| 31 | + persist-credentials: false |
| 32 | + |
| 33 | + - name: Cherry Pick |
| 34 | + env: |
| 35 | + GH_TOKEN: ${{ secrets.CHERRY_PICK_BOT_TOKEN }} |
| 36 | + PR_NUMBER: ${{ github.event.pull_request.number }} |
| 37 | + PR_TITLE: ${{ github.event.pull_request.title }} |
| 38 | + PR_BODY: ${{ github.event.pull_request.body }} |
| 39 | + PR_AUTHOR: ${{ github.event.pull_request.user.login }} |
| 40 | + MERGE_COMMIT_SHA: ${{ github.event.pull_request.merge_commit_sha }} |
| 41 | + BOT_USERNAME: EmmonsCurse |
| 42 | + REPO_NAME: EmmonsCurse/FastDeploy |
| 43 | + run: | |
| 44 | + # Function to post comment |
| 45 | + post_comment() { |
| 46 | + gh pr comment "$PR_NUMBER" --body "$1" |
| 47 | + } |
| 48 | +
|
| 49 | + # Configure git for the original author |
| 50 | + echo "Fetching author info for $PR_AUTHOR..." |
| 51 | + AUTHOR_INFO=$(gh api "/users/$PR_AUTHOR" --jq '{email: .email, name: .name}') |
| 52 | + AUTHOR_EMAIL=$(echo "$AUTHOR_INFO" | jq -r '.email') |
| 53 | + AUTHOR_NAME=$(echo "$AUTHOR_INFO" | jq -r '.name') |
| 54 | +
|
| 55 | + if [ "$AUTHOR_EMAIL" = "null" ] || [ -z "$AUTHOR_EMAIL" ]; then |
| 56 | + AUTHOR_EMAIL="${PR_AUTHOR}@users.noreply.github.com" |
| 57 | + echo "Author email not found, using default: $AUTHOR_EMAIL" |
| 58 | + fi |
| 59 | + if [ "$AUTHOR_NAME" = "null" ] || [ -z "$AUTHOR_NAME" ]; then |
| 60 | + AUTHOR_NAME="${PR_AUTHOR}" |
| 61 | + echo "Author name not found, using username: $AUTHOR_NAME" |
| 62 | + fi |
| 63 | +
|
| 64 | + git config user.name "$AUTHOR_NAME" |
| 65 | + git config user.email "$AUTHOR_EMAIL" |
| 66 | +
|
| 67 | + # Capture current SHA to return to later |
| 68 | + ORIGINAL_HEAD_SHA=$(git rev-parse HEAD) |
| 69 | +
|
| 70 | + # Get labels |
| 71 | + LABELS=$(gh pr view "$PR_NUMBER" --json labels --jq '.labels[].name') |
| 72 | +
|
| 73 | + if [ -z "$LABELS" ]; then |
| 74 | + echo "No labels found." |
| 75 | + exit 0 |
| 76 | + fi |
| 77 | +
|
| 78 | + # Loop through labels |
| 79 | + while read -r label; do |
| 80 | + if [[ "$label" == cherry-pick:* ]]; then |
| 81 | + TARGET_BRANCH=$(echo "${label#cherry-pick:}" | xargs) |
| 82 | +
|
| 83 | + if [ -z "$TARGET_BRANCH" ]; then |
| 84 | + echo "Empty target branch for label '$label', skipping." |
| 85 | + continue |
| 86 | + fi |
| 87 | +
|
| 88 | + echo "Processing cherry-pick to $TARGET_BRANCH" |
| 89 | +
|
| 90 | + # Check if target branch exists on remote |
| 91 | + if ! git ls-remote --exit-code --heads origin "$TARGET_BRANCH"; then |
| 92 | + echo "Target branch $TARGET_BRANCH does not exist." |
| 93 | + post_comment "❌ Cherry-pick failed: Target branch \`$TARGET_BRANCH\` does not exist." |
| 94 | + continue |
| 95 | + fi |
| 96 | +
|
| 97 | + # Create a new branch for the cherry-pick |
| 98 | + NEW_BRANCH="cherry-pick/$PR_NUMBER/$TARGET_BRANCH" |
| 99 | +
|
| 100 | + # Clean up local branch if it exists (from previous run) |
| 101 | + if git show-ref --verify --quiet "refs/heads/$NEW_BRANCH"; then |
| 102 | + git branch -D "$NEW_BRANCH" |
| 103 | + fi |
| 104 | +
|
| 105 | + # Fetch the target branch and checkout a new branch from it |
| 106 | + git fetch origin "$TARGET_BRANCH" |
| 107 | + git checkout -b "$NEW_BRANCH" "origin/$TARGET_BRANCH" |
| 108 | +
|
| 109 | + # Cherry pick |
| 110 | + # Try standard cherry-pick first (for squash merges or single commits) |
| 111 | + if git cherry-pick "$MERGE_COMMIT_SHA"; then |
| 112 | + echo "Cherry-pick successful." |
| 113 | + else |
| 114 | + echo "Standard cherry-pick failed, trying with -m 1 (for merge commits)..." |
| 115 | + git cherry-pick --abort |
| 116 | + if git cherry-pick -m 1 "$MERGE_COMMIT_SHA"; then |
| 117 | + echo "Cherry-pick with -m 1 successful." |
| 118 | + else |
| 119 | + echo "Cherry-pick failed." |
| 120 | + git cherry-pick --abort |
| 121 | + post_comment "❌ Cherry-pick failed: Conflicts detected when cherry-picking to \`$TARGET_BRANCH\`. Please resolve manually." |
| 122 | +
|
| 123 | + # Cleanup |
| 124 | + git checkout "$ORIGINAL_HEAD_SHA" |
| 125 | + git branch -D "$NEW_BRANCH" |
| 126 | + continue |
| 127 | + fi |
| 128 | + fi |
| 129 | +
|
| 130 | + # Push |
| 131 | + # Construct authenticated URL for the fork |
| 132 | + FORK_URL_AUTH="https://${BOT_USERNAME}:${GH_TOKEN}@github.com/${REPO_NAME}.git" |
| 133 | +
|
| 134 | + echo "Pushing to fork..." |
| 135 | + git push "$FORK_URL_AUTH" "$NEW_BRANCH" --force |
| 136 | +
|
| 137 | + # Create PR |
| 138 | + # If PR_TITLE starts with "[", don't insert an extra space. |
| 139 | + if [ "${PR_TITLE:0:1}" = "[" ]; then |
| 140 | + NEW_TITLE="[Cherry-Pick]$PR_TITLE($PR_NUMBER)" |
| 141 | + else |
| 142 | + NEW_TITLE="[Cherry-Pick] $PR_TITLE($PR_NUMBER)" |
| 143 | + fi |
| 144 | +
|
| 145 | + NEW_BODY="Cherry-pick of #$PR_NUMBER (authored by @${PR_AUTHOR}) to \`$TARGET_BRANCH\`. |
| 146 | +
|
| 147 | + devPR:https://github.com/PaddlePaddle/FastDeploy/pull/$PR_NUMBER <!-- For pass CI --> |
| 148 | +
|
| 149 | + --- |
| 150 | +
|
| 151 | + $PR_BODY" |
| 152 | +
|
| 153 | + # Prepare head ref for PR creation (owner:branch) |
| 154 | + HEAD_REF="${BOT_USERNAME}:${NEW_BRANCH}" |
| 155 | +
|
| 156 | + # Check if PR already exists |
| 157 | + EXISTING_PR=$(gh pr list --base "$TARGET_BRANCH" --head "$NEW_BRANCH" --json url --jq '.[0].url') |
| 158 | +
|
| 159 | + if [ -n "$EXISTING_PR" ]; then |
| 160 | + echo "PR already exists: $EXISTING_PR" |
| 161 | + post_comment "ℹ️ Cherry-pick PR already exists: $EXISTING_PR" |
| 162 | + else |
| 163 | + # Create PR using gh CLI, ignoring errors because of "Resource not accessible" false positives |
| 164 | + gh pr create --base "$TARGET_BRANCH" --head "$HEAD_REF" --title "$NEW_TITLE" --body "$NEW_BODY" || true |
| 165 | +
|
| 166 | + # Wait a bit for eventual consistency |
| 167 | + sleep 2 |
| 168 | +
|
| 169 | + # Search for the created PR |
| 170 | + CREATED_PR_URL=$(gh pr list --head "$NEW_BRANCH" --state all --json url --jq '.[0].url') |
| 171 | +
|
| 172 | + if [ -n "$CREATED_PR_URL" ]; then |
| 173 | + echo "Created PR: $CREATED_PR_URL" |
| 174 | + post_comment "✅ Cherry-pick successful! Created PR: $CREATED_PR_URL" |
| 175 | +
|
| 176 | + # Request review |
| 177 | + gh pr review-request "$CREATED_PR_URL" --reviewer "$PR_AUTHOR" || true |
| 178 | + else |
| 179 | + echo "Failed to create PR." |
| 180 | + post_comment "❌ Cherry-pick failed: Could not create PR to \`$TARGET_BRANCH\`." |
| 181 | + continue |
| 182 | + fi |
| 183 | + fi |
| 184 | +
|
| 185 | + # Cleanup for next loop |
| 186 | + git checkout "$ORIGINAL_HEAD_SHA" |
| 187 | + git branch -D "$NEW_BRANCH" |
| 188 | + fi |
| 189 | + done <<< "$LABELS" |
0 commit comments