| ... | @@ -0,0 +1,166 @@ |
| 1 | name: PR Size |
| 2 | |
| 3 | on: |
| 4 | pull_request_target: |
| 5 | types: [opened, reopened, synchronize, ready_for_review, converted_to_draft] |
| 6 | |
| 7 | permissions: |
| 8 | contents: read |
| 9 | issues: write |
| 10 | pull-requests: read |
| 11 | |
| 12 | jobs: |
| 13 | label: |
| 14 | name: Label PR size |
| 15 | runs-on: ubuntu-24.04 |
| 16 | concurrency: |
| 17 | group: pr-size-${{ github.event.pull_request.number }} |
| 18 | cancel-in-progress: true |
| 19 | steps: |
| 20 | - name: Sync PR size label |
| 21 | uses: actions/github-script@v7 |
| 22 | with: |
| 23 | script: | |
| 24 | const issueNumber = context.payload.pull_request.number; |
| 25 | const additions = context.payload.pull_request.additions ?? 0; |
| 26 | const deletions = context.payload.pull_request.deletions ?? 0; |
| 27 | const changedLines = additions + deletions; |
| 28 | |
| 29 | const managedLabels = [ |
| 30 | { |
| 31 | name: "size:XS", |
| 32 | color: "0e8a16", |
| 33 | description: "0-9 changed lines (additions + deletions).", |
| 34 | }, |
| 35 | { |
| 36 | name: "size:S", |
| 37 | color: "5ebd3e", |
| 38 | description: "10-29 changed lines (additions + deletions).", |
| 39 | }, |
| 40 | { |
| 41 | name: "size:M", |
| 42 | color: "fbca04", |
| 43 | description: "30-99 changed lines (additions + deletions).", |
| 44 | }, |
| 45 | { |
| 46 | name: "size:L", |
| 47 | color: "fe7d37", |
| 48 | description: "100-499 changed lines (additions + deletions).", |
| 49 | }, |
| 50 | { |
| 51 | name: "size:XL", |
| 52 | color: "d93f0b", |
| 53 | description: "500-999 changed lines (additions + deletions).", |
| 54 | }, |
| 55 | { |
| 56 | name: "size:XXL", |
| 57 | color: "b60205", |
| 58 | description: "1,000+ changed lines (additions + deletions).", |
| 59 | }, |
| 60 | ]; |
| 61 | |
| 62 | const managedLabelNames = new Set(managedLabels.map((label) => label.name)); |
| 63 | |
| 64 | const resolveSizeLabel = (totalChangedLines) => { |
| 65 | if (totalChangedLines < 10) { |
| 66 | return "size:XS"; |
| 67 | } |
| 68 | |
| 69 | if (totalChangedLines < 30) { |
| 70 | return "size:S"; |
| 71 | } |
| 72 | |
| 73 | if (totalChangedLines < 100) { |
| 74 | return "size:M"; |
| 75 | } |
| 76 | |
| 77 | if (totalChangedLines < 500) { |
| 78 | return "size:L"; |
| 79 | } |
| 80 | |
| 81 | if (totalChangedLines < 1000) { |
| 82 | return "size:XL"; |
| 83 | } |
| 84 | |
| 85 | return "size:XXL"; |
| 86 | }; |
| 87 | |
| 88 | for (const label of managedLabels) { |
| 89 | try { |
| 90 | const { data: existing } = await github.rest.issues.getLabel({ |
| 91 | owner: context.repo.owner, |
| 92 | repo: context.repo.repo, |
| 93 | name: label.name, |
| 94 | }); |
| 95 | |
| 96 | if ( |
| 97 | existing.color !== label.color || |
| 98 | (existing.description ?? "") !== label.description |
| 99 | ) { |
| 100 | await github.rest.issues.updateLabel({ |
| 101 | owner: context.repo.owner, |
| 102 | repo: context.repo.repo, |
| 103 | name: label.name, |
| 104 | color: label.color, |
| 105 | description: label.description, |
| 106 | }); |
| 107 | } |
| 108 | } catch (error) { |
| 109 | if (error.status !== 404) { |
| 110 | throw error; |
| 111 | } |
| 112 | |
| 113 | try { |
| 114 | await github.rest.issues.createLabel({ |
| 115 | owner: context.repo.owner, |
| 116 | repo: context.repo.repo, |
| 117 | name: label.name, |
| 118 | color: label.color, |
| 119 | description: label.description, |
| 120 | }); |
| 121 | } catch (createError) { |
| 122 | if (createError.status !== 422) { |
| 123 | throw createError; |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | const nextLabelName = resolveSizeLabel(changedLines); |
| 130 | |
| 131 | const { data: currentLabels } = await github.rest.issues.listLabelsOnIssue({ |
| 132 | owner: context.repo.owner, |
| 133 | repo: context.repo.repo, |
| 134 | issue_number: issueNumber, |
| 135 | per_page: 100, |
| 136 | }); |
| 137 | |
| 138 | for (const label of currentLabels) { |
| 139 | if (!managedLabelNames.has(label.name) || label.name === nextLabelName) { |
| 140 | continue; |
| 141 | } |
| 142 | |
| 143 | try { |
| 144 | await github.rest.issues.removeLabel({ |
| 145 | owner: context.repo.owner, |
| 146 | repo: context.repo.repo, |
| 147 | issue_number: issueNumber, |
| 148 | name: label.name, |
| 149 | }); |
| 150 | } catch (removeError) { |
| 151 | if (removeError.status !== 404) { |
| 152 | throw removeError; |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | if (!currentLabels.some((label) => label.name === nextLabelName)) { |
| 158 | await github.rest.issues.addLabels({ |
| 159 | owner: context.repo.owner, |
| 160 | repo: context.repo.repo, |
| 161 | issue_number: issueNumber, |
| 162 | labels: [nextLabelName], |
| 163 | }); |
| 164 | } |
| 165 | |
| 166 | core.info(`PR #${issueNumber}: ${changedLines} changed lines -> ${nextLabelName}`); |