forked from romeovs/lcov-reporter-action
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgithub.js
98 lines (86 loc) · 2.54 KB
/
github.js
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Modified from: https://github.com/slavcodev/coverage-monitor-action
// Not needed for now, but could be useful
// const createStatus = async ({ client, context, sha, status }) =>
// client.repos.createCommitStatus({
// ...context.repo,
// sha,
// ...status,
// })
// Every comment written by our action will have this hidden
// header on top, and will be used to identify which comments
// to update/delete etc
const appendHiddenHeaderToComment = (body, hiddenHeader) => hiddenHeader + body;
const listComments = async ({ client, context, prNumber, hiddenHeader }) => {
const { data: existingComments } =
(await client.issues?.listComments({
...context.repo,
issue_number: prNumber,
})) || {};
return (
existingComments?.filter(({ body }) => body.startsWith(hiddenHeader)) ??
[]
);
};
const insertComment = ({ client, context, prNumber, body }, hiddenHeader) =>
client.issues?.createComment({
...context.repo,
issue_number: prNumber,
body: appendHiddenHeaderToComment(body, hiddenHeader),
});
const updateComment = ({ client, context, body, commentId }, hiddenHeader) =>
client.issues?.updateComment({
...context.repo,
comment_id: commentId,
body: appendHiddenHeaderToComment(body, hiddenHeader),
});
const deleteComments = ({ client, context, comments }) =>
Promise.all(
comments.map(({ id }) =>
client.issues?.deleteComment({
...context.repo,
comment_id: id,
}),
),
);
export const upsertComment = async ({
client,
context,
prNumber,
body,
hiddenHeader,
}) => {
if (client.issues) {
const existingComments = await listComments({
client,
context,
prNumber,
hiddenHeader,
});
const last = existingComments.pop();
await deleteComments({
client,
context,
comments: existingComments,
});
return last
? updateComment(
{
client,
context,
body,
commentId: last.id,
},
hiddenHeader,
)
: insertComment(
{
client,
context,
prNumber,
body,
},
hiddenHeader,
);
}
return "";
};