-
Notifications
You must be signed in to change notification settings - Fork 839
216 lines (183 loc) · 8.17 KB
/
pr_check.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
name: Pull Request Validation
on:
pull_request_target:
types:
- opened
- synchronize
jobs:
pr-validation:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '14'
- name: Install dependencies
run: npm install --prefix .github octokit
- name: Validate pull request
id: validate_pr
uses: actions/github-script@v4
with:
github-token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
script: |
const prNumber = context.payload.pull_request.number;
const repoOwner = context.payload.repository.owner.login;
const repoName = context.payload.repository.name;
const { data: pr } = await github.pulls.get({
owner: repoOwner,
repo: repoName,
pull_number: prNumber
});
// Check if the PR content contains an issue reference
const hasIssueReference = /#[0-9]+/.test(pr.body);
if (!hasIssueReference) {
// Close the PR
await github.pulls.update({
owner: repoOwner,
repo: repoName,
pull_number: prNumber,
state: 'closed'
});
// Comment on the closed PR
await github.issues.createComment({
owner: repoOwner,
repo: repoName,
issue_number: prNumber,
body: `Hey @${pr.user.login},\nPlease make sure to link the relevant issue using the appropriate syntax, such as "#issueNumber" 👀. \n Follow the proper guideline and make a new PR again 😀. \n Happy Hacking 💗`
});
console.log(`Closed and commented on pull request #${prNumber} due to missing issue reference.`);
return; // Stop further processing
}
// Get the issue number from the PR content
const issueNumber = pr.body.match(/#([0-9]+)/)[1];
// Get the issue details
const { data: issue } = await github.issues.get({
owner: repoOwner,
repo: repoName,
issue_number: issueNumber
});
// Check if the issue is open
if (issue.state !== 'open') {
// Close the PR
await github.pulls.update({
owner: repoOwner,
repo: repoName,
pull_number: prNumber,
state: 'closed'
});
// Comment on the closed PR
await github.issues.createComment({
owner: repoOwner,
repo: repoName,
issue_number: prNumber,
body: `Hey @${pr.user.login},\n You can't work on a closed issue 👀! \n Follow the proper guideline and make a new PR again 😀. \n Happy Hacking 💗`
});
console.log(`Closed and commented on pull request #${prNumber} due to closed issue reference.`);
return; // Stop further processing
}
// Check if the issue has the "level3" label
const hasLevel3Label = issue.labels.some(label => label.name.toLowerCase() === 'level3');
if (hasLevel3Label) {
// Get the PR files
const { data: files } = await github.pulls.listFiles({
owner: repoOwner,
repo: repoName,
pull_number: prNumber
});
// Check for required changes in the PR
const changes = [];
// Check if a new folder is created inside the "Games" folder and contains a new file
const gamesFolderPath = 'Games/';
const newFolderRegex = new RegExp(`${gamesFolderPath}([^/]+)/.*$`);
const newFolderCreated = files.some(file => newFolderRegex.test(file.filename));
// Check if a README.md file is added in the newly created folder
const readmeRegex = new RegExp(`${gamesFolderPath}([^/]+)/README.md$`);
const readmeAdded = files.some(file => readmeRegex.test(file.filename));
// Comment with the required changes, if any
if (!newFolderCreated) {
changes.push('- Please create a new folder inside the "Games" folder.');
}
if (!readmeAdded) {
changes.push('- Please add a README.md file inside the newly created folder.');
}
// Check if the main README.md file is modified
const mainReadmeModified = files.some(file => file.filename === 'README.md');
if (!mainReadmeModified) {
changes.push('- Please modify the main README.md file.');
}
// Check if an image is added to the assets/images directory
const imageAdded = files.some(file => file.filename.startsWith('assets/images/'));
if (!imageAdded) {
changes.push('- Please add an image to the assets/images directory.');
}
if (changes.length > 0) {
const comment = [
`Hello @${pr.user.login},`,
`You need to make the following changes:`,
...changes.map(change => ` ${change}`),
'',
'Hoping that you will make those changes soon 🚀'
].join('\n');
// Comment on the pull request
await github.pulls.createReview({
owner: repoOwner,
repo: repoName,
pull_number: prNumber,
event: 'REQUEST_CHANGES',
body: comment
});
// Add labels to the pull request
await github.issues.addLabels({
owner: repoOwner,
repo: repoName,
issue_number: prNumber,
labels: ['Changes Requested ⚒️', 'Pending ⏱️']
});
console.log(`Commented on pull request #${prNumber} with required changes.`);
} else {
const existingLabels = pr.labels.map(label => label.name.toLowerCase());
if (existingLabels.includes('changes requested') || existingLabels.includes('pending')) {
// Remove the "changes requested" and "pending" labels
await github.issues.removeLabel({
owner: repoOwner,
repo: repoName,
issue_number: prNumber,
name: 'Changes Requested ⚒️'
});
await github.issues.removeLabel({
owner: repoOwner,
repo: repoName,
issue_number: prNumber,
name: 'Pending ⏱️'
});
// Approve the changes
await github.pulls.createReview({
owner: repoOwner,
repo: repoName,
pull_number: prNumber,
event: 'APPROVE'
});
// Add the "under review" label
await github.issues.addLabels({
owner: repoOwner,
repo: repoName,
issue_number: prNumber,
labels: ['Pending ⏱️']
});
console.log(`Updated pull request #${prNumber} with approved changes.`);
} else {
// Add label to the pull request
await github.issues.addLabels({
owner: repoOwner,
repo: repoName,
issue_number: prNumber,
labels: ['Pending ⏱️']
});
console.log(`Pull request #${prNumber} is valid and meets the requirements.`);
}
}
} else {
console.log(`Pull request #${prNumber} does not have the "level3" label. No further validation is performed.`);
}