This repository has been archived by the owner on Apr 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
entrypoint.js
97 lines (84 loc) · 2.31 KB
/
entrypoint.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const {Toolkit} = require('actions-toolkit')
const tools = new Toolkit()
const commands = {
assign: doAssign,
comment: doComment,
label: doLabel
}
const command = tools.arguments._[0]
if (process.env.DEBUG === 'true') debug()
commands[command](tools.arguments)
.then(() => {
tools.exit.success('action successful')
})
.catch(err => {
tools.log.fatal(err)
tools.exit.failure('action failed')
})
/**
* Apply an assignee to the issue in this action.
*
* ex. `args = 'assign @jclem'`
*/
async function doAssign() {
filterAction(tools.arguments.action)
const assignees = tools.arguments._.slice(1)
tools.log.info('assign', assignees)
return checkStatus(
await tools.github.issues.addAssignees(tools.context.issue({assignees}))
)
}
/**
* Create a new comment on the issue in this action.
*
* ex. `args = 'comment Hello, world!'`
*/
async function doComment() {
filterAction(tools.arguments.action)
const body = tools.arguments._.slice(1).join(' ')
tools.log.info('comment', body)
return checkStatus(
await tools.github.issues.createComment(tools.context.issue({body}))
)
}
/**
* Apply a label to the issue in this action.
*
* ex. `args = 'label bug'`
*/
async function doLabel() {
filterAction(tools.arguments.action)
const labels = tools.arguments._.slice(1)
tools.log.info('label', labels)
return checkStatus(
await tools.github.issues.addLabels(tools.context.issue({labels}))
)
}
function checkStatus(result) {
if (result.status >= 200 && result.status < 300) {
return result
}
tools.exit.failure(`Received status ${result.status} from API.`)
}
function filterAction(action) {
if (!action) return
if (tools.context.payload.action !== action) {
tools.log.note(
`Action "${
tools.context.payload.action
} does not match "${action}" from arguments.`
)
tools.exit.neutral()
}
}
function debug() {
tools.log.debug('Action', tools.context.action)
tools.log.debug('Actor', tools.context.actor)
tools.log.debug('Arguments', tools.arguments)
tools.log.debug('Event', tools.context.event)
tools.log.debug('Ref', tools.context.ref)
tools.log.debug('Sha', tools.context.sha)
tools.log.debug('Workflow', tools.context.workflow)
if (process.env.DEBUG_PAYLOAD === 'true')
tools.log.debug('Payload', tools.context.payload)
}