Skip to content

Commit b852cb9

Browse files
authored
Add Detekt static analysis to CI pipeline (#15)
* Add detekt script and workflow * Fix the detekted issues
1 parent 98daa6c commit b852cb9

16 files changed

Lines changed: 274 additions & 116 deletions

File tree

.github/workflows/_detekt.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: Detekt Static Analysis
2+
3+
on:
4+
workflow_call:
5+
6+
permissions:
7+
contents: read
8+
9+
jobs:
10+
detekt:
11+
name: Run Detekt Analysis
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- name: Checkout repository
16+
uses: actions/checkout@v4
17+
18+
- name: Set up JDK 17
19+
uses: actions/setup-java@v4
20+
with:
21+
distribution: temurin
22+
java-version: 17
23+
24+
- name: Cache Gradle
25+
uses: actions/cache@v4
26+
with:
27+
path: |
28+
~/.gradle/caches
29+
~/.gradle/wrapper
30+
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
31+
restore-keys: |
32+
${{ runner.os }}-gradle-
33+
34+
- name: Grant execute permission for Gradle wrapper
35+
run: chmod +x gradlew
36+
37+
- name: Run Detekt
38+
run: ./gradlew detekt

.github/workflows/_docs.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ jobs:
4747
mkdir -p openmapview/build/dokka/html/licenses
4848
cp openmapview/build/reports/licenses/licenses.html openmapview/build/dokka/html/licenses/index.html
4949
50+
- name: Run Detekt Analysis
51+
run: ./gradlew detekt --no-daemon
52+
53+
- name: Copy Detekt Report to Docs
54+
run: |
55+
mkdir -p openmapview/build/dokka/html/detekt
56+
cp openmapview/build/reports/detekt/detekt.html openmapview/build/dokka/html/detekt/index.html
57+
5058
- name: Upload documentation artifact
5159
uses: actions/upload-artifact@v4
5260
with:

.github/workflows/ci.yml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,33 @@ jobs:
1818
name: Check Copyright Headers
1919
uses: ./.github/workflows/_copyright.yml
2020

21+
detekt:
22+
name: Static Analysis
23+
uses: ./.github/workflows/_detekt.yml
24+
2125
test:
2226
name: Unit Tests
23-
needs: [format, copyright]
27+
needs: [format, copyright, detekt]
2428
uses: ./.github/workflows/_test.yml
2529

2630
coverage:
2731
name: Test Coverage
28-
needs: [format, copyright]
32+
needs: [format, copyright, detekt]
2933
uses: ./.github/workflows/_coverage.yml
3034

3135
docs:
3236
name: Documentation
33-
needs: [format, copyright]
37+
needs: [format, copyright, detekt]
3438
uses: ./.github/workflows/_docs.yml
3539

3640
build-library:
3741
name: Build Library
38-
needs: [format, copyright, test, coverage, docs]
42+
needs: [format, copyright, detekt, test, coverage, docs]
3943
uses: ./.github/workflows/_build-library.yml
4044

4145
build-examples:
4246
name: Build Examples
43-
needs: [format, copyright, test, coverage, docs]
47+
needs: [format, copyright, detekt, test, coverage, docs]
4448
uses: ./.github/workflows/_build-examples.yml
4549

4650
instrumented-test:

.github/workflows/docs-deploy.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ on:
66
paths:
77
- 'openmapview/src/**/*.kt'
88
- 'README.md'
9+
- 'config/detekt/detekt.yml'
910
- '.github/workflows/_docs.yml'
1011
- '.github/workflows/docs-deploy.yml'
1112

.github/workflows/release.yml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,29 @@ jobs:
1313
name: Check Formatting
1414
uses: ./.github/workflows/_format.yml
1515

16+
detekt:
17+
name: Static Analysis
18+
uses: ./.github/workflows/_detekt.yml
19+
1620
test:
1721
name: Unit Tests
18-
needs: format
22+
needs: [format, detekt]
1923
uses: ./.github/workflows/_test.yml
2024

2125
build-library:
2226
name: Build Library
23-
needs: [format, test]
27+
needs: [format, detekt, test]
2428
uses: ./.github/workflows/_build-library.yml
2529

2630
build-examples:
2731
name: Build Examples
28-
needs: [format, test]
32+
needs: [format, detekt, test]
2933
uses: ./.github/workflows/_build-examples.yml
3034

3135
publish:
3236
name: Publish to Maven Central
3337
runs-on: ubuntu-latest
34-
needs: [format, test, build-library, build-examples]
38+
needs: [format, detekt, test, build-library, build-examples]
3539

3640
steps:
3741
- name: Checkout repository

build.gradle.kts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ plugins {
2424
alias(libs.plugins.kotlin.compose) apply false
2525
alias(libs.plugins.spotless) apply false
2626
alias(libs.plugins.dokka) apply false
27+
alias(libs.plugins.detekt) apply false
2728
alias(libs.plugins.nmcp)
2829
}
2930

@@ -37,3 +38,24 @@ nmcpAggregation {
3738
publishAllProjectsProbablyBreakingProjectIsolation()
3839
}
3940

41+
subprojects {
42+
afterEvaluate {
43+
if (plugins.hasPlugin("org.jetbrains.kotlin.android")) {
44+
apply(plugin = "io.gitlab.arturbosch.detekt")
45+
46+
extensions.configure<io.gitlab.arturbosch.detekt.extensions.DetektExtension> {
47+
buildUponDefaultConfig = true
48+
config.setFrom(files("$rootDir/config/detekt/detekt.yml"))
49+
}
50+
51+
tasks.withType<io.gitlab.arturbosch.detekt.Detekt>().configureEach {
52+
reports {
53+
html.required.set(true)
54+
xml.required.set(true)
55+
sarif.required.set(true)
56+
}
57+
}
58+
}
59+
}
60+
}
61+

config/detekt/detekt.yml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
build:
2+
maxIssues: 0
3+
4+
complexity:
5+
active: true
6+
LongMethod:
7+
threshold: 200
8+
LongParameterList:
9+
functionThreshold: 10
10+
constructorThreshold: 12
11+
CyclomaticComplexMethod:
12+
threshold: 50
13+
TooManyFunctions:
14+
thresholdInClasses: 100
15+
thresholdInObjects: 20
16+
LargeClass:
17+
threshold: 3000
18+
ComplexCondition:
19+
threshold: 5
20+
NestedBlockDepth:
21+
threshold: 8
22+
23+
style:
24+
active: true
25+
MagicNumber:
26+
active: false
27+
MaxLineLength:
28+
maxLineLength: 140
29+
ReturnCount:
30+
max: 15
31+
FunctionOnlyReturningConstant:
32+
active: false
33+
LoopWithTooManyJumpStatements:
34+
active: false
35+
UseRequire:
36+
active: true
37+
UnusedPrivateMember:
38+
active: false
39+
40+
naming:
41+
active: true
42+
FunctionNaming:
43+
ignoreAnnotated:
44+
- Composable
45+
46+
empty-blocks:
47+
active: true
48+
49+
comments:
50+
active: false
51+
52+
performance:
53+
active: true
54+
55+
coroutines:
56+
active: true
57+
58+
exceptions:
59+
active: true
60+
SwallowedException:
61+
active: false
62+
TooGenericExceptionCaught:
63+
active: false

docs/CONTRIBUTING.md

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,28 @@ Running `./gradlew spotlessApply` automatically adds this header to any files mi
5151

5252
The CI pipeline includes a copyright check that will fail if any `.kt` files are missing the required header.
5353

54-
### 3. Dependency Management
54+
### 3. Static Code Analysis
55+
56+
All code must pass Detekt static analysis checks. Detekt analyzes code for:
57+
- Code complexity and potential bugs
58+
- Naming conventions
59+
- Performance issues
60+
- Code smells
61+
62+
**Run static analysis:**
63+
64+
```bash
65+
./gradlew detekt
66+
```
67+
68+
The CI pipeline will fail if Detekt finds any issues. View the HTML report at:
69+
```
70+
openmapview/build/reports/detekt/detekt.html
71+
```
72+
73+
The Detekt report is also published at https://afarber.github.io/OpenMapView/detekt/
74+
75+
### 4. Dependency Management
5576

5677
Before adding new dependencies to OpenMapView, verify the following:
5778

@@ -98,8 +119,8 @@ From the repository root, run:
98119
This installs a pre-commit hook that automatically:
99120
1. Checks code formatting with `./scripts/check-format.sh`
100121
2. Checks copyright headers with `./scripts/check-copyright.sh`
101-
3. Blocks the commit if any issues are found
102-
4. Provides instructions to run `./gradlew spotlessApply` to fix issues
122+
3. Runs Detekt static analysis with `./scripts/check-detekt.sh`
123+
4. Blocks the commit if any issues are found
103124

104125
### What Happens on Commit
105126

@@ -112,6 +133,8 @@ Running pre-commit checks...
112133
Code formatting: OK
113134
2. Checking copyright headers...
114135
Copyright headers: OK
136+
3. Running Detekt static analysis...
137+
Static analysis: OK
115138
116139
All pre-commit checks passed!
117140
```
@@ -190,10 +213,11 @@ The CI pipeline runs automatically on all pull requests and includes:
190213

191214
1. **Format Check** - Verifies Spotless formatting
192215
2. **Copyright Check** - Verifies MIT license headers
193-
3. **Unit Tests** - Runs all JVM unit tests
194-
4. **Test Coverage** - Ensures minimum 20% code coverage
195-
5. **Build Library** - Builds the OpenMapView AAR
196-
6. **Build Examples** - Builds all example applications
216+
3. **Static Analysis** - Runs Detekt code analysis
217+
4. **Unit Tests** - Runs all JVM unit tests
218+
5. **Test Coverage** - Ensures minimum 20% code coverage
219+
6. **Build Library** - Builds the OpenMapView AAR
220+
7. **Build Examples** - Builds all example applications
197221

198222
All checks must pass before merging.
199223

@@ -206,6 +230,9 @@ All checks must pass before merging.
206230
# Check formatting
207231
./gradlew spotlessCheck
208232

233+
# Run static analysis
234+
./gradlew detekt
235+
209236
# Run unit tests
210237
./gradlew :openmapview:test
211238

0 commit comments

Comments
 (0)