@@ -3,145 +3,29 @@ name: PR Size Label
33on :
44 pull_request_target :
55 types :
6- - opened
7- - reopened
8- - synchronize
6+ - opened
7+ - reopened
8+ - synchronize
99 workflow_dispatch :
1010
11-
1211permissions :
1312 contents : read
1413 issues : write
1514 pull-requests : write
1615
1716jobs :
18- Label :
17+ label :
1918 runs-on : ubuntu-latest
20-
2119 steps :
22- - name : Label PR by filtered changed lines
23- uses : actions/github-script@v7
24- with :
25- github-token : ${{ secrets.GITHUB_TOKEN }}
26- script : |
27- const owner = context.repo.owner
28- const repo = context.repo.repo
29- const ignoredFilePatterns = [
30- /(?:^|\/)\.cnb\/.+\.ya?ml$/i,
31- /(?:^|\/)\.gemini\/.+\.ya?ml$/i,
32- /(?:^|\/)\.gitee\/.+\.ya?ml$/i,
33- /(?:^|\/)\.github\/.+\.ya?ml$/i,
34- /(?:^|\/)\.idea\/.+\.xml$/i,
35- /(?:^|\/)\.editorconfig$/i,
36- /(?:^|\/)\.gitattributes$/i,
37- /(?:^|\/)\.gitignore$/i,
38- /(?:^|\/).+\.kts$/i,
39- /(?:^|\/).+\.md$/i,
40- /(?:^|\/)LICENSE(?:_.*)?$/i,
41- /(?:^|\/)gradlew(?:\.bat|\.ps1)?$/i,
42- /(?:^|\/)gradle\/wrapper\/gradle-wrapper\.properties$/i,
43- /(?:^|\/)lang\/.+\.properties$/i,
44- /(?:^|\/)l10n\/.+\.properties$/i,
45- /(?:^|\/)checkstyle\.xml$/i,
46- /(?:^|\/).+\.(?:png|jpe?g|gif|ico|svg|json|ttf|woff2?|webp|avif|jxl|heic|mp4|webm|mp3|ogg|wav|txt|toml)$/i
47- ]
48-
49- async function processPR(issue_number) {
50- const files = await github.paginate(github.rest.pulls.listFiles, {
51- owner,
52- repo,
53- pull_number: issue_number,
54- per_page: 100,
55- })
56-
57- const countedFiles = []
58- const ignoredFiles = []
59- let totalChanges = 0
60-
61- for (const file of files) {
62- const ignored = ignoredFilePatterns.some(pattern => pattern.test(file.filename))
63- if (ignored) {
64- ignoredFiles.push(file.filename)
65- continue
66- }
67-
68- const fileChanges = file.changes ?? ((file.additions || 0) + (file.deletions || 0))
69- totalChanges += fileChanges
70- countedFiles.push(`${file.filename} (${fileChanges})`)
71- }
72-
73- let targetLabel
74- if (totalChanges <= 9) targetLabel = '1+'
75- else if (totalChanges <= 39) targetLabel = '10+'
76- else if (totalChanges <= 99) targetLabel = '40+'
77- else if (totalChanges <= 499) targetLabel = '100+'
78- else if (totalChanges <= 999) targetLabel = '500+'
79- else if (totalChanges <= 1999) targetLabel = '1000+'
80- else if (totalChanges <= 4999) targetLabel = '2000+'
81- else targetLabel = '5000+'
82-
83- const currentLabels = await github.paginate(github.rest.issues.listLabelsOnIssue, {
84- owner,
85- repo,
86- issue_number,
87- per_page: 100,
88- })
89-
90- const sizeLabels = currentLabels
91- .map(label => label.name)
92- .filter(name => /^\d+\+$/.test(name))
93-
94- for (const name of sizeLabels) {
95- if (name !== targetLabel) {
96- await github.rest.issues.removeLabel({
97- owner,
98- repo,
99- issue_number,
100- name,
101- }).catch(() => {})
102- }
103- }
104-
105- if (!currentLabels.some(label => label.name === targetLabel)) {
106- await github.rest.issues.addLabels({
107- owner,
108- repo,
109- issue_number,
110- labels: [targetLabel],
111- })
112- }
113-
114- core.info(`Pull request #${issue_number} has ${totalChanges} counted changed lines, applied ${targetLabel}.`)
115- core.info(`Ignored files: ${ignoredFiles.length ? ignoredFiles.join(', ') : 'none'}`)
116- core.info(`Counted files: ${countedFiles.length ? countedFiles.join(', ') : 'none'}`)
117- }
118-
119- if (context.eventName === 'workflow_dispatch') {
120- core.info('Manual trigger detected. Fetching all open pull requests...')
121-
122- const openPRs = await github.paginate(github.rest.pulls.list, {
123- owner,
124- repo,
125- state: 'open',
126- per_page: 100,
127- })
128-
129- const unlabelledPRs = openPRs.filter(pr => {
130- return !pr.labels.some(label => /^\d+\+$/.test(label.name))
131- })
132-
133- core.info(`Found ${openPRs.length} open pull request(s), ${unlabelledPRs.length} of them lack a size label.`)
134-
135- for (const pr of unlabelledPRs) {
136- core.info(`----------------------------------------`)
137- core.info(`Processing unlabelled pull request #${pr.number}: ${pr.title}`)
138- await processPR(pr.number)
139- }
140- } else {
141- const pr = context.payload.pull_request
142- if (!pr) {
143- core.info('No pull request found in payload, skipping.')
144- return
145- }
146- await processPR(pr.number)
147- }
20+ - name : Checkout scripts
21+ uses : actions/checkout@v7
22+ with :
23+ sparse-checkout : |
24+ .github/scripts
25+ sparse-checkout-cone-mode : true
26+ - name : Label PR by filtered changed lines
27+ uses : actions/github-script@v9
28+ with :
29+ script : |
30+ const script = require("./.github/scripts/pr-size-label.js");
31+ await script({ github, context, core });
0 commit comments