Skip to content

Commit 3aa8603

Browse files
authored
提取 pr-size-label 工作流中的 js 代码到独立文件 (HMCL-dev#6458)
1 parent ca7a61e commit 3aa8603

2 files changed

Lines changed: 121 additions & 132 deletions

File tree

.github/scripts/pr-size-label.js

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
module.exports = async ({ github, context, core }) => {
2+
const owner = context.repo.owner;
3+
const repo = context.repo.repo;
4+
const ignoredFilePatterns = [
5+
/(?:^|\/)\.cnb\/.+\.ya?ml$/i,
6+
/(?:^|\/)\.gitee\/.+\.ya?ml$/i,
7+
/(?:^|\/)\.github\/.+\.ya?ml$/i,
8+
/(?:^|\/)\.idea\/.+\.xml$/i,
9+
/(?:^|\/)\.editorconfig$/i,
10+
/(?:^|\/)\.gitattributes$/i,
11+
/(?:^|\/)\.gitignore$/i,
12+
/(?:^|\/).+\.kts$/i,
13+
/(?:^|\/).+\.md$/i,
14+
/(?:^|\/)LICENSE(?:_.*)?$/i,
15+
/(?:^|\/)gradlew(?:\.bat|\.ps1)?$/i,
16+
/(?:^|\/)gradle\/wrapper\/gradle-wrapper\.properties$/i,
17+
/(?:^|\/)lang\/.+\.properties$/i,
18+
/(?:^|\/)l10n\/.+\.properties$/i,
19+
/(?:^|\/)checkstyle\.xml$/i,
20+
/(?:^|\/).+\.(?:png|jpe?g|gif|ico|svg|json|ttf|woff2?|webp|avif|jxl|heic|mp4|webm|mp3|ogg|wav|txt|toml)$/i
21+
];
22+
23+
async function processPR(issueNumber) {
24+
const files = await github.paginate(github.rest.pulls.listFiles, {
25+
owner, repo, pull_number: issueNumber, per_page: 100,
26+
});
27+
28+
const countedFiles = [];
29+
const ignoredFiles = [];
30+
let totalChanges = 0;
31+
32+
for (const file of files) {
33+
const ignored = ignoredFilePatterns.some(pattern => pattern.test(file.filename));
34+
if (ignored) {
35+
ignoredFiles.push(file.filename);
36+
continue;
37+
}
38+
39+
const fileChanges = file.changes ?? ((file.additions || 0) + (file.deletions || 0));
40+
totalChanges += fileChanges;
41+
countedFiles.push(`${file.filename} (${fileChanges})`);
42+
}
43+
44+
let targetLabel;
45+
if (totalChanges <= 9) targetLabel = "1+";
46+
else if (totalChanges <= 39) targetLabel = "10+";
47+
else if (totalChanges <= 99) targetLabel = "40+";
48+
else if (totalChanges <= 499) targetLabel = "100+";
49+
else if (totalChanges <= 999) targetLabel = "500+";
50+
else if (totalChanges <= 1999) targetLabel = "1000+";
51+
else if (totalChanges <= 4999) targetLabel = "2000+";
52+
else targetLabel = "5000+";
53+
54+
const currentLabels = await github.paginate(github.rest.issues.listLabelsOnIssue, {
55+
owner, repo, issue_number: issueNumber, per_page: 100,
56+
});
57+
58+
const sizeLabels = currentLabels.map(label => label.name).filter(name => /^\d+\+$/.test(name));
59+
60+
for (const name of sizeLabels) {
61+
if (name !== targetLabel) {
62+
await github.rest.issues.removeLabel({
63+
owner, repo, issue_number: issueNumber, name,
64+
}).catch(() => { });
65+
}
66+
}
67+
68+
if (!currentLabels.some(label => label.name === targetLabel)) {
69+
await github.rest.issues.addLabels({
70+
owner, repo, issue_number: issueNumber, labels: [targetLabel],
71+
});
72+
}
73+
74+
core.info(`Pull request #${issueNumber} has ${totalChanges} counted changed lines, applied ${targetLabel}.`);
75+
core.info(`Ignored files: ${ignoredFiles.length ? ignoredFiles.join(", ") : "none"}`);
76+
core.info(`Counted files: ${countedFiles.length ? countedFiles.join(", ") : "none"}`);
77+
}
78+
79+
if (context.eventName === "workflow_dispatch") {
80+
core.info("Manual trigger detected. Fetching all open pull requests...");
81+
82+
const openPRs = await github.paginate(github.rest.pulls.list, {
83+
owner, repo, state: "open", per_page: 100,
84+
});
85+
86+
const unlabelledPRs = openPRs.filter(pr => {
87+
return !pr.labels.some(label => /^\d+\+$/.test(label.name));
88+
});
89+
90+
core.info(`Found ${openPRs.length} open pull request(s), ${unlabelledPRs.length} of them lack a size label.`);
91+
92+
for (const pr of unlabelledPRs) {
93+
core.info(`----------------------------------------`);
94+
core.info(`Processing unlabelled pull request #${pr.number}: ${pr.title}`);
95+
await processPR(pr.number);
96+
}
97+
} else {
98+
const pr = context.payload.pull_request;
99+
if (!pr) {
100+
core.info("No pull request found in payload, skipping.");
101+
return;
102+
}
103+
await processPR(pr.number);
104+
}
105+
}

.github/workflows/pr-size-label.yml

Lines changed: 16 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -3,145 +3,29 @@ name: PR Size Label
33
on:
44
pull_request_target:
55
types:
6-
- opened
7-
- reopened
8-
- synchronize
6+
- opened
7+
- reopened
8+
- synchronize
99
workflow_dispatch:
1010

11-
1211
permissions:
1312
contents: read
1413
issues: write
1514
pull-requests: write
1615

1716
jobs:
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

Comments
 (0)