|
| 1 | +# Copyright Red Hat, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +name: Auto-Approve Bot PRs |
| 16 | + |
| 17 | +# This workflow automatically adds labels and approves PRs that match specific criteria: |
| 18 | +# - Created by rhdh-bot (via RHDH GitHub App) |
| 19 | +# - Branch name matches specific patterns (base image updates, version bumps, etc.) |
| 20 | +# - Adds lgtm and approved labels if not present |
| 21 | + |
| 22 | +on: |
| 23 | + pull_request: |
| 24 | + types: [opened, reopened, labeled, ready_for_review] |
| 25 | + |
| 26 | +permissions: |
| 27 | + contents: read |
| 28 | + pull-requests: write |
| 29 | + |
| 30 | +jobs: |
| 31 | + auto-approve: |
| 32 | + name: Auto-Approve and Label PRs |
| 33 | + runs-on: ubuntu-latest |
| 34 | + |
| 35 | + # Only run if PR is from rhdh-bot |
| 36 | + if: github.event.pull_request.user.login == 'rhdh-bot[bot]' |
| 37 | + |
| 38 | + steps: |
| 39 | + - name: Check PR eligibility |
| 40 | + id: check-eligibility |
| 41 | + run: | |
| 42 | + PR_BRANCH="${{ github.event.pull_request.head.ref }}" |
| 43 | + PR_DRAFT="${{ github.event.pull_request.draft }}" |
| 44 | + |
| 45 | + # Don't auto-approve draft PRs |
| 46 | + if [[ "$PR_DRAFT" == "true" ]]; then |
| 47 | + echo "eligible=false" >> $GITHUB_OUTPUT |
| 48 | + echo "reason=PR is in draft state" >> $GITHUB_OUTPUT |
| 49 | + exit 0 |
| 50 | + fi |
| 51 | + |
| 52 | + # Labels will be added automatically if eligible |
| 53 | + |
| 54 | + # Define branch patterns that are eligible for auto-approval |
| 55 | + # Add more patterns as needed |
| 56 | + ELIGIBLE_PATTERNS=( |
| 57 | + "^update-base-images-.*" # Base image updates |
| 58 | + "^pr-bump-to-.*" # Version bump PRs (like #3176) |
| 59 | + "^update-rpm-lockfile$" # RPM lockfile updates |
| 60 | + "^dependencies/.*" # Dependency updates (Backstage, etc.) |
| 61 | + "^renovate/.*" # Renovate PRs |
| 62 | + "^chore/automated-.*" # Other automated chore tasks |
| 63 | + ) |
| 64 | + |
| 65 | + ELIGIBLE=false |
| 66 | + for pattern in "${ELIGIBLE_PATTERNS[@]}"; do |
| 67 | + if [[ "$PR_BRANCH" =~ $pattern ]]; then |
| 68 | + ELIGIBLE=true |
| 69 | + break |
| 70 | + fi |
| 71 | + done |
| 72 | + |
| 73 | + if [[ "$ELIGIBLE" == "true" ]]; then |
| 74 | + echo "eligible=true" >> $GITHUB_OUTPUT |
| 75 | + echo "reason=Branch matches auto-approval pattern" >> $GITHUB_OUTPUT |
| 76 | + else |
| 77 | + echo "eligible=false" >> $GITHUB_OUTPUT |
| 78 | + echo "reason=Branch name does not match any auto-approval pattern" >> $GITHUB_OUTPUT |
| 79 | + fi |
| 80 | +
|
| 81 | + - name: Comment on ineligible PR |
| 82 | + if: steps.check-eligibility.outputs.eligible == 'false' |
| 83 | + run: | |
| 84 | + gh pr comment ${{ github.event.pull_request.number }} --body "**Auto-Approval Skipped** |
| 85 | +
|
| 86 | + **Reason:** ${{ steps.check-eligibility.outputs.reason }} |
| 87 | +
|
| 88 | + This PR will require manual review and approval. |
| 89 | + |
| 90 | + For auto-approval eligibility, PRs must: |
| 91 | + - Be created by \`rhdh-bot\` |
| 92 | + - Not be in draft state |
| 93 | + - Have a branch name matching auto-approval patterns |
| 94 | +
|
| 95 | + If eligible, the workflow will automatically add \`lgtm\` and \`approved\` labels and approve the PR. |
| 96 | + env: |
| 97 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 98 | + |
| 99 | + - name: Add required labels and approve PR |
| 100 | + if: steps.check-eligibility.outputs.eligible == 'true' |
| 101 | + run: | |
| 102 | + # Add the required labels if not already present |
| 103 | + gh pr edit ${{ github.event.pull_request.number }} --add-label "lgtm,approved" |
| 104 | +
|
| 105 | + # Auto-approve the PR |
| 106 | + gh pr review ${{ github.event.pull_request.number }} \ |
| 107 | + --approve \ |
| 108 | + --body "**Auto-Approved** |
| 109 | +
|
| 110 | + This PR has been automatically approved because: |
| 111 | + - Author: \`rhdh-bot\` |
| 112 | + - Branch: \`${{ github.event.pull_request.head.ref }}\` |
| 113 | + - ${{ steps.check-eligibility.outputs.reason }} |
| 114 | +
|
| 115 | + **Labels Added:** \`lgtm\`, \`approved\` |
| 116 | + env: |
| 117 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 118 | + |
0 commit comments