forked from expo/expo-github-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreview-comment.ts
66 lines (55 loc) · 2.19 KB
/
preview-comment.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
import { getBooleanInput, getInput, setOutput, info } from '@actions/core';
import { projectDeepLink, projectInfo, projectLink, projectOwner, projectQR } from '../expo';
import { createIssueComment, pullContext } from '../github';
import { template } from '../utils';
import { executeAction } from '../worker';
export type CommentInput = ReturnType<typeof commentInput>;
export const DEFAULT_ID = `app:@{projectOwner}/{projectSlug} channel:{channel}`;
export const DEFAULT_MESSAGE =
`This pull request was automatically deployed using [Expo GitHub Actions](https://github.com/expo/expo-github-action/tree/main/preview-comment)!\n` +
`\n- Project: **@{projectOwner}/{projectSlug}**` +
`\n- Channel: **{channel}**` +
`\n\n<a href="{projectQR}"><img src="{projectQR}" height="200px" width="200px"></a>`;
export function commentInput() {
return {
channel: getInput('channel') || 'default',
comment: !getInput('comment') || getBooleanInput('comment'),
message: getInput('message') || DEFAULT_MESSAGE,
messageId: getInput('message-id') || DEFAULT_ID,
project: getInput('project'),
githubToken: getInput('github-token'),
};
}
executeAction(commentAction);
export async function commentAction(input: CommentInput = commentInput()) {
const project = await projectInfo(input.project);
if (!project.owner) {
project.owner = await projectOwner();
}
const variables: Record<string, string> = {
projectLink: projectLink(project, input.channel),
projectDeepLink: projectDeepLink(project, input.channel),
projectName: project.name,
projectOwner: project.owner || '',
projectQR: projectQR(project, input.channel),
projectSlug: project.slug,
channel: input.channel,
};
const messageId = template(input.messageId, variables);
const messageBody = template(input.message, variables);
if (!input.comment) {
info(`Skipped comment: 'comment' is disabled`);
} else {
await createIssueComment({
...pullContext(),
token: input.githubToken,
id: messageId,
body: messageBody,
});
}
for (const name in variables) {
setOutput(name, variables[name]);
}
setOutput('messageId', messageId);
setOutput('message', messageBody);
}