Skip to content

Commit f12b97c

Browse files
authored
Add autolabeler github workflow (#247)
1 parent e3345f6 commit f12b97c

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed

.github/autolabeler-config.json

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
2+
{
3+
"breaking change": [
4+
{
5+
"fileStatus": "renamed",
6+
"includeGlobs": ["ct/**", "install/**", "misc/**", "turnkey/**", "vm/**"],
7+
"excludeGlobs": []
8+
},
9+
{
10+
"fileStatus": "removed",
11+
"includeGlobs": ["ct/**", "install/**", "misc/**", "turnkey/**", "vm/**"],
12+
"excludeGlobs": []
13+
}
14+
],
15+
"new script": [
16+
{
17+
"fileStatus": "added",
18+
"includeGlobs": ["ct/**", "install/**", "misc/**", "turnkey/**", "vm/**"],
19+
"excludeGlobs": []
20+
}
21+
],
22+
"update script": [
23+
{
24+
"fileStatus": "modified",
25+
"includeGlobs": ["ct/**", "install/**", "misc/**", "turnkey/**", "vm/**"],
26+
"excludeGlobs": ["misc/build.func", "misc/install.func"]
27+
}
28+
],
29+
"delete script": [
30+
{
31+
"fileStatus": "removed",
32+
"includeGlobs": ["ct/**", "install/**", "misc/**", "turnkey/**", "vm/**"],
33+
"excludeGlobs": []
34+
}
35+
],
36+
"rename script": [
37+
{
38+
"fileStatus": "renamed",
39+
"includeGlobs": ["ct/**", "install/**", "misc/**", "turnkey/**", "vm/**"],
40+
"excludeGlobs": []
41+
}
42+
],
43+
"frontend": [
44+
{
45+
"fileStatus": null,
46+
"includeGlobs": ["frontend/**"],
47+
"excludeGlobs": []
48+
}
49+
],
50+
"documentation": [
51+
{
52+
"fileStatus": null,
53+
"includeGlobs": ["json/**"],
54+
"excludeGlobs": []
55+
}
56+
],
57+
"maintenance": [
58+
{
59+
"fileStatus": null,
60+
"includeGlobs": ["*.md", ".github/**"],
61+
"excludeGlobs": []
62+
}
63+
],
64+
"high risk": [
65+
{
66+
"fileStatus": null,
67+
"includeGlobs": ["misc/build.func", "misc/install.func"],
68+
"excludeGlobs": []
69+
}
70+
]
71+
}

.github/workflows/autolabeler.yml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
name: Auto Label Pull Requests
2+
3+
on:
4+
pull_request_target:
5+
branches: ["main"]
6+
types: [opened, synchronize, reopened, edited]
7+
8+
jobs:
9+
autolabeler:
10+
runs-on: ubuntu-latest
11+
permissions:
12+
pull-requests: write
13+
env:
14+
CONFIG_PATH: .github/autolabeler-config.json
15+
steps:
16+
- name: Checkout repository
17+
uses: actions/checkout@v4
18+
19+
- name: Install minimatch
20+
run: npm install minimatch
21+
22+
- name: Label PR based on config rules
23+
uses: actions/github-script@v7
24+
with:
25+
script: |
26+
const fs = require('fs').promises;
27+
const path = require('path');
28+
const { minimatch } = require('minimatch');
29+
30+
const configPath = path.resolve(process.env.CONFIG_PATH);
31+
const fileContent = await fs.readFile(configPath, 'utf-8');
32+
const autolabelerConfig = JSON.parse(fileContent);
33+
34+
const prNumber = context.payload.pull_request.number;
35+
const prListFilesResponse = await github.rest.pulls.listFiles({
36+
owner: context.repo.owner,
37+
repo: context.repo.repo,
38+
pull_number: prNumber,
39+
});
40+
const prFiles = prListFilesResponse.data;
41+
42+
for (const [label, rules] of Object.entries(autolabelerConfig)) {
43+
const shouldAddLabel = prFiles.some((prFile) => {
44+
return rules.some((rule) => {
45+
const isFileStatusMatch = rule.fileStatus ? rule.fileStatus === prFile.status : true;
46+
const isIncludeGlobMatch = rule.includeGlobs.some((glob) => minimatch(prFile.filename, glob));
47+
const isExcludeGlobMatch = rule.excludeGlobs.some((glob) => minimatch(prFile.filename, glob));
48+
49+
return isFileStatusMatch && isIncludeGlobMatch && !isExcludeGlobMatch;
50+
});
51+
});
52+
53+
if (shouldAddLabel) {
54+
console.log(`Adding label ${label} to PR ${prNumber}`);
55+
await github.rest.issues.addLabels({
56+
owner: context.repo.owner,
57+
repo: context.repo.repo,
58+
issue_number: prNumber,
59+
labels: [label],
60+
});
61+
}
62+
}
63+
64+
65+

0 commit comments

Comments
 (0)