-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcommand.js
66 lines (64 loc) · 2.22 KB
/
command.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
const { Octokit } = require("octokit");
/**
* This is the implementation of your Fusebot command.
* You can run it from Slack with `/fusebot github-dispatch ...`.
* Fusebot is powered by the Fusebit integration platform, https://fusebit.io/
*
* Docs and samples are at https://github.com/fusebit/fusebot
* You can talk to a fellow developer on Slack at https://fusebit.io/contact
* Or tell us what you think with `/fusebot feedback {your-comments}`
*
* Now let's build something already.
* @param ctx {FusebotContext}
*/
module.exports = async (ctx) => {
if (ctx.body.args.length === 0 || ctx.body.args[0] === "help") {
return help(ctx);
}
if (!ctx.configuration.PAT) {
await ctx.client.send(
"Unable to find github PAT in configuration, please refer to https://github.com/fusebit/fusebot/blob/main/samples/githubDispatch/README.md for detail."
);
}
const octokit = new Octokit({ auth: ctx.configuration.PAT });
const [userRepo, workflowId, branchTagName] = ctx.body.args;
let errorMessages = []
if (!userRepo) {
isError = true;
errorMessages.push("Username/repository was not found.");
}
if (!workflowId) {
errorMessages.push("WorkflowId/workflowFileName was not found.");
}
if (!branchTagName) {
errorMessages.push("Branch name or tag name was not found.");
}
if (errorMessages.length !== 0) {
await help(ctx, errorMessages);
return;
}
await octokit.request(
"POST /repos/{owner}/{repo}/actions/workflows/{workflow_id}/dispatches",
{
owner: userRepo.split("/")[0],
repo: userRepo.split("/")[1],
workflow_id: workflowId,
ref: branchTagName,
}
);
await ctx.client.send(
`Workflow ${workflowId} on ${userRepo} triggered with branch/tag ${branchTagName}!`
);
};
const help = async (ctx, optionalMessage) => {
let messageToSend = []
if (optionalMessage) {
messageToSend = messageToSend.concat(optionalMessage)
}
messageToSend.push(`
- /fusebot github-dispatch <username>/<reponame> <workflow id> or <workflow file name ie: publish.yml> <branch name> or <tag name>
- /fusebot github-dispatch help - display this help
e.g.
/fusebot github-dispatch fusebit/fusebot publish.yaml master`);
await ctx.client.send(messageToSend.join('\n'))
};