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: openapi lint #152

Merged
merged 51 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
dd75543
feat: add earthly check lint openapi
bkioshn Jan 11, 2024
529338b
feat: add build spectral
bkioshn Jan 11, 2024
cb46cdd
test: add example of openapi yml file
bkioshn Jan 11, 2024
45f307b
feat: add spectral yml rule set
bkioshn Jan 11, 2024
54c990f
fix: add word to ignore for cspell
bkioshn Jan 11, 2024
8ac6292
fix: example data
bkioshn Jan 11, 2024
8ab9656
Merge branch 'master' into feat/openapi-lint
bkioshn Jan 11, 2024
d3c8069
test: change branch for testing purpose
bkioshn Jan 11, 2024
2618c4b
fix: mod wildcard regex to match multiple hyphen
bkioshn Jan 11, 2024
4b90eab
test: local ci test
bkioshn Jan 11, 2024
e2f72c6
test: error openapi lint
bkioshn Jan 11, 2024
939f350
Merge branch 'master' into feat/openapi-lint
bkioshn Jan 11, 2024
a12a02d
fix: spectral so it can handle json linting
bkioshn Jan 12, 2024
550174c
feat: add minify json bash
bkioshn Jan 12, 2024
9fbbc5e
fix: change example to json format
bkioshn Jan 12, 2024
2da70d1
fix: bash format
bkioshn Jan 12, 2024
d604a11
fix: update minify json cmd
bkioshn Jan 12, 2024
d4b8f57
fix: var
bkioshn Jan 12, 2024
e6157c0
fix: var
bkioshn Jan 12, 2024
05c2613
fix: [] to [[]]
bkioshn Jan 12, 2024
a538c7d
fix: format shell
bkioshn Jan 12, 2024
d067f6a
fix: exec shell cmd
bkioshn Jan 12, 2024
33428c3
fix: shell shebang
bkioshn Jan 12, 2024
d0a6390
fix: use new ci
bkioshn Jan 12, 2024
99b7fd1
fix: ci cli version to 2.2.3
bkioshn Jan 12, 2024
5a531ed
fix: copy shell script
bkioshn Jan 12, 2024
536a79d
test: try create artifact
bkioshn Jan 13, 2024
a7c626a
fix: add file type
bkioshn Jan 13, 2024
4676fea
chore: add documentation
bkioshn Jan 13, 2024
31db21b
fix: syntax
bkioshn Jan 13, 2024
d133334
Merge branch 'master' into feat/openapi-lint
bkioshn Jan 13, 2024
98bc1a0
fix: spectral function
bkioshn Jan 13, 2024
550d8bc
fix: remove base spectral
bkioshn Jan 13, 2024
27f4cbc
fix: create spectral base
bkioshn Jan 13, 2024
78edb61
test: add ls for tesing
bkioshn Jan 13, 2024
5f816e9
fix(openapi): no warning produce from linter
bkioshn Jan 15, 2024
618e894
fix(cspell): replace Lorem ipsum
bkioshn Jan 15, 2024
6e0d17d
fix: copy dir to scan
bkioshn Jan 15, 2024
4e31234
fix: envi conflict
bkioshn Jan 15, 2024
d460ce0
fix (spectral): make ignore rules an example
bkioshn Jan 15, 2024
6052167
fix(spectral): remove workdir
bkioshn Jan 15, 2024
210d2f5
fix(spectral): add work dir on spectral base
bkioshn Jan 16, 2024
2e74c5b
fix(openapi): example
bkioshn Jan 16, 2024
24a94fc
test (spectral): remove copy dir
bkioshn Jan 16, 2024
e3a7a10
fix(spectral): clean up
bkioshn Jan 16, 2024
73d76ec
fix(spectral): clean up
bkioshn Jan 16, 2024
544f7ae
fix(doc): update spectral document
bkioshn Jan 16, 2024
9a159f5
fix(workflow): change branch to master
bkioshn Jan 16, 2024
d27baf3
fix(openapi): example operationId
bkioshn Jan 16, 2024
3b0ca9e
fix(spectral): add CDN version
bkioshn Jan 16, 2024
2818e2d
Merge branch 'master' into feat/openapi-lint
bkioshn Jan 18, 2024
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
16 changes: 16 additions & 0 deletions .spectral.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# References to the rules
# OpenAPI: https://docs.stoplight.io/docs/spectral/4dec24461f3af-open-api-rules#openapi-rules
# OWASP Top 10: https://apistylebook.stoplight.io/docs/owasp-top-10
# Documentations: https://github.com/stoplightio/spectral-documentation/blob/main/src/ruleset.ts

# cspell: words OWASP owasp

