From 1afc7f8409857f473a2596be137e74fef822c96d Mon Sep 17 00:00:00 2001 From: guotongfei <86636834+guotongfei@users.noreply.github.com> Date: Tue, 28 Sep 2021 18:09:21 -0400 Subject: [PATCH] Enhancement: Add support for google-java-format (#1969) * Add support for google-java-format * adding tests and array * fix comma * Update linter.sh * get it in order * fix hava version Co-authored-by: Admiral Awkbar --- .automation/test/google_java_format/README.md | 19 ++++++++++++++++++ .../google_java_format_bad_1.java | 8 ++++++++ .../google_java_format_bad_2.java | 12 +++++++++++ .../google_java_format_good_1.java | 9 +++++++++ .../google_java_format_good_2.java | 13 ++++++++++++ Dockerfile | 10 +++++++++- Dockerfile-slim | 10 +++++++++- README.md | 5 +++-- lib/functions/buildFileList.sh | 1 + lib/functions/linterVersions.sh | 2 +- lib/linter.sh | 20 ++++++++++--------- .../super-linter/controls/super_linter.rb | 1 + 12 files changed, 96 insertions(+), 14 deletions(-) create mode 100644 .automation/test/google_java_format/README.md create mode 100644 .automation/test/google_java_format/google_java_format_bad_1.java create mode 100644 .automation/test/google_java_format/google_java_format_bad_2.java create mode 100644 .automation/test/google_java_format/google_java_format_good_1.java create mode 100644 .automation/test/google_java_format/google_java_format_good_2.java diff --git a/.automation/test/google_java_format/README.md b/.automation/test/google_java_format/README.md new file mode 100644 index 00000000..8fef03d8 --- /dev/null +++ b/.automation/test/google_java_format/README.md @@ -0,0 +1,19 @@ +# Google Java Format Test Cases + +This folder holds the test cases for **Google Java format**. + +## 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/google_java_format/google_java_format_bad_1.java b/.automation/test/google_java_format/google_java_format_bad_1.java new file mode 100644 index 00000000..1d4127e9 --- /dev/null +++ b/.automation/test/google_java_format/google_java_format_bad_1.java @@ -0,0 +1,8 @@ +class java_bad_1 // first letter of classname is not capitalized +{ + + public static void main(String args[]) + { + System.out.println("Hello, World"); + } +} diff --git a/.automation/test/google_java_format/google_java_format_bad_2.java b/.automation/test/google_java_format/google_java_format_bad_2.java new file mode 100644 index 00000000..c728c76f --- /dev/null +++ b/.automation/test/google_java_format/google_java_format_bad_2.java @@ -0,0 +1,12 @@ +public class Application { + + /** + * main. + * + * @param args + */ + public static void main(final String[] args) { + SpringApplication.run(Application.class, args); + } + +} diff --git a/.automation/test/google_java_format/google_java_format_good_1.java b/.automation/test/google_java_format/google_java_format_good_1.java new file mode 100644 index 00000000..70f50e7c --- /dev/null +++ b/.automation/test/google_java_format/google_java_format_good_1.java @@ -0,0 +1,9 @@ +/** + * Represents a good Java file. +*/ +public static class JavaGood { + // Prints "Hello, World" to the terminal window. + private void helloWorld() { + System.out.println("Hello, World"); + } +} diff --git a/.automation/test/google_java_format/google_java_format_good_2.java b/.automation/test/google_java_format/google_java_format_good_2.java new file mode 100644 index 00000000..376cef1f --- /dev/null +++ b/.automation/test/google_java_format/google_java_format_good_2.java @@ -0,0 +1,13 @@ +@SuppressWarnings("checkstyle:hideutilityclassconstructor") +public class Application { + + /** + * main. + * + * @param args + */ + public static void main(final String[] args) { + SpringApplication.run(Application.class, args); + } + +} diff --git a/Dockerfile b/Dockerfile index 15c0e518..e62da418 100644 --- a/Dockerfile +++ b/Dockerfile @@ -69,7 +69,7 @@ RUN apk add --no-cache \ make \ musl-dev \ npm nodejs-current \ - openjdk8-jre \ + openjdk11-jre \ openssl-dev \ perl perl-dev \ py3-setuptools python3-dev \ @@ -287,6 +287,14 @@ RUN echo "http://dl-cdn.alpinelinux.org/alpine/edge/community/" >> /etc/apk/repo | cut -d '"' -f 4) \ && curl --retry 5 --retry-delay 5 -sSL "$CHECKSTYLE_LATEST" \ --output /usr/bin/checkstyle \ +############################## +# Install google-java-format # +############################## + && GOOGLE_JAVA_FORMAT_VERSION=$(curl -s https://github.com/google/google-java-format/releases/latest \ + | cut -d '"' -f 2 | cut -d '/' -f 8 | sed -e 's/v//g') \ + && curl --retry 5 --retry-delay 5 -sSL \ + "https://github.com/google/google-java-format/releases/download/v$GOOGLE_JAVA_FORMAT_VERSION/google-java-format-$GOOGLE_JAVA_FORMAT_VERSION-all-deps.jar" \ + --output /usr/bin/google-java-format \ ################################# # Install luacheck and luarocks # ################################# diff --git a/Dockerfile-slim b/Dockerfile-slim index db98dfa4..3e69af0a 100644 --- a/Dockerfile-slim +++ b/Dockerfile-slim @@ -68,7 +68,7 @@ RUN apk add --no-cache \ make \ musl-dev \ npm nodejs-current \ - openjdk8-jre \ + openjdk11-jre \ openssl-dev \ perl perl-dev \ py3-setuptools python3-dev \ @@ -225,6 +225,14 @@ RUN echo "http://dl-cdn.alpinelinux.org/alpine/edge/community/" >> /etc/apk/repo | cut -d '"' -f 4) \ && curl --retry 5 --retry-delay 5 -sSL "$CHECKSTYLE_LATEST" \ --output /usr/bin/checkstyle \ +############################## +# Install google-java-format # +############################## + && GOOGLE_JAVA_FORMAT_VERSION=$(curl -s https://github.com/google/google-java-format/releases/latest \ + | cut -d '"' -f 2 | cut -d '/' -f 8 | sed -e 's/v//g') \ + && curl --retry 5 --retry-delay 5 -sSL \ + "https://github.com/google/google-java-format/releases/download/v$GOOGLE_JAVA_FORMAT_VERSION/google-java-format-$GOOGLE_JAVA_FORMAT_VERSION-all-deps.jar" \ + --output /usr/bin/google-java-format \ ################################# # Install luacheck and luarocks # ################################# diff --git a/README.md b/README.md index d99d55c2..d998ed2d 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,7 @@ Developers on **GitHub** can call the **GitHub Action** to lint their code base | **Golang** | [golangci-lint](https://github.com/golangci/golangci-lint) | | **Groovy** | [npm-groovy-lint](https://github.com/nvuillam/npm-groovy-lint) | | **HTML** | [HTMLHint](https://github.com/htmlhint/HTMLHint) | -| **Java** | [checkstyle](https://checkstyle.org) | +| **Java** | [checkstyle](https://checkstyle.org) / [google-java-format](https://github.com/google/google-java-format) | | **JavaScript** | [eslint](https://eslint.org/) / [standard js](https://standardjs.com/) | | **JSON** | [eslint-plugin-json](https://www.npmjs.com/package/eslint-plugin-json) | | **JSONC** | [eslint-plugin-jsonc](https://www.npmjs.com/package/eslint-plugin-jsonc) | @@ -337,9 +337,10 @@ But if you wish to select or exclude specific linters, we give you full control | **VALIDATE_GITLEAKS** | `true` | Flag to enable or disable the linting process of the secrets. | | **VALIDATE_GHERKIN** | `true` | Flag to enable or disable the linting process of the Gherkin language. | | **VALIDATE_GO** | `true` | Flag to enable or disable the linting process of the Golang language. | +| **VALIDATE_GOOGLE_JAVA_FORMAT** | `true` | Flag to enable or disable the linting process of the Java language. (Utilizing: google-java-format) | | **VALIDATE_GROOVY** | `true` | Flag to enable or disable the linting process of the language. | | **VALIDATE_HTML** | `true` | Flag to enable or disable the linting process of the HTML language. | -| **VALIDATE_JAVA** | `true` | Flag to enable or disable the linting process of the language. | +| **VALIDATE_JAVA** | `true` | Flag to enable or disable the linting process of the Java language. (Utilizing: checkstyle) | | **VALIDATE_JAVASCRIPT_ES** | `true` | Flag to enable or disable the linting process of the Javascript language. (Utilizing: eslint) | | **VALIDATE_JAVASCRIPT_STANDARD** | `true` | Flag to enable or disable the linting process of the Javascript language. (Utilizing: standard) | | **VALIDATE_JSCPD** | `true` | Flag to enable or disable the JSCPD. | diff --git a/lib/functions/buildFileList.sh b/lib/functions/buildFileList.sh index cb5ecf21..64de4ae4 100755 --- a/lib/functions/buildFileList.sh +++ b/lib/functions/buildFileList.sh @@ -457,6 +457,7 @@ function BuildFileList() { # Append the file to the array # ################################ FILE_ARRAY_JAVA+=("${FILE}") + FILE_ARRAY_GOOGLE_JAVA_FORMAT+=("${FILE}") ############################ # Get the JavaScript files # diff --git a/lib/functions/linterVersions.sh b/lib/functions/linterVersions.sh index b5668546..d7a50725 100755 --- a/lib/functions/linterVersions.sh +++ b/lib/functions/linterVersions.sh @@ -81,7 +81,7 @@ BuildLinterVersions() { GET_VERSION_CMD="$("${LINTER}" -v 2>&1)" elif [[ ${LINTER} == "terrascan" ]]; then GET_VERSION_CMD="$("${LINTER}" version 2>&1)" - elif [[ ${LINTER} == "checkstyle" ]]; then + elif [[ ${LINTER} == "checkstyle" ]] || [[ ${LINTER} == "google-java-format" ]]; then GET_VERSION_CMD="$(java -jar "/usr/bin/${LINTER}" --version 2>&1)" elif [[ ${LINTER} == "clippy" ]]; then GET_VERSION_CMD="$(cargo-clippy --version 2>&1)" diff --git a/lib/linter.sh b/lib/linter.sh index a92eff50..dac100ed 100755 --- a/lib/linter.sh +++ b/lib/linter.sh @@ -202,15 +202,15 @@ fi LANGUAGE_ARRAY=('ANSIBLE' 'ARM' 'BASH' 'BASH_EXEC' 'CLANG_FORMAT' 'CLOUDFORMATION' 'CLOJURE' 'COFFEESCRIPT' 'CPP' 'CSHARP' 'CSS' 'DART' 'DOCKERFILE' 'DOCKERFILE_HADOLINT' 'EDITORCONFIG' 'ENV' 'GITHUB_ACTIONS' - 'GITLEAKS' 'GHERKIN' 'GO' 'GROOVY' 'HTML' 'JAVA' 'JAVASCRIPT_ES' - "${JAVASCRIPT_STYLE_NAME}" 'JSCPD' 'JSON' 'JSONC' 'JSX' 'KUBERNETES_KUBEVAL' - 'KOTLIN' 'LATEX' 'LUA' 'MARKDOWN' 'OPENAPI' 'PERL' 'PHP_BUILTIN' 'PHP_PHPCS' - 'PHP_PHPSTAN' 'PHP_PSALM' 'POWERSHELL' 'PROTOBUF' 'PYTHON_BLACK' - 'PYTHON_PYLINT' 'PYTHON_FLAKE8' 'PYTHON_ISORT' 'PYTHON_MYPY' 'R' 'RAKU' 'RUBY' - 'RUST_2015' 'RUST_2018' 'RUST_CLIPPY' 'SHELL_SHFMT' 'SNAKEMAKE_LINT' - 'SNAKEMAKE_SNAKEFMT' 'STATES' 'SQL' 'SQLFLUFF' 'TEKTON' 'TERRAFORM_TFLINT' - 'TERRAFORM_TERRASCAN' 'TERRAGRUNT' 'TSX' 'TYPESCRIPT_ES' 'TYPESCRIPT_STANDARD' - 'XML' 'YAML') + 'GITLEAKS' 'GHERKIN' 'GO' 'GOOGLE_JAVA_FORMAT' 'GROOVY' 'HTML' 'JAVA' + 'JAVASCRIPT_ES' "${JAVASCRIPT_STYLE_NAME}" 'JSCPD' 'JSON' 'JSONC' 'JSX' + 'KUBERNETES_KUBEVAL' 'KOTLIN' 'LATEX' 'LUA' 'MARKDOWN' 'OPENAPI' 'PERL' + 'PHP_BUILTIN' 'PHP_PHPCS' 'PHP_PHPSTAN' 'PHP_PSALM' 'POWERSHELL' 'PROTOBUF' + 'PYTHON_BLACK' 'PYTHON_PYLINT' 'PYTHON_FLAKE8' 'PYTHON_ISORT' 'PYTHON_MYPY' + 'R' 'RAKU' 'RUBY' 'RUST_2015' 'RUST_2018' 'RUST_CLIPPY' 'SHELL_SHFMT' + 'SNAKEMAKE_LINT' 'SNAKEMAKE_SNAKEFMT' 'STATES' 'SQL' 'SQLFLUFF' 'TEKTON' + 'TERRAFORM_TFLINT' 'TERRAFORM_TERRASCAN' 'TERRAGRUNT' 'TSX' 'TYPESCRIPT_ES' + 'TYPESCRIPT_STANDARD' 'XML' 'YAML') ############################## # Linter command names array # @@ -236,6 +236,7 @@ LINTER_NAMES_ARRAY['GITHUB_ACTIONS']="actionlint" LINTER_NAMES_ARRAY['GITLEAKS']="gitleaks" LINTER_NAMES_ARRAY['GHERKIN']="gherkin-lint" LINTER_NAMES_ARRAY['GO']="golangci-lint" +LINTER_NAMES_ARRAY['GOOGLE_JAVA_FORMAT']="google-java-format" LINTER_NAMES_ARRAY['GROOVY']="npm-groovy-lint" LINTER_NAMES_ARRAY['HTML']="htmlhint" LINTER_NAMES_ARRAY['JAVA']="checkstyle" @@ -837,6 +838,7 @@ LINTER_COMMANDS_ARRAY['GITHUB_ACTIONS']="actionlint -config-file ${GITHUB_ACTION LINTER_COMMANDS_ARRAY['GITLEAKS']="gitleaks -q -c ${GITLEAKS_LINTER_RULES} --no-git -p" LINTER_COMMANDS_ARRAY['GHERKIN']="gherkin-lint -c ${GHERKIN_LINTER_RULES}" LINTER_COMMANDS_ARRAY['GO']="golangci-lint run -c ${GO_LINTER_RULES}" +LINTER_COMMANDS_ARRAY['GOOGLE_JAVA_FORMAT']="java -jar /usr/bin/google-java-format" LINTER_COMMANDS_ARRAY['GROOVY']="npm-groovy-lint -c ${GROOVY_LINTER_RULES} --failon warning" LINTER_COMMANDS_ARRAY['HTML']="htmlhint --config ${HTML_LINTER_RULES}" LINTER_COMMANDS_ARRAY['JAVA']="java -jar /usr/bin/checkstyle -c ${JAVA_LINTER_RULES}" diff --git a/test/inspec/super-linter/controls/super_linter.rb b/test/inspec/super-linter/controls/super_linter.rb index b4586dfa..b1c1a6b7 100644 --- a/test/inspec/super-linter/controls/super_linter.rb +++ b/test/inspec/super-linter/controls/super_linter.rb @@ -98,6 +98,7 @@ control "super-linter-installed-commands" do { linter_name: "clang-format"}, { linter_name: "cfn-lint"}, { linter_name: "checkstyle", version_command: "java -jar /usr/bin/checkstyle --version"}, + { linter_name: "google-java-format", version_command: "java -jar /usr/bin/google-java-format --version"}, { linter_name: "chktex"}, { linter_name: "clippy", linter_command: "clippy", version_command: "cargo-clippy --version"}, { linter_name: "clj-kondo"},