-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
64 lines (52 loc) · 1.86 KB
/
index.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
import { setFailed } from "@actions/core";
import { context } from "@actions/github";
import { getOctokit, getOctokitLogin } from "../client/octokit";
import getUserConfig from "../client/getUserConfig";
import getTemplates from "../client/getTemplates";
import getBotCommands from "./commands/getBotCommands";
import parseComment from "./parseComment";
import Template from "../structures/Template";
import { IssueCommentEvent } from "@octokit/webhooks-types";
import { OctokitClient } from "../client/octokit";
import { CommandsActionClient } from "./types";
import { CommandsActionUserConfigInterface } from "./interfaces";
const run = async (): Promise<void> => {
try {
// Works for both issue comment and PR comment.
if (context.eventName !== "issue_comment") return;
const { owner, repo } = context.issue;
// Create CommandsActionClient object
const octokit: OctokitClient = getOctokit();
const [username, config, templates]: [
string,
CommandsActionUserConfigInterface,
Map<string, Template>
] = await Promise.all([
getOctokitLogin(octokit),
getUserConfig(octokit, owner, repo),
getTemplates(octokit, owner, repo),
]);
const commands = getBotCommands();
const client: CommandsActionClient = {
octokit: octokit,
username: username,
config: config,
templates: templates,
commands: commands,
};
const payload = context.payload as IssueCommentEvent;
if (payload.action === "created") {
const commandsToRun = parseComment(client, payload.comment);
for (const [command, args] of Object.entries(commandsToRun)) {
const command_run = client.commands.get(command);
if (command_run) {
command_run(client, payload, args, owner, repo);
}
}
}
} catch (error) {
setFailed(error.message);
}
};
// Run the script
run();