# Use CDN hosted version for spectral-documentation and spectral-owasp
extends:
- 'spectral:oas'
- 'https://unpkg.com/@stoplight/[email protected]/dist/ruleset.mjs'
- 'https://unpkg.com/@stoplight/[email protected]/dist/ruleset.mjs'

# Ignoring rules example
# rules:
# docs-tags: off
5 changes: 5 additions & 0 deletions Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,8 @@ edit-docs:
LOCALLY

RUN ./earthly/docs/dev/local.py cat-ci-docs:latest

# check-lint-openapi - OpenAPI linting from a given directory
check-lint-openapi:
FROM ./earthly/spectral+spectral-base
DO ./earthly/spectral+BUILD_SPECTRAL --dir="./examples/openapi" --file_type="json"
57 changes: 57 additions & 0 deletions docs/src/guides/json_yaml_linter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
icon: material/star-check-outline
---

<!-- cspell: words OWASP -->

# Spectral / JSON and YAML Linter

JSON and YAML files can be linted with custom rules
using [Spectral](https://github.com/stoplightio/spectral).
The goal of using this linter is to ensure that best practice is followed.

## Configuration

Each repo will need one configuration file in the root of the repository.

* `.spectral.yml` - Configures rules, which can be
used to lint the JSON or YAML files.

There are rules available to be used or customization is possible too.
For more information, please visit [Spectral Document](https://meta.stoplight.io/docs/spectral/e5b9616d6d50c-rulesets)

## Usage

The linter can be used in different purpose,
so a `FUNCTION` named `BUILD_SPECTRAL` is implemented
to make it suit the purpose.

`BUILD_SPECTRAL` contains four main arguments

* `file_type`: the file type that will be lint.
If it is set to `json`, minifying the JSON files will be performed.
JSON and YAML linting are not allowed simultaneously to prevent conflicts.
Enforcing separate linting
ensures accurate analysis for each file type in a folder, avoiding errors.
* `dir`: A directory that contains files to be lint.
* `src`: The root directory
* `rule_set`: Rules set that is used

## Example

The example of using the linter can be found in [link](/Earthfile).
The target `check-lint-openapi` is currently used for linting OpenAPI JSON file.
The current rules set that is being used are:

* [OWASP TOP 10](https://apistylebook.stoplight.io/docs/owasp-top-10)
* [Spectral Documentation](https://github.com/stoplightio/spectral-documentation)
* [OpenAPI](https://docs.stoplight.io/docs/spectral/4dec24461f3af-open-api-rules#openapi-rules)

The example of OpenAPI documentation can be found in [link](/examples/openapi/example.json).
<!-- markdownlint-disable max-one-sentence-per-line -->
!!! Warning
Please note that this OpenAPI documentation provided
in this repository is used for example purposes only.
<!-- markdownlint-enable max-one-sentence-per-line -->
For more information about OpenAPI Specification, please visit the
official [documentation](https://swagger.io/specification/#components-object).
31 changes: 31 additions & 0 deletions earthly/spectral/Earthfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
VERSION --global-cache --use-function-keyword 0.7

# cspell: words ruleset

spectral-base:
FROM stoplight/spectral
WORKDIR /work
COPY . .
RUN chmod +x ./minify-json.sh
SAVE ARTIFACT minify-json.sh

BUILD_SPECTRAL:
FUNCTION

# Specify what file type to lint
ARG file_type = "json"
ARG dir = .
ARG src = .
# Rule set for spectral
ARG rule_set=.spectral.yml

COPY $src .

COPY +spectral-base/minify-json.sh minify-json.sh
# If file type is json, minify the JSON
RUN ./minify-json.sh

RUN spectral \
lint \
$dir/"**/*.{yml,json}" \
--ruleset $rule_set
3 changes: 3 additions & 0 deletions earthly/spectral/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Spectral / JSON and YAML Linter

See [link](/docs/src/guides/json_yaml_linter.md) for the references.
22 changes: 22 additions & 0 deletions earthly/spectral/minify-json.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/sh

# Minify all JSON in the directory if the file type is json
# Check if FILE_TYPE and DIR are not empty
if [[ -n "${file_type}" ]] && [[ -n "${dir}" ]]; then
# Minify all JSON in the directory if the file type is json
if [[ "${file_type}" == "json" ]]; then
# Loop through each JSON file in the directory
for json_file in "${dir}"/*.json; do
# Check if the file is a regular file
if [[ -f "${json_file}" ]]; then
# Minify the JSON file using jq and overwrite the original file
jq -c . "${json_file}" > "${json_file}.tmp" && mv "${json_file}.tmp" "${json_file}"
echo "Compacted: ${json_file}"
fi
done
else
echo "File type is not JSON."
fi
else
echo "FILE_TYPE or DIR is empty."
fi
Loading