Skip to content

Commit

Permalink
feat: add strict checking option (#32)
Browse files Browse the repository at this point in the history
* feat: add strict checking option
* dev: add unit test for bad strict setting
* dev: update error message
  • Loading branch information
Jason3S authored Dec 24, 2020
1 parent f6aba51 commit cb3721d
Show file tree
Hide file tree
Showing 15 changed files with 669 additions and 206 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: streetsidesoftware/cspell-action@v0.2.5
- uses: streetsidesoftware/cspell-action@v1
```
## Usage
```yaml
- uses: streetsidesoftware/cspell-action@v0.2.5
- uses: streetsidesoftware/cspell-action@v1
with:
# Github token used to fetch the list of changed files in the commit.
# Default: ${{ github.token }}
Expand Down
3 changes: 3 additions & 0 deletions action-source/lib/__snapshots__/main.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Pull Request
6/8 ./action.yaml (???.??ms)
7/8 ./README.md (???.??ms)
8/8 ./.github/workflows/lint.yml (???.??ms)
Files checked: 8, Issues found: 0 in 0 files.
Done.
"
`;
Expand All @@ -22,6 +23,7 @@ Pull Request
2/4 ./fixtures/sampleCode/ts/sample.ts (???.??ms)
3/4 ./action.yaml (???.??ms)
4/4 ./.github/workflows/lint.yml (???.??ms)
Files checked: 4, Issues found: 0 in 0 files.
Done.
"
`;
Expand All @@ -30,6 +32,7 @@ exports[`Validate Main event push main.js 1`] = `
"cspell-action
Push
1/1 ./.editorconfig (???.??ms)
Files checked: 1, Issues found: 0 in 0 files.
Done.
"
`;
1 change: 1 addition & 0 deletions action-source/lib/action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe('Validate Action', () => {
${'bad root'} | ${'bad_params/bad_root.json'} | ${new AppError('Bad Configuration.')}
${'missing config'} | ${'bad_params/missing_config.json'} | ${new AppError('Bad Configuration.')}
${'bad inline'} | ${'bad_params/bad_inline.json'} | ${new AppError('Bad Configuration.')}
${'bad strict'} | ${'bad_params/bad_strict.json'} | ${new AppError('Bad Configuration.')}
`(
'$test',
async ({ file, expected }) => {
Expand Down
72 changes: 65 additions & 7 deletions action-source/lib/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import * as path from 'path';
import { AppError } from './error';
import * as glob from 'cspell-glob';
import { existsSync } from 'fs';
import { RunResult } from 'cspell';

interface Context {
githubContext: GitHubContext;
Expand All @@ -26,12 +27,24 @@ const supportedEvents = new Set<EventNames | string>(['push', 'pull_request']);

type InlineWorkflowCommand = 'error' | 'warning' | 'none';

type TrueFalse = 'true' | 'false';

interface ActionParams {
github_token: string;
files: string;
config: string;
root: string;
inline: string;
strict: string;
}

interface ValidActionParams {
github_token: string;
files: string;
config: string;
root: string;
inline: InlineWorkflowCommand;
strict: TrueFalse;
}

async function gatherPullRequestFiles(context: Context): Promise<Set<string>> {
Expand All @@ -57,7 +70,7 @@ async function gatherPushFiles(context: Context): Promise<Set<string>> {
return files || new Set();
}

async function checkSpelling(params: ActionParams, files: string[]): Promise<boolean> {
async function checkSpelling(params: ValidActionParams, files: string[]): Promise<RunResult | true> {
const options: LintOptions = {
root: params.root || process.cwd(),
config: params.config || undefined,
Expand All @@ -82,7 +95,7 @@ async function checkSpelling(params: ActionParams, files: string[]): Promise<boo
);
});
}
return !result.issues.length;
return result.result;
}

function friendlyEventName(eventName: EventNames | string): string {
Expand Down Expand Up @@ -138,15 +151,32 @@ function getActionParams(): ActionParams {
config: core.getInput('config'),
root: core.getInput('root'),
inline: (core.getInput('inline') || 'warning').toLowerCase(),
strict: tf(core.getInput('strict') || 'true'),
};
}

function tf(v: string | boolean | number): TrueFalse | string {
const mapValues: Record<string, TrueFalse> = {
true: 'true',
t: 'true',
false: 'false',
f: 'false',
'0': 'false',
'1': 'true',
};
v = typeof v === 'boolean' || typeof v === 'number' ? (v ? 'true' : 'false') : v;
v = v.toLowerCase();
v = mapValues[v] || v;
return v;
}

function validateActionParams(params: ActionParams) {
const validations = [validateToken, validateConfig, validateRoot, validateInlineLevel];
function validateActionParams(params: ActionParams | ValidActionParams): params is ValidActionParams {
const validations = [validateToken, validateConfig, validateRoot, validateInlineLevel, validateStrict];
const success = validations.map((fn) => fn(params)).reduce((a, b) => a && b, true);
if (!success) {
throw new AppError('Bad Configuration.');
}
return true;
}

function validateToken(params: ActionParams) {
Expand Down Expand Up @@ -181,6 +211,15 @@ function validateInlineLevel(params: ActionParams) {
return success;
}

function validateStrict(params: ActionParams) {
const isStrict = params.strict;
const success = isStrict === 'true' || isStrict === 'false';
if (!success) {
core.error('Invalid strict setting, must be of of (true, false)');
}
return success;
}

const inlineWorkflowCommandSet: Record<InlineWorkflowCommand | string, boolean | undefined> = {
error: true,
warning: true,
Expand All @@ -193,7 +232,9 @@ function isInlineWorkflowCommand(cmd: InlineWorkflowCommand | string): cmd is In

export async function action(githubContext: GitHubContext, octokit: Octokit): Promise<boolean> {
const params = getActionParams();
validateActionParams(params);
if (!validateActionParams(params)) {
return false;
}
const eventName = githubContext.eventName;
if (!isSupportedEvent(eventName)) {
const msg = `Unsupported event: '${eventName}'`;
Expand All @@ -208,6 +249,23 @@ export async function action(githubContext: GitHubContext, octokit: Octokit): Pr
core.info(friendlyEventName(eventName));
const eventFiles = await gatherFiles(context);
const files = filterFiles(context.files, eventFiles);
const noIssues = await checkSpelling(params, [...files]);
return noIssues;
const result = await checkSpelling(params, [...files]);
if (result === true) {
return true;
}

const message = `Files checked: ${result.files}, Issues found: ${result.issues} in ${result.filesWithIssues.size} files.`;
core.info(message);

const fnS = (n: number) => (n === 1 ? '' : 's');

if (params.strict === 'true' && result.issues) {
const filesWithIssues = result.filesWithIssues.size;
const err = `${result.issues} spelling issue${fnS(result.issues)} found in ${filesWithIssues} of the ${
result.files
} file${fnS(result.files)} checked.`;
core.setFailed(err);
}

return !(result.issues + result.errors);
}
5 changes: 5 additions & 0 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ inputs:
Notification level to use with inline reporting of spelling errors.
Allowed values are: warning, error, none
default: warning
strict:
description: >
Determines if the action should be failed if any spelling issues are found.
Allowed values are: true, false
default: true
runs:
using: "node12"
main: "./action/main.js"
Expand Down
2 changes: 1 addition & 1 deletion action/main.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion action/node_modules/@types/node/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit cb3721d

Please sign in to comment.