|
| 1 | +name: "Pull Request Labeler" |
| 2 | +on: |
| 3 | + pull_request_review: |
| 4 | + types: [submitted] |
| 5 | + pull_request_review_comment: |
| 6 | + types: [created, deleted] |
| 7 | + pull_request_target: |
| 8 | + types: [opened, edited, reopened, synchronize] |
| 9 | + |
| 10 | +jobs: |
| 11 | + labeler: |
| 12 | + permissions: |
| 13 | + contents: read |
| 14 | + pull-requests: write |
| 15 | + runs-on: ubuntu-latest |
| 16 | + steps: |
| 17 | + - name: Checkout repository |
| 18 | + uses: actions/checkout@v3 |
| 19 | + |
| 20 | + - name: Labeler |
| 21 | + uses: actions/labeler@v5 |
| 22 | + with: |
| 23 | + repo-token: ${{ secrets.GITHUB_TOKEN }} |
| 24 | + |
| 25 | + - name: Fetch all branches |
| 26 | + run: git fetch --all |
| 27 | + |
| 28 | + - name: Determine base and head branches |
| 29 | + id: branches |
| 30 | + run: | |
| 31 | + base_branch=$(jq -r '.pull_request.base.ref' "$GITHUB_EVENT_PATH") |
| 32 | + head_branch=$(jq -r '.pull_request.head.ref' "$GITHUB_EVENT_PATH") |
| 33 | + echo $base_branch |
| 34 | + echo $head_branch |
| 35 | + echo "base_branch=$base_branch" >> $GITHUB_ENV |
| 36 | + echo "head_branch=$head_branch" >> $GITHUB_ENV |
| 37 | +
|
| 38 | + - name: Calculate diff size |
| 39 | + id: diff |
| 40 | + env: |
| 41 | + BASE_BRANCH: ${{ env.base_branch }} |
| 42 | + HEAD_BRANCH: ${{ env.head_branch }} |
| 43 | + run: | |
| 44 | + echo $BASE_BRANCH |
| 45 | + echo $HEAD_BRANCH |
| 46 | + git checkout $HEAD_BRANCH |
| 47 | + git fetch origin $BASE_BRANCH |
| 48 | + diff_output=$(git diff --shortstat origin/$BASE_BRANCH...$HEAD_BRANCH) |
| 49 | + echo $diff_output |
| 50 | + insertions=$(echo $diff_output | awk '{print ($4 == "" ? 0 : $4)}') |
| 51 | + deletions=$(echo $diff_output | awk '{print ($6 == "" ? 0 : $6)}') |
| 52 | + changed_lines=$((insertions + deletions)) |
| 53 | + echo $changed_lines |
| 54 | + echo "changed_lines=$changed_lines" >> $GITHUB_ENV |
| 55 | +
|
| 56 | + - name: Determine label |
| 57 | + env: |
| 58 | + CHANGED_LINES: ${{ env.changed_lines }} |
| 59 | + id: label |
| 60 | + run: | |
| 61 | + changed_lines=$CHANGED_LINES |
| 62 | + if [ "$changed_lines" -le 9 ]; then |
| 63 | + label="size:s" |
| 64 | + elif [ "$changed_lines" -le 50 ]; then |
| 65 | + label="size:m" |
| 66 | + elif [ "$changed_lines" -le 100 ]; then |
| 67 | + label="size:l" |
| 68 | + elif [ "$changed_lines" -le 500 ]; then |
| 69 | + label="size:xl" |
| 70 | + else |
| 71 | + label="size:xxl" |
| 72 | + fi |
| 73 | + echo "label=$label" >> $GITHUB_ENV |
| 74 | +
|
| 75 | + - name: Fetch current labels |
| 76 | + id: fetch_labels |
| 77 | + uses: actions/github-script@v7 |
| 78 | + with: |
| 79 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 80 | + script: | |
| 81 | + const { data: labels } = await github.rest.issues.listLabelsOnIssue({ |
| 82 | + owner: context.repo.owner, |
| 83 | + repo: context.repo.repo, |
| 84 | + issue_number: context.issue.number, |
| 85 | + }); |
| 86 | + return labels.map(label => label.name); |
| 87 | +
|
| 88 | + - name: Remove old size labels |
| 89 | + id: remove_labels |
| 90 | + uses: actions/github-script@v7 |
| 91 | + env: |
| 92 | + NEW_SIZE_LABEL: ${{ env.label }} |
| 93 | + with: |
| 94 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 95 | + script: | |
| 96 | + const sizeLabels = ['size:s', 'size:m', 'size:l', 'size:xl', 'size:xxl']; |
| 97 | + const newLabel = "${{ env.NEW_SIZE_LABEL }}"; |
| 98 | + const currentLabels = ${{ steps.fetch_labels.outputs.result }}; |
| 99 | + const labelsToRemove = currentLabels.filter(label => sizeLabels.includes(label) && label !== newLabel); |
| 100 | + for (const label of labelsToRemove) { |
| 101 | + await github.rest.issues.removeLabel({ |
| 102 | + owner: context.repo.owner, |
| 103 | + repo: context.repo.repo, |
| 104 | + issue_number: context.issue.number, |
| 105 | + name: label, |
| 106 | + }); |
| 107 | + } |
| 108 | +
|
| 109 | + - name: Add label to PR |
| 110 | + uses: actions-ecosystem/action-add-labels@v1 |
| 111 | + with: |
| 112 | + github_token: ${{ secrets.GITHUB_TOKEN }} |
| 113 | + labels: ${{ env.label }} |
| 114 | + |
| 115 | + add_lgtm_label: |
| 116 | + runs-on: ubuntu-latest |
| 117 | + if: github.event.review.state == 'APPROVED' |
| 118 | + |
| 119 | + steps: |
| 120 | + - name: Checkout repository |
| 121 | + uses: actions/checkout@v3 |
| 122 | + |
| 123 | + - name: Add LGTM label |
| 124 | + uses: actions-ecosystem/action-add-labels@v1 |
| 125 | + with: |
| 126 | + github_token: ${{ secrets.GITHUB_TOKEN }} |
| 127 | + labels: lgtm |
0 commit comments