Files
npm-audit-action/src/main.ts

55 lines
1.6 KiB
TypeScript
Raw Normal View History

2019-12-08 19:17:13 +09:00
import * as core from '@actions/core'
import * as github from '@actions/github'
import Octokit, {IssuesCreateResponse} from '@octokit/rest'
import {Audit} from './audit'
import {IssueOption} from './interface'
import * as issue from './issue'
import * as pr from './pr'
2019-12-08 19:17:13 +09:00
export async function run(): Promise<void> {
2019-12-08 19:17:13 +09:00
try {
// run `npm audit`
const audit = new Audit()
audit.run()
core.info(audit.stdout)
2019-12-08 22:10:35 +09:00
if (audit.foundVulnerability()) {
// vulnerabilities are found
2019-12-08 22:10:35 +09:00
// get GitHub information
const ctx = JSON.parse(core.getInput('github_context'))
const token: string = core.getInput('github_token', {required: true})
const client: Octokit = new github.GitHub(token)
if (ctx.event_name === 'pull_request') {
await pr.createComment(
token,
github.context.repo.owner,
github.context.repo.repo,
ctx.event.number,
audit.strippedStdout()
)
core.setFailed('This repo has some vulnerabilities')
return
} else {
core.debug('open an issue')
// remove control characters and create a code block
const issueBody = audit.strippedStdout()
const option: IssueOption = issue.getIssueOption(issueBody)
const {
data: createdIssue
}: Octokit.Response<IssuesCreateResponse> = await client.issues.create({
...github.context.repo,
...option
})
core.debug(`#${createdIssue.number}`)
core.setFailed('This repo has some vulnerabilities')
}
}
2019-12-08 19:17:13 +09:00
} catch (error) {
core.setFailed(error.message)
}
}
run()