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

API linting implementation guideline (#1) #110

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
17 changes: 11 additions & 6 deletions artifacts/linting_rules/.github/workflows/megalinter.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
---
# MegaLinter GitHub Action configuration file
# More info at https://megalinter.io
# CAMARA Project - Github Action for Pull Reqests
# 31.01.2024 - initial version

name: MegaLinter

on: # yamllint disable-line rule:truthy
Expand Down Expand Up @@ -31,16 +34,16 @@ jobs:
steps:
# Git Checkout
- name: Checkout Code
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
fetch-depth: 0 # If you use VALIDATE_ALL_CODEBASE = true, you can remove this line to improve performances
- name: Install Spectral
run: npm install -g @stoplight/spectral
- name: Install Spectral functions
run: npm install -g @stoplight/spectral-functions
- name: Run spectral:oas Spectral Linting
run: spectral lint code/API_definitions/openapi.yaml --verbose --ruleset .spectral.yml
# - name: Run spectral:oas Spectral Linting
# run: spectral lint code/API_definitions/*.yaml --verbose --ruleset .spectral.yml
# Replace openapi.yaml file with your API specification file

# MegaLinter
Expand All @@ -52,19 +55,21 @@ jobs:
env:
# All available variables are described in documentation
# https://megalinter.io/configuration/
PRINT_ALPACA: false
# VALIDATE_ALL_CODEBASE: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }} # Validates all source when push on main, else just the git diff with main. Override with true if you always want to lint all sources
VALIDATE_ALL_CODEBASE: true
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# ADD YOUR CUSTOM ENV VARIABLES HERE OR DEFINE THEM IN A FILE .mega-linter.yml AT THE ROOT OF YOUR REPOSITORY
DISABLE: COPYPASTE,MARKDOWN
DISABLE_LINTERS: SPELL_CSPELL,SPELL_LYCHEE,YAML_PRETTIER,REPOSITORY_SEMGREP,REPOSITORY_DEVSKIM,REPOSITORY_KICS,REPOSITORY_TRIVY,REPOSITORY_CHECKOV,REPOSITORY_GITLEAKS,JAVA_PMD
DISABLE_LINTERS: SPELL_CSPELL,SPELL_LYCHEE,YAML_PRETTIER,REPOSITORY_GRYPE, REPOSITORY_SEMGREP,REPOSITORY_DEVSKIM,REPOSITORY_KICS,REPOSITORY_TRIVY,REPOSITORY_TRIVY_SBOM,REPOSITORY_TRUFFLEHOG,REPOSITORY_CHECKOV,REPOSITORY_GITLEAKS,YAML_V8R,JAVA_PMD,JAVA_CHECKSTYLE
YAML_YAMLLINT_CONFIG_FILE: ".yamllint.yaml"
JAVA_CHECKSTYLE_CONFIG_FILE: "javalint.xml"
OPENAPI_SPECTRAL_CONFIG_FILE: ".spectral.yml"
YAML_YAMLLINT_FILTER_REGEX_INCLUDE: "(code/)"

# Upload MegaLinter artifacts
- name: Archive production artifacts
if: ${{ success() }} || ${{ failure() }}
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: MegaLinter reports
path: |
Expand Down
36 changes: 36 additions & 0 deletions artifacts/linting_rules/.github/workflows/spectral_oas_lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
# CAMARA Project - workflow configuration to manually run CAMARA OAS rules
# see https://docs.github.com/en/actions/using-workflows/manually-running-a-workflow
# 31.01.2024 - initial version

name: Spectral manual run

on: workflow_dispatch

concurrency:
group: ${{ github.ref }}-${{ github.workflow }}
cancel-in-progress: true

jobs:
build:
name: Spectral linting
runs-on: ubuntu-latest
permissions:
# Give the default GITHUB_TOKEN write permission to commit and push, comment issues & post new PR
# Remove the ones you do not need
contents: write
issues: write
pull-requests: write
steps:
# Git Checkout
- name: Checkout Code
uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
fetch-depth: 0 # If you use VALIDATE_ALL_CODEBASE = true, you can remove this line to improve performances
- name: Install Spectral
run: npm install -g @stoplight/spectral
- name: Install Spectral functions
run: npm install -g @stoplight/spectral-functions
- name: Run Spectral linting
run: spectral lint code/API_definitions/*.yaml --verbose --ruleset .spectral.yml
66 changes: 56 additions & 10 deletions artifacts/linting_rules/.spectral.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# CAMARA Project - linting ruleset - documentation avaialable here:
# https://github.com/camaraproject/Commonalities/blob/main/documentation/Linting-rules.md
# 31.01.2024 - initial version

extends: "spectral:oas"
functions:
- camara-reserved-words
Expand Down Expand Up @@ -36,15 +40,15 @@ rules:
typed-enum: true
oas3-api-servers: true
oas3-examples-value-or-externalValue: true
oas3-operation-security-defined: true
oas3-operation-security-defined: false
oas3-parameter-description: false
oas3-schema: true
oas3-server-not-example.com: false
oas3-server-trailing-slash: true
oas3-unused-component: true
oas3-valid-media-example: true
oas3-valid-schema-example: true
oas3-server-variables: true
# oas3-server-variables: true

# Custom Rules Utilizing Spectral's Built-in Functions and JavaScript Implementations

Expand Down Expand Up @@ -134,20 +138,62 @@ rules:
then:
function: camara-reserved-words
recommended: true # Set to true/false to enable/disable this rule


camara-routes-description:
rartych marked this conversation as resolved.
Show resolved Hide resolved
message: "Functionality method description Warning: Each method should have description."
severity: warn
description: |
This rule checks if each operation (POST, GET, DELETE, PUT, PATCH, OPTIONS) in your API specification has a description.
Ensure that you have added a 'summary' field for each operation in your OpenAPI specification.
given:
- "$.paths.*.post"
- "$.paths.*.get"
- "$.paths.*.delete"
- "$.paths.*.put"
- "$.paths.*.patch"
- "$.paths.*.options"
then:
field: description
function: truthy
recommended: true # Set to true/false to enable/disable this rule

camara-parameters-descriptions:
message: "Parameter description is missing or empty: {{error}}"
severity: warn
description: |
This Spectral rule ensures that each parameter in the API specification, including components and properties, has a descriptive and meaningful description.
This Spectral rule ensures that each path parameter in the API specification has a descriptive and meaningful description.
given:
- "$.paths..parameters.*"
then:
field: description
function: truthy
recommended: true # Set to true/false to enable/disable this rule

camara-response-descriptions:
message: "Parameter description is missing or empty: {{error}}"
severity: warn
description: |
This Spectral rule ensures that each responese object in the API specification has a descriptive and meaningful description.
given:
- "$.paths..responses.*"
then:
field: description
function: truthy
recommended: true # Set to true/false to enable/disable this rule

camara-properties-descriptions:
message: "Property description is missing or empty: {{error}}"
severity: warn
description: |
This Spectral rule ensures that each propoerty within objects in the API specification has a descriptive and meaningful description.
given:
- "$.components.*.*"
- "$.components.*.*.properties.*"
then:
field: description
function: truthy
recommended: true # Set to true/false to enable/disable this rule

camara-operation-summary:
message: "Operation Summary Warning: Each operation should include a short summary for better understanding."
severity: warn
Expand All @@ -169,7 +215,7 @@ rules:
camara-discriminator-use:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

description: |
Ensure that API definition YAML files with oneOf or anyOf sections include a discriminator object for serialization, deserialization, and validation.
severity: warn
severity: hint
given: "$..[?(@.oneOf || @.anyOf)]"
then:
field: discriminator
Expand Down Expand Up @@ -201,12 +247,12 @@ rules:
recommended: true # Set to true/false to enable/disable this rule

camara-parameter-casing-convention:
rartych marked this conversation as resolved.
Show resolved Hide resolved
description: This rule checks Paths should follow a specific case convention kebab-case.
description: Paths should be kebab-case.
severity: error
message: "{{property}} should be kebab-case: {{error}}"
message: "{{property}} is not kebab-case: {{error}}"
given: $.paths[*]~
then:
function: casing
function: pattern
functionOptions:
type: kebab
match: "^\/([a-z0-9]+(-[a-z0-9]+)*)?(\/[a-z0-9]+(-[a-z0-9]+)*|\/{.+})*$" # doesn't allow /asasd{asdas}sadas pattern or not closed braces
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not using built-in type: kebab

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested and updated PR with spectral core function kebab

recommended: true # Set to true/false to enable/disable this rule
6 changes: 5 additions & 1 deletion artifacts/linting_rules/.yamllint.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
---
# CAMARA Project - YAML linting configuration for yamllint https://yamllint.readthedocs.io/en/latest/rules.html
# 31.01.2024 - initial version

yaml-files:
- '*.yaml'
Expand All @@ -11,6 +13,7 @@ rules:
colons: enable
commas: enable
comments:
min-spaces-from-content: 1
level: error
comments-indentation:
level: error
Expand All @@ -24,7 +27,8 @@ rules:
key-ordering: disable
line-length: disable
new-line-at-end-of-file: enable
new-lines: enable
new-lines:
rartych marked this conversation as resolved.
Show resolved Hide resolved
type: platform
octal-values: disable
quoted-strings: disable
trailing-spaces: enable
Expand Down
Loading