Add support for OpenAPI

This commit is contained in:
Justin Kalland 2020-06-25 08:13:19 +02:00
parent a96ab33ea8
commit eac560b35b
9 changed files with 146 additions and 5 deletions

View file

@ -0,0 +1,13 @@
# OpenAPI Test Cases
This folder holds the test cases for **OpenAPI**.
## Additional Docs
No Additional information is needed for this test case.
## Good Test Cases
The test cases denoted: `LANGUAGE_good_FILE.EXTENSION` are all valid, and should pass successfully when linted.
- **Note:** They are linted utilizing the default linter rules.
## Bad Test Cases
The test cases denoted: `LANGUAGE_bad_FILE.EXTENSION` are **NOT** valid, and should trigger errors when linted.
- **Note:** They are linted utilizing the default linter rules.

View file

@ -0,0 +1 @@
openapi: '3.0.0'

View file

@ -0,0 +1,3 @@
{
"openapi": "3.0.0"
}

View file

@ -0,0 +1,13 @@
openapi: 3.0.0
info:
title: Example
version: '1.0'
contact:
name: Justin Kalland
email: justin@kalland.com
description: Test for super-linter
servers:
- url: 'http://localhost:3000'
paths: {}
tags:
- name: example

View file

@ -0,0 +1,23 @@
{
"openapi": "3.0.0",
"info": {
"title": "Example",
"version": "1.0",
"contact": {
"name": "Justin Kalland",
"email": "justin@kalland.com"
},
"description": "Test for super-linter"
},
"servers": [
{
"url": "http://localhost:3000"
}
],
"paths": {},
"tags": [
{
"name": "example"
}
]
}

View file

@ -27,7 +27,7 @@ RUN apk add --no-cache \
libxml2-utils perl \ libxml2-utils perl \
ruby ruby-dev ruby-bundler ruby-rdoc make \ ruby ruby-dev ruby-bundler ruby-rdoc make \
py3-setuptools ansible-lint \ py3-setuptools ansible-lint \
go \ go \
openjdk8-jre \ openjdk8-jre \
php7 \ php7 \
ca-certificates less ncurses-terminfo-base \ ca-certificates less ncurses-terminfo-base \
@ -72,6 +72,7 @@ RUN npm config set package-lock false \
eslint-plugin-jest \ eslint-plugin-jest \
stylelint \ stylelint \
stylelint-config-standard \ stylelint-config-standard \
@stoplight/spectral \
&& npm --no-cache install \ && npm --no-cache install \
markdownlint-cli \ markdownlint-cli \
jsonlint prettyjson \ jsonlint prettyjson \
@ -168,6 +169,7 @@ ENV GITHUB_SHA=${GITHUB_SHA} \
VALIDATE_ENV=${VALIDATE_ENV} \ VALIDATE_ENV=${VALIDATE_ENV} \
VALIDATE_KOTLIN=${VALIDATE_KOTLIN} \ VALIDATE_KOTLIN=${VALIDATE_KOTLIN} \
VALIDATE_POWERSHELL=${VALIDATE_POWERSHELL} \ VALIDATE_POWERSHELL=${VALIDATE_POWERSHELL} \
VALIDATE_OPENAPI=${VALIDATE_OPENAPI} \
ANSIBLE_DIRECTORY=${ANSIBLE_DIRECTORY} \ ANSIBLE_DIRECTORY=${ANSIBLE_DIRECTORY} \
RUN_LOCAL=${RUN_LOCAL} \ RUN_LOCAL=${RUN_LOCAL} \
TEST_CASE_RUN=${TEST_CASE_RUN} \ TEST_CASE_RUN=${TEST_CASE_RUN} \

View file

