Skip to content

Commit

Permalink
fix: Handle non-incremental workflows (#927)
Browse files Browse the repository at this point in the history
* fix: Handle non-incremental workflows

fix: #926
  • Loading branch information
Jason3S committed Aug 29, 2022
1 parent 6b6ac0b commit ba2a012
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 14 deletions.
83 changes: 83 additions & 0 deletions action-src/src/__snapshots__/action.test.ts.snap
Expand Up @@ -103,3 +103,86 @@ 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/** 5`] = `Array []`;

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

exports[`Validate Action check files with issues fixtures/sampleCode/** 7`] = `
Array [
Array [
"::warning::Unable to determine which files have changed, checking files: fixtures/sampleCode/**
",
],
Array [
"'fork'
",
],
Array [
"Files checked: 1, Issues found: 0 in 0 files.
",
],
Array [
"
",
],
Array [
"::set-output name=success::true
",
],
Array [
"
",
],
Array [
"::set-output name=number_of_files_checked::1
",
],
Array [
"
",
],
Array [
"::set-output name=number_of_issues::0
",
],
Array [
"
",
],
Array [
"::set-output name=number_of_files_with_issues::0
",
],
Array [
"
",
],
Array [
"::set-output name=files_with_issues::[]
",
],
Array [
"
",
],
Array [
"::set-output name=result::{\\"success\\":true,\\"number_of_issues\\":0,\\"number_of_files_checked\\":1,\\"files_with_issues\\":[]}
",
],
]
`;

exports[`Validate Action check files with issues fixtures/sampleCode/** 8`] = `
Array [
"::warning::Unable to determine which files have changed, checking files: fixtures/sampleCode/**",
"'fork'",
"Files checked: 1, Issues found: 0 in 0 files.",
"::set-output name=success::true",
"::set-output name=number_of_files_checked::1",
"::set-output name=number_of_issues::0",
"::set-output name=number_of_files_with_issues::0",
"::set-output name=files_with_issues::[]",
"::set-output name=result::{\\"success\\":true,\\"number_of_issues\\":0,\\"number_of_files_checked\\":1,\\"files_with_issues\\":[]}",
]
`;
12 changes: 6 additions & 6 deletions action-src/src/action.test.ts
Expand Up @@ -37,7 +37,6 @@ describe('Validate Action', () => {
${'missing config'} | ${'bad_params/missing_config.json'} | ${new AppError('Bad Configuration.')}
${'bad inline'} | ${'bad_params/bad_inline.json'} | ${new AppError('Bad Configuration.')}
${'bad_incremental_files_only'} | ${'bad_params/bad_incremental_files_only.json'} | ${new AppError('Bad Configuration.')}
${'bad_unsupported_event'} | ${'bad_params/bad_unsupported_event.json'} | ${new AppError("Unsupported event: 'fork'")}
${'bad strict'} | ${'bad_params/bad_strict.json'} | ${new AppError('Bad Configuration.')}
`(
'$test',
Expand Down Expand Up @@ -94,16 +93,17 @@ describe('Validate Action', () => {
);

test.each`
files | expected
${'fixtures/sampleCode/**'} | ${false}
files | incremental | contextFile | expected
${'fixtures/sampleCode/**'} | ${false} | ${'pull_request_with_files.json'} | ${false}
${'fixtures/sampleCode/**'} | ${true} | ${'bad_params/bad_unsupported_event.json'} | ${true}
`(
'check files with issues $files',
async ({ files, expected }) => {
async ({ files, incremental, contextFile, expected }) => {
const warnings: string[] = [];
spyWarn.mockImplementation((msg: string) => warnings.push(msg));
const context = createContextFromFile('pull_request_with_files.json', {
const context = createContextFromFile(contextFile, {
INPUT_FILES: files,
INPUT_INCREMENTAL_FILES_ONLY: 'false',
INPUT_INCREMENTAL_FILES_ONLY: incremental ? 'true' : 'false',
});
const octokit = createOctokit();
await expect(action(context, octokit)).resolves.toBe(expected);
Expand Down
15 changes: 11 additions & 4 deletions action-src/src/action.ts
Expand Up @@ -5,7 +5,6 @@ import { RunResult } from 'cspell';
import * as glob from 'cspell-glob';
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';
Expand All @@ -19,7 +18,7 @@ interface Context {
}

type EventNames = 'push' | 'pull_request';
const supportedEvents = new Set<EventNames | string>(['push', 'pull_request']);
const supportedIncrementalEvents = new Set<EventNames | string>(['push', 'pull_request']);

async function gatherPullRequestFiles(context: Context): Promise<Set<string>> {
const { github, githubContext } = context;
Expand Down Expand Up @@ -83,7 +82,7 @@ function friendlyEventName(eventName: EventNames | string): string {
}

function isSupportedEvent(eventName: EventNames | string): eventName is EventNames {
return supportedEvents.has(eventName);
return supportedIncrementalEvents.has(eventName);
}

async function gatherFilesFromContext(context: Context): Promise<Set<string>> {
Expand Down Expand Up @@ -132,12 +131,20 @@ function filterFiles(globPattern: string, files: Set<string>): Set<string> {
return matchingFiles;
}

/**
* Run the action based upon the githubContext.
* @param githubContext
* @param octokit
* @returns a promise that resolves to `true` if no issues were found.
*/
export async function action(githubContext: GitHubContext, octokit: Octokit): Promise<boolean> {
const params = getActionParams();
validateActionParams(params, core.error);
const eventName = githubContext.eventName;
if (params.incremental_files_only === 'true' && !isSupportedEvent(eventName)) {
throw new AppError(`Unsupported event: '${eventName}'`);
params.files = params.files || '**';
core.warning('Unable to determine which files have changed, checking files: ' + params.files);
params.incremental_files_only = 'false';
}
const context: Context = {
githubContext,
Expand Down
15 changes: 11 additions & 4 deletions action/lib/action.js
Expand Up @@ -28,12 +28,11 @@ const core = __importStar(require("@actions/core"));
const glob = __importStar(require("cspell-glob"));
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']);
const supportedIncrementalEvents = new Set(['push', 'pull_request']);
async function gatherPullRequestFiles(context) {
var _a;
const { github, githubContext } = context;
Expand Down Expand Up @@ -82,7 +81,7 @@ function friendlyEventName(eventName) {
}
}
function isSupportedEvent(eventName) {
return supportedEvents.has(eventName);
return supportedIncrementalEvents.has(eventName);
}
async function gatherFilesFromContext(context) {
if (context.useEventFiles) {
Expand Down Expand Up @@ -121,12 +120,20 @@ function filterFiles(globPattern, files) {
}
return matchingFiles;
}
/**
* Run the action based upon the githubContext.
* @param githubContext
* @param octokit
* @returns a promise that resolves to `true` if no issues were found.
*/
async function action(githubContext, octokit) {
const params = (0, getActionParams_1.getActionParams)();
(0, ActionParams_1.validateActionParams)(params, core.error);
const eventName = githubContext.eventName;
if (params.incremental_files_only === 'true' && !isSupportedEvent(eventName)) {
throw new error_1.AppError(`Unsupported event: '${eventName}'`);
params.files = params.files || '**';
core.warning('Unable to determine which files have changed, checking files: ' + params.files);
params.incremental_files_only = 'false';
}
const context = {
githubContext,
Expand Down

0 comments on commit ba2a012

Please sign in to comment.