forked from hasithaishere/route-commenter-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
405 lines (356 loc) · 13.5 KB
/
index.js
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/**
* @fileoverview This GitHub Action script detects changes in route files within a monorepo,
* adds comments to the PR where route changes are detected, and requests changes if necessary.
* It also adds a specific label to the PR if route changes are found.
*/
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const { Octokit } = require('@octokit/rest');
const { context } = require('@actions/github');
// Constants
const GITHUB_TOKEN = process.env.GITHUB_TOKEN;
const COMMENT_FILE_PATH = process.env.INPUT_COMMENT_CONTENT_FILE;
const TAG_NAME = process.env.INPUT_TAG_NAME || 'change-route';
const octokit = new Octokit({ auth: GITHUB_TOKEN });
/**
* Gets the list of changed files in the pull request.
* @returns {Promise<string[]>} - An array of filenames that have been changed.
*/
async function getChangedFiles() {
const prNumber = context.payload.pull_request.number;
const owner = context.repo.owner;
const repo = context.repo.repo;
const { data: files } = await octokit.rest.pulls.listFiles({
owner,
repo,
pull_number: prNumber,
});
return files.map(file => file.filename);
}
/**
* Checks if the specified folder is a service folder by looking for a 'routes' subfolder.
* @param {string} folderPath - The path of the folder to check.
* @returns {boolean} - True if the folder is a service folder, false otherwise.
*/
function isServiceFolder(folderPath) {
return fs.existsSync(path.join(folderPath, 'routes'));
}
/**
* Recursively gets all route files in the specified folder.
* @param {string} folderPath - The path of the folder to search for route files.
* @returns {string[]} - An array of paths to the route files.
*/
function getRouteFiles(folderPath) {
let routeFiles = [];
const items = fs.readdirSync(folderPath, { withFileTypes: true });
for (const item of items) {
const itemPath = path.join(folderPath, item.name);
if (item.isDirectory()) {
if (item.name !== 'lambda' && isServiceFolder(itemPath)) {
const routesPath = path.join(itemPath, 'routes');
routeFiles = routeFiles.concat(getRouteFiles(routesPath));
} else {
routeFiles = routeFiles.concat(getRouteFiles(itemPath));
}
} else if (item.isFile() && item.name.endsWith('.js')) {
routeFiles.push(itemPath);
}
}
return routeFiles;
}
/**
* Detects the routes in the specified file that have changed based on the changed lines.
* @param {string} filePath - The path to the file to analyze.
* @param {number[]} changedLines - An array of line numbers that have changed in the file.
* @returns {Object[]} - An array of route objects that have changed.
*/
async function detectRoutesInFile(filePath, changedLines, type = 'modified') {
let data = '';
if (type === 'modified') {
data = fs.readFileSync(filePath, 'utf8');
} else if (type === 'deleted') {
const fileContent = await octokit.repos.getContent({
owner: context.repo.owner,
repo: context.repo.repo,
path: filePath,
ref: context.payload.pull_request.base.ref
});
data = Buffer.from(fileContent.data.content, 'base64').toString('utf8');
}
const lines = data.split('\n');
let routerVariable = null;
let insideRoute = false;
let startLine = 0;
let routeContent = '';
const routes = [];
for (let lineNumber = 0; lineNumber < lines.length; lineNumber++) {
const line = lines[lineNumber];
if (!routerVariable) {
const match = line.match(/const\s+(\w+)\s*=\s*express\.Router\(\)/);
if (match) {
routerVariable = match[1];
}
}
if (routerVariable) {
const routePattern = new RegExp(`^\\s*${routerVariable}\\.`);
if (routePattern.test(line) && !insideRoute) {
insideRoute = true;
startLine = lineNumber + 1;
routeContent = `${line}\n`;
// Check if the route is a single-line route
if (line.trim().endsWith(');')) {
const endLine = lineNumber + 1;
routes.push({
startLine,
endLine,
code: routeContent.trim(),
type: 'single-line',
});
insideRoute = false;
routeContent = '';
}
} else if (insideRoute) {
routeContent += `${line}\n`;
if (line.trim() === ');' || line.includes(');')) {
const endLine = lineNumber + 1;
routes.push({
startLine,
endLine,
code: routeContent.trim(),
type: 'multi-line',
});
insideRoute = false;
routeContent = '';
}
}
}
}
if (insideRoute) {
routes.push({
startLine,
endLine: lines.length,
code: routeContent.trim(),
type: 'incomplete',
});
}
return routes.filter(route => changedLines.some(line => line >= route.startLine && line <= route.endLine));
}
/**
* Gets the diff hunks for the specified file.
* @param {string} filePath - The path to the file to get the diff hunks for.
* @returns {Promise<Object[]>} - An array of diff hunk objects.
*/
async function getDiffHunks(filePath) {
const diffOutput = execSync(`git diff --unified=0 HEAD~1 HEAD ${filePath}`).toString();
const diffHunks = diffOutput.split('\n').filter(line => line.startsWith('@@')).map(hunk => {
const match = hunk.match(/@@ \-(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@/);
return {
originalStart: parseInt(match[1], 10),
newStart: parseInt(match[2], 10),
};
});
return diffHunks;
}
/**
* Finds the corresponding line number in the original file for the specified target line in the diff hunks.
* @param {Object[]} diffHunks - An array of diff hunk objects.
* @param {number} targetLine - The target line number in the diff hunks.
* @returns {number|null} - The corresponding line number in the original file, or null if not found.
*/
function findDiffHunkLineNumber(diffHunks, targetLine) {
for (const hunk of diffHunks) {
if (targetLine >= hunk.newStart) {
return targetLine - hunk.newStart + hunk.originalStart;
}
}
return null;
}
/**
* Gets the lines that need to be commented on based on the detected routes and changed lines.
* @param {Object[]} routes - An array of route objects.
* @param {number[]} changedLines - An array of changed line numbers.
* @returns {number[]} - An array of line numbers to be commented on.
*/
function getCommentingLines(routes, changedLines) {
const commentingLines = [];
changedLines.sort().forEach((line) => {
routes.forEach((route, index) => {
if (route.selectedLine === undefined && line >= route.startLine && line <= route.endLine) {
routes[index]['selectedLine'] = line;
commentingLines.push(line);
}
});
});
return commentingLines;
}
/**
* Gets the existing comments made by the bot in the pull request.
* @param {string} owner - The owner of the repository.
* @param {string} repo - The name of the repository.
* @param {number} pullNumber - The pull request number.
* @param {string} botUsername - The username of the bot.
* @returns {Promise<Object[]>} - An array of existing comment objects.
*/
async function getExistingComments(owner, repo, pullNumber, botUsername) {
const { data: comments } = await octokit.rest.pulls.listReviewComments({
owner,
repo,
pull_number: pullNumber,
});
return comments.filter(comment => comment.user.login === botUsername);
}
/**
* Adds comments to the pull request for the specified lines and file.
* @param {number[]} commentingLines - An array of line numbers to comment on.
* @param {string} file - The path to the file to comment on.
* @param {Object[]} existingComments - An array of existing comment objects.
* @param {string} commentBody - The body of the comment to add.
* @returns {Promise<Object>} - An object indicating whether a comment was added.
*/
async function addPRComments(commentingLines, file, existingComments, commentBody, side = 'RIGHT') {
let commentAdded = false;
if (commentingLines.length > 0) {
const { data: pr } = await octokit.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.payload.pull_request.number,
});
for (const line of commentingLines) {
const existingComment = existingComments.find(comment => comment.path === file && comment.original_line === line);
if (!existingComment) {
await octokit.rest.pulls.createReviewComment({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.payload.pull_request.number,
body: commentBody,
commit_id: pr.head.sha,
path: file,
line: line,
side
});
commentAdded = true;
await new Promise(resolve => setTimeout(resolve, 1000));
}
}
}
return { commentAdded };
}
/**
* Gets the body of the comment from the specified file or returns a default comment body.
* @returns {Promise<string>} - The body of the comment.
*/
async function getCommentBody() {
// Default comment body
let commentBody = `
### Route Changed
- [ ] Fully reviewed all the configuration changes and fully aware the effect of the route
`;
try {
commentBody = fs.readFileSync(COMMENT_FILE_PATH, 'utf8');
} catch (error) {
console.error('👿 Unable to read comment content file. Using default comment body.');
}
return commentBody;
}
/**
* Adds a label to the pull request if it does not already exist.
* @param {string} owner - The owner of the repository.
* @param {string} repo - The name of the repository.
* @param {number} prNumber - The pull request number.
* @param {string} labelName - The name of the label to add.
* @returns {Promise<void>}
*/
async function addLabelIfNotExists(owner, repo, prNumber, labelName) {
const { data: { labels } } = await octokit.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.payload.pull_request.number,
});
const labelExists = labels.some(label => label.name === labelName);
if (!labelExists) {
await octokit.rest.issues.setLabels({
owner,
repo,
issue_number: prNumber,
labels: [labelName],
});
}
}
/**
* Main function to execute the GitHub Action.
* @returns {Promise<void>}
*/
async function main() {
const rootPath = 'service';
const changedFiles = await getChangedFiles();
const botUsername = 'github-actions[bot]';
// Configure Git user
try {
execSync('git config --global user.email "action@github.com"');
execSync('git config --global user.name "GitHub Action"');
} catch (error) {
console.error(`👿 Error setting Git config: ${error.message}`);
}
const commentBody = await getCommentBody();
let commentAdded = false;
for (const file of changedFiles) {
if (file.startsWith(rootPath) && file.includes('routes') && file.endsWith('.js')) {
const diffOutput = execSync(`git diff --unified=0 HEAD~1 HEAD ${file}`).toString();
const changedLines = diffOutput
.split('\n')
.filter(line => line.startsWith('@@'))
.flatMap(line => {
const match = line.match(/\+(\d+)(,\d+)?/);
if (match) {
const start = parseInt(match[1], 10);
const count = match[2] ? parseInt(match[2].substring(1), 10) : 1;
return Array.from({ length: count }, (_, i) => start + i);
}
return [];
});
const deletedLines = diffOutput
.split('\n')
.filter(line => line.startsWith('@@'))
.flatMap(line => {
const match = line.match(/\-(\d+)(,\d+)?/);
if (match) {
const start = parseInt(match[1], 10);
const count = match[2] ? parseInt(match[2].substring(1), 10) : 1;
return Array.from({ length: count }, (_, i) => start + i);
}
return [];
});
const newBranchRoutes = await detectRoutesInFile(file, changedLines);
const commentingLinesModified = getCommentingLines(newBranchRoutes, changedLines);
const baseBranchRoutes = await detectRoutesInFile(file, deletedLines, 'deleted');
const commentingLinesDeleted = getCommentingLines(baseBranchRoutes, deletedLines);
const existingComments = await getExistingComments(context.repo.owner, context.repo.repo, context.payload.pull_request.number, botUsername);
const modifiedExistingComments = [];
const deletedExistingComments = [];
for (const existingComment of existingComments) {
if (existingComment.side === 'RIGHT') {
modifiedExistingComments.push(existingComment);
} else if (existingComment.side === 'LEFT') {
deletedExistingComments.push(existingComment);
}
}
const modifiedCommentStatus = await addPRComments(commentingLinesModified, file, modifiedExistingComments, commentBody);
const deletedCommentStatus = await addPRComments(commentingLinesDeleted, file, deletedExistingComments, commentBody, 'LEFT');
commentAdded = commentAdded || modifiedCommentStatus.commentAdded || deletedCommentStatus.commentAdded;
}
}
if (commentAdded) {
// Request changes after adding comments
await octokit.rest.pulls.createReview({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.payload.pull_request.number,
event: 'COMMENT',
body: 'Please address the comments related to the route changes.',
});
// Add 'change-route' label with red color
await addLabelIfNotExists(context.repo.owner, context.repo.repo, context.payload.pull_request.number, TAG_NAME);
}
}
main();