@ -38,6 +38,7 @@ Developers on **GitHub** can call the **GitHub Action** to lint their code base
| **PowerShell** | [PSScriptAnalyzer](https://github.com/PowerShell/Psscriptanalyzer) | | **PowerShell** | [PSScriptAnalyzer](https://github.com/PowerShell/Psscriptanalyzer) |
| **ENV** | [dotenv-linter](https://github.com/dotenv-linter/dotenv-linter) | | **ENV** | [dotenv-linter](https://github.com/dotenv-linter/dotenv-linter) |
| **Kotlin** | [ktlint](https://github.com/pinterest/ktlint) | | **Kotlin** | [ktlint](https://github.com/pinterest/ktlint) |
| **OpenAPI** | [spectral](https://github.com/stoplightio/spectral) |
## How to use ## How to use
To use this **GitHub** Action you will need to complete the following: To use this **GitHub** Action you will need to complete the following:
@ -144,6 +145,7 @@ and won't run anything unexpected.
| **VALIDATE_CSS** | `true` | Flag to enable or disable the linting process of the language. | | **VALIDATE_CSS** | `true` | Flag to enable or disable the linting process of the language. |
| **VALIDATE_ENV** | `true` | Flag to enable or disable the linting process of the language. | | **VALIDATE_ENV** | `true` | Flag to enable or disable the linting process of the language. |
| **VALIDATE_KOTLIN** | `true` | Flag to enable or disable the linting process of the language. | | **VALIDATE_KOTLIN** | `true` | Flag to enable or disable the linting process of the language. |
| **VALIDATE_OPENAPI** | `true` | Flag to enable or disable the linting process of the language. |
| **ANSIBLE_DIRECTORY** | `/ansible` | Flag to set the root directory for Ansible file location(s). | | **ANSIBLE_DIRECTORY** | `/ansible` | Flag to set the root directory for Ansible file location(s). |
| **ACTIONS_RUNNER_DEBUG** | `false` | Flag to enable additional information about the linter, versions, and additional output. | | **ACTIONS_RUNNER_DEBUG** | `false` | Flag to enable additional information about the linter, versions, and additional output. |
| **DISABLE_ERRORS** | `false` | Flag to have the linter complete with exit code 0 even if errors were detected. | | **DISABLE_ERRORS** | `false` | Flag to have the linter complete with exit code 0 even if errors were detected. |

9
TEMPLATES/.openapirc.yml Normal file
View file

@ -0,0 +1,9 @@
---
##########################
##########################
## OpenAPI Linter rules ##
##########################
##########################
extends: spectral:oas

View file

@ -54,6 +54,9 @@ POWERSHELL_LINTER_RULES="$DEFAULT_RULES_LOCATION/$POWERSHELL_FILE_NAME" # Pat
# CSS Vars # CSS Vars
CSS_FILE_NAME='.stylelintrc.json' # Name of the file CSS_FILE_NAME='.stylelintrc.json' # Name of the file
CSS_LINTER_RULES="$DEFAULT_RULES_LOCATION/$CSS_FILE_NAME" # Path to the CSS lint rules CSS_LINTER_RULES="$DEFAULT_RULES_LOCATION/$CSS_FILE_NAME" # Path to the CSS lint rules
# OpenAPI Vars
OPENAPI_FILE_NAME='.openapirc.yml' # Name of the file
OPENAPI_LINTER_RULES="$DEFAULT_RULES_LOCATION/$OPENAPI_FILE_NAME" # Path to the OpenAPI lint rules
####################################### #######################################
# Linter array for information prints # # Linter array for information prints #
@ -61,7 +64,7 @@ CSS_LINTER_RULES="$DEFAULT_RULES_LOCATION/$CSS_FILE_NAME" # Path to th
LINTER_ARRAY=("jsonlint" "yamllint" "xmllint" "markdownlint" "shellcheck" LINTER_ARRAY=("jsonlint" "yamllint" "xmllint" "markdownlint" "shellcheck"
"pylint" "perl" "rubocop" "coffeelint" "eslint" "standard" "pylint" "perl" "rubocop" "coffeelint" "eslint" "standard"
"ansible-lint" "/dockerfilelint/bin/dockerfilelint" "golangci-lint" "tflint" "ansible-lint" "/dockerfilelint/bin/dockerfilelint" "golangci-lint" "tflint"
"stylelint" "dotenv-linter" "powershell" "ktlint") "stylelint" "dotenv-linter" "powershell" "ktlint" "spectral")
############################# #############################
# Language array for prints # # Language array for prints #
@ -69,7 +72,7 @@ LINTER_ARRAY=("jsonlint" "yamllint" "xmllint" "markdownlint" "shellcheck"
LANGUAGE_ARRAY=('YML' 'JSON' 'XML' 'MARKDOWN' 'BASH' 'PERL' 'PHP' 'RUBY' 'PYTHON' LANGUAGE_ARRAY=('YML' 'JSON' 'XML' 'MARKDOWN' 'BASH' 'PERL' 'PHP' 'RUBY' 'PYTHON'
'COFFEESCRIPT' 'ANSIBLE' 'JAVASCRIPT_STANDARD' 'JAVASCRIPT_ES' 'COFFEESCRIPT' 'ANSIBLE' 'JAVASCRIPT_STANDARD' 'JAVASCRIPT_ES'
'TYPESCRIPT_STANDARD' 'TYPESCRIPT_ES' 'DOCKER' 'GO' 'TERRAFORM' 'TYPESCRIPT_STANDARD' 'TYPESCRIPT_ES' 'DOCKER' 'GO' 'TERRAFORM'
'ENV' 'POWERSHELL' 'KOTLIN') 'ENV' 'POWERSHELL' 'KOTLIN' 'OPENAPI')
################### ###################
# GitHub ENV Vars # # GitHub ENV Vars #
@ -102,6 +105,7 @@ VALIDATE_ENV="${VALIDATE_ENV}" # Boolean to validate lang
VALIDATE_TERRAFORM="${VALIDATE_TERRAFORM}" # Boolean to validate language VALIDATE_TERRAFORM="${VALIDATE_TERRAFORM}" # Boolean to validate language
VALIDATE_POWERSHELL="${VALIDATE_POWERSHELL}" # Boolean to validate language VALIDATE_POWERSHELL="${VALIDATE_POWERSHELL}" # Boolean to validate language
VALIDATE_KOTLIN="${VALIDATE_KOTLIN}" # Boolean to validate language VALIDATE_KOTLIN="${VALIDATE_KOTLIN}" # Boolean to validate language
VALIDATE_OPENAPI="${VALIDATE_OPENAPI}" # Boolean to validate language
TEST_CASE_RUN="${TEST_CASE_RUN}" # Boolean to validate only test cases TEST_CASE_RUN="${TEST_CASE_RUN}" # Boolean to validate only test cases
DISABLE_ERRORS="${DISABLE_ERRORS}" # Boolean to enable warning-only output without throwing errors DISABLE_ERRORS="${DISABLE_ERRORS}" # Boolean to enable warning-only output without throwing errors
@ -149,6 +153,7 @@ FILE_ARRAY_POWERSHELL=() # Array of files to check
FILE_ARRAY_CSS=() # Array of files to check FILE_ARRAY_CSS=() # Array of files to check
FILE_ARRAY_ENV=() # Array of files to check FILE_ARRAY_ENV=() # Array of files to check
FILE_ARRAY_KOTLIN=() # Array of files to check FILE_ARRAY_KOTLIN=() # Array of files to check
FILE_ARRAY_OPENAPI=() # Array of files to check
############ ############
# Counters # # Counters #
@ -175,6 +180,7 @@ ERRORS_FOUND_POWERSHELL=0 # Count of errors found
ERRORS_FOUND_CSS=0 # Count of errors found ERRORS_FOUND_CSS=0 # Count of errors found
ERRORS_FOUND_ENV=0 # Count of errors found ERRORS_FOUND_ENV=0 # Count of errors found
ERRORS_FOUND_KOTLIN=0 # Count of errors found ERRORS_FOUND_KOTLIN=0 # Count of errors found
ERRORS_FOUND_OPENAPI=0 # Count of errors found
################################################################################ ################################################################################
########################## FUNCTIONS BELOW ##################################### ########################## FUNCTIONS BELOW #####################################
@ -556,6 +562,19 @@ LintAnsibleFiles()
fi fi
fi fi
} }
################################################################################
#### Function DetectOpenAPIFile ################################################
DetectOpenAPIFile()
{
egrep '"openapi":|"swagger":|^openapi:|^swagger:' $GITHUB_WORKSPACE/$1 > /dev/null
if [ $? -eq 0 ]; then
return 0
else
return 1
fi
}
################################################################################ ################################################################################
#### Function GetGitHubVars #################################################### #### Function GetGitHubVars ####################################################
GetGitHubVars() GetGitHubVars()
@ -756,6 +775,7 @@ GetValidationInfo()
VALIDATE_CSS=$(echo "$VALIDATE_CSS" | awk '{print tolower($0)}') VALIDATE_CSS=$(echo "$VALIDATE_CSS" | awk '{print tolower($0)}')
VALIDATE_ENV=$(echo "$VALIDATE_ENV" | awk '{print tolower($0)}') VALIDATE_ENV=$(echo "$VALIDATE_ENV" | awk '{print tolower($0)}')
VALIDATE_KOTLIN=$(echo "$VALIDATE_KOTLIN" | awk '{print tolower($0)}') VALIDATE_KOTLIN=$(echo "$VALIDATE_KOTLIN" | awk '{print tolower($0)}')
VALIDATE_OPENAPI=$(echo "$VALIDATE_OPENAPI" | awk '{print tolower($0)}')
################################################ ################################################
# Determine if any linters were explicitly set # # Determine if any linters were explicitly set #
@ -782,7 +802,8 @@ GetValidationInfo()
-n "$VALIDATE_POWERSHELL" || \ -n "$VALIDATE_POWERSHELL" || \
-n "$VALIDATE_CSS" || \ -n "$VALIDATE_CSS" || \
-n "$VALIDATE_ENV" || \ -n "$VALIDATE_ENV" || \
-n "$VALIDATE_KOTLIN" ]]; then -n "$VALIDATE_KOTLIN" || \
-n "$VALIDATE_OPENAPI" ]]; then
ANY_SET="true" ANY_SET="true"
fi fi
@ -1094,6 +1115,19 @@ GetValidationInfo()
VALIDATE_KOTLIN="true" VALIDATE_KOTLIN="true"
fi fi
#######################################
# Validate if we should check OPENAPI #
#######################################
if [[ "$ANY_SET" == "true" ]]; then
# Some linter flags were set - only run those set to true
if [[ -z "$VALIDATE_OPENAPI" ]]; then
# OPENAPI flag was not set - default to false
VALIDATE_OPENAPI="false"
fi
else
# No linter flags were set - default all to true
VALIDATE_OPENAPI="true"
fi
####################################### #######################################
# Print which linters we are enabling # # Print which linters we are enabling #
@ -1208,6 +1242,11 @@ GetValidationInfo()
else else
PRINT_ARRAY+=("- Excluding [KOTLIN] files in code base...") PRINT_ARRAY+=("- Excluding [KOTLIN] files in code base...")
fi fi
if [[ "$VALIDATE_OPENAPI" == "true" ]]; then
PRINT_ARRAY+=("- Validating [OPENAPI] files in code base...")
else
PRINT_ARRAY+=("- Excluding [OPENAPI] files in code base...")
fi
############################## ##############################
# Validate Ansible Directory # # Validate Ansible Directory #
@ -1391,6 +1430,12 @@ BuildFileList()
# Append the file to the array # # Append the file to the array #
################################ ################################
FILE_ARRAY_YML+=("$FILE") FILE_ARRAY_YML+=("$FILE")
############################
# Check if file is OpenAPI #
############################
if DetectOpenAPIFile $FILE; then
FILE_ARRAY_OPENAPI+=("$FILE")
fi
########################################################## ##########################################################
# Set the READ_ONLY_CHANGE_FLAG since this could be exec # # Set the READ_ONLY_CHANGE_FLAG since this could be exec #
########################################################## ##########################################################
@ -1403,6 +1448,12 @@ BuildFileList()
# Append the file to the array # # Append the file to the array #
################################ ################################
FILE_ARRAY_JSON+=("$FILE") FILE_ARRAY_JSON+=("$FILE")
############################
# Check if file is OpenAPI #
############################
if DetectOpenAPIFile $FILE; then
FILE_ARRAY_OPENAPI+=("$FILE")
fi
########################################################## ##########################################################
# Set the READ_ONLY_CHANGE_FLAG since this could be exec # # Set the READ_ONLY_CHANGE_FLAG since this could be exec #
########################################################## ##########################################################
@ -2122,7 +2173,8 @@ Footer()
[ "$ERRORS_FOUND_RUBY" -ne 0 ] || \ [ "$ERRORS_FOUND_RUBY" -ne 0 ] || \
[ "$ERRORS_FOUND_CSS" -ne 0 ] || \ [ "$ERRORS_FOUND_CSS" -ne 0 ] || \
[ "$ERRORS_FOUND_ENV" -ne 0 ] || \ [ "$ERRORS_FOUND_ENV" -ne 0 ] || \
[ "$ERRORS_FOUND_KOTLIN" -ne 0 ]; then [ "$ERRORS_FOUND_KOTLIN" -ne 0 ] || \
[ "$ERRORS_FOUND_OPENAPI" -ne 0 ]; then
# Failed exit # Failed exit
echo "Exiting with errors found!" echo "Exiting with errors found!"
exit 1 exit 1
@ -2184,6 +2236,7 @@ RunTestCases()
TestCodebase "CSS" "stylelint" "stylelint --config $CSS_LINTER_RULES" ".*\.\(css\)\$" TestCodebase "CSS" "stylelint" "stylelint --config $CSS_LINTER_RULES" ".*\.\(css\)\$"
TestCodebase "ENV" "dotenv-linter" "dotenv-linter" ".*\.\(env\)\$" TestCodebase "ENV" "dotenv-linter" "dotenv-linter" ".*\.\(env\)\$"
TestCodebase "KOTLIN" "ktlint" "ktlint" ".*\.\(kt\|kts\)\$" TestCodebase "KOTLIN" "ktlint" "ktlint" ".*\.\(kt\|kts\)\$"
TestCodebase "OPENAPI" "spectral" "spectral lint -r $OPENAPI_LINTER_RULES" ".*\.\(yml\|yaml\|json\)\$"
################# #################
# Footer prints # # Footer prints #
@ -2526,6 +2579,28 @@ if [ "$VALIDATE_POWERSHELL" == "true" ]; then
LintCodebase "POWERSHELL" "pwsh" "pwsh -c Invoke-ScriptAnalyzer -EnableExit -Settings $POWERSHELL_LINTER_RULES -Path" ".*\.\(ps1\|psm1\|psd1\|ps1xml\|pssc\|psrc\|cdxml\)\$" "${FILE_ARRAY_POWERSHELL[@]}" LintCodebase "POWERSHELL" "pwsh" "pwsh -c Invoke-ScriptAnalyzer -EnableExit -Settings $POWERSHELL_LINTER_RULES -Path" ".*\.\(ps1\|psm1\|psd1\|ps1xml\|pssc\|psrc\|cdxml\)\$" "${FILE_ARRAY_POWERSHELL[@]}"
fi fi
###################
# OPENAPI LINTING #
###################
if [ "$VALIDATE_OPENAPI" == "true" ]; then
# If we are validating all codebase we need to build file list because not every yml/json file is an OpenAPI file
if [ "$VALIDATE_ALL_CODEBASE" == "true" ]; then
LIST_FILES=($(cd "$GITHUB_WORKSPACE" || exit; find . -type f -regex ".*\.\(yml\|yaml\|json\)\$" 2>&1))
for FILE in "${LIST_FILES[@]}"
do
if DetectOpenAPIFile $FILE; then
FILE_ARRAY_OPENAPI+=("$FILE")
fi
done
fi
##########################
# Lint the OpenAPI files #
##########################
# LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY"
LintCodebase "OPENAPI" "spectral" "spectral lint -r $OPENAPI_LINTER_RULES" "disabledfileext" "${FILE_ARRAY_OPENAPI[@]}"
fi
########## ##########
# Footer # # Footer #
########## ##########