-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
67 lines (60 loc) · 2.23 KB
/
index.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
const { WebClient } = require('@slack/web-api');
const { Octokit } = require('@octokit/rest');
const Core = require('@actions/core');
const Github = require('@actions/github');
const EventHandler = require('./handler/eventHandler');
const SLACK_TOKEN = Core.getInput('SLACK_TOKEN');
const GITHUB_TOKEN = Core.getInput('GITHUB_TOKEN');
const ACTION_TYPE = Core.getInput('ACTION_TYPE');
/**
* The main function to run the GitHub Action.
* It initializes the Slack and GitHub clients, and handles different types of GitHub events.
*
* @throws Will throw an error and exit the process if there is an issue with the payload or the action type.
*/
async function run() {
const web = new WebClient(SLACK_TOKEN);
const octokit = new Octokit({ auth: GITHUB_TOKEN });
const handler = new EventHandler(octokit, web);
// Github.context provides the payload of the GitHub event that triggered the action.
// The structure of the payload object depends on the type of event that triggered the workflow.
// For instance, for a pull request event, it will contain details about the pull request.
// FYI: https://docs.github.com/ko/actions/learn-github-actions/contexts
const { context } = Github;
if (!context.payload) {
console.error('Invalid payload');
process.exit(1);
}
try {
switch (ACTION_TYPE) {
case 'schedule':
await handler.handleSchedule(context.payload);
break;
case 'approve':
await handler.handleApprove(context.payload);
break;
case 'comment':
await handler.handleComment(context.payload);
break;
case 'review_requested':
case 'changes_requested':
await handler.handleReviewRequested(context.payload);
break;
case 'deploy': {
const ec2Name = Core.getInput('EC2_NAME');
const imageTag = Core.getInput('IMAGE_TAG');
const jobStatus = Core.getInput('JOB_STATUS');
await handler.handleDeploy(context, ec2Name, imageTag, jobStatus);
break;
}
default:
console.error('Unknown action type:', ACTION_TYPE);
process.exit(1);
}
} catch (error) {
console.error('Error executing action:', error);
process.exit(1);
}
console.log('Message sent to Slack!');
}
run();