|
| 1 | +--- |
| 2 | +# SPDX-FileCopyrightText: (C) 2025 - 2026 Intel Corporation |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +name: "[CI/CD] Auto-Update Pull Requests" |
| 6 | +run-name: "[CI/CD] Auto-Update PRs based on ${{ github.ref_name }}" |
| 7 | + |
| 8 | +on: # yamllint disable-line rule:truthy |
| 9 | + push: |
| 10 | + pull_request: |
| 11 | + types: |
| 12 | + - auto_merge_enabled |
| 13 | + workflow_dispatch: {} |
| 14 | + |
| 15 | +concurrency: |
| 16 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 17 | + cancel-in-progress: true |
| 18 | + |
| 19 | +permissions: {} |
| 20 | + |
| 21 | +jobs: |
| 22 | + update-pull-requests: |
| 23 | + name: "Update pull requests targeting this branch" |
| 24 | + if: github.event_name == 'push' || github.event_name == 'workflow_dispatch' |
| 25 | + runs-on: ubuntu-latest |
| 26 | + permissions: |
| 27 | + contents: write |
| 28 | + pull-requests: write |
| 29 | + |
| 30 | + env: |
| 31 | + # Only PRs carrying this label are updated (drafts included). |
| 32 | + UPDATE_LABEL: "auto-update" |
| 33 | + |
| 34 | + steps: |
| 35 | + - name: "Find and update pull requests" |
| 36 | + env: |
| 37 | + GH_TOKEN: ${{ secrets.GH_PAT }} |
| 38 | + BASE_BRANCH: ${{ github.ref_name }} |
| 39 | + run: | |
| 40 | + echo "Searching for open PRs targeting '${BASE_BRANCH}'" |
| 41 | +
|
| 42 | + if ! pr_list_output=$(gh pr list \ |
| 43 | + --repo "$GITHUB_REPOSITORY" \ |
| 44 | + --base "$BASE_BRANCH" \ |
| 45 | + --label "$UPDATE_LABEL" \ |
| 46 | + --state open \ |
| 47 | + --json number,isDraft,headRefName,title \ |
| 48 | + --jq '.[] | @json'); then |
| 49 | + echo "ERROR: gh pr list failed — check API access and token permissions." |
| 50 | + exit 1 |
| 51 | + fi |
| 52 | +
|
| 53 | + mapfile -t prs <<< "$pr_list_output" |
| 54 | + # When output is empty, mapfile produces one empty element — normalize it. |
| 55 | + if [[ ${#prs[@]} -eq 1 && -z "${prs[0]}" ]]; then |
| 56 | + prs=() |
| 57 | + fi |
| 58 | +
|
| 59 | + if [[ ${#prs[@]} -eq 0 ]]; then |
| 60 | + echo "No open PRs with label '${UPDATE_LABEL}' targeting '${BASE_BRANCH}' — nothing to do." |
| 61 | + exit 0 |
| 62 | + fi |
| 63 | +
|
| 64 | + echo "Found ${#prs[@]} candidate PR(s)." |
| 65 | +
|
| 66 | + failed=0 |
| 67 | + for pr_json in "${prs[@]}"; do |
| 68 | + pr_number=$(echo "$pr_json" | jq -r '.number') |
| 69 | + is_draft=$(echo "$pr_json" | jq -r '.isDraft') |
| 70 | + head_ref=$(echo "$pr_json" | jq -r '.headRefName') |
| 71 | + title=$(echo "$pr_json" | jq -r '.title') |
| 72 | +
|
| 73 | + echo "→ PR #${pr_number}: '${title}' (head: ${head_ref}, draft: ${is_draft})" |
| 74 | +
|
| 75 | + if gh pr update-branch "$pr_number" --repo "$GITHUB_REPOSITORY"; then |
| 76 | + echo " ✓ PR #${pr_number} updated successfully." |
| 77 | + else |
| 78 | + echo " ✗ PR #${pr_number} could not be updated (may already be up to date or have a merge conflict)." |
| 79 | + failed=$((failed + 1)) |
| 80 | + fi |
| 81 | + done |
| 82 | +
|
| 83 | + echo "Done. Failed: ${failed}." |
| 84 | + if [[ $failed -gt 0 ]]; then |
| 85 | + echo "Warning: ${failed} PR(s) could not be updated — review the logs above." |
| 86 | + fi |
| 87 | +
|
| 88 | + remove-label-on-automerge: |
| 89 | + name: "Remove auto-update label when auto-merge is enabled" |
| 90 | + if: github.event_name == 'pull_request' && github.event.action == 'auto_merge_enabled' |
| 91 | + runs-on: ubuntu-latest |
| 92 | + permissions: |
| 93 | + pull-requests: write |
| 94 | + |
| 95 | + env: |
| 96 | + UPDATE_LABEL: "auto-update" |
| 97 | + |
| 98 | + steps: |
| 99 | + - name: "Remove '${{ env.UPDATE_LABEL }}' label from PR" |
| 100 | + env: |
| 101 | + GH_TOKEN: ${{ secrets.GH_PAT }} |
| 102 | + PR_NUMBER: ${{ github.event.pull_request.number }} |
| 103 | + run: | |
| 104 | + # Query labels first and fail explicitly if the GitHub CLI request fails. |
| 105 | + if ! pr_labels=$(gh pr view "$PR_NUMBER" \ |
| 106 | + --repo "$GITHUB_REPOSITORY" \ |
| 107 | + --json labels \ |
| 108 | + --jq '.labels[].name'); then |
| 109 | + echo "Failed to read labels for PR #${PR_NUMBER}." |
| 110 | + exit 1 |
| 111 | + fi |
| 112 | +
|
| 113 | + # Treat a missing label as a normal no-op, but do not mask query failures. |
| 114 | + if ! printf '%s\n' "$pr_labels" | grep -Fxq -- "$UPDATE_LABEL"; then |
| 115 | + echo "PR #${PR_NUMBER} does not have the '${UPDATE_LABEL}' label — nothing to do." |
| 116 | + exit 0 |
| 117 | + fi |
| 118 | +
|
| 119 | + echo "Removing '${UPDATE_LABEL}' label from PR #${PR_NUMBER} (auto-merge enabled)." |
| 120 | + gh pr edit "$PR_NUMBER" \ |
| 121 | + --repo "$GITHUB_REPOSITORY" \ |
| 122 | + --remove-label "$UPDATE_LABEL" |
| 123 | + echo "Label removed." |
0 commit comments