-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.ts
215 lines (193 loc) · 6.81 KB
/
main.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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import * as core from '@actions/core'
import * as github from '@actions/github'
import {inspect} from 'util'
import {
getInputs,
tokeniseCommand,
getCommandsConfig,
configIsValid,
actorHasPermission,
getSlashCommandPayload
} from './command-helper'
import {GitHubHelper, ClientPayload} from './github-helper'
import * as utils from './utils'
async function run(): Promise<void> {
try {
// Check required context properties exist (satisfy type checking)
if (
!github.context.payload.action ||
!github.context.payload.issue ||
!github.context.payload.comment
) {
throw new Error('Required context properties are missing.')
}
// Only handle 'created' and 'edited' event types
if (!['created', 'edited'].includes(github.context.payload.action)) {
core.warning(
`Event type '${github.context.payload.action}' not supported.`
)
return
}
// Get action inputs
const inputs = getInputs()
core.debug(`Inputs: ${inspect(inputs)}`)
// Check required inputs
if (!inputs.token) {
throw new Error(`Missing required input 'token'.`)
}
// Get configuration for registered commands
const config = getCommandsConfig(inputs)
core.debug(`Commands config: ${inspect(config)}`)
// Check the config is valid
const configError = configIsValid(config)
if (configError) {
throw new Error(configError)
}
// Get the comment body and id
const commentBody: string = github.context.payload.comment.body
const commentId: number = github.context.payload.comment.id
core.debug(`Comment body: ${commentBody}`)
core.debug(`Comment id: ${commentId}`)
// Check if the first line of the comment is a slash command
const firstLine = commentBody.split(/\r?\n/)[0].trim()
if (firstLine.length < 2 || firstLine.charAt(0) != '/') {
console.debug(
'The first line of the comment is not a valid slash command.'
)
return
}
// Tokenise the first line (minus the leading slash)
const commandTokens = tokeniseCommand(firstLine.slice(1))
core.debug(`Command tokens: ${inspect(commandTokens)}`)
// Check if the command is registered for dispatch
let configMatches = config.filter(function (cmd) {
return cmd.command == commandTokens[0]
})
core.debug(`Config matches on 'command': ${inspect(configMatches)}`)
if (configMatches.length == 0) {
core.info(`Command '${commandTokens[0]}' is not registered for dispatch.`)
return
}
// Filter matching commands by issue type
const isPullRequest = 'pull_request' in github.context.payload.issue
configMatches = configMatches.filter(function (cmd) {
return (
cmd.issue_type == 'both' ||
(cmd.issue_type == 'issue' && !isPullRequest) ||
(cmd.issue_type == 'pull-request' && isPullRequest)
)
})
core.debug(`Config matches on 'issue_type': ${inspect(configMatches)}`)
if (configMatches.length == 0) {
const issueType = isPullRequest ? 'pull request' : 'issue'
core.info(
`Command '${commandTokens[0]}' is not configured for the issue type '${issueType}'.`
)
return
}
// Filter matching commands by whether or not to allow edits
if (github.context.payload.action == 'edited') {
configMatches = configMatches.filter(function (cmd) {
return cmd.allow_edits
})
core.debug(`Config matches on 'allow_edits': ${inspect(configMatches)}`)
if (configMatches.length == 0) {
core.info(
`Command '${commandTokens[0]}' is not configured to allow edits.`
)
return
}
}
// Create github clients
const githubHelper = new GitHubHelper(inputs.token)
const githubHelperReaction = new GitHubHelper(inputs.reactionToken)
// At this point we know the command is registered
// Add the "eyes" reaction to the comment
if (inputs.reactions)
await githubHelperReaction.tryAddReaction(
github.context.repo,
commentId,
'eyes'
)
// Get the actor permission
const actorPermission = await githubHelper.getActorPermission(
github.context.repo,
github.context.actor
)
core.debug(`Actor permission: ${actorPermission}`)
// Filter matching commands by the user's permission level
configMatches = configMatches.filter(function (cmd) {
return actorHasPermission(actorPermission, cmd.permission)
})
core.debug(`Config matches on 'permission': ${inspect(configMatches)}`)
if (configMatches.length == 0) {
core.info(
`Command '${commandTokens[0]}' is not configured for the user's permission level '${actorPermission}'.`
)
return
}
// Determined that the command should be dispatched
core.info(`Command '${commandTokens[0]}' to be dispatched.`)
// Define payload
const clientPayload: ClientPayload = {
github: github.context
}
// Truncate the body to keep the size of the payload under the max
if (
clientPayload.github.payload.issue &&
clientPayload.github.payload.issue.body
) {
clientPayload.github.payload.issue.body =
clientPayload.github.payload.issue.body.slice(0, 1000)
}
// Get the pull request context for the dispatch payload
if (isPullRequest) {
const pullRequest = await githubHelper.getPull(
github.context.repo,
github.context.payload.issue.number
)
// Truncate the body to keep the size of the payload under the max
if (pullRequest.body) {
pullRequest.body = pullRequest.body.slice(0, 1000)
}
clientPayload['pull_request'] = pullRequest
}
// Dispatch for each matching configuration
for (const cmd of configMatches) {
// Generate slash command payload
clientPayload.slash_command = getSlashCommandPayload(
commandTokens,
cmd.static_args
)
core.debug(
`Slash command payload: ${inspect(clientPayload.slash_command)}`
)
// Dispatch the command
await githubHelper.createDispatch(cmd, clientPayload)
}
// Add the "rocket" reaction to the comment
if (inputs.reactions)
await githubHelperReaction.tryAddReaction(
github.context.repo,
commentId,
'rocket'
)
} catch (error) {
core.debug(inspect(error))
const message: string = utils.getErrorMessage(error)
// Handle validation errors from workflow dispatch
if (
message.startsWith('Unexpected inputs provided') ||
(message.startsWith('Required input') &&
message.endsWith('not provided')) ||
message.startsWith('No ref found for:') ||
message == `Workflow does not have 'workflow_dispatch' trigger`
) {
core.setOutput('error-message', message)
core.warning(message)
} else {
core.setFailed(message)
}
}
}
run()