feat(app/updater): debug CI builds

This commit is contained in:
rhunk 2024-09-08 21:25:42 +02:00
parent 6a22224d58
commit 4fe386e227
2 changed files with 39 additions and 10 deletions

View File

@ -27,12 +27,37 @@ object Updater {
val latestVersion = latestRelease.getAsJsonPrimitive("tag_name").asString
if (latestVersion.removePrefix("v") == BuildConfig.VERSION_NAME) return@runCatching null
LatestRelease(latestVersion, endpoint.url.toString().replace("api.", "").replace("repos/", ""))
LatestRelease(
versionName = latestVersion,
releaseUrl = endpoint.url.toString().replace("api.", "").replace("repos/", "")
)
}.onFailure {
AbstractLogger.directError("Failed to fetch latest release", it)
}.getOrNull()
private fun fetchLatestDebugCI() = runCatching {
val actionRuns = OkHttpClient().newCall(Request.Builder().url("https://api.github.com/repos/rhunk/SnapEnhance/actions/runs?event=workflow_dispatch").build()).execute().use {
if (!it.isSuccessful) throw Throwable("Failed to fetch CI runs: ${it.code}")
JsonParser.parseString(it.body.string()).asJsonObject
}
val debugRuns = actionRuns.getAsJsonArray("workflow_runs")?.mapNotNull { it.asJsonObject }?.filter { run ->
run.getAsJsonPrimitive("conclusion")?.asString == "success" && run.getAsJsonPrimitive("path")?.asString == ".github/workflows/debug.yml"
} ?: throw Throwable("No debug CI runs found")
val latestRun = debugRuns.firstOrNull() ?: throw Throwable("No debug CI runs found")
val headSha = latestRun.getAsJsonPrimitive("head_sha")?.asString ?: throw Throwable("No head sha found")
if (headSha == BuildConfig.GIT_HASH) return@runCatching null
LatestRelease(
versionName = headSha.substring(0, headSha.length.coerceAtMost(7)) + "-debug",
releaseUrl = latestRun.getAsJsonPrimitive("html_url")?.asString?.replace("github.com", "nightly.link") ?: return@runCatching null
)
}.onFailure {
AbstractLogger.directError("Failed to fetch latest debug CI", it)
}.getOrNull()
val latestRelease by lazy {
fetchLatestRelease()
if (BuildConfig.DEBUG) fetchLatestDebugCI() else fetchLatestRelease()
}
}

View File

@ -209,9 +209,7 @@ class HomeRootSection : Routes.Route() {
context.database.getQuickTiles()
}
val latestUpdate by rememberAsyncMutableState(defaultValue = null) {
if (!BuildConfig.DEBUG) Updater.latestRelease else null
}
val latestUpdate by rememberAsyncMutableState(defaultValue = null) { Updater.latestRelease }
if (latestUpdate != null) {
Spacer(modifier = Modifier.height(10.dp))
@ -228,15 +226,21 @@ class HomeRootSection : Routes.Route() {
fontWeight = FontWeight.Bold,
)
Text(
fontSize = 12.sp, text = translation.format(
fontSize = 12.sp,
text = translation.format(
"update_content",
"version" to (latestUpdate?.versionName ?: "unknown")
), lineHeight = 20.sp
),
lineHeight = 20.sp,
overflow = TextOverflow.Ellipsis,
)
}
Button(onClick = {
latestUpdate?.releaseUrl?.let { openExternalLink(it) }
}, modifier = Modifier.height(40.dp)) {
Button(
modifier = Modifier.height(40.dp),
onClick = {
latestUpdate?.releaseUrl?.let { openExternalLink(it) }
}
) {
Text(text = translation["update_button"])
}
}