-
Notifications
You must be signed in to change notification settings - Fork 73
/
pr.ts
81 lines (70 loc) · 1.9 KB
/
pr.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import * as core from '@actions/core';
import { context, getOctokit } from '@actions/github';
import * as dedent from 'dedent';
import invariant from 'ts-invariant';
import { Config } from '../config';
export async function handlePullRequestMessage(
config: Config,
projectName: string,
output: string,
): Promise<void> {
const {
githubToken,
command,
stackName,
editCommentOnPr,
} = config;
const heading = `#### :tropical_drink: \`${command}\` on ${projectName}/${stackName}`;
const summary = '<summary>Pulumi report</summary>';
const rawBody = output.substring(0, 64_000);
// a line break between heading and rawBody is needed
// otherwise the backticks won't work as intended
const body = dedent`
${heading}
<details>
${summary}
\`\`\`
${rawBody}
\`\`\`
${
rawBody.length === 64_000
? '**Warn**: The output was too long and trimmed.'
: ''
}
</details>
`;
const { payload, repo } = context;
// Assumes PR numbers are always positive.
const nr = config.commentOnPrNumber || payload.pull_request?.number;
invariant(nr, 'Missing pull request event data.');
const octokit = getOctokit(githubToken);
try {
if (editCommentOnPr) {
const { data: comments } = await octokit.rest.issues.listComments({
...repo,
issue_number: nr,
});
const comment = comments.find((comment) =>
comment.body.startsWith(heading) && comment.body.includes(summary),
);
// If comment exists, update it.
if (comment) {
await octokit.rest.issues.updateComment({
...repo,
comment_id: comment.id,
body,
});
return;
}
}
} catch {
core.warning(
'Not able to edit comment, defaulting to creating a new comment.',
);
}
await octokit.rest.issues.createComment({
...repo,
issue_number: nr,
body,
});
}