Skip to content

Commit

Permalink
Merge pull request #20 from gravatar/hamorillo/13-detekt
Browse files Browse the repository at this point in the history
Adding Detekt for static code analysis
  • Loading branch information
hamorillo authored Jan 17, 2024
2 parents 108eca9 + 96b1c87 commit f4cc942
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 3 deletions.
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 {
// 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
}

0 comments on commit f4cc942

Please sign in to comment.