Minor improvements

This commit is contained in:
Ryan Harg 2024-03-01 13:57:10 +01:00
parent 5e446b84a0
commit 26ddd41345
5 changed files with 35 additions and 28 deletions

View file

@ -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 {

View file

@ -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...")
}
}
}

View file

@ -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")
}

View file

@ -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,14 +27,13 @@ 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) }

View file

@ -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
}
private fun repoPath(repositoryPath: String) =
if (repositoryPath.startsWith(urlPrefix)) {
repositoryPath.substring(urlPrefix.length)
} else {
repositoryPath
}
}