add test for issue option (#20)

This commit is contained in:
Naoki Oketani
2019-12-09 21:31:55 +09:00
committed by GitHub
parent 4b8e261c55
commit be0cdcbe10
5 changed files with 171 additions and 3137 deletions

44
__tests__/issue.test.ts Normal file
View File

@@ -0,0 +1,44 @@
import * as issue from '../src/issue'
import {IssueOption} from '../src/interface'
describe('getIssueOption', () => {
test('without assignee and label', () => {
process.env.INPUT_ISSUE_TITLE = 'npm audit found vulnerabilities'
const expected: IssueOption = {
title: 'npm audit found vulnerabilities',
body: 'hi',
assignees: [''],
labels: ['']
}
expect(issue.getIssueOption('hi')).toEqual(expected)
})
test('with 1 assignee and 1 label', () => {
process.env.INPUT_ISSUE_TITLE = 'npm audit found vulnerabilities'
process.env.INPUT_ISSUE_ASSIGNEES = 'alice'
process.env.INPUT_ISSUE_LABELS = 'foo'
const expected: IssueOption = {
title: 'npm audit found vulnerabilities',
body: 'hi',
assignees: ['alice'],
labels: ['foo']
}
expect(issue.getIssueOption('hi')).toEqual(expected)
})
test('with 2 assignees and 2 labels', () => {
process.env.INPUT_ISSUE_TITLE = 'npm audit found vulnerabilities'
process.env.INPUT_ISSUE_ASSIGNEES = 'alice,bob'
process.env.INPUT_ISSUE_LABELS = 'foo,bar'
const expected: IssueOption = {
title: 'npm audit found vulnerabilities',
body: 'hi',
assignees: ['alice', 'bob'],
labels: ['foo', 'bar']
}
expect(issue.getIssueOption('hi')).toEqual(expected)
})
})

3220
dist/index.js vendored

File diff suppressed because one or more lines are too long

6
src/interface.ts Normal file
View File

@@ -0,0 +1,6 @@
export interface IssueOption {
title: string
body: string
assignees?: string[]
labels?: string[]
}

17
src/issue.ts Normal file
View File

@@ -0,0 +1,17 @@
import * as core from '@actions/core'
import {IssueOption} from './interface'
export function getIssueOption(body: string): IssueOption {
return {
title: core.getInput('issue_title'),
body,
assignees: core
.getInput('issue_assignees')
.replace(/\s+/g, '')
.split(','),
labels: core
.getInput('issue_labels')
.replace(/\s+/g, '')
.split(',')
}
}

View File

@@ -3,6 +3,8 @@ import * as github from '@actions/github'
import stripAnsi from 'strip-ansi'
import Octokit, {IssuesCreateResponse} from '@octokit/rest'
import {Audit} from './audit'
import * as issue from '../src/issue'
import {IssueOption} from '../src/interface'
async function run(): Promise<void> {
try {
@@ -22,25 +24,14 @@ async function run(): Promise<void> {
// remove control characters and create a code block
const issueBody = `\`\`\`\n${stripAnsi(audit.stdout)}\n\`\`\``
const issueOptions = {
title: core.getInput('issue_title'),
body: issueBody,
assignees: core
.getInput('issue_assignees')
.replace(/\s+/g, '')
.split(','),
labels: core
.getInput('issue_labels')
.replace(/\s+/g, '')
.split(',')
}
const option: IssueOption = issue.getIssueOption(issueBody)
const {
data: issue
data: createdIssue
}: Octokit.Response<IssuesCreateResponse> = await client.issues.create({
...github.context.repo,
...issueOptions
...option
})
core.debug(`#${issue.number}`)
core.debug(`#${createdIssue.number}`)
} catch (error) {
core.setFailed(error.message)
}