Skip to content

Commit

Permalink
Merge pull request #187 from stoplightio/ntk/proper_sha
Browse files Browse the repository at this point in the history
fix: use dedicated sha extractions per kind of event
  • Loading branch information
XVincentX authored May 2, 2020
2 parents d71d844 + 2b3ef89 commit 7db38ab
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 98 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ COPY src ./src
COPY tsconfig.json tsconfig.json

RUN yarn
RUN yarn build || true
RUN yarn build

###############################################################

Expand Down
21 changes: 11 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,26 @@
},
"homepage": "https://github.com/XVincentX/spectral-action#readme",
"devDependencies": {
"@types/lodash": "^4.14.149",
"@types/node": "^13.7.7",
"@types/urijs": "^1.19.6",
"husky": "^4.2.3",
"prettier": "^1.19.1",
"@types/lodash": "^4.14.150",
"@types/node": "^13.13.4",
"@types/urijs": "^1.19.8",
"husky": "^4.2.5",
"prettier": "^2.0.5",
"pretty-quick": "^2.0.1",
"typescript": "^3.8.3"
},
"dependencies": {
"@actions/core": "^1.2.3",
"@actions/core": "^1.2.4",
"@actions/github": "^2.1.1",
"@octokit/graphql": "^4.3.1",
"@octokit/request": "^5.3.2",
"@octokit/request": "^5.4.2",
"@octokit/rest": "^16.35.0",
"@stoplight/json": "^3.5.1",
"@stoplight/json": "^3.7.1",
"@stoplight/spectral": "^5.3.0",
"fast-glob": "^3.2.2",
"fp-ts": "^2.5.3",
"io-ts": "^2.1.2",
"fp-ts": "^2.5.4",
"fp-ts-contrib": "^0.1.15",
"io-ts": "^2.2.1",
"lodash": "^4.17.15",
"tslib": "^1.11.1"
},
Expand Down
6 changes: 6 additions & 0 deletions src/.prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
printWidth: 120,
singleQuote: true,
trailingComma: 'es5',
arrowParens: 'avoid',
};
18 changes: 9 additions & 9 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as t from 'io-ts';
import * as D from 'io-ts/lib/Decoder';

