Skip to content

Commit

Permalink
feat(app/updater): debug CI builds
Browse files Browse the repository at this point in the history
  • Loading branch information
rhunk committed Sep 8, 2024
1 parent 6a22224 commit 4fe386e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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"])
}
}
Expand Down

0 comments on commit 4fe386e

Please sign in to comment.