-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathcomment.ts
More file actions
49 lines (43 loc) · 1.58 KB
/
comment.ts
File metadata and controls
49 lines (43 loc) · 1.58 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
import { readFile } from 'node:fs/promises';
import { logDebug, logInfo, logWarning } from './log.js';
import type { ProviderAPIClient, Settings } from './models.js';
export async function commentOnPR(
mdPath: string,
api: ProviderAPIClient,
settings: Pick<Settings, 'jobId'>,
): Promise<number> {
const markdown = await readFile(mdPath, 'utf8');
const identifier = settings.jobId
? `<!-- generated by @code-pushup/ci [jobId=${settings.jobId}] -->`
: '<!-- generated by @code-pushup/ci -->';
const body = truncateBody(
`${markdown}\n\n${identifier}\n`,
api.maxCommentChars,
);
const comments = await api.listComments();
logDebug(`Fetched ${comments.length} comments for pull request`);
const prevComment = comments.find(comment =>
comment.body.includes(identifier),
);
logDebug(
prevComment
? `Found previous comment ${prevComment.id} from Code PushUp`
: 'Previous Code PushUp comment not found',
);
if (prevComment) {
const updatedComment = await api.updateComment(prevComment.id, body);
logInfo(`Updated body of comment ${updatedComment.url}`);
return updatedComment.id;
}
const createdComment = await api.createComment(body);
logInfo(`Created new comment ${createdComment.url}`);
return createdComment.id;
}
function truncateBody(body: string, max: number): string {
const truncateWarning = '...*[Comment body truncated]*';
if (body.length > max) {
logWarning(`Comment body is too long. Truncating to ${max} characters.`);
return body.slice(0, max - truncateWarning.length) + truncateWarning;
}
return body;
}