Skip to content

Commit 213f100

Browse files
committed
Wire the update check into AppWiring and the subcommand lifecycle
Final piece of the periodic update-check feature: hooks the policy + fetcher from the previous commits into the live CLI. AppWiring: - Adds `updateCheck: UpdateCheck` to the interface. - DefaultAppWiring lazily constructs a `KtorReleaseFetcher` (kept open for reuse between sibling commands) and the `UpdateCheck` instance. The state file defaults to `~/.git-jaspr/update-check.json`. - `close()` shuts the fetcher down only if it was actually used, so commands that short-circuit the check pay zero cost. Cli.kt: - Adds `--no-update-check` global flag on GitJasprRoot. The flag binds to the existing ChainedValueSource so users can also set `update-check=false` in `~/.jaspr.properties`. Help text mentions the properties key + env-var alternative. - New `isUpdateCheckSkipped()` predicate composes all suppression signals: the flag (or properties value), `JASPR_NO_UPDATE_CHECK` env var, well-known CI env vars (CI, GITHUB_ACTIONS, GITLAB_CI, etc.), and `System.console() == null` (non-TTY stdout). The predicate is injected into AppWiring so the policy class can call it without knowing about Clikt. - GitJasprSubcommand: new `open val skipUpdateCheck: Boolean = false` for subcommands that want to suppress the notice on a per-command basis. `run()` calls `appWiring.updateCheck.maybeNotice()` after doRun() succeeds and renders the result with `theme.muted`. The rendering sits next to the existing "Tip:" line so the notice ends up at the bottom of command output rather than competing with it. UpdateCheck.kt: - Adds `UpdateNotice.formatMessage()` so the wiring layer doesn't hardcode the message format. Deliberately doesn't name a package manager — jaspr ships via Homebrew, native-image, and source, so we link to the GitHub release page and let it describe install methods. Test: one new test covering `UpdateNotice.formatMessage()`. The 17 existing UpdateCheck tests + 5 fetcher tests still pass; full suite runs clean (428 tests, 0 failures). commit-id: If8fbb861
1 parent e94e65b commit 213f100

5 files changed

Lines changed: 153 additions & 4 deletions

File tree

‎git-jaspr/src/main/kotlin/sims/michael/gitjaspr/AppWiring.kt‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import io.ktor.client.engine.cio.*
77
import io.ktor.client.plugins.auth.*
88
import io.ktor.client.plugins.auth.providers.*
99
import java.io.Closeable
10+
import java.io.File
11+
import java.time.Instant
1012
import kotlinx.serialization.json.Json
1113
import sims.michael.gitjaspr.graphql.ErrorMappingInterceptor
1214
import sims.michael.gitjaspr.graphql.RateLimitRetryInterceptor
@@ -16,13 +18,17 @@ interface AppWiring : Closeable {
1618
val config: Config
1719
val json: Json
1820
val gitClient: GitClient
21+
val updateCheck: UpdateCheck
1922
}
2023

2124
class DefaultAppWiring(
2225
githubToken: String,
2326
override val config: Config,
2427
override val gitClient: GitClient,
2528
private val renderer: Renderer,
29+
private val updateCheckSkip: () -> Boolean = { true },
30+
private val updateCheckStateFile: File =
31+
File(System.getenv("HOME")).resolve(".git-jaspr/update-check.json"),
2632
) : AppWiring {
2733

2834
private val gitHubClientWiring =
@@ -42,7 +48,30 @@ class DefaultAppWiring(
4248
GitJaspr(gitHubClient, gitClient, config, renderer = renderer)
4349
}
4450

45-
override fun close() = gitHubClientWiring.close()
51+
private val releaseFetcherLazy: Lazy<KtorReleaseFetcher> = lazy { KtorReleaseFetcher() }
52+
private val releaseFetcher: KtorReleaseFetcher
53+
get() = releaseFetcherLazy.value
54+
55+
override val updateCheck: UpdateCheck by lazy {
56+
UpdateCheck(
57+
stateFile = updateCheckStateFile,
58+
fetcher = releaseFetcher,
59+
now = Instant::now,
60+
currentVersion = VERSION,
61+
json = json,
62+
skip = updateCheckSkip,
63+
)
64+
}
65+
66+
override fun close() {
67+
try {
68+
gitHubClientWiring.close()
69+
} finally {
70+
// Close the lazy release fetcher only if it was initialized — most commands never
71+
// exercise the update-check path (skip predicate short-circuits before construction).
72+
if (releaseFetcherLazy.isInitialized()) releaseFetcher.close()
73+
}
74+
}
4675
}
4776

