|
| 1 | +## Code Quality |
| 2 | +Autoscaler uses [Pre-commit](https://pre-commit.com/) for keeping our code base clean. It is a package manager fo git hooks. |
| 3 | +Upon a git commit, it checks .go and .java files for potential code problems. |
| 4 | + |
| 5 | +Two static code analyzers are used as git-hooks which are defined in the .pre-commit-config file. |
| 6 | + |
| 7 | +- Java Formatter : Checkstyle and Google formatter are used along-with [Google Styles](https://github.com/google/google-java-format) |
| 8 | +- Golangci: [Golangci-lint](https://github.com/golangci/golangci-lint) is used |
| 9 | + |
| 10 | +We recommend to install pre-commit on develop machines. This will help to catch issue before code review. |
| 11 | + |
| 12 | +### IntelliJ java code style import |
| 13 | +To get intellij to play nice with the google code style checker we need to import the code style for intellij |
| 14 | +To do this follow the IDE setup on the [google format home page](https://github.com/google/google-java-format#intellij-android-studio-and-other-jetbrains-ides) |
| 15 | + |
| 16 | +### Install Pre-Commit |
| 17 | + |
| 18 | +Install [Pre-commit](https://pre-commit.com/) on developer laptop |
| 19 | +- using pip |
| 20 | +```bash |
| 21 | +pip install pre-commit |
| 22 | +pre-commit install |
| 23 | +``` |
| 24 | +- using curl |
| 25 | +```bash |
| 26 | +curl https://pre-commit.com/install-local.py | python - |
| 27 | +pre-commit install |
| 28 | +``` |
| 29 | + |
| 30 | +- using homebrew |
| 31 | +```bash |
| 32 | +brew install pre-commit |
| 33 | +pre-commit install |
| 34 | +``` |
| 35 | +- using conda |
| 36 | +```bash |
| 37 | +conda install -c conda-forge pre-commit |
| 38 | +pre-commit install |
| 39 | +``` |
| 40 | + |
| 41 | +## Usage |
| 42 | +``` |
| 43 | +git add <files> |
| 44 | +git commit -m <message> |
| 45 | +``` |
| 46 | + |
| 47 | +## Real World Example |
| 48 | + |
| 49 | +### Commit changes from Local |
| 50 | + |
| 51 | +```bash |
| 52 | +$ git commit -m "aas82 fix GHA linter: Golang and Java - local" |
| 53 | +[WARNING] Unstaged files detected. |
| 54 | +[INFO] Stashing unstaged files to /Users/<USER>/.cache/pre-commit/patch1629118042-59848. |
| 55 | +java-formatter...........................................................Failed |
| 56 | +- hook id: java-formatter |
| 57 | +- exit code: 1 |
| 58 | + |
| 59 | +Running Checkstyle using <APP_AUTOSCALER_REPO>/.cache/$CHECKSTYLE_JAR_NAME... |
| 60 | +[WARN] <APP_AUTOSCALER_REPO>/scheduler/src/main/java/org/cloudfoundry/autoscaler/scheduler/util/DataValidationHelper.java:19:19: Abbreviation in name 'LINTTT_CHECK' must contain no more than '1' consecutive capital letters. [AbbreviationAsWordInName] |
| 61 | +[WARN] <APP_AUTOSCALER_REPO>scheduler/src/main/java/org/cloudfoundry/autoscaler/scheduler/util/DataValidationHelper.java:19:19: Member name 'LINTTT_CHECK' must match pattern '^[a-z][a-z0-9][a-zA-Z0-9]*$'. [MemberName] |
| 62 | + |
| 63 | +golangci-lint............................................................Failed |
| 64 | +- hook id: golangci-lint |
| 65 | +- exit code: 2 |
| 66 | + |
| 67 | +<APP_AUTOSCALER_REPO>/src/autoscaler |
| 68 | +golangci-lint run |
| 69 | +api/brokerserver/broker_handler.go:24: File is not `gofmt`-ed with `-s` (gofmt) |
| 70 | + logger lager.Logger |
| 71 | +make: *** [lint] Error 1 |
| 72 | + |
| 73 | +[INFO] Restored changes from /Users/<USER>/.cache/pre-commit/patch1629118042-59848. |
| 74 | + |
| 75 | +``` |
| 76 | +In the above output, Some issues has been reported: |
| 77 | + |
| 78 | +**Go File:** incorrect formatting in api/brokerserver/broker_handler.go:24 (reported by Golangci-lint) |
| 79 | + |
| 80 | +**Java:** Incorrect variable name reported by Java-formatter |
| 81 | + |
| 82 | +To fix them: |
| 83 | + |
| 84 | +For Go: |
| 85 | +``` |
| 86 | +gofmt -s -w api/brokerserver/broker_handler.go |
| 87 | +
|
| 88 | +``` |
| 89 | +For Java, just correct the variable name |
| 90 | + |
| 91 | +Upon committing again, Golangci-lint passed but java-formatter has reported some formatting problems: |
| 92 | +```bash |
| 93 | +$ git commit -m "aas82 fix GHA linter: Golang and Java - local" |
| 94 | +[WARNING] Unstaged files detected. |
| 95 | +[INFO] Stashing unstaged files to /Users/<USER>/.cache/pre-commit/patch1629118377-61629. |
| 96 | +java-formatter...........................................................Failed |
| 97 | +- hook id: java-formatter |
| 98 | +- exit code: 2 |
| 99 | + |
| 100 | +Running Checkstyle using <APP_AUTOSCALER_REPO>/.cache/CHECKSTYLE_JAR_NAME... |
| 101 | +============================================================ |
| 102 | +Google Formatting using <APP_AUTOSCALER_REPO>/.cache/google-java-format-1.11.0-all-deps.jar... |
| 103 | +Incorrect formatting found: |
| 104 | +Please correct the formatting of the files(s) using one of the following options: |
| 105 | + java --add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED -jar <APP_AUTOSCALER_REPO>/.cache/google-java-format-1.11.0-all-deps.jar -replace --skip-javadoc-formatting scheduler/src/main/java/org/cloudfoundry/autoscaler/scheduler/util/DataValidationHelper.java |
| 106 | + |
| 107 | +golangci-lint............................................................Passed |
| 108 | +[INFO] Restored changes from /Users/<USER>/.cache/pre-commit/patch1629118377-61629. |
| 109 | + |
| 110 | +``` |
| 111 | + |
| 112 | +To fix them, just execute the command as suggested by the java-formatter. It will auto-format the java sources. |
| 113 | +```bash |
| 114 | +java --add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED \ |
| 115 | + --add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED \ |
| 116 | + --add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED \ |
| 117 | + --add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED \ |
| 118 | + --add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED |
| 119 | + -jar <APP_AUTOSCALER_REPO>/.cache/google-java-format-1.11.0-all-deps.jar \ |
| 120 | + -replace --skip-javadoc-formatting scheduler/src/main/java/org/cloudfoundry/autoscaler/scheduler/util/DataValidationHelper.java |
| 121 | + |
| 122 | +``` |
| 123 | + |
| 124 | +## Successful Commit |
| 125 | + |
| 126 | +```bash |
| 127 | +$ git commit -m "aas82 fix GHA linter: Golang and Java - local" |
| 128 | +[WARNING] Unstaged files detected. |
| 129 | +[INFO] Stashing unstaged files to /Users/<USER>/.cache/pre-commit/patch1629118875-64154. |
| 130 | +java-formatter...........................................................Passed |
| 131 | +golangci-lint............................................................Passed |
| 132 | +[INFO] Restored changes from /Users/<USER>/.cache/pre-commit/patch1629118875-64154. |
| 133 | +[aas82-verify-linters cff357fa] aas82 fix GHA linter: Golang and Java - local |
| 134 | + 3 files changed, 8 insertions(+), 4 deletions(-) |
| 135 | + |
| 136 | +``` |
| 137 | + |
| 138 | +### Skip Pre-Commit Git Hook Locally |
| 139 | +`git commit --no-verify -m "<COMMI_MESSAGE"` |
| 140 | + |
| 141 | + |
| 142 | +**Note:** The same static code analyzers are used via GitHub Actions. |
0 commit comments