MCPcopy Index your code
hub / github.com/code-pushup/cli / commentOnPR

Function commentOnPR

packages/ci/src/lib/comment.ts:5–40  ·  view source on GitHub ↗
(
  mdPath: string,
  api: ProviderAPIClient,
  settings: Pick<Settings, 'jobId'>,
)

Source from the content-addressed store, hash-verified

3import type { ProviderAPIClient, Settings } from './models.js';
4
5export async function commentOnPR(
6 mdPath: string,
7 api: ProviderAPIClient,
8 settings: Pick<Settings, 'jobId'>,
9): Promise<number> {
10 const markdown = await readFile(mdPath, 'utf8');
11 const identifier = settings.jobId
12 ? `<!-- generated by @code-pushup/ci [jobId=${settings.jobId}] -->`
13 : '<!-- generated by @code-pushup/ci -->';
14 const body = truncateBody(
15 `${markdown}\n\n${identifier}\n`,
16 api.maxCommentChars,
17 );
18
19 const comments = await api.listComments();
20 logDebug(`Fetched ${comments.length} comments for pull request`);
21
22 const prevComment = comments.find(comment =>
23 comment.body.includes(identifier),
24 );
25 logDebug(
26 prevComment
27 ? `Found previous comment ${prevComment.id} from Code PushUp`
28 : 'Previous Code PushUp comment not found',
29 );
30
31 if (prevComment) {
32 const updatedComment = await api.updateComment(prevComment.id, body);
33 logInfo(`Updated body of comment ${updatedComment.url}`);
34 return updatedComment.id;
35 }
36
37 const createdComment = await api.createComment(body);
38 logInfo(`Created new comment ${createdComment.url}`);
39 return createdComment.id;
40}
41
42function truncateBody(body: string, max: number): string {
43 const truncateWarning = '...*[Comment body truncated]*';

Callers 3

runInMonorepoModeFunction · 0.85
runInStandaloneModeFunction · 0.85

Calls 4

readFileFunction · 0.85
truncateBodyFunction · 0.85
logDebugFunction · 0.85
logInfoFunction · 0.85

Tested by

no test coverage detected