Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: output spell checking result #888

Merged
merged 8 commits into from
Aug 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .github/workflows/test-action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ jobs:
steps:
- uses: actions/checkout@v3
- uses: ./
id: cspell-action

test-action-with-file: # run the action
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: ./
id: cspell-action
with:
files: |
**/*.ts
Expand All @@ -27,20 +29,46 @@ jobs:
steps:
- uses: actions/checkout@v3
- uses: ./
id: cspell-action
with:
incremental_files_only: false
files: |
**
.*/**
- name: Show Results
env:
outputs: ${{ toJSON(steps.cspell-action.outputs) }}
run: |
echo "$outputs"

test-action-no-increment-verbose: # run the action
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: ./
id: cspell-action
with:
incremental_files_only: false
files: |
**
.*/**
verbose: true

test-action-files-with-issues: # run the action
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: ./
id: cspell-action
with:
incremental_files_only: false
files: |
fixtures/**
config: ./fixtures/cspell.json
strict: false
inline: none
- name: Show Results
env:
outputs: ${{ toJSON(steps.cspell-action.outputs) }}
run: |
echo "$outputs"
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,44 @@ const cspell = {
module.exports = cspell;
```

## Outputs

```yaml
outputs:
success:
description: |
"true" if no spelling issues were found, otherwise "false".
number_of_files_checked:
description: |
The actual number of files that were checked.
number_of_issues:
description: |
The number of issues found.
number_of_files_with_issues:
description: |
The number of files that had issues.
files_with_issues:
description: |
List of files with issues. Use `fromJSON()` to decode.
The files are relative to the repository root.
results:
description: |
The JSON encoded results.
```

Example Output:

```json
{
"success": "false",
"number_of_files_checked": "3",
"number_of_issues": "2",
"number_of_files_with_issues": "1",
"files_with_issues": "[\"src/withErrors.ts\"]",
"result": "{\"success\":false,\"number_of_issues\":2,\"number_of_files_checked\":3,\"files_with_issues\":[\"src/withErrors.ts\"]}"
}
```

<!---
cspell:ignore medicalterms

Expand Down
93 changes: 93 additions & 0 deletions action-src/src/__snapshots__/action.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,96 @@ Array [
`;

exports[`Validate Action check all **/*.md 1`] = `Array []`;

exports[`Validate Action check files with issues fixtures/sampleCode/** 1`] = `
Array [
"../fixtures/sampleCode/samples_with_errors/withErrors.ts:5:19 Unknown word (Functon)",
"../fixtures/sampleCode/samples_with_errors/withErrors.ts:5:27 Unknown word (countt)",
]
`;

exports[`Validate Action check files with issues fixtures/sampleCode/** 2`] = `Array []`;

exports[`Validate Action check files with issues fixtures/sampleCode/** 3`] = `
Array [
Array [
"Pull Request
",
],
Array [
"::warning file=../fixtures/sampleCode/samples_with_errors/withErrors.ts,line=5,col=19::Unknown word (Functon)
",
],
Array [
"::warning file=../fixtures/sampleCode/samples_with_errors/withErrors.ts,line=5,col=27::Unknown word (countt)
",
],
Array [
"Files checked: 2, Issues found: 2 in 1 files.
",
],
Array [
"
",
],
Array [
"::set-output name=success::false
",
],
Array [
"
",
],
Array [
"::set-output name=number_of_files_checked::2
",
],
Array [
"
",
],
Array [
"::set-output name=number_of_issues::2
",
],
Array [
"
",
],
Array [
"::set-output name=number_of_files_with_issues::1
",
],
Array [
"
",
],
Array [
"::set-output name=files_with_issues::[\\"../fixtures/sampleCode/samples_with_errors/withErrors.ts\\"]
",
],
Array [
"
",
],
Array [
"::set-output name=result::{\\"success\\":false,\\"number_of_issues\\":2,\\"number_of_files_checked\\":2,\\"files_with_issues\\":[\\"../fixtures/sampleCode/samples_with_errors/withErrors.ts\\"]}
",
],
]
`;

exports[`Validate Action check files with issues fixtures/sampleCode/** 4`] = `
Array [
"Pull Request",
"::warning file=../fixtures/sampleCode/samples_with_errors/withErrors.ts,line=5,col=19::Unknown word (Functon)",
"::warning file=../fixtures/sampleCode/samples_with_errors/withErrors.ts,line=5,col=27::Unknown word (countt)",
"Files checked: 2, Issues found: 2 in 1 files.",
"::set-output name=success::false",
"::set-output name=number_of_files_checked::2",
"::set-output name=number_of_issues::2",
"::set-output name=number_of_files_with_issues::1",
"::set-output name=files_with_issues::[\\"../fixtures/sampleCode/samples_with_errors/withErrors.ts\\"]",
"::set-output name=result::{\\"success\\":false,\\"number_of_issues\\":2,\\"number_of_files_checked\\":2,\\"files_with_issues\\":[\\"../fixtures/sampleCode/samples_with_errors/withErrors.ts\\"]}",
]
`;
23 changes: 22 additions & 1 deletion action-src/src/action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,34 @@ describe('Validate Action', () => {
INPUT_INCREMENTAL_FILES_ONLY: 'false',
});
const octokit = createOctokit();
expect.assertions(3);
await expect(action(context, octokit)).resolves.toBe(expected);
expect(warnings).toMatchSnapshot();
expect(spyStdout).toHaveBeenCalled();
},
timeout
);

test.each`
files | expected
${'fixtures/sampleCode/**'} | ${false}
`(
'check files with issues $files',
async ({ files, expected }) => {
const warnings: string[] = [];
spyWarn.mockImplementation((msg: string) => warnings.push(msg));
const context = createContextFromFile('pull_request_with_files.json', {
INPUT_FILES: files,
INPUT_INCREMENTAL_FILES_ONLY: 'false',
});
const octokit = createOctokit();
await expect(action(context, octokit)).resolves.toBe(expected);
expect(warnings).toMatchSnapshot();
expect(spyLog.mock.calls).toMatchSnapshot();
expect(spyStdout.mock.calls).toMatchSnapshot();
expect(spyStdout.mock.calls.map((call) => call.join('').trim()).filter((a) => !!a)).toMatchSnapshot();
},
timeout
);
});

function cleanEnv() {
Expand Down
35 changes: 31 additions & 4 deletions action-src/src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { Context as GitHubContext } from '@actions/github/lib/context';
import { Octokit } from '@octokit/core';
import { RunResult } from 'cspell';
import * as glob from 'cspell-glob';
import { format } from 'util';
import { AppError } from './error';
import { fetchFilesForCommits, getPullRequestFiles } from './github';
import * as path from 'path';
import { ActionParams, validateActionParams } from './ActionParams';
import { AppError } from './error';
import { getActionParams } from './getActionParams';
import { fetchFilesForCommits, getPullRequestFiles } from './github';
import { CSpellReporterForGithubAction } from './reporter';
import { lint, LintOptions } from './spell';

Expand Down Expand Up @@ -140,7 +140,6 @@ export async function action(githubContext: GitHubContext, octokit: Octokit): Pr
};

core.info(friendlyEventName(eventName));
core.debug(format('Options: %o', params));
const files = await gatherFilesFromContext(context);
const result = await checkSpelling(params, [...files]);
if (result === true) {
Expand All @@ -150,6 +149,8 @@ export async function action(githubContext: GitHubContext, octokit: Octokit): Pr
const message = `Files checked: ${result.files}, Issues found: ${result.issues} in ${result.filesWithIssues.size} files.`;
core.info(message);

outputResult(result);

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

if (params.strict === 'true' && result.issues) {
Expand All @@ -162,3 +163,29 @@ export async function action(githubContext: GitHubContext, octokit: Octokit): Pr

return !(result.issues + result.errors);
}

function outputResult(runResult: RunResult) {
const result = normalizeResult(runResult);

core.setOutput('success', result.success);
core.setOutput('number_of_files_checked', result.number_of_files_checked);
core.setOutput('number_of_issues', result.number_of_issues);
core.setOutput('number_of_files_with_issues', result.files_with_issues.length);
core.setOutput('files_with_issues', normalizeFiles(result.files_with_issues));
core.setOutput('result', result);
}

function normalizeResult(result: RunResult) {
const { issues: number_of_issues, files: number_of_files_checked, filesWithIssues } = result;
return {
success: !number_of_issues,
number_of_issues,
number_of_files_checked,
files_with_issues: normalizeFiles(filesWithIssues).slice(0, 1000),
};
}

function normalizeFiles(files: Iterable<string>): string[] {
const cwd = process.cwd();
return [...files].map((file) => path.relative(cwd, file));
}
21 changes: 21 additions & 0 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,27 @@ inputs:
Allowed values are: true, false
default: "false"
required: false
outputs:
success:
description: |
"true" if no spelling issues were found, otherwise "false".
number_of_files_checked:
description: |
The actual number of files that were checked.
number_of_issues:
description: |
The number of issues found.
number_of_files_with_issues:
description: |
The number of files that had issues.
files_with_issues:
description: |
List of files with issues. Use `fromJSON()` to decode.
The files are relative to the repository root.
results:
description: |
The JSON encoded results.

runs:
using: "node16"
main: "./action/lib/main_root.js"
Expand Down
30 changes: 26 additions & 4 deletions action/lib/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
exports.action = void 0;
const core = __importStar(require("@actions/core"));
const glob = __importStar(require("cspell-glob"));
const util_1 = require("util");
const error_1 = require("./error");
const github_1 = require("./github");
const path = __importStar(require("path"));
const ActionParams_1 = require("./ActionParams");
const error_1 = require("./error");
const getActionParams_1 = require("./getActionParams");
const github_1 = require("./github");
const reporter_1 = require("./reporter");
const spell_1 = require("./spell");
const supportedEvents = new Set(['push', 'pull_request']);
Expand Down Expand Up @@ -129,14 +129,14 @@ async function action(githubContext, octokit) {
useEventFiles: params.incremental_files_only === 'true',
};
core.info(friendlyEventName(eventName));
core.debug((0, util_1.format)('Options: %o', params));
const files = await gatherFilesFromContext(context);
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);
outputResult(result);
const fnS = (n) => (n === 1 ? '' : 's');
if (params.strict === 'true' && result.issues) {
const filesWithIssues = result.filesWithIssues.size;
Expand All @@ -146,3 +146,25 @@ async function action(githubContext, octokit) {
return !(result.issues + result.errors);
}
exports.action = action;
function outputResult(runResult) {
const result = normalizeResult(runResult);
core.setOutput('success', result.success);
core.setOutput('number_of_files_checked', result.number_of_files_checked);
core.setOutput('number_of_issues', result.number_of_issues);
core.setOutput('number_of_files_with_issues', result.files_with_issues.length);
core.setOutput('files_with_issues', normalizeFiles(result.files_with_issues));
core.setOutput('result', result);
}
function normalizeResult(result) {
const { issues: number_of_issues, files: number_of_files_checked, filesWithIssues } = result;
return {
success: !number_of_issues,
number_of_issues,
number_of_files_checked,
files_with_issues: normalizeFiles(filesWithIssues).slice(0, 1000),
};
}
function normalizeFiles(files) {
const cwd = process.cwd();
return [...files].map((file) => path.relative(cwd, file));
}