-
Notifications
You must be signed in to change notification settings - Fork 46
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: custom rulesets #129
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8d0d035
chore: add .editorconfig file
nulltoken 74d47a3
chore: clean up TypeScript build
nulltoken ab9f094
chore: move dependencies under the proper package.json section
nulltoken 63c1faa
chore: do not require from the user to pass the GitHub token
nulltoken 98edc5c
chore: use fixed version of dependent actions
nulltoken e8ae798
chore: move test file under a fixtures folder
nulltoken 7fa7f6e
feat: support custom rulesets
nulltoken dafd2dc
chore: enhance documentation
nulltoken 78bd149
fix: do not create the checkrun with the default 'queued' status
nulltoken 787c73b
chore: enhance logging
nulltoken 4bfd735
chore: track a potential future enhancement
nulltoken a0b1317
chore: fail the main action on fatal linting errors
nulltoken b277ed1
fix: tie annotations to proper commit
nulltoken 13042a4
fix: Have separate Lint checks for push and pull requests
nulltoken File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
extends: spectral:oas | ||
|
||
except: | ||
'fixtures/test.oas.yml#/paths/~1todos/get': | ||
- operation-description | ||
|
||
'fixtures/non_existent.yml#': | ||
- oas3-api-servers | ||
- openapi-tags | ||
|
||
'fixtures/non_existent.yml#/info': | ||
- info-contact | ||
|
||
'fixtures/non_existent.yml#/paths/~1todos/get': | ||
- operation-description |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
// See https://go.microsoft.com/fwlink/?LinkId=827846 | ||
// for the documentation about the extensions.json format | ||
"recommendations": ["editorconfig.editorconfig"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,34 @@ | ||
FROM node:13 as builder | ||
FROM node:12 as builder | ||
|
||
COPY package* ./ | ||
COPY package.json yarn.lock ./ | ||
RUN yarn | ||
|
||
COPY src ./src | ||
COPY tsconfig.json tsconfig.json | ||
|
||
RUN ./node_modules/.bin/tsc || true | ||
RUN yarn | ||
RUN yarn build || true | ||
|
||
############################################################### | ||
|
||
FROM node:13 as installer | ||
FROM node:12 as dependencies | ||
|
||
ENV NODE_ENV production | ||
COPY package.json package.json | ||
COPY package.json yarn.lock ./ | ||
XVincentX marked this conversation as resolved.
Show resolved
Hide resolved
|
||
RUN yarn --production | ||
|
||
FROM node:13-alpine as runtime | ||
RUN curl -sfL https://install.goreleaser.com/github.com/tj/node-prune.sh | bash | ||
RUN ./bin/node-prune | ||
|
||
############################################################### | ||
|
||
FROM node:12-alpine as runtime | ||
|
||
ENV NODE_ENV production | ||
|
||
COPY package.json /action/package.json | ||
|
||
COPY --from=builder dist /action/dist | ||
COPY --from=installer node_modules /action/node_modules | ||
COPY --from=dependencies node_modules /action/node_modules | ||
|
||
ENTRYPOINT ["node", "/action/dist/index.js"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,38 @@ | ||
# Spectral Lint Action | ||
# Spectral Linter Action | ||
|
||
![](https://raw.githubusercontent.com/stoplightio/spectral/master/img/spectral-banner.png) | ||
|
||
This action uses [Spectral 4.x](https://github.com/stoplightio/spectral) to lint your OpenAPI files. | ||
This action uses [Spectral](https://github.com/stoplightio/spectral) from [Stoplight](https://stoplight.io/) to lint your OpenAPI documents, or any other JSON/YAML files. | ||
|
||
![](./image.png) | ||
|
||
## Usage | ||
|
||
See [action.yml](action.yml) | ||
|
||
```yaml | ||
name: Run Spectral on Pull Requests | ||
|
||
on: | ||
- pull_request | ||
|
||
jobs: | ||
build: | ||
name: Run Spectral | ||
runs-on: ubuntu-latest | ||
steps: | ||
# Check out the repository | ||
- uses: actions/checkout@v2 | ||
|
||
# Run Spectral | ||
- uses: stoplightio/spectral-action@v0.5.5 | ||
with: | ||
file_glob: 'doc/api/*.yaml' | ||
``` | ||
|
||
### Inputs | ||
|
||
- **file_glob:** Pattern describing the set of files to lint. Defaults to `*.oas.{json,yml,yaml}`. (_Note:_ Pattern syntax is documented in the [fast-glob](https://www.npmjs.com/package/fast-glob) package documentation) | ||
- **spectral_ruleset:** Custom ruleset to load in Spectral. When unspecified, will try to load the default `.spectral.yaml` ruleset if it exists; otherwise, the default built-in Spectral rulesets will be loaded. | ||
|
||
## Configuration | ||
|
||
Spectral Action will respect your [Spectral Rulesets](https://stoplight.io/p/docs/gh/stoplightio/spectral/docs/getting-started/rulesets.md), which can be defined, extended, and overriden by placing `.spectral.yml` in the root of your repository. | ||
philsturgeon marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
|
||
[*.{ts,yaml,yml,json,md}] | ||
indent_style = space | ||
indent_size = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,8 @@ import { promises as fs } from 'fs'; | |
import { array } from 'fp-ts/lib/Array'; | ||
import { flatten } from 'lodash'; | ||
import { Config } from './config'; | ||
import { runSpectral, createSpectral } from './spectral'; | ||
import { runSpectral, createSpectral, fileWithContent } from './spectral'; | ||
import { pluralizer } from './utils'; | ||
import { createGithubCheck, createOctokitInstance, getRepositoryInfoFromEvent, updateGithubCheck } from './octokit'; | ||
import glob from 'fast-glob'; | ||
import { error, info, setFailed } from '@actions/core'; | ||
|
@@ -23,24 +24,33 @@ import * as path from 'path'; | |
|
||
const CHECK_NAME = 'Lint'; | ||
const traverseTask = array.traverse(T.task); | ||
type fileWithContent = { file: string; content: string }; | ||
|
||
const createSpectralAnnotations = (ruleset: string, parsed: fileWithContent[], basePath: string) => | ||
pipe( | ||
createSpectral(ruleset), | ||
TE.chain(spectral => { | ||
const spectralRuns = parsed.map(v => | ||
pipe( | ||
runSpectral(spectral, v.content), | ||
TE.map(rules => ({ path: v.file, rules })) | ||
runSpectral(spectral, v), | ||
TE.map(results => { | ||
info(`Done linting '${v.path}'`); | ||
|
||
Comment on lines
+36
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it really worth to add logging here? It's a side effect and it should be encapsulated in an It would better to use a Writer or simply hold off on logging here |
||
if (results.length === 0) { | ||
info(' No issue detected'); | ||
} else { | ||
info(` /!\\ ${pluralizer(results.length, 'issue')} detected`); | ||
} | ||
|
||
return { path: v.path, results }; | ||
}) | ||
) | ||
); | ||
return array.sequence(TE.taskEither)(spectralRuns); | ||
}), | ||
TE.map(results => | ||
flatten( | ||
results.map(validationResult => { | ||
return validationResult.rules.map<ChecksUpdateParamsOutputAnnotations>(vl => { | ||
return validationResult.results.map<ChecksUpdateParamsOutputAnnotations>(vl => { | ||
const annotation_level: ChecksUpdateParamsOutputAnnotations['annotation_level'] = | ||
vl.severity === DiagnosticSeverity.Error | ||
? 'failure' | ||
|
@@ -66,21 +76,23 @@ const createSpectralAnnotations = (ruleset: string, parsed: fileWithContent[], b | |
) | ||
); | ||
|
||
const readFilesToAnalyze = (path: string) => { | ||
const readFilesToAnalyze = (pattern: string, workingDir: string) => { | ||
const path = join(workingDir, pattern); | ||
|
||
const readFile = (file: string) => TE.tryCatch(() => fs.readFile(file, { encoding: 'utf8' }), E.toError); | ||
|
||
return pipe( | ||
TE.tryCatch(() => glob(path), E.toError), | ||
TE.map(fileList => { | ||
info(`Files to check: ${fileList.join(',')}`); | ||
info(`Using glob '${pattern}' under '${workingDir}', found ${pluralizer(fileList.length, 'file')} to lint`); | ||
philsturgeon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return fileList; | ||
}), | ||
TE.chain(fileList => | ||
pipe( | ||
traverseTask(fileList, file => | ||
traverseTask(fileList, path => | ||
pipe( | ||
readFile(file), | ||
TE.map(content => ({ file, content })) | ||
readFile(path), | ||
TE.map<string, fileWithContent>(content => ({ path, content })) | ||
) | ||
), | ||
T.map(e => { | ||
|
@@ -109,9 +121,16 @@ const createConfigFromEnv = pipe( | |
const program = pipe( | ||
TE.fromIOEither(createConfigFromEnv), | ||
TE.chain( | ||
({ GITHUB_EVENT_PATH, INPUT_REPO_TOKEN, GITHUB_SHA, GITHUB_WORKSPACE, INPUT_FILE_GLOB, INPUT_SPECTRAL_RULESET }) => | ||
({ | ||
INPUT_EVENT_NAME, | ||
GITHUB_EVENT_PATH, | ||
INPUT_REPO_TOKEN, | ||
GITHUB_WORKSPACE, | ||
INPUT_FILE_GLOB, | ||
INPUT_SPECTRAL_RULESET, | ||
}) => | ||
pipe( | ||
getRepositoryInfoFromEvent(GITHUB_EVENT_PATH), | ||
getRepositoryInfoFromEvent(GITHUB_EVENT_PATH, INPUT_EVENT_NAME), | ||
TE.chain(event => | ||
pipe( | ||
createOctokitInstance(INPUT_REPO_TOKEN), | ||
|
@@ -120,27 +139,43 @@ const program = pipe( | |
), | ||
TE.chain(({ octokit, event }) => | ||
pipe( | ||
createGithubCheck(octokit, event, CHECK_NAME, GITHUB_SHA), | ||
createGithubCheck(octokit, event, `${CHECK_NAME} (${event.eventName})`), | ||
TE.map(check => ({ octokit, event, check })) | ||
) | ||
), | ||
TE.chain(({ octokit, event, check }) => | ||
pipe( | ||
readFilesToAnalyze(join(GITHUB_WORKSPACE, INPUT_FILE_GLOB)), | ||
readFilesToAnalyze(INPUT_FILE_GLOB, GITHUB_WORKSPACE), | ||
TE.chain(fileContents => createSpectralAnnotations(INPUT_SPECTRAL_RULESET, fileContents, GITHUB_WORKSPACE)), | ||
TE.chain(annotations => | ||
updateGithubCheck( | ||
octokit, | ||
CHECK_NAME, | ||
check, | ||
event, | ||
annotations, | ||
annotations.findIndex(f => f.annotation_level === 'failure') === -1 ? 'success' : 'failure' | ||
pipe( | ||
updateGithubCheck( | ||
octokit, | ||
check, | ||
event, | ||
annotations, | ||
annotations.findIndex(f => f.annotation_level === 'failure') === -1 ? 'success' : 'failure' | ||
), | ||
TE.map(checkResponse => { | ||
info( | ||
`Check run '${checkResponse.data.name}' concluded with '${checkResponse.data.conclusion}' (${checkResponse.data.html_url})` | ||
); | ||
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_NAME, check, event, [], 'failure', e.message); | ||
return updateGithubCheck(octokit, check, event, [], 'failure', e.message); | ||
}) | ||
) | ||
) | ||
|
@@ -153,7 +188,7 @@ program().then(result => | |
result, | ||
E.fold( | ||
e => error(e.message), | ||
() => info('Worked fine') | ||
() => info('Analysis is complete') | ||
) | ||
) | ||
); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really need this file in the repository?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@XVincentX This will pop up a dialog driving the user to install the
editorconfig
extension to try and keep the style clean by automatically applying fixups on save (trimming trailing whitespaces, ...). Of course, that will only work with vscode.cf. https://code.visualstudio.com/docs/editor/extension-gallery#_workspace-recommended-extensions
If you don't think that adds any value, I'll drop it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I am aware of the feature. I have always been against IDE specific files in the repository, but I do not mind if you want to keep it here.
I would personally remove it.