Skip to content

Commit

Permalink
Merge pull request #238 from hossain-khan/refactor-di
Browse files Browse the repository at this point in the history
[REFACTOR] Moved `StatsGeneratorApplication` into constructor injection
  • Loading branch information
hossain-khan authored Dec 31, 2023
2 parents 6fc3216 + 42dfa85 commit 2814c16
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 15 deletions.
5 changes: 3 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,12 @@ dependencies {
// =======================
//
testImplementation(kotlin("test"))
testImplementation("com.google.truth:truth:1.1.3")
testImplementation("com.google.truth:truth:1.2.0")
testImplementation("com.squareup.okhttp3:mockwebserver:4.10.0")
testImplementation("io.mockk:mockk:1.13.8")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit5:1.9.20")
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.1")
testImplementation("org.junit.jupiter:junit-jupiter:5.9.2")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit5:1.9.20")
}

tasks.test {
Expand Down
6 changes: 4 additions & 2 deletions src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import dev.hossain.githubstats.BuildConfig
import dev.hossain.githubstats.di.appModule
import dev.hossain.githubstats.logging.Log
import kotlinx.coroutines.runBlocking
import org.koin.core.component.inject
import org.koin.core.context.GlobalContext.startKoin
import org.koin.java.KoinJavaComponent.inject

/**
* Runs PR stats on specified repository for each GitHub users defined in the config.
Expand All @@ -19,9 +21,9 @@ fun main() {
modules(appModule) // Initializes dependency injection for the app
}

runBlocking {
val statsGeneratorApplication = StatsGeneratorApplication()
val statsGeneratorApplication: StatsGeneratorApplication by inject(StatsGeneratorApplication::class.java)

runBlocking {
// 💡 Generates stats for each user as PR author - for all PRs created by the user
statsGeneratorApplication.generateAuthorStats()

Expand Down
19 changes: 10 additions & 9 deletions src/main/kotlin/StatsGeneratorApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,30 @@ import dev.hossain.githubstats.logging.Log
import dev.hossain.githubstats.util.AppConfig
import dev.hossain.i18n.Resources
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import kotlin.system.measureTimeMillis
import kotlin.time.Duration.Companion.milliseconds

/**
* App for generating PR stats for users that created PRs and reviewed PRs.
* See [generateAuthorStats] and [generateReviewerStats] for details.
*/
class StatsGeneratorApplication : KoinComponent {
private val prReviewerStatsService: PrReviewerStatsService by inject()
private val prAuthorStatsService: PrAuthorStatsService by inject()
private val resources: Resources by inject()

class StatsGeneratorApplication(
private val prReviewerStatsService: PrReviewerStatsService,
private val prAuthorStatsService: PrAuthorStatsService,
/**
* Localized resources for printing messages.
*/
private val resources: Resources,
/**
* Config loader that provides configs from `[LOCAL_PROPERTIES_FILE]`
*/
private val appConfig: AppConfig by inject()

private val appConfig: AppConfig,
/**
* Get all the available stats formatters - such as ASCII table, CSV writer and so on.
* @see StatsFormatter
*/
private val formatters: List<StatsFormatter> = getKoin().getAll()
private val formatters: List<StatsFormatter>
) : KoinComponent {

/**
* Generates stats for user as PR author
Expand Down
25 changes: 23 additions & 2 deletions src/main/kotlin/dev/hossain/githubstats/di/Koin.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.hossain.githubstats.di

import StatsGeneratorApplication
import dev.hossain.githubstats.AppConstants
import dev.hossain.githubstats.PrAuthorStatsService
import dev.hossain.githubstats.PrReviewerStatsService
Expand Down Expand Up @@ -42,8 +43,18 @@ val appModule = module {
userTimeZone = get()
)
}
factory { IssueSearchPagerService(githubApiService = get(), errorProcessor = get()) }
factory { TimelineEventsPagerService(githubApiService = get(), errorProcessor = get()) }
factory {
IssueSearchPagerService(
githubApiService = get(),
errorProcessor = get()
)
}
factory {
TimelineEventsPagerService(
githubApiService = get(),
errorProcessor = get()
)
}
factory {
PrReviewerStatsService(
pullRequestStatsRepo = get(),
Expand All @@ -63,6 +74,16 @@ val appModule = module {
single { ErrorProcessor() }
single { UserTimeZone() }

single {
StatsGeneratorApplication(
prReviewerStatsService = get(),
prAuthorStatsService = get(),
resources = get(),
appConfig = get(),
formatters = getAll()
)
}

// Localization
single { ResourceBundle.getBundle("strings", Locale.getDefault()) }
factory { ResourcesImpl(resourceBundle = get()) } bind Resources::class
Expand Down

0 comments on commit 2814c16

Please sign in to comment.