Automating PR Tagging in GitHub with Smart Labels!

Automating PR Tagging in GitHub with Smart Labels!

Just added a helpful GitHub Actions workflow that automatically adds labels to pull requests targeting the dev branch based on the type and size of changes made. It improves our review process, helps categorize PRs, and flags critical updates immediately.


Article content

Here's the actual YAML file I used:

name: Label Pull Requests

on:
  pull_request:
    types: [opened, synchronize]

permissions:
  contents: read
  pull-requests: write
  issues: write  

jobs:
  label-pr:
    if: github.event.pull_request.head.repo.full_name == github.repository
    runs-on: ubuntu-latest

    steps:
      - name: Checkout PR
        uses: actions/checkout@v4

      - name: Get changed files
        id: changed
        uses: tj-actions/changed-files@v44
        with:
          separator: ','

      - name: Get diff stats
        id: diff
        run: |
          FILE_COUNT=$(git diff --name-only origin/${{ github.event.pull_request.base.ref }} | wc -l)
          LINE_COUNT=$(git diff --stat origin/${{ github.event.pull_request.base.ref }} | awk '{added+=$1; deleted+=$3} END {print added + deleted}')
          echo "file_count=$FILE_COUNT" >> $GITHUB_OUTPUT
          echo "line_count=$LINE_COUNT" >> $GITHUB_OUTPUT

      - name: Determine labels
        id: labels
        run: |
          FILES="${{ steps.changed.outputs.all_changed_files }}"
          LABELS=""

          if echo "$FILES" | grep -E 'App\.(js|jsx|ts|tsx)|index\.(js|ts)|navigation/|screens/|components/|context/|hooks/' > /dev/null; then
            LABELS="$LABELS rn-core-change"
          fi

          if echo "$FILES" | grep -E '\.env|env\.(ts|js)' > /dev/null; then
            LABELS="$LABELS env"
          fi

          if echo "$FILES" | grep -E 'android/|ios/|package\.json|build\.gradle|Podfile' > /dev/null; then
            LABELS="$LABELS critical-change"
          fi

          if [ "${{ steps.diff.outputs.line_count }}" -gt 500 ] || [ "${{ steps.diff.outputs.file_count }}" -gt 10 ]; then
            LABELS="$LABELS major-change"
          else
            LABELS="$LABELS minor-change"
          fi

          echo "labels=$LABELS" >> $GITHUB_OUTPUT

      - name: Apply labels to PR
        if: steps.labels.outputs.labels != ''
        uses: actions-ecosystem/action-add-labels@v1
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          labels: ${{ steps.labels.outputs.labels }}        

How it works:

  • Triggers on PRs: When a pull request is opened or updated (opened, synchronize) for the same repo.
  • Uses tj-actions/changed-files to detect changed files.

Calculates :

  • Total changed files
  • Total added/deleted lines

Adds labels like:

  • rn-core-change → Core app files (screens/components/hooks)
  • env → Environment file changes
  • critical-change → Platform-level or build tool updates
  • major-change vs. minor-change → Based on file and line count

Why this matters:

  • Reduces manual triaging of PRs
  • Helps reviewers quickly prioritize
  • Makes changelog generation easier later


Let automation handle the grunt work, so we can focus on writing great code. If you want help setting this up in your repo or team, happy to share!

#GitHubActions #DevOps #ReactNative #OpenSourceTools #Automation #CI_CD #Productivity #EngineeringExcellence

To view or add a comment, sign in

Explore content categories