Set Issue labels by inputs & add unit test (#12)

* Set Issue labels by inputs

* Add unit test
This commit is contained in:
Naoki Oketani
2019-12-09 12:43:13 +09:00
committed by GitHub
parent 9931b4d0d0
commit 14d92785e4
7 changed files with 122 additions and 25 deletions

View File

@@ -12,7 +12,7 @@
"import/no-namespace": "off", "import/no-namespace": "off",
"no-unused-vars": "off", "no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": "error", "@typescript-eslint/no-unused-vars": "error",
"@typescript-eslint/explicit-member-accessibility": ["error", {"accessibility": "no-public"}], "@typescript-eslint/explicit-member-accessibility": "off",
"@typescript-eslint/no-require-imports": "error", "@typescript-eslint/no-require-imports": "error",
"@typescript-eslint/array-type": "error", "@typescript-eslint/array-type": "error",
"@typescript-eslint/await-thenable": "error", "@typescript-eslint/await-thenable": "error",

View File

@@ -8,7 +8,8 @@ GitHub Action to run `npm audit`
|Parameter|Required|Default Value|Description| |Parameter|Required|Default Value|Description|
|:--:|:--:|:--:|:--| |:--:|:--:|:--:|:--|
|issue_assignees|false|N/A|Issue assignees (separeted by commma)| |issue_assignees|false|N/A|Issue assignees (separated by commma)|
|issue_labels|false|N/A|Issue labels (separated by commma)|
|issue_title|false|npm audit found vulnerabilities|Issue title| |issue_title|false|npm audit found vulnerabilities|Issue title|
|token|true|N/A|GitHub Access Token.<br>${{ secrets.GITHUB_TOKEN }} is recommended.| |token|true|N/A|GitHub Access Token.<br>${{ secrets.GITHUB_TOKEN }} is recommended.|

15
__tests__/audit.test.ts Normal file
View File

@@ -0,0 +1,15 @@
import {Audit} from '../src/audit'
const audit = new Audit()
describe('npm audit', () => {
test('finds vulnerabilities', () => {
audit.status = 1
expect(audit.foundVulnerability()).toBeTruthy()
})
test('does not find vulnerabilities', () => {
audit.status = 0
expect(audit.foundVulnerability()).toBeFalsy()
})
})

View File

@@ -6,7 +6,10 @@ inputs:
description: 'GitHub access token used to create an issue' description: 'GitHub access token used to create an issue'
required: true required: true
issue_assignees: issue_assignees:
description: 'Issue assignees (separeted by commma)' description: 'Issue assignees (separated by commma)'
required: false
issue_labels:
description: 'Issue assignees (separated by commma)'
required: false required: false
issue_title: issue_title:
description: 'Issue title' description: 'Issue title'

69
dist/index.js vendored
View File

@@ -3012,6 +3012,55 @@ const windowsRelease = release => {
module.exports = windowsRelease; module.exports = windowsRelease;
/***/ }),
/***/ 50:
/***/ (function(__unusedmodule, exports, __webpack_require__) {
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const child_process_1 = __webpack_require__(129);
class Audit {
constructor() {
this.stdout = '';
this.status = null;
}
run() {
return __awaiter(this, void 0, void 0, function* () {
const result = child_process_1.spawnSync('npm', ['audit'], {
encoding: 'utf-8'
});
if (result.error) {
throw result.error;
}
if (result.status === null) {
throw new Error('the subprocess terminated due to a signal.');
}
if (result.stderr && result.stderr.length > 0) {
throw new Error(result.stderr);
}
this.status = result.status;
this.stdout = result.stdout;
});
}
foundVulnerability() {
// `npm audit` return 1 when it found vulnerabilities
return this.status === 1;
}
}
exports.Audit = Audit;
/***/ }), /***/ }),
/***/ 87: /***/ 87:
@@ -4620,18 +4669,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
const core = __importStar(__webpack_require__(470)); const core = __importStar(__webpack_require__(470));
const github = __importStar(__webpack_require__(469)); const github = __importStar(__webpack_require__(469));
const strip_ansi_1 = __importDefault(__webpack_require__(90)); const strip_ansi_1 = __importDefault(__webpack_require__(90));
const child_process_1 = __webpack_require__(129); const audit_1 = __webpack_require__(50);
function run() { function run() {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
try { try {
const result = child_process_1.spawnSync('npm', ['audit'], { const audit = new audit_1.Audit();
encoding: 'utf-8' audit.run();
}); core.info(audit.stdout);
if (result.stderr && result.stderr.length > 0) { if (!audit.foundVulnerability()) {
throw new Error(result.stderr);
}
core.info(result.stdout);
if (result.status === 0) {
// vulnerabilities are not found // vulnerabilities are not found
return; return;
} }
@@ -4639,13 +4684,17 @@ function run() {
const token = core.getInput('token', { required: true }); const token = core.getInput('token', { required: true });
const client = new github.GitHub(token); const client = new github.GitHub(token);
// remove control characters and create a code block // remove control characters and create a code block
const issueBody = `\`\`\`\n${strip_ansi_1.default(result.stdout)}\n\`\`\``; const issueBody = `\`\`\`\n${strip_ansi_1.default(audit.stdout)}\n\`\`\``;
const issueOptions = { const issueOptions = {
title: core.getInput('issue_title'), title: core.getInput('issue_title'),
body: issueBody, body: issueBody,
assignees: core assignees: core
.getInput('issue_assignees') .getInput('issue_assignees')
.replace(/\s+/g, '') .replace(/\s+/g, '')
.split(','),
labels: core
.getInput('issue_labels')
.replace(/\s+/g, '')
.split(',') .split(',')
}; };
const { data: issue } = yield client.issues.create(Object.assign(Object.assign({}, github.context.repo), issueOptions)); const { data: issue } = yield client.issues.create(Object.assign(Object.assign({}, github.context.repo), issueOptions));

30
src/audit.ts Normal file
View File

@@ -0,0 +1,30 @@
import {spawnSync, SpawnSyncReturns} from 'child_process'
export class Audit {
stdout: string = ''
status: number | null = null
public async run(): Promise<void> {
const result: SpawnSyncReturns<string> = spawnSync('npm', ['audit'], {
encoding: 'utf-8'
})
if (result.error) {
throw result.error
}
if (result.status === null) {
throw new Error('the subprocess terminated due to a signal.')
}
if (result.stderr && result.stderr.length > 0) {
throw new Error(result.stderr)
}
this.status = result.status
this.stdout = result.stdout
}
public foundVulnerability(): boolean {
// `npm audit` return 1 when it found vulnerabilities
return this.status === 1
}
}

View File

@@ -2,21 +2,16 @@ import * as core from '@actions/core'
import * as github from '@actions/github' import * as github from '@actions/github'
import stripAnsi from 'strip-ansi' import stripAnsi from 'strip-ansi'
import Octokit, {IssuesCreateResponse} from '@octokit/rest' import Octokit, {IssuesCreateResponse} from '@octokit/rest'
import {spawnSync, SpawnSyncReturns} from 'child_process' import {Audit} from './audit'
async function run(): Promise<void> { async function run(): Promise<void> {
try { try {
const result: SpawnSyncReturns<string> = spawnSync('npm', ['audit'], { const audit = new Audit()
encoding: 'utf-8' audit.run()
})
if (result.stderr && result.stderr.length > 0) { core.info(audit.stdout)
throw new Error(result.stderr)
}
core.info(result.stdout) if (!audit.foundVulnerability()) {
if (result.status === 0) {
// vulnerabilities are not found // vulnerabilities are not found
return return
} }
@@ -26,13 +21,17 @@ async function run(): Promise<void> {
const client: Octokit = new github.GitHub(token) const client: Octokit = new github.GitHub(token)
// remove control characters and create a code block // remove control characters and create a code block
const issueBody = `\`\`\`\n${stripAnsi(result.stdout)}\n\`\`\`` const issueBody = `\`\`\`\n${stripAnsi(audit.stdout)}\n\`\`\``
const issueOptions = { const issueOptions = {
title: core.getInput('issue_title'), title: core.getInput('issue_title'),
body: issueBody, body: issueBody,
assignees: core assignees: core
.getInput('issue_assignees') .getInput('issue_assignees')
.replace(/\s+/g, '') .replace(/\s+/g, '')
.split(','),
labels: core
.getInput('issue_labels')
.replace(/\s+/g, '')
.split(',') .split(',')
} }
const { const {