mirror of
https://github.com/actions/github-script.git
synced 2026-01-10 11:43:08 +00:00
Use TypeScript
This commit is contained in:
49
src/main.ts
Normal file
49
src/main.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import * as core from '@actions/core'
|
||||
import {context, GitHub} from '@actions/github'
|
||||
|
||||
process.on('unhandledRejection', handleError)
|
||||
main().catch(handleError)
|
||||
|
||||
async function main() {
|
||||
const AsyncFunction = Object.getPrototypeOf(async () => {}).constructor
|
||||
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 client = new GitHub(token, opts)
|
||||
const script = core.getInput('script', {required: true})
|
||||
const fn = new AsyncFunction('require', 'github', 'context', script)
|
||||
const result = await fn(require, client, context)
|
||||
|
||||
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 handleError(err: any) {
|
||||
console.error(err)
|
||||
|
||||
if (err && err.message) {
|
||||
core.setFailed(err.message)
|
||||
} else {
|
||||
core.setFailed(`Unhandled error: ${err}`)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user