Skip to content

Commit

Permalink
fix: Have separate Lint checks for push and pull requests
Browse files Browse the repository at this point in the history
  • Loading branch information
nulltoken committed Apr 27, 2020
1 parent 7921c5d commit 8b2b579
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 51 deletions.
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ inputs:
[Learn more about `GITHUB_TOKEN`](https://help.github.com/en/actions/configuring-and-managing-workflows/authenticating-with-the-github_token#about-the-github_token-secret)
default: ${{ github.token }}
event_name:
required: true
description: |
The name of the event that triggered the workflow
default: ${{ github.event_name }}
runs:
using: docker
image: Dockerfile
Expand Down
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const Config = t.strict({
INPUT_REPO_TOKEN: t.string,
GITHUB_WORKSPACE: t.string,
INPUT_FILE_GLOB: t.string,
GITHUB_ACTION: t.string,
INPUT_EVENT_NAME: t.string,
INPUT_SPECTRAL_RULESET: t.string,
});

Expand Down
101 changes: 56 additions & 45 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,55 +120,66 @@ const createConfigFromEnv = pipe(

const program = pipe(
TE.fromIOEither(createConfigFromEnv),
TE.chain(({ GITHUB_EVENT_PATH, INPUT_REPO_TOKEN, GITHUB_WORKSPACE, INPUT_FILE_GLOB, INPUT_SPECTRAL_RULESET }) =>
pipe(
getRepositoryInfoFromEvent(GITHUB_EVENT_PATH),
TE.chain(event =>
pipe(
createOctokitInstance(INPUT_REPO_TOKEN),
TE.map(octokit => ({ octokit, event }))
)
),
TE.chain(({ octokit, event }) =>
pipe(
createGithubCheck(octokit, event, CHECK_NAME),
TE.map(check => ({ octokit, event, check }))
)
),
TE.chain(({ octokit, event, check }) =>
pipe(
readFilesToAnalyze(INPUT_FILE_GLOB, GITHUB_WORKSPACE),
TE.chain(fileContents => createSpectralAnnotations(INPUT_SPECTRAL_RULESET, fileContents, GITHUB_WORKSPACE)),
TE.chain(annotations =>
pipe(
updateGithubCheck(
octokit,
CHECK_NAME,
check,
event,
annotations,
annotations.findIndex(f => f.annotation_level === 'failure') === -1 ? 'success' : 'failure'
),
TE.map(checkResponse => {
TE.chain(
({
INPUT_EVENT_NAME,
GITHUB_EVENT_PATH,
INPUT_REPO_TOKEN,
GITHUB_WORKSPACE,
INPUT_FILE_GLOB,
INPUT_SPECTRAL_RULESET,
}) =>
pipe(
getRepositoryInfoFromEvent(GITHUB_EVENT_PATH, INPUT_EVENT_NAME),
TE.chain(event =>
pipe(
createOctokitInstance(INPUT_REPO_TOKEN),
TE.map(octokit => ({ octokit, event }))
)
),
TE.chain(({ octokit, event }) =>
pipe(
createGithubCheck(octokit, event, `${CHECK_NAME} (${event.eventName})`),
TE.map(check => ({ octokit, event, check }))
)
),
TE.chain(({ octokit, event, check }) =>
pipe(
readFilesToAnalyze(INPUT_FILE_GLOB, GITHUB_WORKSPACE),
TE.chain(fileContents => createSpectralAnnotations(INPUT_SPECTRAL_RULESET, fileContents, GITHUB_WORKSPACE)),
TE.chain(annotations =>
pipe(
updateGithubCheck(
octokit,
check,
event,
annotations,
annotations.findIndex(f => f.annotation_level === 'failure') === -1 ? 'success' : 'failure'
),
TE.map(checkResponse => {
info(
`Commit ${event.sha} has been annotated (https://github.com/${event.owner}/${event.owner}/commit/${event.sha})`
`Check run '${checkResponse.data.name}' concluded with '${checkResponse.data.conclusion}' (${checkResponse.data.html_url})`
);
const fatalErrors = annotations.filter(a => a.annotation_level === 'failure');
if (fatalErrors.length > 0) {
setFailed(`${pluralizer(fatalErrors.length, 'fatal issue')} detected. Failing the process.`);
}

return checkResponse;
})
)
),
TE.orElse(e => {
setFailed(e.message);
return updateGithubCheck(octokit, CHECK_NAME, check, event, [], 'failure', e.message);
})
info(
`Commit ${event.sha} has been annotated (https://github.com/${event.owner}/${event.repo}/commit/${event.sha})`
);

const fatalErrors = annotations.filter(a => a.annotation_level === 'failure');
if (fatalErrors.length > 0) {
setFailed(`${pluralizer(fatalErrors.length, 'fatal issue')} detected. Failing the process.`);
}

return checkResponse;
})
)
),
TE.orElse(e => {
setFailed(e.message);
return updateGithubCheck(octokit, check, event, [], 'failure', e.message);
})
)
)
)
)
)
);

Expand Down
13 changes: 8 additions & 5 deletions src/octokit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@ export const createGithubCheck = (octokit: GitHub, event: IRepositoryInfo, name:
export interface IRepositoryInfo {
owner: string;
repo: string;
eventName: string;
sha: string;
}

export const getRepositoryInfoFromEvent = (eventPath: string): TE.TaskEither<Error, IRepositoryInfo> =>
export const getRepositoryInfoFromEvent = (
eventPath: string,
eventName: string
): TE.TaskEither<Error, IRepositoryInfo> =>
pipe(
TE.fromEither(E.tryCatch<Error, Event>(() => require(eventPath), E.toError)),
TE.map(event => {
Expand All @@ -44,13 +48,12 @@ export const getRepositoryInfoFromEvent = (eventPath: string): TE.TaskEither<Err
owner: { login: owner },
} = repository;
const { name: repo } = repository;
return { owner, repo, sha: after };
return { owner, repo, eventName, sha: after };
})
);

export const updateGithubCheck = (
octokit: GitHub,
actionName: string,
check: Response<ChecksCreateResponse>,
event: IRepositoryInfo,
annotations: ChecksUpdateParamsOutputAnnotations[],
Expand All @@ -62,13 +65,13 @@ export const updateGithubCheck = (
octokit.checks.update({
check_run_id: check.data.id,
owner: event.owner,
name: actionName,
name: check.data.name,
repo: event.repo,
status: 'completed',
conclusion,
completed_at: new Date().toISOString(),
output: {
title: actionName,
title: check.data.name,
summary: message
? message
: conclusion === 'success'
Expand Down

0 comments on commit 8b2b579

Please sign in to comment.