[CI/CD] Auto-Update PRs based on automatic-pr-update #2
Workflow file for this run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --- | |
| # SPDX-FileCopyrightText: (C) 2025 - 2026 Intel Corporation | |
| # SPDX-License-Identifier: Apache-2.0 | |
| name: "[CI/CD] Auto-Update Feature-Branch Pull Requests" | |
| run-name: "[CI/CD] Auto-Update PRs based on ${{ github.ref_name }}" | |
| on: # yamllint disable-line rule:truthy | |
| push: | |
| pull_request: | |
| types: | |
| - auto_merge_enabled | |
| workflow_dispatch: {} | |
| concurrency: | |
| group: ${{ github.workflow }}-${{ github.ref }} | |
| cancel-in-progress: true | |
| permissions: {} | |
| jobs: | |
| update-pull-requests: | |
| name: "Update pull requests targeting ${{ github.ref_name }}" | |
| if: github.event_name == 'push' || github.event_name == 'workflow_dispatch' | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| env: | |
| # Only PRs carrying this label are updated (drafts included). | |
| UPDATE_LABEL: "auto-update" | |
| steps: | |
| - name: "Find and update pull requests" | |
| env: | |
| GH_TOKEN: ${{ secrets.GH_PAT }} | |
| BASE_BRANCH: ${{ github.ref_name }} | |
| run: | | |
| echo "Searching for open PRs targeting '${BASE_BRANCH}'" | |
| mapfile -t prs < <(gh pr list \ | |
| --repo "$GITHUB_REPOSITORY" \ | |
| --base "$BASE_BRANCH" \ | |
| --label "$UPDATE_LABEL" \ | |
| --state open \ | |
| --json number,isDraft,headRefName,title \ | |
| --jq '.[] | @json') | |
| if [[ ${#prs[@]} -eq 0 ]]; then | |
| echo "No open PRs with label '${UPDATE_LABEL}' target '${BASE_BRANCH}' — nothing to do." | |
| exit 0 | |
| fi | |
| echo "Found ${#prs[@]} candidate PR(s)." | |
| failed=0 | |
| for pr_json in "${prs[@]}"; do | |
| pr_number=$(echo "$pr_json" | jq -r '.number') | |
| is_draft=$(echo "$pr_json" | jq -r '.isDraft') | |
| head_ref=$(echo "$pr_json" | jq -r '.headRefName') | |
| title=$(echo "$pr_json" | jq -r '.title') | |
| echo "→ PR #${pr_number}: '${title}' (head: ${head_ref}, draft: ${is_draft})" | |
| if gh pr update-branch "$pr_number" --repo "$GITHUB_REPOSITORY"; then | |
| echo " ✓ PR #${pr_number} updated successfully." | |
| else | |
| echo " ✗ PR #${pr_number} could not be updated (may already be up to date or have a merge conflict)." | |
| failed=$((failed + 1)) | |
| fi | |
| done | |
| echo "Done. Failed: ${failed}." | |
| if [[ $failed -gt 0 ]]; then | |
| echo "Warning: ${failed} PR(s) could not be updated — review the logs above." | |
| fi | |
| remove-label-on-automerge: | |
| name: "Remove auto-update label when auto-merge is enabled" | |
| if: github.event_name == 'pull_request' && github.event.action == 'auto_merge_enabled' | |
| runs-on: ubuntu-latest | |
| permissions: | |
| pull-requests: write | |
| env: | |
| UPDATE_LABEL: "auto-update" | |
| steps: | |
| - name: "Remove '${{ env.UPDATE_LABEL }}' label from PR" | |
| env: | |
| GH_TOKEN: ${{ secrets.GH_PAT }} | |
| PR_NUMBER: ${{ github.event.pull_request.number }} | |
| run: | | |
| # Check if the label is present before attempting removal. | |
| has_label=$(gh pr view "$PR_NUMBER" \ | |
| --repo "$GITHUB_REPOSITORY" \ | |
| --json labels \ | |
| --jq --arg lbl "$UPDATE_LABEL" '.labels[].name | select(. == $lbl)' \ | |
| | grep -c . || true) | |
| if [[ "$has_label" -eq 0 ]]; then | |
| echo "PR #${PR_NUMBER} does not have the '${UPDATE_LABEL}' label — nothing to do." | |
| exit 0 | |
| fi | |
| echo "Removing '${UPDATE_LABEL}' label from PR #${PR_NUMBER} (auto-merge enabled)." | |
| gh pr edit "$PR_NUMBER" \ | |
| --repo "$GITHUB_REPOSITORY" \ | |
| --remove-label "$UPDATE_LABEL" | |
| echo "Label removed." |