export const Config = t.strict({
GITHUB_EVENT_PATH: t.string,
INPUT_REPO_TOKEN: t.string,
GITHUB_WORKSPACE: t.string,
INPUT_FILE_GLOB: t.string,
INPUT_EVENT_NAME: t.string,
INPUT_SPECTRAL_RULESET: t.string,
export const Config = D.type({
GITHUB_EVENT_PATH: D.string,
INPUT_REPO_TOKEN: D.string,
GITHUB_WORKSPACE: D.string,
INPUT_FILE_GLOB: D.string,
INPUT_EVENT_NAME: D.string,
INPUT_SPECTRAL_RULESET: D.string,
});

export type Config = t.TypeOf<typeof Config>;
export type Config = D.TypeOf<typeof Config>;
7 changes: 3 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { join } from 'path';
import { DiagnosticSeverity } from '@stoplight/types';
import { warning } from '@actions/core';
import { EOL } from 'os';
import { promises as fs } from 'fs';
import { array } from 'fp-ts/lib/Array';
import { flatten } from 'lodash';
Expand All @@ -16,7 +15,7 @@ import * as IO from 'fp-ts/lib/IO';
import * as TE from 'fp-ts/lib/TaskEither';
import * as T from 'fp-ts/lib/Task';
import * as E from 'fp-ts/lib/Either';
import { failure } from 'io-ts/lib/PathReporter';
import { draw } from 'io-ts/lib/Tree';
import { pipe } from 'fp-ts/lib/pipeable';
import { identity } from 'lodash';
import { ChecksUpdateParamsOutputAnnotations } from '@octokit/rest';
Expand Down Expand Up @@ -110,7 +109,7 @@ const getEnv = IO.of(process.env);
const decodeConfig = (env: NodeJS.ProcessEnv) =>
pipe(
Config.decode(env),
E.mapLeft(e => new Error(failure(e).join(EOL)))
E.mapLeft(e => new Error(draw(e)))
);

const createConfigFromEnv = pipe(
Expand All @@ -130,7 +129,7 @@ const program = pipe(
INPUT_SPECTRAL_RULESET,
}) =>
pipe(
getRepositoryInfoFromEvent(GITHUB_EVENT_PATH, INPUT_EVENT_NAME),
TE.fromEither(getRepositoryInfoFromEvent(GITHUB_EVENT_PATH, INPUT_EVENT_NAME)),
TE.chain(event =>
pipe(
createOctokitInstance(INPUT_REPO_TOKEN),
Expand Down
73 changes: 50 additions & 23 deletions src/octokit.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import { GitHub } from '@actions/github';
import * as TE from 'fp-ts/lib/TaskEither';
import * as E from 'fp-ts/lib/Either';
import { pipe } from 'fp-ts/lib/pipeable';
import * as D from 'io-ts/lib/Decoder';
import { draw } from 'io-ts/lib/Tree';
import { Do } from 'fp-ts-contrib/lib/Do';
import { ChecksCreateResponse, ChecksUpdateParamsOutputAnnotations, ChecksUpdateParams, Response } from '@octokit/rest';
import { pipe } from 'fp-ts/lib/pipeable';

type Event = {
after: string;
repository: {
name: string;
owner: {
login: string;
};
};
};
const EventDecoder = D.type({
after: D.string,
repository: D.type({
name: D.string,
owner: D.type({
login: D.string,
}),
}),
});

type Event = D.TypeOf<typeof EventDecoder>;

export const createOctokitInstance = (token: string) => TE.fromEither(E.tryCatch(() => new GitHub(token), E.toError));

Expand All @@ -36,22 +41,44 @@ export interface IRepositoryInfo {
sha: string;
}

export const getRepositoryInfoFromEvent = (
eventPath: string,
eventName: string
): TE.TaskEither<Error, IRepositoryInfo> =>
const extractSha = (eventName: string, event: any): E.Either<Error, string> => {
switch (eventName) {
case 'pull_request':
return E.right(event.pull_request.head.sha);
case 'push':
return E.right(event.after);
default:
return E.left(Error(`Unsupported event '${eventName}'`));
}
};

function buildRepositoryInfoFrom(event: Event, eventName: string, sha: string): IRepositoryInfo {
const { repository } = event;
const {
owner: { login: owner },
} = repository;
const { name: repo } = repository;

return { owner, repo, eventName, sha };
}

const parseEventFile = (eventPath: string) =>
pipe(
TE.fromEither(E.tryCatch<Error, Event>(() => require(eventPath), E.toError)),
TE.map(event => {
const { repository, after } = event;
const {
owner: { login: owner },
} = repository;
const { name: repo } = repository;
return { owner, repo, eventName, sha: after };
})
E.tryCatch<Error, unknown>(() => require(eventPath), E.toError),
E.chain(event =>
pipe(
EventDecoder.decode(event),
E.mapLeft(errors => new Error(draw(errors)))
)
)
);

export const getRepositoryInfoFromEvent = (eventPath: string, eventName: string): E.Either<Error, IRepositoryInfo> =>
Do(E.either)
.bind('event', parseEventFile(eventPath))
.bindL('sha', ({ event }) => extractSha(eventName, event))
.return(({ event, sha }) => buildRepositoryInfoFrom(event, eventName, sha));

export const updateGithubCheck = (
octokit: GitHub,
check: Response<ChecksCreateResponse>,
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"resolveJsonModule": true
"resolveJsonModule": true,
"skipLibCheck": true
}
}
Loading

0 comments on commit 7db38ab

Please sign in to comment.