GH Actions label adjustment, fix permissions and enable auto quarter labels (#8078)

* GH Actions: adjust permissions in order to change labels on PR
* Add label by date, check if "Read to merge is present", fix bugs
This commit is contained in:
Igor
2025-04-12 19:42:10 +02:00
committed by GitHub
parent 5795f7202a
commit 1684d06eb1

View File

@@ -1,54 +1,69 @@
name: Automatic Pull Request Labeling
# Sets labels automatically based on:
# - PR size (job: label-size)
# - File categories using .github/labeler config (job: label-category)
# - PR creation date for quarterly tracking (job: label-by-date)
# - Removes "Ready to merge" label on PR update (job: label-remove)
run-name: 'Set labels - PR #${{ github.event.pull_request.number }} ("${{ github.event.pull_request.title }}")'
#
# Set labels for pull requests automatically based on size (modified via job 'label-size') and file categories (modified via .github/labeler)
#
on: pull_request_target
# Grant required permissions globally
permissions:
contents: read # Required for checking changed files
pull-requests: write # Required for labeling PRs
issues: write # Required for adding/removing labels
jobs:
label-remove:
permissions:
contents: read # for pascalgn/size-label-action to determine modified files
pull-requests: write # for pascalgn/size-label-action to add labels to PRs
name: "Remove Ready to merge"
if: ${{ github.event.action == 'opened' || github.event.action == 'reopened' || github.event.action == 'synchronize' }}
runs-on: ubuntu-latest
steps:
- uses: PauMAVA/add-remove-label-action@v1.0.3
- name: Checkout the pull request
uses: actions/checkout@v4
- name: Check for label using GH CLI
id: check
run: |
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
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Remove "Ready to merge" label
if: steps.check.outputs.has_label == 'true'
uses: PauMAVA/add-remove-label-action@v1.0.3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
add: ""
remove: "Ready to merge"
label-category:
permissions:
contents: read # for actions/labeler to determine modified files
pull-requests: write # for actions/labeler to add labels to PRs
name: "Category labels"
if: ${{ github.event.action == 'opened' || github.event.action == 'reopened' || github.event.action == 'synchronize' }}
name: "Category Labels"
runs-on: ubuntu-latest
if: ${{ github.event.action == 'opened' || github.event.action == 'reopened' || github.event.action == 'synchronize' }}
steps:
# Checks out the repository to read files for matching with labeler config
- uses: actions/checkout@v4
# Applies labels based on the .github/labeler.yml config
- uses: actions/labeler@v5
with:
repo-token: "${{ secrets.GITHUB_TOKEN }}"
label-size:
permissions:
contents: read # for pascalgn/size-label-action to determine modified files
pull-requests: write # for pascalgn/size-label-action to add labels to PRs
name: "Size label"
if: ${{ github.event.action == 'opened' || github.event.action == 'reopened' || github.event.action == 'synchronize' }}
name: "Size Label"
runs-on: ubuntu-latest
if: ${{ github.event.action == 'opened' || github.event.action == 'reopened' || github.event.action == 'synchronize' }}
steps:
- name: size-label
uses: "pascalgn/size-label-action@v0.5.5"
# Automatically adds size labels based on total changed lines
- name: Label by size
uses: pascalgn/size-label-action@v0.5.5
env:
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
with:
@@ -58,3 +73,41 @@ jobs:
"50": "medium",
"250": "large"
}
label-by-date:
name: "Date label (Quarters)"
runs-on: ubuntu-latest
if: ${{ github.event.action == 'opened' || github.event.action == 'reopened' || github.event.action == 'synchronize' }}
steps:
# Determines the label (02, 05, 08, 11) based on PR creation month
- name: Determine quarter label
env:
PR_CREATED_AT: ${{ github.event.pull_request.created_at }}
run: |
echo "PR created at: $PR_CREATED_AT"
# Extract the numeric month (e.g., 04 for April)
MONTH=$(date -d "$PR_CREATED_AT" +%m | sed 's/^0*//')
echo "Month extracted: $MONTH"
# Determine quarter-end label based on month
if [ "$MONTH" -le 2 ] || [ "$MONTH" -eq 12 ]; then
LABEL="02"
elif [ "$MONTH" -le 5 ]; then
LABEL="05"
elif [ "$MONTH" -le 8 ]; then
LABEL="08"
else
LABEL="11"
fi
# Set as environment variable for next step
echo "LABEL=${LABEL}" >> $GITHUB_ENV
# Adds the quarter label to the PR
- name: Add quarter label
uses: PauMAVA/add-remove-label-action@v1.0.3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
add: "${{ env.LABEL }}"