forked from actions/github-script
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
69 lines (59 loc) · 1.62 KB
/
Copy pathmain.ts
File metadata and controls
69 lines (59 loc) · 1.62 KB
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
import * as core from '@actions/core'
import {context, GitHub} from '@actions/github'
import * as vm from 'vm'
process.on('unhandledRejection', handleError)
main().catch(handleError)
async function main() {
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 opts: {[key: string]: any} = {}
if (debug === 'true') opts.log = console
if (userAgent != null) opts.userAgent = userAgent
if (previews != null) opts.previews = previews.split(',')
const github = new GitHub(token, opts)
const script = core.getInput('script', {required: true})
const fn = wrapFunction(script)
const result = await vm.runInNewContext(
fn,
{
...global,
github,
console,
context,
actions: {core},
require: require // Otherwise, the build step will compile this incorrectly.
},
{
lineOffset: -1
}
)
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)
}
function wrapFunction(fn: string) {
return `(async function() {
${fn}
})()`
}
function handleError(err: any) {
console.error(err)
if (err && err.message) {
core.setFailed(err.message)
} else {
core.setFailed(`Unhandled error: ${err}`)
}
}