-
Notifications
You must be signed in to change notification settings - Fork 21
/
writeSummaryToPR.ts
63 lines (50 loc) · 1.85 KB
/
writeSummaryToPR.ts
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
import * as github from '@actions/github';
import * as core from '@actions/core';
const COMMENT_MARKER = (markerPostfix = 'root') => `<!-- vitest-coverage-report-marker-${markerPostfix} -->`;
type Octokit = ReturnType<typeof github.getOctokit>;
const writeSummaryToPR = async ({ summary, markerPostfix }: {
summary: typeof core.summary;
markerPostfix?: string;
}) => {
if (!github.context.payload.pull_request) {
core.info('[vitest-coverage-report] Not in the context of a pull request. Skipping comment creation.');
return;
}
const gitHubToken = core.getInput('github-token').trim();
const octokit: Octokit = github.getOctokit(gitHubToken);
const commentBody = `${summary.stringify()}\n\n${COMMENT_MARKER(markerPostfix)}`;
const existingComment = await findCommentByBody(octokit, COMMENT_MARKER(markerPostfix));
if (existingComment) {
await octokit.rest.issues.updateComment({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
comment_id: existingComment.id,
body: commentBody,
});
} else {
await octokit.rest.issues.createComment({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
issue_number: github.context.payload.pull_request.number,
body: commentBody,
});
}
}
async function findCommentByBody(octokit: Octokit, commentBodyIncludes: string) {
const commentsIterator = octokit.paginate.iterator(
octokit.rest.issues.listComments,
{
owner: github.context.repo.owner,
repo: github.context.repo.repo,
issue_number: github.context.payload.pull_request!.number,
}
);
for await (const { data: comments } of commentsIterator) {
const comment = comments.find((comment) => comment.body?.includes(commentBodyIncludes));
if (comment) return comment;
}
return undefined;
}
export {
writeSummaryToPR
};