4877
class GitHubClientWiring(

‎git-jaspr/src/main/kotlin/sims/michael/gitjaspr/Cli.kt‎

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,14 @@ applicable.
239239
"Print the effective configuration to standard output (for debugging)"
240240
}
241241

242+
private val updateCheckEnabled by
243+
option().flag("--no-update-check", default = true).help {
244+
"Periodically check whether a newer jaspr is available and surface a one-line notice. " +
245+
"Disable persistently by setting `update-check.enabled=false` in " +
246+
"~/$CONFIG_FILE_NAME, or for a single shell with " +
247+
"$JASPR_NO_UPDATE_CHECK_ENV_VAR=1."
248+
}
249+
242250
private val theme by
243251
option(
244252
"--theme",
@@ -268,9 +276,30 @@ applicable.
268276
dontPushRegex,
269277
showTips,
270278
)
271-
return DefaultAppWiring(token, config, gitClient, renderer)
279+
return DefaultAppWiring(
280+
githubToken = token,
281+
config = config,
282+
gitClient = gitClient,
283+
renderer = renderer,
284+
updateCheckSkip = ::isUpdateCheckSkipped,
285+
)
272286
}
273287

288+
/**
289+
* Predicate composing all reasons to skip the update check:
290+
* - `--no-update-check` (or `update-check.enabled=false` in `~/$CONFIG_FILE_NAME` via the same
291+
* Clikt value source as other flags).
292+
* - `$JASPR_NO_UPDATE_CHECK_ENV_VAR` env var set to any non-empty value.
293+
* - Running under CI (any of the well-known CI env vars set).
294+
* - Stdout not connected to a TTY — the notice would just become noise in piped output or
295+
* redirected logs.
296+
*/
297+
private fun isUpdateCheckSkipped(): Boolean =
298+
!updateCheckEnabled ||
299+
!System.getenv(JASPR_NO_UPDATE_CHECK_ENV_VAR).isNullOrEmpty() ||
300+
CI_ENV_VARS.any { !System.getenv(it).isNullOrEmpty() } ||
301+
System.console() == null
302+
274303
private fun determineGithubInfo(gitClient: GitClient): GitHubInfo {
275304
val host = gitHubOptions.githubHost
276305
val owner = gitHubOptions.repoOwner
@@ -477,13 +506,26 @@ abstract class GitJasprSubcommand(
477506

478507
abstract suspend fun doRun()
479508

509+
/**
510+
* Subcommands set this to true to suppress the trailing "update available" notice (e.g., when
511+
* the command's output is consumed by a script and a free-form line would break parsing).
512+
* Default false — the global `--no-update-check` flag plus the env/CI/TTY skip predicate cover
513+
* the common opt-outs already.
514+
*/
515+
open val skipUpdateCheck: Boolean = false
516+
480517
override suspend fun run() {
481518
val logger = Cli.logger
482519
try {
483520
doRun()
484521
cliContext.tipProvider?.getNextTip()?.let { tip ->
485522
renderer.info { muted("Tip: $tip") }
486523
}
524+
if (!skipUpdateCheck) {
525+
appWiring.updateCheck.maybeNotice()?.let { notice ->
526+
renderer.info { muted(notice.formatMessage()) }
527+
}
528+
}
487529
} catch (e: GitJasprException) {
488530
logger.debug("An error occurred", e)
489531
renderer.error { e.message }
@@ -2071,6 +2113,14 @@ const val DEFAULT_TARGET_REF = "main"
20712113
const val DEFAULT_REMOTE_NAME = "origin"
20722114
const val COMMIT_ID_LABEL = "commit-id"
20732115
private const val GITHUB_TOKEN_ENV_VAR = "GIT_JASPR_TOKEN"
2116+
const val JASPR_NO_UPDATE_CHECK_ENV_VAR = "JASPR_NO_UPDATE_CHECK"
2117+
2118+
/**
2119+
* Environment variables we treat as "running under CI." Any of these set to a non-empty value
2120+
* suppresses the update-check notice — those logs are not interactive.
2121+
*/
2122+
private val CI_ENV_VARS =
2123+
listOf("CI", "GITHUB_ACTIONS", "GITLAB_CI", "BUILDKITE", "CIRCLECI", "TRAVIS", "JENKINS_URL")
20742124

20752125
/** Completes with unique remote branch names (remote prefix stripped). */
20762126
private val remoteBranchCandidates =

‎git-jaspr/src/main/kotlin/sims/michael/gitjaspr/UpdateCheck.kt‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,17 @@ fun interface ReleaseFetcher {
4141

4242
/**
4343
* A surfaceable update notice. The render layer formats this into a single muted line at the end of
44-
* the command output.
44+
* the command output via [formatMessage].
4545
*/
46-
data class UpdateNotice(val currentVersion: String, val latestVersion: String, val url: String)
46+
data class UpdateNotice(val currentVersion: String, val latestVersion: String, val url: String) {
47+
/**
48+
* Single-line message intended for `theme.muted` rendering. Deliberately does not name a
49+
* specific package manager — jaspr ships via Homebrew, native-image, and source builds, so we
50+
* point at the GitHub release page and let it describe install methods.
51+
*/
52+
fun formatMessage(): String =
53+
"Update available: $latestVersion (you have $currentVersion). $url"
54+
}
4755

4856
/**
4957
* Coordinates "is a newer jaspr available?" checks. Reads/writes state at [stateFile], asks

‎git-jaspr/src/test/kotlin/sims/michael/gitjaspr/KtorReleaseFetcherTest.kt‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,48 @@ class KtorReleaseFetcherTest {
7979
}
8080
}
8181

82+
@Test
83+
@EnabledIfEnvironmentVariable(named = "RUN_NETWORK_TESTS", matches = "1")
84+
fun `integration - end-to-end notice rendering for an outdated current version`() {
85+
// Exercises the full stack (fetch → policy → formatted notice) against real GitHub. Pins
86+
// the "current version" to v0.0.1 so any real release qualifies as newer.
87+
val tempState =
88+
java.nio.file.Files.createTempDirectory("update-check-integration")
89+
.resolve("state.json")
90+
.toFile()
91+
try {
92+
KtorReleaseFetcher().use { fetcher ->
93+
val check =
94+
UpdateCheck(
95+
stateFile = tempState,
96+
fetcher = fetcher,
97+
now = java.time.Instant::now,
98+
currentVersion = "v0.0.1",
99+
interval = java.time.Duration.ofHours(24),
100+
json = kotlinx.serialization.json.Json,
101+
skip = { false },
102+
)
103+
val notice = assertNotNull(check.maybeNotice())
104+
assertEquals("v0.0.1", notice.currentVersion)
105+
assertTrue(
106+
notice.latestVersion.startsWith("v"),
107+
"Expected a v-prefixed tag, got: ${notice.latestVersion}",
108+
)
109+
assertTrue(
110+
notice.url.startsWith(
111+
"https://github.com/$UPDATE_CHECK_REPO_OWNER/$UPDATE_CHECK_REPO_NAME/releases/tag/"
112+
),
113+
"Expected a real release URL, got: ${notice.url}",
114+
)
115+
val message = notice.formatMessage()
116+
assertTrue(message.startsWith("Update available:"))
117+
assertTrue(message.contains(notice.latestVersion))
118+
}
119+
} finally {
120+
tempState.parentFile?.deleteRecursively()
121+
}
122+
}
123+
82124
companion object {
83125
private lateinit var server: EmbeddedServer<*, *>
84126

‎git-jaspr/src/test/kotlin/sims/michael/gitjaspr/UpdateCheckTest.kt‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,26 @@ class UpdateCheckTest {
3232
tempDir.deleteRecursively()
3333
}
3434

35+
// ---------- notice formatting ----------
36+
37+
@Test
38+
fun `formatMessage renders versions and url, no package-manager hint`() {
39+
val notice =
40+
UpdateNotice(
41+
currentVersion = "v2.1.0-beta.2",
42+
latestVersion = "v2.1.0-beta.3",
43+
url = "https://github.com/MichaelSims/git-jaspr/releases/tag/v2.1.0-beta.3",
44+
)
45+
val msg = notice.formatMessage()
46+
assertEquals(
47+
"Update available: v2.1.0-beta.3 (you have v2.1.0-beta.2). " +
48+
"https://github.com/MichaelSims/git-jaspr/releases/tag/v2.1.0-beta.3",
49+
msg,
50+
)
51+
// Sanity: no package manager named, so we don't mislead non-Homebrew users.
52+
assertTrue(!msg.contains("brew") && !msg.contains("apt") && !msg.contains("dnf"))
53+
}
54+
3555
// ---------- SemVer ----------
3656

3757
@Test

0 commit comments

Comments
 (0)