|
| 1 | +name: "Labels: auto on PR" |
| 2 | + |
| 3 | +# Sets labels automatically based on: |
| 4 | +# - PR size (job: label-size) |
| 5 | +# - File categories using .github/labeler config (job: label-category) |
| 6 | +# - PR creation date for quarterly tracking (job: label-by-date) |
| 7 | +# - Removes "Ready to merge" label on PR update (job: label-remove) |
| 8 | + |
| 9 | +run-name: 'Set labels - PR #${{ github.event.pull_request.number }} ("${{ github.event.pull_request.title }}")' |
| 10 | + |
| 11 | +on: pull_request_target |
| 12 | + |
| 13 | +# Grant required permissions globally |
| 14 | +permissions: |
| 15 | + contents: read # Required for checking changed files |
| 16 | + pull-requests: write # Required for labeling PRs |
| 17 | + issues: write # Required for adding/removing labels |
| 18 | + |
| 19 | +jobs: |
| 20 | + label-remove: |
| 21 | + name: "Remove Ready to merge" |
| 22 | + if: ${{ github.event.action == 'opened' || github.event.action == 'reopened' || github.event.action == 'synchronize' }} |
| 23 | + runs-on: ubuntu-latest |
| 24 | + |
| 25 | + steps: |
| 26 | + - name: Checkout the pull request |
| 27 | + uses: actions/checkout@v6 |
| 28 | + |
| 29 | + - name: Check for label using GH CLI |
| 30 | + id: check |
| 31 | + run: | |
| 32 | + gh pr view ${{ github.event.pull_request.number }} --json labels -q '.labels[].name' | grep -q 'Ready to merge' && echo "has_label=true" >> $GITHUB_OUTPUT || echo "has_label=false" >> $GITHUB_OUTPUT |
| 33 | + env: |
| 34 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 35 | + |
| 36 | + - name: Remove "Ready to merge" label |
| 37 | + if: steps.check.outputs.has_label == 'true' |
| 38 | + uses: PauMAVA/add-remove-label-action@v1.0.3 |
| 39 | + with: |
| 40 | + github_token: ${{ secrets.GITHUB_TOKEN }} |
| 41 | + add: "" |
| 42 | + remove: "Ready to merge" |
| 43 | + |
| 44 | + label-category: |
| 45 | + name: "Category Labels" |
| 46 | + runs-on: ubuntu-latest |
| 47 | + if: ${{ github.event.action == 'opened' || github.event.action == 'reopened' || github.event.action == 'synchronize' }} |
| 48 | + |
| 49 | + steps: |
| 50 | + # Checks out the repository to read files for matching with labeler config |
| 51 | + - uses: actions/checkout@v6 |
| 52 | + |
| 53 | + # Applies labels based on the .github/labeler.yml config |
| 54 | + - uses: actions/labeler@v6 |
| 55 | + with: |
| 56 | + repo-token: "${{ secrets.GITHUB_TOKEN }}" |
| 57 | + |
| 58 | + label-size: |
| 59 | + name: "Size Label" |
| 60 | + runs-on: ubuntu-latest |
| 61 | + if: ${{ github.event.action == 'opened' || github.event.action == 'reopened' || github.event.action == 'synchronize' }} |
| 62 | + |
| 63 | + steps: |
| 64 | + # Automatically adds size labels based on total changed lines |
| 65 | + - name: Label by size |
| 66 | + uses: pascalgn/size-label-action@v0.5.5 |
| 67 | + env: |
| 68 | + GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}" |
| 69 | + with: |
| 70 | + sizes: > |
| 71 | + { |
| 72 | + "0": "small", |
| 73 | + "50": "medium", |
| 74 | + "250": "large" |
| 75 | + } |
| 76 | +
|
| 77 | + label-by-date: |
| 78 | + name: "Date label (Quarters)" |
| 79 | + runs-on: ubuntu-latest |
| 80 | + if: ${{ github.event.action == 'opened' || github.event.action == 'reopened' || github.event.action == 'synchronize' }} |
| 81 | + |
| 82 | + steps: |
| 83 | + # Determines the label (02, 05, 08, 11) based on PR creation month |
| 84 | + - name: Determine quarter label |
| 85 | + env: |
| 86 | + PR_CREATED_AT: ${{ github.event.pull_request.created_at }} |
| 87 | + run: | |
| 88 | + echo "PR created at: $PR_CREATED_AT" |
| 89 | +
|
| 90 | + # Extract the numeric month (e.g., 04 for April) |
| 91 | + MONTH=$(date -d "$PR_CREATED_AT" +%m | sed 's/^0*//') |
| 92 | + echo "Month extracted: $MONTH" |
| 93 | +
|
| 94 | + # Determine quarter-end label based on month |
| 95 | + if [ "$MONTH" -le 2 ] || [ "$MONTH" -eq 12 ]; then |
| 96 | + LABEL="02" |
| 97 | + elif [ "$MONTH" -le 5 ]; then |
| 98 | + LABEL="05" |
| 99 | + elif [ "$MONTH" -le 8 ]; then |
| 100 | + LABEL="08" |
| 101 | + else |
| 102 | + LABEL="11" |
| 103 | + fi |
| 104 | +
|
| 105 | + # Set as environment variable for next step |
| 106 | + echo "LABEL=${LABEL}" >> $GITHUB_ENV |
| 107 | +
|
| 108 | + # Adds the quarter label to the PR |
| 109 | + - name: Add quarter label |
| 110 | + uses: PauMAVA/add-remove-label-action@v1.0.3 |
| 111 | + with: |
| 112 | + github_token: ${{ secrets.GITHUB_TOKEN }} |
| 113 | + add: "${{ env.LABEL }}" |
0 commit comments