1+ name : PR Labeler
2+ on :
3+ pull_request :
4+ types : [opened, synchronize, reopened]
5+
6+ jobs :
7+ label-pr :
8+ runs-on : ubuntu-latest
9+ permissions :
10+ contents : read
11+ pull-requests : write
12+ steps :
13+ - uses : actions/checkout@v3
14+
15+ - name : Label PR based on changed files
16+ uses : actions/github-script@v6
17+ with :
18+ github-token : ${{ secrets.GITHUB_TOKEN }}
19+ script : |
20+ const changedFiles = await github.paginate(
21+ github.rest.pulls.listFiles,
22+ {
23+ owner: context.repo.owner,
24+ repo: context.repo.repo,
25+ pull_number: context.issue.number,
26+ }
27+ );
28+
29+ const filePathsToLabels = {
30+ 'node/': 'c-node',
31+ 'client/': 'c-client',
32+ 'integration_test/': 'c-integration_test',
33+ 'jsonrpc/': 'c-jsonrpc',
34+ 'types/': 'c-types',
35+ 'verify/': 'c-verify'
36+ };
37+
38+ const labelsToAdd = new Set();
39+
40+ changedFiles.forEach(file => {
41+ const filePath = file.filename;
42+ for (const [path, label] of Object.entries(filePathsToLabels)) {
43+ if (filePath.startsWith(path)) {
44+ labelsToAdd.add(label);
45+ break;
46+ }
47+ }
48+ });
49+
50+ if (labelsToAdd.size > 0) {
51+ await github.rest.issues.addLabels({
52+ owner: context.repo.owner,
53+ repo: context.repo.repo,
54+ issue_number: context.issue.number,
55+ labels: Array.from(labelsToAdd)
56+ });
57+ }
0 commit comments