From 26ddd413459dbf8c30a8e557a4fd3ed86d788ecc Mon Sep 17 00:00:00 2001 From: Ryan Harg Date: Fri, 1 Mar 2024 13:57:10 +0100 Subject: [PATCH] Minor improvements --- app/build.gradle.kts | 2 +- .../main/kotlin/de/rpr/githubreleases/App.kt | 13 +++++------- .../de/rpr/githubreleases/Extensions.kt | 9 +++++++++ .../de/rpr/githubreleases/feed/FeedService.kt | 20 +++++++++---------- .../de/rpr/githubreleases/model/GithubRepo.kt | 19 ++++++++++-------- 5 files changed, 35 insertions(+), 28 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 94ac9b2..e7782ed 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -15,7 +15,7 @@ plugins { // Apply the application plugin to add support for building a CLI application in Java. application - id("com.google.cloud.tools.jib") version "3.4.0" + id("com.google.cloud.tools.jib") version "3.4.1" } repositories { diff --git a/app/src/main/kotlin/de/rpr/githubreleases/App.kt b/app/src/main/kotlin/de/rpr/githubreleases/App.kt index 9f3d205..5a79501 100644 --- a/app/src/main/kotlin/de/rpr/githubreleases/App.kt +++ b/app/src/main/kotlin/de/rpr/githubreleases/App.kt @@ -9,11 +9,8 @@ import java.util.concurrent.Executors import java.util.concurrent.TimeUnit fun main() { - App::class.java.classLoader.getResource("logo.txt")?.readText()!!.split("\n") - .forEach { - log(it) - } - log("github-release-bot starting up") + logo("github-release-bot") + val config = Config() val mastodonClientFactory = MastodonClientFactory() val publishers = Publishers(config, mastodonClientFactory) @@ -26,9 +23,8 @@ class App( private val config: Config, private val releaseRepo: ReleaseRepository, private val httpClient: OkHttpClient, - private val publishers: Publishers + private val publishers: Publishers, ) { - init { log(config.toString()) } @@ -42,14 +38,15 @@ class App( private fun execute() { config.accounts.forEach { account -> log("Processing releases feed for ${account.repo.name}...") + val existingReleases = releaseRepo.getExistingReleases(account.repo) val feedService = FeedService(account.repo, httpClient) val newReleases = feedService.getNewReleases(existingReleases) val publisher = publishers.forName(account.name) val publishedReleases = publisher.sendReleases(newReleases) releaseRepo.save(publishedReleases) + log("Finished feed processing...") } } } - diff --git a/app/src/main/kotlin/de/rpr/githubreleases/Extensions.kt b/app/src/main/kotlin/de/rpr/githubreleases/Extensions.kt index a16f3cc..f00346f 100644 --- a/app/src/main/kotlin/de/rpr/githubreleases/Extensions.kt +++ b/app/src/main/kotlin/de/rpr/githubreleases/Extensions.kt @@ -4,4 +4,13 @@ import java.io.File import java.nio.file.Path fun String.toFile(): File = File(this) + fun String.toPath(): Path = this.toFile().toPath() + +fun logo(applicationName: String) { + object {}::class.java.classLoader.getResource("logo.txt")?.readText()!!.split("\n") + .forEach { + log(it) + } + log("$applicationName starting up") +} diff --git a/app/src/main/kotlin/de/rpr/githubreleases/feed/FeedService.kt b/app/src/main/kotlin/de/rpr/githubreleases/feed/FeedService.kt index 65fd439..26c29de 100644 --- a/app/src/main/kotlin/de/rpr/githubreleases/feed/FeedService.kt +++ b/app/src/main/kotlin/de/rpr/githubreleases/feed/FeedService.kt @@ -1,25 +1,24 @@ package de.rpr.githubreleases.feed import com.ouattararomuald.syndication.Syndication -import de.rpr.githubreleases.* +import de.rpr.githubreleases.log import de.rpr.githubreleases.model.GithubRepo import de.rpr.githubreleases.model.Release import de.rpr.githubreleases.model.Releases import de.rpr.githubreleases.model.asCollection -import de.rpr.terminenbg.LocalDateTimeAdapter import okhttp3.OkHttpClient -import java.time.LocalDateTime import java.time.OffsetDateTime import java.time.ZoneId class FeedService( private val githubRepo: GithubRepo, - httpClient: OkHttpClient + httpClient: OkHttpClient, ) { - private val syndication: Syndication = Syndication( - url = "${githubRepo.url}/releases.atom", - httpClient = httpClient - ) + private val syndication: Syndication = + Syndication( + url = "${githubRepo.url}/releases.atom", + httpClient = httpClient, + ) fun getNewReleases(existingReleases: Releases): Releases { log("Consuming releases feed for ${githubRepo.repoPath}") @@ -28,17 +27,16 @@ class FeedService( return feedReader.readAtom() .items ?.map { - val created = OffsetDateTime.parse(it.lastUpdatedTime) val link = it.links!!.first().href!! Release( id = it.title, link = link, created = created.atZoneSameInstant(ZoneId.of("UTC")).toLocalDateTime(), - githubRepo = githubRepo + githubRepo = githubRepo, ) } ?.filter { !existingReleases.contains(it) } ?.asCollection() ?: Releases() } -} \ No newline at end of file +} diff --git a/app/src/main/kotlin/de/rpr/githubreleases/model/GithubRepo.kt b/app/src/main/kotlin/de/rpr/githubreleases/model/GithubRepo.kt index 9d28da0..507fe8c 100644 --- a/app/src/main/kotlin/de/rpr/githubreleases/model/GithubRepo.kt +++ b/app/src/main/kotlin/de/rpr/githubreleases/model/GithubRepo.kt @@ -1,10 +1,12 @@ package de.rpr.githubreleases.model -data class GithubRepo(@Transient private val repositoryPath: String) { - +data class GithubRepo( + @Transient private val repositoryPath: String, +) { @Transient private val urlPrefix = "https://github.com/" @Transient val repoPath: String + @Transient val name: String init { @@ -18,9 +20,10 @@ data class GithubRepo(@Transient private val repositoryPath: String) { val url = urlPrefix + repoPath - private fun repoPath(repositoryPath: String) = if (repositoryPath.startsWith(urlPrefix)) { - repositoryPath.substring(19) - } else { - repositoryPath - } -} \ No newline at end of file + private fun repoPath(repositoryPath: String) = + if (repositoryPath.startsWith(urlPrefix)) { + repositoryPath.substring(urlPrefix.length) + } else { + repositoryPath + } +}