Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Detekt for static code analysis #20

Merged
merged 3 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/android.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,7 @@ jobs:
run: chmod +x gradlew
- name: Check code style
run: ./gradlew ktlintcheck
- name: Static code analysis
run: ./gradlew detekt
- name: Build with Gradle
run: ./gradlew build
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,15 @@ You can also try to let Ktlint fix the code style issues. Just use:
./gradlew ktlintFormat
./gradlew :gravatar:ktlintFormat
./gradlew :app:ktlintFormat
```

## Code static analysis

We use [Detekt](https://github.com/detekt/detekt) to perform static code analysis. You can run
Detekt via a gradle command:

```
./gradlew detekt
./gradlew :gravatar:detekt
./gradlew :app:detekt
```
10 changes: 10 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ plugins {

// Ktlint
id("org.jlleitschuh.gradle.ktlint")

// Detekt
id("io.gitlab.arturbosch.detekt")
}

android {
Expand Down Expand Up @@ -33,6 +36,13 @@ android {
kotlinOptions {
jvmTarget = "1.8"
}
detekt {
config.setFrom("${project.rootDir}/config/detekt/detekt.yml")
source.setFrom("src")
autoCorrect = false
buildUponDefaultConfig = true
parallel = false
}
}

dependencies {
Expand Down
5 changes: 4 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ plugins {

// Ktlint
id("org.jlleitschuh.gradle.ktlint") version "12.1.0" apply false
}

// Detekt
id("io.gitlab.arturbosch.detekt") version "1.23.4" apply false
}
42 changes: 42 additions & 0 deletions config/detekt/detekt.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Default Config: https://github.com/detekt/detekt/blob/main/detekt-core/src/main/resources/default-detekt-config.yml
# Formatting Config: https://github.com/detekt/detekt/blob/main/detekt-formatting/src/main/resources/config/config.yml
# Compose Config: https://detekt.dev/docs/introduction/compose/

config:
warningsAsErrors: true

complexity:
LongParameterList:
ignoreDefaultParameters: true
ignoreAnnotated: ['Inject', 'Composable']
LongMethod:
ignoreAnnotated: ['Composable']
TooManyFunctions:
active: false
CyclomaticComplexMethod:
ignoreSimpleWhenEntries: true

coroutines:
GlobalCoroutineUsage:
active: true

naming:
FunctionNaming:
ignoreAnnotated: ['Composable']

style:
DataClassShouldBeImmutable:
active: true
MagicNumber:
ignoreEnums: true
ignoreAnnotated: ['Composable']
ignorePropertyDeclaration: true
SpacingBetweenPackageAndImports:
active: true
UnusedImports:
active: true
UnusedPrivateMember:
ignoreAnnotated: ['Preview']
WildcardImport:
active: true
excludeImports: []
11 changes: 11 additions & 0 deletions gravatar/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ plugins {

// Ktlint
id("org.jlleitschuh.gradle.ktlint")

// Detekt
id("io.gitlab.arturbosch.detekt")
}

android {
Expand Down Expand Up @@ -32,6 +35,14 @@ android {
kotlinOptions {
jvmTarget = "1.8"
}

detekt {
config.setFrom("${project.rootDir}/config/detekt/detekt.yml")
source.setFrom("src")
autoCorrect = false
buildUponDefaultConfig = true
parallel = true
}
}

dependencies {
Expand Down
4 changes: 2 additions & 2 deletions gravatar/src/main/java/com/gravatar/GravatarApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ class GravatarApi(okHttpClient: OkHttpClient? = null) {
Log.w(LOG_TAG, "Network call unsuccessful trying to upload Gravatar: $response.body")
val error: ErrorType =
when (response.code()) {
408 -> ErrorType.TIMEOUT
in 500..599 -> ErrorType.SERVER
HttpResponseCode.HTTP_CLIENT_TIMEOUT -> ErrorType.TIMEOUT
in HttpResponseCode.SERVER_ERRORS -> ErrorType.SERVER
else -> ErrorType.UNKNOWN
}
gravatarUploadListener.onError(error)
Expand Down
11 changes: 11 additions & 0 deletions gravatar/src/main/java/com/gravatar/HttpResponseCode.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.gravatar

object HttpResponseCode {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

// 4xx codes
const val HTTP_CLIENT_TIMEOUT = 408

// 5xx codes
private const val HTTP_INTERNAL_ERROR = 500
private const val NETWORK_CONNECT_TIMEOUT_ERROR = 599
val SERVER_ERRORS = HTTP_INTERNAL_ERROR..NETWORK_CONNECT_TIMEOUT_ERROR
}