Skip to content

Commit eacc8ea

Browse files
committed
Adds missing styleguide folder
1 parent cea5e32 commit eacc8ea

File tree

7 files changed

+642
-0
lines changed

7 files changed

+642
-0
lines changed

style-guide/README.md

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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.

style-guide/checkstyle.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
## Properties files for checkstyle jar
2+
checkstyle.config.path=style-guide
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0"?>
2+
3+
<!DOCTYPE suppressions PUBLIC
4+
"-//Checkstyle//DTD SuppressionFilter Configuration 1.2//EN"
5+
"https://checkstyle.org/dtds/suppressions_1_2.dtd">
6+
7+
<suppressions>
8+
<suppress checks="MissingJavadocMethod" />
9+
<suppress checks="MissingJavadocType" />
10+
<suppress checks="SummaryJavadoc" />
11+
<suppress checks="NonEmptyAtclauseDescription"/>
12+
<suppress checks="PackageName"/>
13+
<suppress checks="LineLength"/>
14+
<suppress checks="VariableDeclarationUsageDistance"/>
15+
<suppress checks="EmptyLineSeparator"/>
16+
</suppressions>

style-guide/golangci-lint-0.1.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env bash
2+
3+
###################################################################################################
4+
# This pre-commit hook displays Golangci issues
5+
###################################################################################################
6+
set -e -o pipefail
7+
make lint
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env bash
2+
set -eu
3+
export PATH=${HOME}/go/bin:${PATH}
4+
###################################################################################################
5+
# This script downloads google formatter and displays formatting issues on Github actions
6+
###################################################################################################
7+
GOOGLE_JAR_VERSION=${GOOGLE_JAR_VERSION:-"1.22.0"}
8+
GOOGLE_JAR_NAME=${GOOGLE_JAR_NAME:-"google-java-format-${GOOGLE_JAR_VERSION}-all-deps.jar"}
9+
! [ -e "$GOOGLE_JAR_NAME" ] && \
10+
curl -fLJO "https://github.com/google/google-java-format/releases/download/v$GOOGLE_JAR_VERSION/$GOOGLE_JAR_NAME"
11+
# shellcheck disable=SC2046
12+
files_to_be_formatted=$(java \
13+
-jar "${GOOGLE_JAR_NAME}" --dry-run --skip-javadoc-formatting $(find src/autoscaler/scheduler -name '*.java' ! -name 'CloudFoundryConfigurationProcessorTest.java'))
14+
15+
if [ -n "$files_to_be_formatted" ]; then
16+
echo "Formatter Results..."
17+
echo "Files require reformatting:"
18+
echo "${files_to_be_formatted}"
19+
exit 1
20+
fi

0 commit comments

Comments
 (0)