|
| 1 | +# |
| 2 | +# Copyright (c) 2025 Huawei Technologies Co., Ltd. All Rights Reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# This file is a part of the vllm-ascend project. |
| 16 | +# |
| 17 | + |
| 18 | +name: Handle /cherry-pick Command |
| 19 | + |
| 20 | +on: |
| 21 | + repository_dispatch: |
| 22 | + types: [cherry-pick-command] |
| 23 | + |
| 24 | +defaults: |
| 25 | + run: |
| 26 | + shell: bash -el {0} |
| 27 | + |
| 28 | +permissions: |
| 29 | + contents: write |
| 30 | + actions: write |
| 31 | + pull-requests: write |
| 32 | + issues: write |
| 33 | + |
| 34 | +jobs: |
| 35 | + cherry-pick: |
| 36 | + name: Cherry-pick PR to target branch |
| 37 | + runs-on: linux-amd64-cpu-4-hk |
| 38 | + container: |
| 39 | + image: swr.cn-southwest-2.myhuaweicloud.com/base_image/ascend-ci/vllm-ascend/act-environments-ubuntu:act-22.04 |
| 40 | + steps: |
| 41 | + - name: Install system dependencies |
| 42 | + run: | |
| 43 | + sudo mkdir -p -m 755 /etc/apt/keyrings |
| 44 | + curl -sL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo tee /etc/apt/keyrings/githubcli-archive-keyring.gpg > /dev/null |
| 45 | + sudo chmod go+r /etc/apt/keyrings/githubcli-archive-keyring.gpg |
| 46 | + echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null |
| 47 | + sudo apt-get update -y |
| 48 | + sudo apt-get install gh -y |
| 49 | +
|
| 50 | + - name: Check user authorization |
| 51 | + id: auth |
| 52 | + env: |
| 53 | + GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }} |
| 54 | + run: | |
| 55 | + ACTOR='${{ github.event.client_payload.github.payload.comment.user.login }}' |
| 56 | + PR_AUTHOR='${{ github.event.client_payload.pull_request.user.login }}' |
| 57 | + REPO='${{ github.event.client_payload.github.payload.repository.full_name }}' |
| 58 | +
|
| 59 | + if [ "$ACTOR" = "$PR_AUTHOR" ]; then |
| 60 | + echo "User $ACTOR is the PR author, authorized." |
| 61 | + echo "authorized=true" >> "$GITHUB_OUTPUT" |
| 62 | + else |
| 63 | + ROLE_NAME=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN" \ |
| 64 | + "https://api.github.com/repos/$REPO/collaborators/$ACTOR/permission" | jq -r '.role_name // empty') |
| 65 | + case "$ROLE_NAME" in |
| 66 | + triage|write|maintain|admin) |
| 67 | + echo "authorized=true" >> "$GITHUB_OUTPUT" |
| 68 | + ;; |
| 69 | + *) |
| 70 | + echo "::error::User $ACTOR is not the PR author and does not have triage+ permission." |
| 71 | + echo "authorized=false" >> "$GITHUB_OUTPUT" |
| 72 | + ;; |
| 73 | + esac |
| 74 | + fi |
| 75 | +
|
| 76 | + - name: Parse target branch |
| 77 | + id: parse |
| 78 | + run: | |
| 79 | + TARGET_BRANCH='${{ github.event.client_payload.slash_command.args.all }}' |
| 80 | + TARGET_BRANCH=$(echo "$TARGET_BRANCH" | xargs) |
| 81 | +
|
| 82 | + if [ -z "$TARGET_BRANCH" ]; then |
| 83 | + echo "::error::No target branch specified. Usage: /cherry-pick <target_branch_name>" |
| 84 | + echo "valid_args=false" >> "$GITHUB_OUTPUT" |
| 85 | + else |
| 86 | + echo "target_branch=$TARGET_BRANCH" >> "$GITHUB_OUTPUT" |
| 87 | + echo "valid_args=true" >> "$GITHUB_OUTPUT" |
| 88 | + fi |
| 89 | +
|
| 90 | + - name: Post unauthorized comment |
| 91 | + if: steps.auth.outputs.authorized == 'false' |
| 92 | + uses: peter-evans/create-or-update-comment@v5 |
| 93 | + with: |
| 94 | + token: ${{ secrets.PAT_TOKEN }} |
| 95 | + repository: ${{ github.event.client_payload.github.payload.repository.full_name }} |
| 96 | + comment-id: ${{ github.event.client_payload.github.payload.comment.id }} |
| 97 | + body: | |
| 98 | + [Bot]: cherry-pick command failed: you do not have permission. Only the PR author or users with triage+ permission can trigger /cherry-pick. |
| 99 | + reactions: confused |
| 100 | + |
| 101 | + - name: Post invalid-args comment |
| 102 | + if: steps.parse.outputs.valid_args == 'false' |
| 103 | + uses: peter-evans/create-or-update-comment@v5 |
| 104 | + with: |
| 105 | + token: ${{ secrets.PAT_TOKEN }} |
| 106 | + repository: ${{ github.event.client_payload.github.payload.repository.full_name }} |
| 107 | + comment-id: ${{ github.event.client_payload.github.payload.comment.id }} |
| 108 | + body: | |
| 109 | + [Bot]: cherry-pick command failed: no target branch specified. Usage: /cherry-pick <target_branch_name> |
| 110 | + reactions: confused |
| 111 | + |
| 112 | + - name: Fail if unauthorized or invalid args |
| 113 | + if: steps.auth.outputs.authorized == 'false' || steps.parse.outputs.valid_args == 'false' |
| 114 | + run: exit 1 |
| 115 | + |
| 116 | + - name: Checkout repo |
| 117 | + uses: actions/checkout@v7 |
| 118 | + with: |
| 119 | + token: ${{ secrets.PAT_TOKEN }} |
| 120 | + fetch-depth: 0 |
| 121 | + |
| 122 | + - name: Cherry-pick and create PR |
| 123 | + id: cherry |
| 124 | + env: |
| 125 | + GH_TOKEN: ${{ secrets.PAT_TOKEN }} |
| 126 | + GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }} |
| 127 | + run: | |
| 128 | + PR_NUMBER='${{ github.event.client_payload.pull_request.number }}' |
| 129 | + PR_TITLE='${{ github.event.client_payload.pull_request.title }}' |
| 130 | + PR_BODY='${{ github.event.client_payload.pull_request.body }}' |
| 131 | + PR_AUTHOR='${{ github.event.client_payload.pull_request.user.login }}' |
| 132 | + PR_BASE_SHA='${{ github.event.client_payload.pull_request.base.sha }}' |
| 133 | + PR_HEAD_SHA='${{ github.event.client_payload.pull_request.head.sha }}' |
| 134 | + TARGET_BRANCH='${{ steps.parse.outputs.target_branch }}' |
| 135 | + REPO='${{ github.event.client_payload.github.payload.repository.full_name }}' |
| 136 | +
|
| 137 | + # Get the PAT_TOKEN owner's username |
| 138 | + TOKEN_USER=$(gh api user --jq '.login') |
| 139 | + echo "PAT_TOKEN user: $TOKEN_USER" |
| 140 | +
|
| 141 | + # Configure git for push |
| 142 | + git config user.name "github-actions[bot]" |
| 143 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 144 | + # origin for fetching from the upstream repo |
| 145 | + git remote set-url origin "https://x-access-token:${GITHUB_TOKEN}@github.com/${REPO}.git" |
| 146 | + # fork remote for pushing cherry-pick branch |
| 147 | + git remote add fork "https://x-access-token:${GITHUB_TOKEN}@github.com/${TOKEN_USER}/vllm-ascend.git" |
| 148 | +
|
| 149 | + # Fetch target branch and PR's base/head SHAs |
| 150 | + git fetch origin "$TARGET_BRANCH" "$PR_BASE_SHA" "$PR_HEAD_SHA" 2>/dev/null || true |
| 151 | +
|
| 152 | + # Verify target branch exists |
| 153 | + if ! git rev-parse --verify "origin/$TARGET_BRANCH" >/dev/null 2>&1; then |
| 154 | + echo "::error::Target branch '$TARGET_BRANCH' does not exist." |
| 155 | + echo "cherry_status=missing_branch" >> "$GITHUB_OUTPUT" |
| 156 | + exit 1 |
| 157 | + fi |
| 158 | +
|
| 159 | + # Create a new branch from target |
| 160 | + SANITIZED_TARGET=$(echo "$TARGET_BRANCH" | tr '/' '-') |
| 161 | + CHERRY_BRANCH="cherry-pick/pr-${PR_NUMBER}-to-${SANITIZED_TARGET}" |
| 162 | + git checkout -b "$CHERRY_BRANCH" "origin/$TARGET_BRANCH" |
| 163 | +
|
| 164 | + # Apply the PR's changes as a single commit. |
| 165 | + # Using git diff with three-dot (...) from merge-base to PR head |
| 166 | + # avoids picking up unrelated commits from the base branch, which |
| 167 | + # works for both merged and unmerged PRs. |
| 168 | + echo "PR base: $PR_BASE_SHA" |
| 169 | + echo "PR head: $PR_HEAD_SHA" |
| 170 | +
|
| 171 | + # Generate the diff to a temp file (avoids shell variable size limits |
| 172 | + # for large PRs) so we can check emptiness and apply it. |
| 173 | + # Use three-dot (...) to diff from merge-base to PR head, so we |
| 174 | + # only pick up the PR's unique changes — not unrelated commits |
| 175 | + # that landed on the base branch between PR_BASE_SHA and the |
| 176 | + # rebased PR_HEAD_SHA. |
| 177 | + DIFF_FILE=/tmp/cherry_diff.patch |
| 178 | + git diff "$PR_BASE_SHA"..."$PR_HEAD_SHA" > "$DIFF_FILE" |
| 179 | +
|
| 180 | + if [ ! -s "$DIFF_FILE" ]; then |
| 181 | + echo "Diff is empty, changes already included in target branch." |
| 182 | + echo "cherry_status=already_included" >> "$GITHUB_OUTPUT" |
| 183 | + exit 0 |
| 184 | + fi |
| 185 | +
|
| 186 | + # Apply the diff with 3-way merge for better conflict resolution |
| 187 | + APPLY_LOG=/tmp/cherry_apply_err |
| 188 | + if ! git apply --3way "$DIFF_FILE" 2>"$APPLY_LOG"; then |
| 189 | + echo "::error::git apply failed. See details below:" |
| 190 | + cat "$APPLY_LOG" |
| 191 | + echo "cherry_status=conflict" >> "$GITHUB_OUTPUT" |
| 192 | + exit 1 |
| 193 | + fi |
| 194 | +
|
| 195 | + # Commit the applied changes as a single commit with PR metadata |
| 196 | + git commit -m "$PR_TITLE" -m "Cherry-pick of PR #${PR_NUMBER} by @${PR_AUTHOR}" |
| 197 | +
|
| 198 | + # Push the new branch to the PAT_TOKEN user's fork |
| 199 | + git push -f fork "$CHERRY_BRANCH" |
| 200 | +
|
| 201 | + # Check if a cherry-pick PR already exists for this target branch. |
| 202 | + # gh pr list --head requires "owner:branch" format for fork branches, |
| 203 | + # but it can be unreliable. We also add `2>/dev/null || true` to ensure |
| 204 | + # the step doesn't fail even if the query returns nothing. |
| 205 | + EXISTING_PR_URL=$(gh pr list \ |
| 206 | + --repo "$REPO" \ |
| 207 | + --head "${TOKEN_USER}:${CHERRY_BRANCH}" \ |
| 208 | + --base "$TARGET_BRANCH" \ |
| 209 | + --state open \ |
| 210 | + --json url --jq '.[0].url // empty' 2>/dev/null || true) |
| 211 | +
|
| 212 | + if [ -n "$EXISTING_PR_URL" ]; then |
| 213 | + # PR already exists — branch has been force-pushed, just notify |
| 214 | + echo "Cherry-pick PR already exists: $EXISTING_PR_URL" |
| 215 | + echo "cherry_status=refreshed" >> "$GITHUB_OUTPUT" |
| 216 | + echo "existing_pr_url=$EXISTING_PR_URL" >> "$GITHUB_OUTPUT" |
| 217 | + else |
| 218 | + # Create a new PR from the fork branch. |
| 219 | + # If the PR already exists (gh pr list missed it), gh pr create will |
| 220 | + # fail — we parse the error to extract the existing PR URL instead of |
| 221 | + # letting the step crash. |
| 222 | + NEW_PR_TITLE="[Cherry-pick][${TARGET_BRANCH}]${PR_TITLE} (from #${PR_NUMBER})" |
| 223 | + NEW_PR_BODY="Cherry-pick of PR #${PR_NUMBER} onto \`${TARGET_BRANCH}\`."$'\n'$'\n'"Original PR: #${PR_NUMBER}"$'\n'"Original author: @${PR_AUTHOR}"$'\n'$'\n'"---"$'\n'"${PR_BODY}" |
| 224 | +
|
| 225 | + CREATE_LOG=/tmp/gh_pr_create.log |
| 226 | + if NEW_PR_URL=$(gh pr create \ |
| 227 | + --repo "$REPO" \ |
| 228 | + --title "$NEW_PR_TITLE" \ |
| 229 | + --body "$NEW_PR_BODY" \ |
| 230 | + --base "$TARGET_BRANCH" \ |
| 231 | + --head "${TOKEN_USER}:${CHERRY_BRANCH}" 2>"$CREATE_LOG"); then |
| 232 | + echo "new_pr_url=$NEW_PR_URL" >> "$GITHUB_OUTPUT" |
| 233 | + echo "cherry_status=success" >> "$GITHUB_OUTPUT" |
| 234 | + else |
| 235 | + # If PR already exists, gh pr create fails with "already exists" |
| 236 | + # and includes the existing PR URL in the message. |
| 237 | + EXISTING_PR_URL=$(grep -oE 'https://github.com/[^ ]+/pull/[0-9]+' "$CREATE_LOG" | head -1 || true) |
| 238 | + if [ -n "$EXISTING_PR_URL" ]; then |
| 239 | + echo "Cherry-pick PR already exists: $EXISTING_PR_URL" |
| 240 | + echo "cherry_status=refreshed" >> "$GITHUB_OUTPUT" |
| 241 | + echo "existing_pr_url=$EXISTING_PR_URL" >> "$GITHUB_OUTPUT" |
| 242 | + else |
| 243 | + echo "::error::Failed to create PR:" |
| 244 | + cat "$CREATE_LOG" |
| 245 | + echo "cherry_status=create_failed" >> "$GITHUB_OUTPUT" |
| 246 | + exit 1 |
| 247 | + fi |
| 248 | + fi |
| 249 | + fi |
| 250 | +
|
| 251 | + - name: Comment success result |
| 252 | + if: steps.cherry.outputs.cherry_status == 'success' |
| 253 | + uses: peter-evans/create-or-update-comment@v5 |
| 254 | + with: |
| 255 | + token: ${{ secrets.PAT_TOKEN }} |
| 256 | + repository: ${{ github.event.client_payload.github.payload.repository.full_name }} |
| 257 | + comment-id: ${{ github.event.client_payload.github.payload.comment.id }} |
| 258 | + body: | |
| 259 | + [Bot]: cherry-pick completed successfully. New PR created: ${{ steps.cherry.outputs.new_pr_url }} |
| 260 | + reactions: hooray |
| 261 | + |
| 262 | + - name: Comment refreshed result |
| 263 | + if: steps.cherry.outputs.cherry_status == 'refreshed' |
| 264 | + uses: peter-evans/create-or-update-comment@v5 |
| 265 | + with: |
| 266 | + token: ${{ secrets.PAT_TOKEN }} |
| 267 | + repository: ${{ github.event.client_payload.github.payload.repository.full_name }} |
| 268 | + comment-id: ${{ github.event.client_payload.github.payload.comment.id }} |
| 269 | + body: | |
| 270 | + [Bot]: cherry-pick branch has been refreshed. Existing PR: ${{ steps.cherry.outputs.existing_pr_url }} |
| 271 | + reactions: hooray |
| 272 | + |
| 273 | + - name: Comment already included |
| 274 | + if: steps.cherry.outputs.cherry_status == 'already_included' |
| 275 | + uses: peter-evans/create-or-update-comment@v5 |
| 276 | + with: |
| 277 | + token: ${{ secrets.PAT_TOKEN }} |
| 278 | + repository: ${{ github.event.client_payload.github.payload.repository.full_name }} |
| 279 | + comment-id: ${{ github.event.client_payload.github.payload.comment.id }} |
| 280 | + body: | |
| 281 | + [Bot]: cherry-pick skipped: the changes from this PR are already included in the target branch `${{ steps.parse.outputs.target_branch }}`. |
| 282 | + reactions: eyes |
| 283 | + |
| 284 | + - name: Comment conflict failure |
| 285 | + if: failure() && steps.cherry.outputs.cherry_status == 'conflict' |
| 286 | + uses: peter-evans/create-or-update-comment@v5 |
| 287 | + with: |
| 288 | + token: ${{ secrets.PAT_TOKEN }} |
| 289 | + repository: ${{ github.event.client_payload.github.payload.repository.full_name }} |
| 290 | + comment-id: ${{ github.event.client_payload.github.payload.comment.id }} |
| 291 | + body: | |
| 292 | + [Bot]: cherry-pick failed due to merge conflicts. Please cherry-pick manually. |
| 293 | + reactions: confused |
| 294 | + |
| 295 | + - name: Comment generic failure |
| 296 | + if: failure() && steps.cherry.outputs.cherry_status != 'conflict' && steps.cherry.outputs.cherry_status != 'success' && steps.cherry.outputs.cherry_status != 'refreshed' && steps.cherry.outputs.cherry_status != 'already_included' |
| 297 | + uses: peter-evans/create-or-update-comment@v5 |
| 298 | + with: |
| 299 | + token: ${{ secrets.PAT_TOKEN }} |
| 300 | + repository: ${{ github.event.client_payload.github.payload.repository.full_name }} |
| 301 | + comment-id: ${{ github.event.client_payload.github.payload.comment.id }} |
| 302 | + body: | |
| 303 | + [Bot]: cherry-pick command failed. Please check the [workflow run](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) for details. |
| 304 | + reactions: confused |
0 commit comments