Skip to content

Commit 0f3b50e

Browse files
authored
feat(workflow): add automated pull request triage (#513)
1 parent 43c1e7c commit 0f3b50e

2 files changed

Lines changed: 141 additions & 0 deletions

File tree

.github/scripts/triagePr.js

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
module.exports = async ({github, context}) => {
2+
const owner = context.repo.owner;
3+
const repo = context.repo.repo;
4+
const pr = context.payload.pull_request;
5+
const prNumber = pr.number;
6+
const username = pr.user.login;
7+
8+
const backendFiles = [];
9+
const webFiles = [];
10+
const mobileFiles = [];
11+
const devopsFiles = [];
12+
13+
const labels = [];
14+
let primaryArea = null;
15+
let reviewer = null;
16+
17+
try {
18+
const changedFiles = await github.paginate(
19+
github.rest.pulls.listFiles,
20+
{
21+
owner,
22+
repo,
23+
pull_number: prNumber
24+
}
25+
);
26+
27+
changedFiles.forEach((file) => {
28+
const fileName = file.filename;
29+
30+
if(fileName.startsWith('apps/backend/')){
31+
backendFiles.push(fileName);
32+
}else if(fileName.startsWith('apps/web/')){
33+
webFiles.push(fileName);
34+
}else if(fileName.startsWith('apps/mobile/')){
35+
mobileFiles.push(fileName)
36+
}else if(fileName.startsWith('.github/') || fileName.startsWith('infra/') || fileName.startsWith('terraform/')){
37+
devopsFiles.push(fileName)
38+
}
39+
})
40+
41+
42+
if(backendFiles.length > 0){
43+
labels.push('backend')
44+
45+
if(!primaryArea){
46+
primaryArea = 'backend';
47+
reviewer = '@Harxhit'
48+
}
49+
}
50+
if(mobileFiles.length > 0){
51+
labels.push('mobile');
52+
if(!primaryArea){
53+
primaryArea = 'mobile';
54+
reviewer = '@blankirigaya'
55+
}
56+
}
57+
if(webFiles.length > 0){
58+
labels.push('web');
59+
if(!primaryArea){
60+
primaryArea = 'web';
61+
reviewer = '@ShantKhatri'
62+
}
63+
}
64+
if(devopsFiles.length > 0){
65+
labels.push('devops')
66+
if(!primaryArea){
67+
primaryArea = 'devops'
68+
reviewer = '@ShantKhatri'
69+
}
70+
}
71+
72+
if(labels.length === 0){
73+
return;
74+
}
75+
76+
await github.rest.issues.addLabels({
77+
owner,
78+
repo,
79+
issue_number: prNumber,
80+
labels
81+
});
82+
const body = `Hi @${username},
83+
84+
Thanks for opening this pull request.
85+
86+
This PR has been automatically classified based on the files modified.
87+
88+
### Applied Labels
89+
90+
${labels.map(label => `- ${label}`).join('\n')}
91+
92+
### Primary Review Area
93+
94+
* ${primaryArea}
95+
96+
### Reviewer
97+
98+
${reviewer} has been identified as the primary reviewer for this pull request.
99+
100+
If you have any questions regarding the affected area or implementation details, feel free to reach out to the assigned reviewer.
101+
102+
Thank you for your contribution! `;
103+
104+
105+
await github.rest.issues.createComment({
106+
owner,
107+
repo,
108+
issue_number: prNumber,
109+
body: body
110+
});
111+
112+
} catch (error) {
113+
console.error(error)
114+
}
115+
116+
}

.github/workflows/triagePr.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Pull request triage
2+
3+
on:
4+
pull_request:
5+
types: [opened]
6+
7+
permissions:
8+
pull-requests: write
9+
issues: write
10+
11+
jobs:
12+
assignLabel:
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- name: Checkout
17+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd #v6.0.2
18+
19+
- name: Triage pull request
20+
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 #v9.0.0
21+
with:
22+
github-token: ${{ secrets.GITHUB_TOKEN }}
23+
script: |
24+
const script = require('./.github/scripts/triagePr.js');
25+
await script({ github, context });

0 commit comments

Comments
 (0)