From 037865ec11141b80b0da29a28e25cd7b8ff1e295 Mon Sep 17 00:00:00 2001 From: Wonjun Kim Date: Sun, 21 Jun 2020 16:59:18 +0900 Subject: [PATCH] Add kotlin support to super-linter --- .automation/test/kotlin/README.md | 13 ++++++ .automation/test/kotlin/kotlin_bad_1.kt | 6 +++ .automation/test/kotlin/kotlint_good_1.kt | 6 +++ Dockerfile | 9 +++- README.md | 2 + docs/disabling-linters.md | 25 +++++++++++ lib/linter.sh | 55 +++++++++++++++++++++-- 7 files changed, 111 insertions(+), 5 deletions(-) create mode 100644 .automation/test/kotlin/README.md create mode 100644 .automation/test/kotlin/kotlin_bad_1.kt create mode 100644 .automation/test/kotlin/kotlint_good_1.kt diff --git a/.automation/test/kotlin/README.md b/.automation/test/kotlin/README.md new file mode 100644 index 00000000..f1fd9cfe --- /dev/null +++ b/.automation/test/kotlin/README.md @@ -0,0 +1,13 @@ +# Kotlin Test Cases +This folder holds the test cases for **Kotlin**. + +## 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. diff --git a/.automation/test/kotlin/kotlin_bad_1.kt b/.automation/test/kotlin/kotlin_bad_1.kt new file mode 100644 index 00000000..ad02ddc8 --- /dev/null +++ b/.automation/test/kotlin/kotlin_bad_1.kt @@ -0,0 +1,6 @@ +fun main() { + val n = "World"; + val v = "Hello, ${n}!"; + + println(v); +} \ No newline at end of file diff --git a/.automation/test/kotlin/kotlint_good_1.kt b/.automation/test/kotlin/kotlint_good_1.kt new file mode 100644 index 00000000..6e1f1a51 --- /dev/null +++ b/.automation/test/kotlin/kotlint_good_1.kt @@ -0,0 +1,6 @@ +fun main() { + val n = "World" + val v = "Hello, $n!" + + println(v) +} diff --git a/Dockerfile b/Dockerfile index a93c72bc..1021e0bd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -27,7 +27,7 @@ RUN apk add --no-cache \ libxml2-utils perl \ ruby ruby-dev ruby-bundler ruby-rdoc make \ py3-setuptools ansible-lint \ - go + go openjdk8-jre ##################### # Run Pip3 Installs # @@ -108,6 +108,12 @@ RUN curl -Ls "$(curl -Ls https://api.github.com/repos/terraform-linters/tflint/r RUN wget "https://github.com/dotenv-linter/dotenv-linter/releases/latest/download/dotenv-linter-alpine-x86_64.tar.gz" -O - -q | tar -xzf - \ && mv "dotenv-linter" /usr/bin +################## +# Install ktlint # +################## +RUN curl -sSLO https://github.com/pinterest/ktlint/releases/download/0.37.2/ktlint && chmod a+x ktlint \ + && mv "ktlint" /usr/bin/ + ########################################### # Load GitHub Env Vars for GitHub Actions # ########################################### @@ -135,6 +141,7 @@ ENV GITHUB_SHA=${GITHUB_SHA} \ VALIDATE_TERRAFORM=${VALIDATE_TERRAFORM} \ VALIDATE_CSS=${VALIDATE_CSS} \ VALIDATE_ENV=${VALIDATE_ENV} \ + VALIDATE_KOTLIN=${VALIDATE_KOTLIN} \ ANSIBLE_DIRECTORY=${ANSIBLE_DIRECTORY} \ RUN_LOCAL=${RUN_LOCAL} \ TEST_CASE_RUN=${TEST_CASE_RUN} \ diff --git a/README.md b/README.md index 8e6d6f39..0c3328f0 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ Developers on **GitHub** can call the **GitHub Action** to lint their code base | **XML** | [LibXML](http://xmlsoft.org/) | | **YAML** | [YamlLint](https://github.com/adrienverge/yamllint) | | **ENV** | [dotenv-linter](https://github.com/dotenv-linter/dotenv-linter) | +| **Kotlin** | [ktlint](https://github.com/pinterest/ktlint) | ## How to use To use this **GitHub** Action you will need to complete the following: @@ -137,6 +138,7 @@ and won't run anything unexpected. | **VALIDATE_TERRAFORM** | `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_KOTLIN** | `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). | | **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. | diff --git a/docs/disabling-linters.md b/docs/disabling-linters.md index b972d31c..0972bfda 100644 --- a/docs/disabling-linters.md +++ b/docs/disabling-linters.md @@ -22,6 +22,7 @@ Below is examples and documentation for each language and the various methods to - [Terraform](#terraform) - [CSS](#stylelint) - [ENV](#dotenv-linter) +- [Kotlin](#kotlin) @@ -564,3 +565,27 @@ a {} ### dotenv-linter disable entire file - There is currently **No** way to disable rules inline of the file(s) + +-------------------------------------------------------------------------------- + +## Kotlin +- [ktlint](https://github.com/pinterest/ktlint) + +### ktlint Config file +- There is no top level *configuration file* available at this time + +### ktlint disable single line +```kotlin +import package.* // ktlint-disable no-wildcard-imports +``` + +### ktlint disable code block +```kotlin +/* ktlint-disable no-wildcard-imports */ +import package.a.* +import package.b.* +/* ktlint-enable no-wildcard-imports */ +``` + +### ktlint disable entire file +- There is currently **No** way to disable rules inline of the file(s) diff --git a/lib/linter.sh b/lib/linter.sh index ca88f558..5d468ab0 100755 --- a/lib/linter.sh +++ b/lib/linter.sh @@ -59,14 +59,14 @@ CSS_LINTER_RULES="$DEFAULT_RULES_LOCATION/$CSS_FILE_NAME" # Path to th LINTER_ARRAY=("jsonlint" "yamllint" "xmllint" "markdownlint" "shellcheck" "pylint" "perl" "rubocop" "coffeelint" "eslint" "standard" "ansible-lint" "/dockerfilelint/bin/dockerfilelint" "golangci-lint" "tflint" - "stylelint" "dotenv-linter") + "stylelint" "dotenv-linter" "ktlint") ############################# # Language array for prints # ############################# LANGUAGE_ARRAY=('YML' 'JSON' 'XML' 'MARKDOWN' 'BASH' 'PERL' 'RUBY' 'PYTHON' 'COFFEESCRIPT' 'ANSIBLE' 'JAVASCRIPT_STANDARD' 'JAVASCRIPT_ES' - 'TYPESCRIPT_STANDARD' 'TYPESCRIPT_ES' 'DOCKER' 'GO' 'TERRAFORM' 'CSS' "ENV") + 'TYPESCRIPT_STANDARD' 'TYPESCRIPT_ES' 'DOCKER' 'GO' 'TERRAFORM' 'CSS' 'ENV' 'KOTLIN') ################### # GitHub ENV Vars # @@ -96,6 +96,7 @@ VALIDATE_GO="${VALIDATE_GO}" # Boolean to validate lang VALIDATE_TERRAFORM="${VALIDATE_TERRAFORM}" # Boolean to validate language VALIDATE_CSS="${VALIDATE_CSS}" # Boolean to validate language VALIDATE_ENV="${VALIDATE_ENV}" # Boolean to validate language +VALIDATE_KOTLIN="${VALIDATE_KOTLIN}" # Boolean to validate language 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 @@ -140,6 +141,7 @@ FILE_ARRAY_GO=() # Array of files to check FILE_ARRAY_TERRAFORM=() # Array of files to check FILE_ARRAY_CSS=() # Array of files to check FILE_ARRAY_ENV=() # Array of files to check +FILE_ARRAY_KOTLIN=() # Array of files to check ############ # Counters # @@ -163,6 +165,7 @@ ERRORS_FOUND_GO=0 # Count of errors found ERRORS_FOUND_TERRAFORM=0 # Count of errors found ERRORS_FOUND_CSS=0 # Count of errors found ERRORS_FOUND_ENV=0 # Count of errors found +ERRORS_FOUND_KOTLIN=0 # Count of errors found ################################################################################ ########################## FUNCTIONS BELOW ##################################### @@ -741,6 +744,7 @@ GetValidationInfo() VALIDATE_TERRAFORM=$(echo "$VALIDATE_TERRAFORM" | awk '{print tolower($0)}') VALIDATE_CSS=$(echo "$VALIDATE_CSS" | awk '{print tolower($0)}') VALIDATE_ENV=$(echo "$VALIDATE_ENV" | awk '{print tolower($0)}') + VALIDATE_KOTLIN=$(echo "$VALIDATE_KOTLIN" | awk '{print tolower($0)}') ################################################ # Determine if any linters were explicitly set # @@ -764,7 +768,8 @@ GetValidationInfo() -n "$VALIDATE_GO" || \ -n "$VALIDATE_TERRAFORM" || \ -n "$VALIDATE_CSS" || \ - -n "$VALIDATE_ENV" ]]; then + -n "$VALIDATE_ENV" || \ + -n "$VALIDATE_KOTLIN" ]]; then ANY_SET="true" fi @@ -1034,6 +1039,21 @@ GetValidationInfo() VALIDATE_ENV="true" fi + ###################################### + # Validate if we should check KOTLIN # + ###################################### + if [[ "$ANY_SET" == "true" ]]; then + # Some linter flags were set - only run those set to true + if [[ -z "$VALIDATE_KOTLIN" ]]; then + # ENV flag was not set - default to false + VALIDATE_KOTLIN="false" + fi + else + # No linter flags were set - default all to true + VALIDATE_KOTLIN="true" + fi + + ####################################### # Print which linters we are enabling # ####################################### @@ -1132,6 +1152,11 @@ GetValidationInfo() else PRINT_ARRAY+=("- Excluding [ENV] files in code base...") fi + if [[ "$VALIDATE_KOTLIN" == "true" ]]; then + PRINT_ARRAY+=("- Validating [KOTLIN] files in code base...") + else + PRINT_ARRAY+=("- Excluding [KOTLIN] files in code base...") + fi ############################## # Validate Ansible Directory # @@ -1479,6 +1504,15 @@ BuildFileList() # Set the READ_ONLY_CHANGE_FLAG since this could be exec # ########################################################## READ_ONLY_CHANGE_FLAG=1 + elif [ "$FILE_TYPE" == "kt" ] || [ "$FILE_TYPE" == "kts" ]; then + ################################ + # Append the file to the array # + ################################ + FILE_ARRAY_KOTLIN+=("$FILE") + ########################################################## + # Set the READ_ONLY_CHANGE_FLAG since this could be exec # + ########################################################## + READ_ONLY_CHANGE_FLAG=1 elif [ "$FILE" == "Dockerfile" ]; then ################################ # Append the file to the array # @@ -1992,7 +2026,8 @@ Footer() [ "$ERRORS_FOUND_TERRAFORM" -ne 0 ] || \ [ "$ERRORS_FOUND_RUBY" -ne 0 ] || \ [ "$ERRORS_FOUND_CSS" -ne 0 ] || \ - [ "$ERRORS_FOUND_ENV" -ne 0 ]; then + [ "$ERRORS_FOUND_ENV" -ne 0 ] || \ + [ "$ERRORS_FOUND_KOTLIN" -ne 0 ]; then # Failed exit echo "Exiting with errors found!" exit 1 @@ -2051,6 +2086,7 @@ RunTestCases() TestCodebase "TERRAFORM" "tflint" "tflint -c $TERRAFORM_LINTER_RULES" ".*\.\(tf\)\$" TestCodebase "CSS" "stylelint" "stylelint --config $CSS_LINTER_RULES" ".*\.\(css\)\$" TestCodebase "ENV" "dotenv-linter" "dotenv-linter" ".*\.\(env\)\$" + TestCodebase "KOTLIN" "ktlint" "ktlint" ".*\.\(kt\|kts\)\$" ################# # Footer prints # @@ -2347,6 +2383,17 @@ if [ "$VALIDATE_ENV" == "true" ]; then LintCodebase "ENV" "dotenv-linter" "dotenv-linter" ".*\.\(env\)\$" "${FILE_ARRAY_ENV[@]}" fi +################## +# KOTLIN LINTING # +################## +if [ "$VALIDATE_KOTLIN" == "true" ]; then + ####################### + # Lint the Kotlin files # + ####################### + # LintCodebase "FILE_TYPE" "LINTER_NAME" "LINTER_CMD" "FILE_TYPES_REGEX" "FILE_ARRAY" + LintCodebase "KOTLIN" "ktlint" "ktlint" ".*\.\(kt\|kts\)\$" "${FILE_ARRAY_ENV[@]}" +fi + ################## # DOCKER LINTING # ##################