Repository navigation
Expand file tree
/
Copy pathupdate-mutation-pr-comment.js
More file actions
52 lines (46 loc) · 1.53 KB
/
Copy pathupdate-mutation-pr-comment.js
File metadata and controls
52 lines (46 loc) · 1.53 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
const fs = require('fs');
function buildMutationSummaryCommentBody(marker, summaryMarkdown) {
return `${marker}\n${summaryMarkdown}`;
}
function selectExistingMutationComment(comments, marker, botLogin = 'github-actions[bot]') {
const matches = comments.filter((comment) =>
comment &&
comment.user &&
comment.user.login === botLogin &&
typeof comment.body === 'string' &&
comment.body.includes(marker));
return matches.length === 0 ? undefined : matches[matches.length - 1];
}
async function upsertMutationSummaryComment({ github, context, summaryPath, marker = '<!-- folderdiff-mutation-summary -->', botLogin = 'github-actions[bot]' }) {
const body = buildMutationSummaryCommentBody(marker, fs.readFileSync(summaryPath, 'utf8'));
const issue_number = context.issue.number;
const { owner, repo } = context.repo;
const comments = await github.paginate(github.rest.issues.listComments, {
owner,
repo,
issue_number,
per_page: 100,
});
const existing = selectExistingMutationComment(comments, marker, botLogin);
if (existing) {
await github.rest.issues.updateComment({
owner,
repo,
comment_id: existing.id,
body,
});
return { action: 'updated', commentId: existing.id };
}
const created = await github.rest.issues.createComment({
owner,
repo,
issue_number,
body,
});
return { action: 'created', commentId: created.data.id };
}
module.exports = {
buildMutationSummaryCommentBody,
selectExistingMutationComment,
upsertMutationSummaryComment,
};