forked from actions/github-script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
82 lines (71 loc) · 2.35 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
import * as core from '@actions/core'
import {context, getOctokit} from '@actions/github'
import {defaults as defaultGitHubOptions} from '@actions/github/lib/utils'
import {retry} from '@octokit/plugin-retry'
import {RequestRequestOptions} from '@octokit/types'
import {callAsyncFunction} from './async-function'
import * as mixins from './mixins'
import {getRetryOptions, parseNumberArray, RetryOptions} from './retry-options'
import {wrapRequire} from './wrap-require'
process.on('unhandledRejection', handleError)
main().catch(handleError)
type Options = {
log?: Console
userAgent?: string
previews?: string[]
retry?: RetryOptions
request?: RequestRequestOptions
}
async function main(): Promise<void> {
const token = core.getInput('github-token', {required: true})
const debug = core.getInput('debug')
const userAgent = core.getInput('user-agent')
const previews = core.getInput('previews')
const retries = parseInt(core.getInput('retries'))
const exemptStatusCodes = parseNumberArray(
core.getInput('retry-exempt-status-codes')
)
const [retryOpts, requestOpts] = getRetryOptions(
retries,
exemptStatusCodes,
defaultGitHubOptions
)
const opts: Options = {}
if (debug === 'true') opts.log = console
if (userAgent != null) opts.userAgent = userAgent
if (previews != null) opts.previews = previews.split(',')
if (retryOpts) opts.retry = retryOpts
if (requestOpts) opts.request = requestOpts
const github = getOctokit(token, opts, retry)
const script = core.getInput('script', {required: true})
// Using property/value shorthand on `require` (e.g. `{require}`) causes compilation errors.
const result = await callAsyncFunction(
{
require: wrapRequire,
__original_require__: __non_webpack_require__,
github,
context,
mixins
},
script
)
let encoding = core.getInput('result-encoding')
encoding = encoding ? encoding : 'json'
let output
switch (encoding) {
case 'json':
output = JSON.stringify(result)
break
case 'string':
output = String(result)
break
default:
throw new Error('"result-encoding" must be either "string" or "json"')
}
core.setOutput('result', output)
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function handleError(err: any): void {
console.error(err)
core.setFailed(`Unhandled error: ${err}`)
}