-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcheck-pr.js
More file actions
279 lines (244 loc) · 9.53 KB
/
Copy pathcheck-pr.js
File metadata and controls
279 lines (244 loc) · 9.53 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
const axios = require('axios');
const githubToken = process.env.GITHUB_TOKEN;
const { GITHUB_REPOSITORY, GITHUB_PR_NUMBER } = process.env;
const [owner, repo] = GITHUB_REPOSITORY.split('/');
async function getPRDetails(prNumber) {
const url = `https://api.github.com/repos/${owner}/${repo}/pulls/${prNumber}`;
try {
const response = await axios.get(url, {
headers: {
Authorization: `token ${githubToken}`,
},
});
return response.data;
} catch (error) {
if (error.response && error.response.status === 404) {
console.log(`PR #${prNumber} not found in repository ${owner}/${repo}, skipping...`);
return null;
} else {
throw error;
}
}
}
async function getIssueDetails(issueOwner, issueRepo, issueNumber) {
try {
const url = `https://api.github.com/repos/${issueOwner}/${issueRepo}/issues/${issueNumber}`;
const response = await axios.get(url, {
headers: {
Authorization: `token ${githubToken}`,
},
});
return response.data;
} catch (error) {
if (error.response && error.response.status === 404) {
console.log(`Issue #${issueNumber} not found in repository ${issueOwner}/${issueRepo}, skipping...`);
return null;
} else {
throw error;
}
}
}
async function getContributors() {
const url = `https://api.github.com/repos/${owner}/${repo}/contributors`;
const response = await axios.get(url, {
headers: {
Authorization: `token ${githubToken}`,
},
});
return response.data;
}
function stripHTMLTags(text) {
return text.replace(/<\/?[^>]+(>|$)/g, '');
}
function removeCodeBlocks(text) {
// Remove fenced code blocks (triple backticks or tildes)
text = text.replace(/```[\s\S]*?```/g, '');
text = text.replace(/~~~[\s\S]*?~~~/g, '');
// Remove inline code (single backticks)
text = text.replace(/`[^`]*`/g, '');
return text;
}
function extractPRReferences(text) {
// Regex to match PR references with any number of digits
const prRegex =
/(?:^|\s)(?:Fixes|Closes|Resolves|See|PR|Pull Request)?\s*(?:https?:\/\/github\.com\/([\w.-]+)\/([\w.-]+)\/pull\/(\d+)|([\w.-]+)\/([\w.-]+)#(\d+)|#(\d+))(?!\w)/gm;
const matches = [];
let match;
while ((match = prRegex.exec(text)) !== null) {
const refOwner = match[1] || match[4] || owner;
const refRepo = match[2] || match[5] || repo;
const prNumber = match[3] || match[6] || match[7];
matches.push({
owner: refOwner,
repo: refRepo,
prNumber,
});
}
return matches;
}
function extractIssueReferences(text) {
// Regex to match issue references with any number of digits
// Supports 'Fixes #123', 'owner/repo#123', 'https://github.com/owner/repo/issues/123'
const issueRegex =
/(?:^|\s)(?:Fixes|Closes|Resolves|See|Issue)?\s*(?:(?:https?:\/\/github\.com\/([\w.-]+)\/([\w.-]+)\/issues\/(\d+))|([\w.-]+)\/([\w.-]+)#(\d+)|#(\d+))(?!\w)/gm;
const issues = [];
let match;
while ((match = issueRegex.exec(text)) !== null) {
const issueOwner = match[1] || match[4] || owner;
const issueRepo = match[2] || match[5] || repo;
const issueNumber = match[3] || match[6] || match[7];
issues.push({
owner: issueOwner,
repo: issueRepo,
issueNumber,
});
}
return issues;
}
function cleanText(text) {
let cleanText = text;
cleanText = stripHTMLTags(cleanText);
cleanText = removeCodeBlocks(cleanText);
return cleanText;
}
async function checkPRLabelsAndMilestone(pr) {
const { labels: prLabels, milestone: prMilestone } = pr;
if (!prLabels || prLabels.length === 0) {
throw new Error('The PR has no labels.');
}
if (!prMilestone) {
throw new Error('The PR has no milestone.');
}
}
function isDependabotOrSnykPR(pr) {
return ((pr.user.login === 'dependabot[bot]') || (pr.user.login === 'swirlds-automation'));
}
async function processIssueReferencesInText(text) {
const issueReferences = extractIssueReferences(text);
let hasValidIssueReference = false;
if (issueReferences.length > 0) {
for (const issueRef of issueReferences) {
// Only process issues from the same repository
if (issueRef.owner === owner && issueRef.repo === repo) {
hasValidIssueReference = true;
const issue = await getIssueDetails(issueRef.owner, issueRef.repo, issueRef.issueNumber);
if (issue) {
const { labels: issueLabels, milestone: issueMilestone } = issue;
if (!issueLabels || issueLabels.length === 0) {
throw new Error(`Associated issue #${issueRef.issueNumber} has no labels.`);
}
if (!issueMilestone) {
throw new Error(`Associated issue #${issueRef.issueNumber} has no milestone.`);
}
}
} else {
console.log(
`Issue #${issueRef.issueNumber} is from a different repository (${issueRef.owner}/${issueRef.repo}), skipping...`
);
}
}
if (!hasValidIssueReference) {
throw new Error('The PR description must reference at least one issue from the current repository.');
} else {
console.log('All associated issues have labels and milestones.');
}
} else {
throw new Error('The PR description must reference at least one issue from the current repository.');
}
}
async function processPRReferencesInText(text, contributors) {
const prReferences = extractPRReferences(text);
if (prReferences.length === 0) {
console.log('No associated PRs found in PR description.');
} else {
for (const prRef of prReferences) {
// Only process PRs from the same repository
if (prRef.owner === owner && prRef.repo === repo) {
await processReferencedPR(prRef, contributors);
} else {
console.log(
`PR #${prRef.prNumber} is from a different repository (${prRef.owner}/${prRef.repo}), skipping...`
);
// Skip processing issue references from external PRs
}
}
}
}
async function processReferencedPR(prRef, contributors) {
// Attempt to fetch the PR to validate its existence
const referencedPR = await getPRDetails(prRef.prNumber);
if (!referencedPR) {
console.log(`PR #${prRef.prNumber} does not exist, skipping...`);
return; // Skip if PR not found
}
const authorLogin = referencedPR.user.login;
const isContributor = contributors.some((contributor) => contributor.login === authorLogin);
if (!isContributor) {
console.log(
`PR author ${authorLogin} is not a contributor, skipping issue matching for PR #${prRef.prNumber}.`
);
return;
}
// Clean the referenced PR body
const refPrBody = cleanText(referencedPR.body);
// Extract issue references from the referenced PR description
const refIssueReferences = extractIssueReferences(refPrBody);
if (refIssueReferences.length === 0) {
console.log(`No associated issues found in PR #${prRef.prNumber} description.`);
} else {
for (const issueRef of refIssueReferences) {
// Only process issues from the same repository
if (issueRef.owner === owner && issueRef.repo === repo) {
const issue = await getIssueDetails(
issueRef.owner,
issueRef.repo,
issueRef.issueNumber
);
if (issue) {
const { labels: issueLabels, milestone: issueMilestone } = issue;
if (!issueLabels || issueLabels.length === 0) {
throw new Error(
`Associated issue #${issueRef.issueNumber} has no labels.`
);
}
if (!issueMilestone) {
throw new Error(
`Associated issue #${issueRef.issueNumber} has no milestone.`
);
}
}
} else {
console.log(
`Issue #${issueRef.issueNumber} is from a different repository (${issueRef.owner}/${issueRef.repo}), skipping...`
);
}
}
console.log(
`PR #${prRef.prNumber} and all associated issues have labels and milestones.`
);
}
}
async function run() {
try {
const pr = await getPRDetails(GITHUB_PR_NUMBER);
if (!pr) {
throw new Error(`PR #${GITHUB_PR_NUMBER} not found.`);
}
await checkPRLabelsAndMilestone(pr);
if (isDependabotOrSnykPR(pr)) {
console.log('Dependabot or snyk PR detected. Skipping issue reference requirement.');
return;
} else {
const cleanBody = cleanText(pr.body);
await processIssueReferencesInText(cleanBody);
}
const contributors = await getContributors();
const cleanBody = cleanText(pr.body);
await processPRReferencesInText(cleanBody, contributors);
console.log('All checks completed.');
} catch (error) {
console.error(error.message);
process.exit(1);
}
}
run();