2019-12-08 19:17:13 +09:00
|
|
|
import * as core from '@actions/core'
|
2019-12-09 08:51:14 +09:00
|
|
|
import * as github from '@actions/github'
|
|
|
|
|
import stripAnsi from 'strip-ansi'
|
|
|
|
|
import Octokit, {IssuesCreateResponse} from '@octokit/rest'
|
|
|
|
|
import {spawnSync, SpawnSyncReturns} from 'child_process'
|
2019-12-08 19:17:13 +09:00
|
|
|
|
|
|
|
|
async function run(): Promise<void> {
|
|
|
|
|
try {
|
2019-12-08 22:10:35 +09:00
|
|
|
const result: SpawnSyncReturns<string> = spawnSync('npm', ['audit'], {
|
2019-12-09 08:51:14 +09:00
|
|
|
encoding: 'utf-8'
|
|
|
|
|
})
|
2019-12-08 19:17:13 +09:00
|
|
|
|
2019-12-08 22:10:35 +09:00
|
|
|
if (result.stderr && result.stderr.length > 0) {
|
|
|
|
|
throw new Error(result.stderr)
|
|
|
|
|
}
|
2019-12-08 19:17:13 +09:00
|
|
|
|
2019-12-08 22:10:35 +09:00
|
|
|
core.info(result.stdout)
|
|
|
|
|
|
|
|
|
|
if (result.status === 0) {
|
|
|
|
|
// vulnerabilities are not found
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
core.debug('open an issue')
|
2019-12-09 08:51:14 +09:00
|
|
|
const token: string = core.getInput('token', {required: true})
|
|
|
|
|
const client: Octokit = new github.GitHub(token)
|
|
|
|
|
|
|
|
|
|
// remove control characters and create a code block
|
|
|
|
|
const issueBody = `\`\`\`\n${stripAnsi(result.stdout)}\n\`\`\``
|
|
|
|
|
const issueOptions = {
|
|
|
|
|
title: core.getInput('issue_title'),
|
2019-12-09 09:41:29 +09:00
|
|
|
body: issueBody,
|
|
|
|
|
assignees: core
|
|
|
|
|
.getInput('issue_assignees')
|
|
|
|
|
.replace(/\s+/g, '')
|
|
|
|
|
.split(',')
|
2019-12-09 08:51:14 +09:00
|
|
|
}
|
|
|
|
|
const {
|
|
|
|
|
data: issue
|
|
|
|
|
}: Octokit.Response<IssuesCreateResponse> = await client.issues.create({
|
|
|
|
|
...github.context.repo,
|
|
|
|
|
...issueOptions
|
|
|
|
|
})
|
|
|
|
|
core.debug(`#${issue.number}`)
|
2019-12-08 19:17:13 +09:00
|
|
|
} catch (error) {
|
|
|
|
|
core.setFailed(error.message)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
run()
|