-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.js
More file actions
74 lines (61 loc) · 1.91 KB
/
Copy pathgithub.js
File metadata and controls
74 lines (61 loc) · 1.91 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
// GitHub integration: PR head sha, diff fetch, and review comment upsert.
const { cfg } = require('./config');
const { Octokit } = require('@octokit/rest');
const octokit = new Octokit({ auth: cfg.githubToken });
async function getPrHeadSha({ repoOwner, repoName, prNumber }) {
const { data } = await octokit.pulls.get({
owner: repoOwner,
repo: repoName,
pull_number: prNumber,
});
return data.head.sha;
}
async function fetchDiff({ repoOwner, repoName, prNumber }) {
const { data } = await octokit.pulls.get({
owner: repoOwner,
repo: repoName,
pull_number: prNumber,
mediaType: { format: 'diff' },
});
return data;
}
async function upsertReviewComment({ repoOwner, repoName, prNumber, headSha, findings }) {
const body = `<!-- turtlecode:${headSha} -->\n` + renderReview(findings);
const { data: comments } = await octokit.issues.listComments({
owner: repoOwner,
repo: repoName,
issue_number: prNumber,
});
const existing = comments.find(
(c) => typeof c.body === 'string' && c.body.startsWith('<!-- turtlecode:')
);
if (existing) {
await octokit.issues.updateComment({
owner: repoOwner,
repo: repoName,
comment_id: existing.id,
body,
});
} else {
await octokit.issues.createComment({
owner: repoOwner,
repo: repoName,
issue_number: prNumber,
body,
});
}
}
const SEVERITY_ORDER = { critical: 0, warning: 1, info: 2 };
function renderReview(findings) {
if (!findings || findings.length === 0) {
return 'TurtleCode: no issues found.';
}
const sorted = [...findings].sort(
(a, b) => SEVERITY_ORDER[a.severity] - SEVERITY_ORDER[b.severity]
);
const bullets = sorted
.map((f) => `- **${f.severity}** \`${f.file}:${f.line}\` — ${f.comment}`)
.join('\n');
return '**TurtleCode review**\n\n' + bullets;
}
module.exports = { getPrHeadSha, fetchDiff, upsertReviewComment, renderReview };