Skip to content

Commit

Permalink
fix: add output-type parameter, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
prototypicalpro committed Jul 30, 2020
1 parent 93afc95 commit 9f29486
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 24 deletions.
153 changes: 151 additions & 2 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,152 @@
test('runs tests', async () => {
expect(true).toBe(true)
import * as cp from 'child_process'
import * as path from 'path'
import {Inputs} from '../src/inputs'

async function execAsync(
command: string,
opts: cp.ExecOptions
): Promise<{out: string; err: string; code: number}> {
return new Promise((resolve, reject) => {
cp.exec(command, opts, (err, outstd, errstd) =>
err !== null && err.code === undefined
? reject(err)
: resolve({
out: outstd,
err: errstd,
code: err !== null ? (err.code as number) : 0
})
)
})
}

async function runAction(
env: NodeJS.ProcessEnv
): Promise<{out: string; err: string; code: number}> {
const ip = path.join(__dirname, '..', 'lib', 'main.js')
return execAsync(`node ${ip}`, {env})
}

function getInputName(input: string): string {
return `INPUT_${input.replace(/ /g, '_').toUpperCase()}`
}

describe('main', () => {
beforeEach(() => {
process.env[getInputName(Inputs.REPO)] = 'newrelic/repolinter-action'
process.env[getInputName(Inputs.OUTPUT_TYPE)] = 'off'
delete process.env[getInputName(Inputs.CONFIG_FILE)]
delete process.env[getInputName(Inputs.CONFIG_URL)]
})

test('throws when no token is supplied', async () => {
delete process.env[getInputName(Inputs.TOKEN)]

const {out, code} = await runAction(process.env)

// console.debug(out)
expect(code).not.toEqual(0)
expect(out).toContain(
`::error::Input required and not supplied: ${Inputs.TOKEN}`
)
})

test('throws when an invalid token is supplied and output-type is not off', async () => {
process.env[getInputName(Inputs.TOKEN)] = '2'
process.env[getInputName(Inputs.OUTPUT_TYPE)] = 'issue'

const {out, code} = await runAction(process.env)

expect(code).not.toEqual(0)
expect(out).toContain('Bad credentials')
// console.debug(out)
})

test('throws when no output-type is supplied', async () => {
delete process.env[getInputName(Inputs.OUTPUT_TYPE)]

const {out, code} = await runAction(process.env)

expect(code).not.toEqual(0)
expect(out).toContain(
`::error::Input required and not supplied: ${Inputs.OUTPUT_TYPE}`
)
})

test('throws when an invalid output-type is supplied', async () => {
process.env[getInputName(Inputs.OUTPUT_TYPE)] = 'string-cheese'

const {out, code} = await runAction(process.env)

expect(code).not.toEqual(0)
expect(out).toContain('string-cheese')
})

test('throws when no repository is supplied', async () => {
delete process.env[getInputName(Inputs.REPO)]

const {out, code} = await runAction(process.env)

expect(code).not.toEqual(0)
expect(out).toContain(
`::error::Input required and not supplied: ${Inputs.REPO}`
)
// console.debug(out)
})

test('throws when an invalid config-url is specified', async () => {
process.env[getInputName(Inputs.CONFIG_URL)] = 'notadomain'

const {out, code} = await runAction(process.env)

expect(code).not.toEqual(0)
expect(out).toContain('notadomain')
// console.debug(out)
})

test('throws when an invalid config-file is specified', async () => {
process.env[getInputName(Inputs.CONFIG_FILE)] = 'notafile'

const {out, code} = await runAction(process.env)

expect(code).not.toEqual(0)
expect(out).toContain('notafile')
// console.debug(out)
})

test('runs a test config', async () => {
process.env[getInputName(Inputs.CONFIG_FILE)] = path.resolve(
__dirname,
'testconfig.json'
)

const {out, code} = await runAction(process.env)

// console.debug(out)
expect(code).toEqual(0)
expect(out).toContain('testconfig.json')
expect(out).not.toContain('undefined')
})

test('runs a URL config', async () => {
process.env[getInputName(Inputs.CONFIG_URL)] =
'https://raw.githubusercontent.com/aperture-science-incorporated/.github/master/repolinter.json'

const {out, code} = await runAction(process.env)

// console.debug(out)
expect(code).toEqual(0)
expect(out).toContain(
'https://raw.githubusercontent.com/aperture-science-incorporated/.github/master/repolinter.json'
)
expect(out).not.toContain('undefined')
})

test('runs the default config', async () => {
const {out, code} = await runAction(process.env)

// console.debug(out)
expect(code).toEqual(0)
expect(out).toContain('default')
expect(out).not.toContain('undefined')
})
})
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ inputs:
required: false
description: 'Repository name with owner. For example, actions/checkout'
default: ${{ github.repository }}
output-type:
required: false
description: 'Where to push the results from the Repolinter output. Can be "off" (console only) or "issue" (create a github issue).'
default: 'off'
runs:
using: 'node12'
main: 'dist/index.js'
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions src/inputs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const enum Inputs {
CONFIG_URL = 'config-url',
CONFIG_FILE = 'config-file',
TOKEN = 'token',
REPO = 'repository',
OUTPUT_TYPE = 'output-type'
}
43 changes: 23 additions & 20 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
import * as core from '@actions/core'
import * as github from '@actions/github'
import {Inputs} from './inputs'
import {lint, resultFormatter, markdownFormatter} from 'repolinter'
import getConfig from './getConfig'

const INPUT_CONFIG_URL = 'config-url'
const INPUT_CONFIG_FILE = 'config-file'
const INPUT_TOKEN = 'token'
const INPUT_REPO = 'repository'
function getInputs(): {[key: string]: string} {
return {
CONFIG_URL: core.getInput(Inputs.CONFIG_URL),
CONFIG_FILE: core.getInput(Inputs.CONFIG_FILE),
TOKEN: core.getInput(Inputs.TOKEN, {required: true}),
REPO: core.getInput(Inputs.REPO, {required: true}),
OUTPUT_TYPE: core.getInput(Inputs.OUTPUT_TYPE, {required: true})
}
}

async function run(): Promise<void> {
// load the configuration from file or url, depending on which one is configured
let config
try {
config = await getConfig({
configFile: core.getInput(INPUT_CONFIG_FILE),
configUrl: core.getInput(INPUT_CONFIG_URL)
// get all inputs
const {CONFIG_FILE, CONFIG_URL, TOKEN, REPO, OUTPUT_TYPE} = getInputs()
// verify the output type is correct
if (OUTPUT_TYPE !== 'off' && OUTPUT_TYPE !== 'issue')
return core.setFailed(`Invalid output paramter value ${OUTPUT_TYPE}`)
// get the config
const config = await getConfig({
configFile: CONFIG_FILE,
configUrl: CONFIG_URL
})
} catch (e) {
return core.setFailed(e)
}

try {
// run the linter!
const result = await lint('.', undefined, true, config)
core.debug(JSON.stringify(result))
// print the formatted result
Expand All @@ -32,13 +39,9 @@ async function run(): Promise<void> {
// if the result is not a pass or an error, open an issue
// TODO: what to do if the run errors
// TODO: automatically create the repolinter label
if (!result.passed) {
const octokit = github.getOctokit(
core.getInput(INPUT_TOKEN, {required: true})
)
const [owner, repo] = core
.getInput(INPUT_REPO, {required: true})
.split('/')
if (!result.passed && OUTPUT_TYPE === 'issue') {
const octokit = github.getOctokit(TOKEN)
const [owner, repo] = REPO.split('/')

await octokit.issues.create({
owner,
Expand Down

0 comments on commit 9f29486

Please sign in to comment.