feat(updates): install any kept Preview build, link builds to their PR and commit, flag stale test builds (#2624)

* feat(updates): install any kept Preview build, link builds to their PR and commit, flag stale test builds

* fix(updates): address review of the Updates tab history and test-build watch

* fix(updates): notify on switch to a followed PR, keep higher-version rebuilds installable

* fix(ci): let full reruns replace every artifact, note only expired PR builds

* fix(ci): leave the rebuild note only when the PR build is too old to rerun

* fix(ci): poll unfinished PR builds together and queue version-bump runs

* fix(ci): also rebuild PR builds that were running when the versionCode changed

* fix(ci): rebuild fork and just-finished PR builds, recheck the PR head before a rerun

* fix(ci): fail the rebuild sweep when finding candidates fails

* fix(ci): time the bump from the rebuild run's creation and scan unfinished builds of any age

* fix(ci): check the latest build of every open PR instead of scanning run history

* fix(ci): count PR builds that finish between the version push and the rebuild run

* fix(ci): use the push time from the event as the rebuild boundary
This commit is contained in:
Kurt Himebauch authored and GitHub committed 2026-11-14 16:20:29 -05:00
1 parent c847659789
commit 87c571f443
47 files changed
+1579 -87

No files matched your search

@@ -18,7 +18,7 @@ object AppUpdates {
private const val TAG = "AppUpdates"
private const val REPO = "Droid-Deck/DroidDeck"
private const val CI_REPO = "Droid-Deck/DroidDeck-CI"
private const val CATALOG_URL = "https://raw.githubusercontent.com/$CI_REPO/main/catalog.json"
private val CATALOG_URL = "https://raw.githubusercontent.com/$CI_REPO/${BuildConfig.UPDATE_CATALOG_BRANCH}/catalog.json"
private const val PREFS = "app_updates"
private const val KEY_CATALOG = "catalog"
private const val KEY_CHANNEL = "channel"
@@ -49,7 +49,21 @@ object AppUpdates {
val publishedAt: Long,
val url: String,
val apk: Apk?,
)
val author: String = "",
val committedAt: Long = 0L,
/** The PR a Preview build merged, 0 for a direct push. Test builds use [pr]. */
val mergedPr: Int = 0,
/** The PR's newest commit when the catalog was made; differs from [commit] while a rebuild is pending. */
val headCommit: String = "",
/** When the APK itself was uploaded; release-note edits leave it unchanged. */
val builtAt: Long = 0L,
) {
val linkedPr: Int get() = if (pr > 0) pr else mergedPr
val shortCommit: String get() = commit.take(7)
val newerCommitPushed: Boolean get() =
headCommit.length >= 7 && !headCommit.startsWith(commit, ignoreCase = true) &&
!commit.startsWith(headCommit, ignoreCase = true)
}
class PreviewChange(
val commit: String,
@@ -64,7 +78,11 @@ object AppUpdates {
val tests: List<Release>,
val checkedAt: Long,
val recentPreviews: List<PreviewChange> = emptyList(),
)
val previews: List<Release> = emptyList(),
) {
/** Every published Preview build, newest commit first; older catalogs only name the newest. */
val previewBuilds: List<Release> get() = previews.ifEmpty { listOfNotNull(preview) }
}
class Installed(
val commit: String,
@@ -88,6 +106,8 @@ object AppUpdates {
val kind: PreviewHistoryKind,
val buildsBehind: Int,
val changes: List<PreviewChange>,
val builds: List<Release> = emptyList(),
val installedIndex: Int = -1,
)
enum class Offer {
@@ -149,7 +169,13 @@ object AppUpdates {
compareVersions(r.version ?: "", me.version) > 0 -> Offer.UPDATE
else -> Offer.AHEAD
}
Channel.NIGHTLY -> if (me.pr != 0) Offer.SWITCH else Offer.UPDATE
Channel.NIGHTLY -> when {
me.pr != 0 -> Offer.SWITCH
previewIndex(catalog, me) > 0 -> Offer.UPDATE
previewIndex(catalog, me) < 0 && me.committedAt > 0 && r.committedAt > 0 &&
me.committedAt > r.committedAt -> Offer.AHEAD
else -> Offer.UPDATE
}
Channel.TEST -> if (me.pr == r.pr) Offer.UPDATE else Offer.SWITCH
}
}
@@ -162,12 +188,39 @@ object AppUpdates {
}
fun isRunning(r: Release, me: Installed = installed()): Boolean {
if (me.commit.length < 7 || r.commit.length < 7 || r.pr != me.pr) return false
if (me.commit.length < 7 || r.commit.length < 7 || r.pr != me.pr || r.versionCode > me.versionCode) return false
return me.commit.startsWith(r.commit, ignoreCase = true) ||
r.commit.startsWith(me.commit, ignoreCase = true)
}
/** Where the installed build sits in [Catalog.previewBuilds], or -1 when it is not one of them. */
fun previewIndex(catalog: Catalog, me: Installed = installed()): Int {
if (me.pr != 0) return -1
val builds = catalog.previewBuilds
val matching = builds.indices.filter { matchesInstalledCommit(builds[it].commit, me.commit) }
return matching.singleOrNull() ?: -1
}
/** How many Preview builds landed on main after this test build was made. */
fun previewsSince(catalog: Catalog, test: Release): Int =
catalog.previews.count {
(it.committedAt.takeIf { at -> at > 0 } ?: it.publishedAt) > (test.builtAt.takeIf { at -> at > 0 } ?: test.publishedAt)
}
fun installable(r: Release, me: Installed = installed()): Boolean =
me.updatable && installBlock(r, me) == null && !isRunning(r, me)
fun previewHistory(catalog: Catalog, me: Installed = installed()): PreviewHistory? {
if (catalog.previews.isNotEmpty()) {
val builds = catalog.previews
val index = previewIndex(catalog, me)
val kind = when {
index == 0 -> PreviewHistoryKind.CURRENT
index > 0 -> PreviewHistoryKind.BEHIND
else -> PreviewHistoryKind.RECENT
}
return PreviewHistory(kind, maxOf(index, 0), emptyList(), builds, index)
}
if (!me.ciBuild || catalog.recentPreviews.isEmpty()) return null
val stable = catalog.stable
if (
@@ -225,7 +278,8 @@ object AppUpdates {
if (stable != null && isRunning(stable, me)) return Guess(Follow(Channel.STABLE), true)
val publishedPreview = catalog != null && me.updatable && (
catalog.preview?.let { isRunning(it, me) } == true ||
catalog.recentPreviews.any { matchesInstalledCommit(it.commit, me.commit) }
catalog.recentPreviews.any { matchesInstalledCommit(it.commit, me.commit) } ||
catalog.previewBuilds.any { matchesInstalledCommit(it.commit, me.commit) }
)
if (stable?.version?.let { compareVersions(me.version, it) < 0 } == true) return Guess(Follow(Channel.STABLE), !publishedPreview)
return Guess(Follow(if (publishedPreview) Channel.NIGHTLY else Channel.STABLE), false)
@@ -237,7 +291,7 @@ object AppUpdates {
prefs(context).edit().putString(KEY_CHANNEL, value).apply()
}
private fun stored(context: Context): Follow? {
fun stored(context: Context): Follow? {
val v = prefs(context).getString(KEY_CHANNEL, null) ?: return null
return when {
v == "stable" -> Follow(Channel.STABLE)
@@ -281,6 +335,7 @@ object AppUpdates {
}
val tests = root.optJSONArray("tests") ?: JSONArray()
val recentPreviews = root.optJSONArray("recentPreviews") ?: JSONArray()
val previews = root.optJSONArray("previews") ?: JSONArray()
return Catalog(
root.optJSONObject("stable")?.let { readPublishedRelease(it, packageName) },
root.optJSONObject("preview")?.let { readPublishedRelease(it, packageName) },
@@ -294,6 +349,10 @@ object AppUpdates {
.sortedByDescending { it.publishedAt }
.distinctBy { it.commit.lowercase() }
.take(10),
(0 until previews.length())
.map { readPublishedRelease(previews.getJSONObject(it), packageName) }
.filter { it.pr == 0 }
.distinctBy { it.commit.lowercase() },
)
}
@@ -327,6 +386,11 @@ object AppUpdates {
o.optLong("publishedAt"),
o.optString("url"),
apk,
o.optString("author").takeIf { Regex("^[A-Za-z0-9-]{1,39}(\\[bot\\])?$").matches(it) }.orEmpty(),
o.optLong("committedAt"),
o.optInt("mergedPr").coerceAtLeast(0),
o.optString("headCommit").takeIf { Regex("^[0-9a-fA-F]{7,40}$").matches(it) }.orEmpty(),
o.optLong("builtAt"),
)
}
@@ -425,6 +489,11 @@ object AppUpdates {
.put("publishedAt", r.publishedAt)
.put("url", r.url)
.put("apk", r.apk?.let(::writeApk))
.put("author", r.author)
.put("committedAt", r.committedAt)
.put("mergedPr", r.mergedPr)
.put("headCommit", r.headCommit)
.put("builtAt", r.builtAt)
private fun readCachedRelease(o: JSONObject): Release = Release(
o.getString("tag"),
@@ -447,6 +516,11 @@ object AppUpdates {
it.getString("signerSha256"),
)
},
o.optString("author"),
o.optLong("committedAt"),
o.optInt("mergedPr"),
o.optString("headCommit"),
o.optLong("builtAt"),
)
internal fun writeCatalog(c: Catalog): JSONObject = JSONObject()
@@ -454,6 +528,7 @@ object AppUpdates {
.put("preview", c.preview?.let(::writeRelease))
.put("tests", JSONArray().apply { c.tests.forEach { put(writeRelease(it)) } })
.put("recentPreviews", JSONArray().apply { c.recentPreviews.forEach { put(writePreviewChange(it)) } })
.put("previews", JSONArray().apply { c.previews.forEach { put(writeRelease(it)) } })
.put("checkedAt", c.checkedAt)
private fun writePreviewChange(c: PreviewChange): JSONObject = JSONObject()
@@ -466,6 +541,7 @@ object AppUpdates {
val tests = o.optJSONArray("tests") ?: JSONArray()
val preview = o.optJSONObject("preview") ?: o.optJSONObject("nightly")
val recentPreviews = o.optJSONArray("recentPreviews") ?: JSONArray()
val previews = o.optJSONArray("previews") ?: JSONArray()
return Catalog(
o.optJSONObject("stable")?.let(::readCachedRelease),
preview?.let(::readCachedRelease),
@@ -476,6 +552,7 @@ object AppUpdates {
.sortedByDescending { it.publishedAt }
.distinctBy { it.commit.lowercase() }
.take(10),
(0 until previews.length()).map { readCachedRelease(previews.getJSONObject(it)) },
)
}
@@ -0,0 +1,102 @@
package com.droiddeck.launcher.update
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.job.JobInfo
import android.app.job.JobParameters
import android.app.job.JobScheduler
import android.app.job.JobService
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import com.droiddeck.launcher.FrontEndActivity
import com.droiddeck.launcher.R
import com.droiddeck.launcher.stores.download.StoreDownloadService
/** Checks the catalog in the background while a PR test build is followed, and says when that PR has a new build. */
class UpdateWatch : JobService() {
override fun onStartJob(params: JobParameters): Boolean {
Thread({
runCatching { check(applicationContext) }
jobFinished(params, false)
}, "update-watch").start()
return true
}
override fun onStopJob(params: JobParameters): Boolean = true
companion object {
private const val JOB_ID = 41_230
private const val NOTIFICATION_ID = 41_231
private const val CHANNEL_ID = "test-builds"
private const val PREFS = "app_updates"
private const val KEY_SEEN = "watch_seen"
private const val INTERVAL_MS = 60 * 60 * 1000L
fun sync(context: Context, follow: AppUpdates.Follow, current: AppUpdates.Release? = null) {
val scheduler = context.getSystemService(JobScheduler::class.java) ?: return
if (follow.channel != AppUpdates.Channel.TEST) {
scheduler.cancel(JOB_ID)
return
}
current?.let { seen(context, it) }
if (scheduler.getPendingJob(JOB_ID) != null) return
val job = JobInfo.Builder(JOB_ID, ComponentName(context, UpdateWatch::class.java))
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
.setPeriodic(INTERVAL_MS)
.build()
runCatching { scheduler.schedule(job) }
}
fun seen(context: Context, release: AppUpdates.Release) {
prefs(context).edit().putString(KEY_SEEN, key(release)).apply()
}
private fun key(r: AppUpdates.Release) = "${r.pr}:${r.commit}:${r.versionCode}:${r.apk?.sha256.orEmpty()}"
internal fun check(context: Context) {
val catalog = AppUpdates.refresh(context)
val follow = AppUpdates.follow(context, catalog)
if (follow.channel != AppUpdates.Channel.TEST) return
val release = AppUpdates.release(catalog, follow) ?: return
if (prefs(context).getString(KEY_SEEN, null) == key(release)) return
val me = AppUpdates.installed()
val offer = AppUpdates.offer(catalog, follow, me)
if (!me.updatable || AppUpdates.installBlock(release, me) != null ||
(offer != AppUpdates.Offer.UPDATE && offer != AppUpdates.Offer.SWITCH)
) return
if (post(context, release)) seen(context, release)
}
private fun post(context: Context, release: AppUpdates.Release): Boolean {
val manager = context.getSystemService(NotificationManager::class.java) ?: return false
manager.createNotificationChannel(
NotificationChannel(CHANNEL_ID, context.getString(R.string.upd_watch_channel), NotificationManager.IMPORTANCE_DEFAULT),
)
if (!manager.areNotificationsEnabled() ||
manager.getNotificationChannel(CHANNEL_ID)?.importance == NotificationManager.IMPORTANCE_NONE
) return false
val open = PendingIntent.getActivity(
context, NOTIFICATION_ID,
Intent(context, FrontEndActivity::class.java).putExtra(StoreDownloadService.EXTRA_NAV, "updates")
.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_REORDER_TO_FRONT),
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT,
)
val text = context.getString(R.string.upd_watch_text, release.title.ifBlank { release.commit.take(7) })
val notification = Notification.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_stat_session)
.setContentTitle(context.getString(R.string.upd_watch_title, release.pr))
.setContentText(text)
.setStyle(Notification.BigTextStyle().bigText(text))
.setContentIntent(open)
.setAutoCancel(true)
.build()
return runCatching { manager.notify(NOTIFICATION_ID, notification) }.isSuccess
}
private fun prefs(context: Context) = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
}
}