Skip to content

Commit

Permalink
feat: add directory input to specify a custom working directory
Browse files Browse the repository at this point in the history
  • Loading branch information
prototypicalpro committed Sep 10, 2020
1 parent 8b10ff7 commit 8e0b796
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 4 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ Currently this action uses the [newrelic-forks/repolinter](https://github.com/ne
```yaml
- uses: repolinter-action@v1
with:
# The directory Repolinter should run against. Accepts an absolute path
# or a path relative to $GITHUB_WORKSPACE.
#
# Defaults to $GITHUB_WORKSPACE.
directory: ''

# A path to the JSON/YAML Repolinter ruleset to use, relative to the workflow
# working directory (i.e. under `$GITHUB_WORKSPACE`).
#
Expand Down
14 changes: 14 additions & 0 deletions __tests__/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ function getInputName(input: string): string {

function getBaseEnv(): NodeJS.ProcessEnv {
const ret: NodeJS.ProcessEnv = {}
ret[getInputName(ActionInputs.DIRECTORY)] = '.'
ret[getInputName(ActionInputs.REPO)] = 'newrelic/repolinter-action'
ret[getInputName(ActionInputs.OUTPUT_TYPE)] = 'exit-code'
ret[getInputName(ActionInputs.OUTPUT_NAME)] = 'Open Source Policy Issues'
Expand Down Expand Up @@ -97,4 +98,17 @@ describe('integration', () => {
expect(out).toContain('passingtestconfig.json')
expect(out).not.toContain('undefined')
})

test('runs a config in a custom directory', async () => {
const baseEnv = getBaseEnv()
baseEnv[getInputName(ActionInputs.DIRECTORY)] = './__tests__/testfolder'
baseEnv[getInputName(ActionInputs.CONFIG_FILE)] =
'./__tests__/testfolder/nestedtestconfig.json'

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

expect(code).toEqual(0)
expect(out).toContain('nestedtestconfig.json')
expect(out).not.toContain('undefined')
})
})
52 changes: 52 additions & 0 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe('main', () => {
// reset process.env
process.env = {}
// reset inputs
process.env[getInputName(ActionInputs.DIRECTORY)] = '.'
process.env[getInputName(ActionInputs.REPO)] = 'newrelic/repolinter-action'
process.env[getInputName(ActionInputs.OUTPUT_TYPE)] = 'exit-code'
process.env[getInputName(ActionInputs.OUTPUT_NAME)] =
Expand All @@ -48,6 +49,18 @@ describe('main', () => {
})
})

test('throws when no directory is supplied', async () => {
delete process.env[getInputName(ActionInputs.DIRECTORY)]

await run()
const outputs = getOutputs(spooledStdout)

// console.debug(out)
expect(outputs[ActionOutputs.ERRORED]).toEqual('true')
expect(outputs[ActionOutputs.PASSED]).toEqual('false')
expect(process.exitCode).not.toEqual(0)
})

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

Expand Down Expand Up @@ -288,6 +301,45 @@ describe('main', () => {
expect(process.exitCode).not.toEqual(0)
})

test('runs in a custom directory', async () => {
process.env[getInputName(ActionInputs.DIRECTORY)] = './__tests__/testfolder'
process.env[getInputName(ActionInputs.CONFIG_FILE)] =
'./__tests__/testfolder/nestedtestconfig.json'

await run()
const outputs = getOutputs(spooledStdout)

expect(outputs[ActionOutputs.ERRORED]).toEqual('false')
expect(outputs[ActionOutputs.PASSED]).toEqual('true')
expect(process.exitCode).toEqual(0)
})

test('throws when given an invalid directory', async () => {
process.env[getInputName(ActionInputs.DIRECTORY)] = 'notafolder'
process.env[getInputName(ActionInputs.CONFIG_FILE)] =
'./__tests__/testfolder/nestedtestconfig.json'

await run()
const outputs = getOutputs(spooledStdout)

expect(outputs[ActionOutputs.ERRORED]).toEqual('true')
expect(outputs[ActionOutputs.PASSED]).toEqual('false')
expect(process.exitCode).not.toEqual(0)
})

test('throws when given an file instead of a directory', async () => {
process.env[getInputName(ActionInputs.DIRECTORY)] = 'action.yml'
process.env[getInputName(ActionInputs.CONFIG_FILE)] =
'./__tests__/testfolder/nestedtestconfig.json'

await run()
const outputs = getOutputs(spooledStdout)

expect(outputs[ActionOutputs.ERRORED]).toEqual('true')
expect(outputs[ActionOutputs.PASSED]).toEqual('false')
expect(process.exitCode).not.toEqual(0)
})

test('throws if the config is invalid', async () => {
const configPath = path.resolve(__dirname, 'invalidtestconfig.json')
process.env[getInputName(ActionInputs.CONFIG_FILE)] = configPath
Expand Down
Empty file added __tests__/testfolder/README
Empty file.
17 changes: 17 additions & 0 deletions __tests__/testfolder/nestedtestconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"$schema": "https://raw.githubusercontent.com/prototypicalpro/repolinter/master/rulesets/schema.json",
"version": 2,
"axioms": {},
"rules": {
"readme-file-exists": {
"level": "error",
"rule": {
"type": "file-existence",
"options": {
"globsAny": ["README*"],
"nocase": true
}
}
}
}
}
10 changes: 9 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ name: 'Repolinter Action'
description: 'Runs Repolinter against a repository, then uses the results to open an issue.'
author: 'New Relic Opensource'
inputs:
directory:
required: false
description: >
The directory Repolinter should run against. Accepts an absolute path
or a path relative to $GITHUB_WORKSPACE.
Defaults to $GITHUB_WORKSPACE.
default: ${{ github.workspace }}
token:
required: false
description: >
Expand Down Expand Up @@ -29,7 +37,7 @@ inputs:
config_file:
required: false
description: >
A path to the JSON or YAML Repolinter ruleset to use, relative to the workflow
A path to the JSON or YAML Repolinter ruleset to use, relative to the
working directory (i.e. under `$GITHUB_WORKSPACE`).
This option is mutually exclusive with config_url. If this option and
Expand Down
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.

1 change: 1 addition & 0 deletions src/inputs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const enum ActionInputs {
DIRECTORY = 'directory',
TOKEN = 'token',
USERNAME = 'username',
CONFIG_URL = 'config_url',
Expand Down
15 changes: 14 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import {
markdownFormatter,
jsonFormatter
} from 'repolinter'
import * as fs from 'fs'
import getConfig from './getConfig'
import createOrUpdateIssue from './createorUpdateIssue'

function getInputs(): {[key: string]: string} {
return {
DIRECTORY: core.getInput(ActionInputs.DIRECTORY, {required: true}),
TOKEN: core.getInput(ActionInputs.TOKEN),
USERNAME: core.getInput(ActionInputs.USERNAME, {required: true}),
CONFIG_URL: core.getInput(ActionInputs.CONFIG_URL),
Expand All @@ -30,6 +32,7 @@ export default async function run(disableRetry?: boolean): Promise<void> {
try {
// get all inputs
const {
DIRECTORY,
TOKEN,
USERNAME,
CONFIG_FILE,
Expand All @@ -40,6 +43,16 @@ export default async function run(disableRetry?: boolean): Promise<void> {
LABEL_NAME,
LABEL_COLOR
} = getInputs()
// verify the directory exists and is a directory
try {
const stat = await fs.promises.stat(DIRECTORY)
if (!stat.isDirectory())
throw new Error(
`Supplied input directory ${DIRECTORY} is not a directory`
)
} catch (e) {
throw e
}
// verify the output type is correct
if (OUTPUT_TYPE !== 'exit-code' && OUTPUT_TYPE !== 'issue')
throw new Error(`Invalid output paramter value ${OUTPUT_TYPE}`)
Expand All @@ -59,7 +72,7 @@ export default async function run(disableRetry?: boolean): Promise<void> {
configUrl: CONFIG_URL
})
// run the linter!
const result = await lint('.', undefined, config, true)
const result = await lint(DIRECTORY, undefined, config, true)
core.debug(JSON.stringify(result))
// print the formatted result
core.startGroup('Repolinter Output')
Expand Down

0 comments on commit 8e0b796

Please sign in to comment.