Skip to main content

anodizer_core/git/
github_api.rs

1use anyhow::{Context as _, Result, bail};
2use std::collections::HashMap;
3use std::path::Path;
4use std::process::Command;
5use std::sync::{Mutex, OnceLock};
6
7use super::git_output_in;
8use super::slug::RepoSlug;
9use super::tags::create_and_push_tag_in;
10
11/// GET a GitHub API endpoint via the `gh` CLI (single request, no pagination).
12///
13/// Returns the parsed JSON response. Useful for endpoints that return a single
14/// object (e.g. the Compare API) rather than a paginated array.
15pub fn gh_api_get(endpoint: &str, token: Option<&str>) -> Result<serde_json::Value> {
16    gh_api_get_with_binary(Path::new("gh"), endpoint, token)
17}
18
19/// GET a GitHub API endpoint via `gh_binary` (single request, no pagination).
20///
21/// Path-taking sibling of [`gh_api_get`] so tests can point at a missing or
22/// stub binary inside a `tempfile::tempdir()` without mutating `PATH`.
23/// When `gh_binary` has no separator (e.g. `Path::new("gh")`),
24/// [`Command::new`] falls back to a PATH lookup — the production
25/// behavior — so the wrapper is a true no-op in normal use.
26pub fn gh_api_get_with_binary(
27    gh_binary: &Path,
28    endpoint: &str,
29    token: Option<&str>,
30) -> Result<serde_json::Value> {
31    let mut cmd = Command::new(gh_binary);
32    cmd.args(["api", endpoint]);
33    if let Some(tok) = token {
34        cmd.env("GITHUB_TOKEN", tok);
35    }
36    let output = cmd
37        .stdout(std::process::Stdio::piped())
38        .stderr(std::process::Stdio::piped())
39        .output()
40        .with_context(|| format!("failed to spawn gh CLI ({})", gh_binary.display()))?;
41    if !output.status.success() {
42        let stderr_raw = String::from_utf8_lossy(&output.stderr);
43        let raw = format!("gh api GET {} failed: {}", endpoint, stderr_raw.trim());
44        bail!("{}", redact_gh_stderr(&raw, token));
45    }
46    let stdout = String::from_utf8_lossy(&output.stdout);
47    serde_json::from_str(&stdout).context("failed to parse gh api response")
48}
49
50/// Cache key for [`COMMIT_LOGIN_CACHE`]: `(owner, repo, author_email)`.
51type LoginCacheKey = (String, String, String);
52
53/// Process-wide memo of commit-author login lookups. Failed lookups are
54/// cached as `None` so an offline / unauthenticated run costs at most one
55/// API attempt per unique author email, even when several CLI entry points
56/// render changelogs for many crates in the same invocation.
57static COMMIT_LOGIN_CACHE: OnceLock<Mutex<HashMap<LoginCacheKey, Option<String>>>> =
58    OnceLock::new();
59
60/// Resolve a commit author's GitHub login from a representative commit SHA
61/// via `GET /repos/{owner}/{repo}/commits/{sha}` → `.author.login`.
62///
63/// Best-effort by design: any failure (no `gh`, no auth, offline, unknown
64/// SHA, commit email not linked to a GitHub account) returns `None` with at
65/// most a debug-level trace — callers fall back to name-based rendering and
66/// must never fail a release pipeline over a missing login.
67///
68/// Results (including failures) are memoized process-wide per
69/// `(owner, repo, email)`, so each unique author email costs one API call
70/// per run regardless of how many commits or crates reference it.
71pub fn commit_author_login(
72    owner: &str,
73    repo: &str,
74    email: &str,
75    sha: &str,
76    token: Option<&str>,
77) -> Option<String> {
78    commit_author_login_with_binary(Path::new("gh"), owner, repo, email, sha, token)
79}
80
81/// Path-taking sibling of [`commit_author_login`] so tests can point at a
82/// missing or stub binary without mutating `PATH`.
83pub fn commit_author_login_with_binary(
84    gh_binary: &Path,
85    owner: &str,
86    repo: &str,
87    email: &str,
88    sha: &str,
89    token: Option<&str>,
90) -> Option<String> {
91    if owner.is_empty() || repo.is_empty() || email.is_empty() || sha.is_empty() {
92        return None;
93    }
94    let key = (owner.to_string(), repo.to_string(), email.to_string());
95    let cache = COMMIT_LOGIN_CACHE.get_or_init(|| Mutex::new(HashMap::new()));
96    // A poisoned lock only means another thread panicked mid-insert; the map
97    // itself is still a valid memo, so recover it rather than panic here.
98    {
99        let guard = match cache.lock() {
100            Ok(g) => g,
101            Err(poisoned) => poisoned.into_inner(),
102        };
103        if let Some(hit) = guard.get(&key) {
104            return hit.clone();
105        }
106    }
107    let endpoint = format!("/repos/{owner}/{repo}/commits/{sha}");
108    let resolved = match gh_api_get_with_binary(gh_binary, &endpoint, token) {
109        Ok(v) => v
110            .pointer("/author/login")
111            .and_then(|l| l.as_str())
112            .filter(|s| !s.is_empty())
113            .map(str::to_string),
114        Err(e) => {
115            tracing::debug!(
116                "commit_author_login: lookup for {} failed (keeping name-based rendering): {}",
117                email,
118                e
119            );
120            None
121        }
122    };
123    let mut guard = match cache.lock() {
124        Ok(g) => g,
125        Err(poisoned) => poisoned.into_inner(),
126    };
127    guard.insert(key, resolved.clone());
128    resolved
129}
130
131/// Resolve the GitHub token for API calls through the codebase-standard
132/// chain: explicit value (CLI flag / context option) → `ANODIZER_GITHUB_TOKEN`
133/// → `GITHUB_TOKEN`. Empty strings count as absent at every link — GitHub
134/// Actions materializes missing secrets as `""`, which must not
135/// short-circuit the fallback to the next link.
136///
137/// `env` is the injectable env source (pass `Context::env_var` or a
138/// map-backed closure in tests) so the chain is testable without mutating
139/// process env.
140pub fn resolve_github_token_with_env(
141    explicit: Option<&str>,
142    env: &dyn Fn(&str) -> Option<String>,
143) -> Option<String> {
144    let non_empty = |s: String| if s.is_empty() { None } else { Some(s) };
145    explicit
146        .filter(|t| !t.is_empty())
147        .map(str::to_string)
148        .or_else(|| env("ANODIZER_GITHUB_TOKEN").and_then(non_empty))
149        .or_else(|| env("GITHUB_TOKEN").and_then(non_empty))
150}
151
152/// Process-env wrapper of [`resolve_github_token_with_env`] for call sites
153/// without a `Context` (e.g. the `bump`/`tag` changelog-sync write path,
154/// whose commands expose no `--token` flag).
155pub fn resolve_github_token(explicit: Option<&str>) -> Option<String> {
156    resolve_github_token_with_env(explicit, &|key| std::env::var(key).ok())
157}
158
159/// Redact secrets from `gh` CLI stderr before interpolating into a bail
160/// message. `token` is the `GITHUB_TOKEN` value passed to the
161/// subprocess; if the user-supplied token leaks (e.g. via a verbose `gh`
162/// error that echoes the auth header), it is replaced with `$GITHUB_TOKEN`
163/// regardless of whether the value matches the `redact::is_secret`
164/// heuristics. Also strips inline URL credentials and any other secret
165/// env-var values reachable from the parent process env.
166fn redact_gh_stderr(stderr: &str, token: Option<&str>) -> String {
167    let mut env: Vec<(String, String)> = std::env::vars().collect();
168    if let Some(tok) = token
169        && !tok.is_empty()
170    {
171        env.push(("GITHUB_TOKEN".to_string(), tok.to_string()));
172    }
173    crate::redact::with_env(stderr, &env)
174}
175
176/// GET a GitHub API endpoint via the `gh` CLI, with pagination.
177///
178/// Returns a JSON array of all pages concatenated. The caller is responsible for
179/// ensuring that `gh` is installed and authenticated.
180pub fn gh_api_get_paginated(endpoint: &str, token: Option<&str>) -> Result<Vec<serde_json::Value>> {
181    gh_api_get_paginated_with_binary(Path::new("gh"), endpoint, token)
182}
183
184/// Paginated GET via `gh_binary`. Path-taking sibling of
185/// [`gh_api_get_paginated`].
186pub fn gh_api_get_paginated_with_binary(
187    gh_binary: &Path,
188    endpoint: &str,
189    token: Option<&str>,
190) -> Result<Vec<serde_json::Value>> {
191    let mut cmd = Command::new(gh_binary);
192    cmd.args(["api", "--paginate", endpoint]);
193    if let Some(tok) = token {
194        cmd.env("GITHUB_TOKEN", tok);
195    }
196    let output = cmd
197        .stdout(std::process::Stdio::piped())
198        .stderr(std::process::Stdio::piped())
199        .output()
200        .with_context(|| format!("failed to spawn gh CLI ({})", gh_binary.display()))?;
201
202    if !output.status.success() {
203        let stderr_raw = String::from_utf8_lossy(&output.stderr);
204        let raw = format!("gh api GET {} failed: {}", endpoint, stderr_raw.trim());
205        bail!("{}", redact_gh_stderr(&raw, token));
206    }
207
208    let stdout = String::from_utf8_lossy(&output.stdout);
209
210    // Try parsing the entire response first before falling back to splitting.
211    // This avoids the split_inclusive(']') approach corrupting non-array responses.
212    if let Ok(serde_json::Value::Array(arr)) = serde_json::from_str::<serde_json::Value>(&stdout) {
213        return Ok(arr);
214    }
215    if let Ok(val) = serde_json::from_str::<serde_json::Value>(&stdout) {
216        // Single object response (e.g. non-list endpoint) — wrap in a vec.
217        return Ok(vec![val]);
218    }
219
220    // Whole-parse failed — gh --paginate may return multiple JSON arrays
221    // concatenated (e.g. `[...][...]`). Split on `]` boundaries and parse each chunk.
222    let mut all_items = Vec::new();
223    for chunk in stdout.split_inclusive(']') {
224        let trimmed = chunk.trim();
225        if trimmed.is_empty() {
226            continue;
227        }
228        if let Ok(serde_json::Value::Array(arr)) =
229            serde_json::from_str::<serde_json::Value>(trimmed)
230        {
231            all_items.extend(arr);
232        } else if let Ok(val) = serde_json::from_str::<serde_json::Value>(trimmed) {
233            all_items.push(val);
234        } else {
235            // Log unparseable chunks so corrupt data doesn't go unnoticed.
236            // The chunk may carry secret-shaped request/response data, and the
237            // tracing subscriber performs NO redaction of its own — so redact
238            // here (process-env secret values + inline URL credentials) before
239            // emitting. Cap the logged chunk at 200 bytes — an HTTP body in an
240            // error context should convey "what server said" without dumping a
241            // multi-MB stack trace to the user's terminal.
242            let snippet = &trimmed[..trimmed.len().min(200)];
243            let redacted = crate::redact::redact_process_env(snippet);
244            tracing::warn!(
245                "gh_api_get_paginated: failed to parse JSON chunk ({} bytes): {:?}",
246                trimmed.len(),
247                redacted,
248            );
249        }
250    }
251    Ok(all_items)
252}
253
254/// POST via `gh_binary`. Internal helper consumed by
255/// [`create_tag_via_github_api_in`]; takes an explicit binary path so
256/// tests can drive the failure path against a missing or stub binary.
257fn gh_api_post_with_binary(
258    gh_binary: &Path,
259    endpoint: &str,
260    body: &serde_json::Value,
261    log: &crate::log::StageLogger,
262) -> Result<serde_json::Value> {
263    let body_str = serde_json::to_string(body)?;
264
265    let mut cmd = Command::new(gh_binary);
266    cmd.args(["api", "--method", "POST", endpoint, "--input", "-"]);
267
268    // `gh api` may echo a token from the parent env (`GITHUB_TOKEN` /
269    // `GH_TOKEN`) on stderr; carry the full process env on a logger clone so
270    // the helper's redaction matches the prior `redact_process_env` coverage
271    // (broader than this caller's attached env). The "gh CLI" label keeps the
272    // spawn-failure string the caller pattern-matches on
273    // (`failed to spawn gh CLI`) for its git-fallback decision.
274    let redacting_log = log.clone().with_env(std::env::vars().collect::<Vec<_>>());
275    let output = crate::run::run_checked_with_stdin(
276        &mut cmd,
277        body_str.as_bytes(),
278        &redacting_log,
279        "gh CLI",
280    )?;
281
282    let response: serde_json::Value = serde_json::from_slice(&output.stdout)
283        .with_context(|| format!("failed to parse GitHub API response from {}", endpoint))?;
284    Ok(response)
285}
286
287/// Create a tag via the GitHub API (using the `gh` CLI).
288///
289/// This avoids the need for local git push access. Requires the `gh` CLI to be
290/// installed and authenticated (`gh auth login`). The GitHub API creates a
291/// lightweight tag object pointing at the HEAD commit on the default branch.
292///
293/// Falls back to [`create_and_push_tag_in`] if `gh` is not available.
294///
295/// `slug` is the resolved repository identity (config override -> remote),
296/// supplied by the caller so the API target and the rest of the pipeline agree
297/// on one owner/repo rather than this function re-deriving its own.
298pub fn create_tag_via_github_api(
299    slug: &RepoSlug,
300    tag: &str,
301    message: &str,
302    dry_run: bool,
303    log: &crate::log::StageLogger,
304    strict: bool,
305) -> Result<()> {
306    create_tag_via_github_api_in(
307        &std::env::current_dir()?,
308        Path::new("gh"),
309        slug,
310        tag,
311        message,
312        dry_run,
313        log,
314        strict,
315    )
316}
317
318/// Path-taking sibling of [`create_tag_via_github_api`].
319///
320/// `cwd` is the repository the tag should be created against (used for the
321/// `git rev-parse HEAD` lookup and the local `git tag -a` fallback when
322/// `gh_binary` is missing). `slug` is the resolved owner/repo. `gh_binary` is
323/// the path to the `gh` CLI; pass `Path::new("gh")` to keep the production
324/// PATH-lookup behavior.
325#[allow(clippy::too_many_arguments)]
326pub fn create_tag_via_github_api_in(
327    cwd: &Path,
328    gh_binary: &Path,
329    slug: &RepoSlug,
330    tag: &str,
331    message: &str,
332    dry_run: bool,
333    log: &crate::log::StageLogger,
334    strict: bool,
335) -> Result<()> {
336    if dry_run {
337        log.status(&format!(
338            "(dry-run) would create tag {} via GitHub API (\"{}\")",
339            tag, message
340        ));
341        return Ok(());
342    }
343
344    let owner = slug.owner();
345    let repo = slug.name();
346
347    // Get the current HEAD SHA to point the tag at — via the canonical
348    // single sha resolver, not a private `rev-parse` here.
349    let sha = super::get_head_commit_in(cwd)?;
350
351    let body = serde_json::json!({
352        "tag": tag,
353        "message": message,
354        "object": sha,
355        "type": "commit",
356        "tagger": {
357            "name": git_output_in(cwd, &["config", "user.name"]).unwrap_or_else(|_| "anodizer".to_string()),
358            "email": git_output_in(cwd, &["config", "user.email"]).unwrap_or_else(|_| "anodizer@users.noreply.github.com".to_string()),
359            "date": crate::sde::resolve_now().to_rfc3339(),
360        }
361    });
362
363    let tag_endpoint = format!("/repos/{owner}/{repo}/git/tags");
364    let response = match gh_api_post_with_binary(gh_binary, &tag_endpoint, &body, log) {
365        Ok(resp) => resp,
366        Err(e) => {
367            if e.to_string().contains("failed to spawn gh CLI") {
368                if strict {
369                    anyhow::bail!(
370                        "gh CLI not found, cannot create tag via GitHub API (strict mode)"
371                    );
372                }
373                log.warn("gh CLI not found, falling back to local git tag + push");
374                return create_and_push_tag_in(cwd, tag, message, dry_run, log, strict);
375            }
376            return Err(e);
377        }
378    };
379
380    let tag_sha = response["sha"]
381        .as_str()
382        .ok_or_else(|| anyhow::anyhow!("GitHub API response missing 'sha' field"))?;
383
384    let ref_body = serde_json::json!({
385        "ref": format!("refs/tags/{}", tag),
386        "sha": tag_sha,
387    });
388
389    let ref_endpoint = format!("/repos/{owner}/{repo}/git/refs");
390    gh_api_post_with_binary(gh_binary, &ref_endpoint, &ref_body, log)?;
391
392    Ok(())
393}
394
395#[cfg(test)]
396mod tests {
397    use super::*;
398
399    /// `dry_run=true` must short-circuit before any subprocess spawn.
400    #[test]
401    fn create_tag_dry_run_short_circuits() {
402        let log = crate::log::StageLogger::new("test", crate::log::Verbosity::Quiet);
403        let slug = RepoSlug::for_test("owner", "repo");
404        // Even with no git repo / no gh CLI, dry-run must succeed.
405        let result = create_tag_via_github_api(&slug, "v1.0.0", "msg", true, &log, false);
406        assert!(result.is_ok(), "dry-run must succeed: {result:?}");
407    }
408
409    /// Redact: the token must be replaced with the literal `$GITHUB_TOKEN`
410    /// placeholder when it appears verbatim in the stderr output. Catches
411    /// the case where `gh` echoes the auth header in a verbose error.
412    #[test]
413    fn redact_gh_stderr_replaces_token_value() {
414        let secret = "ghp_abcdefghijklmnopqrstuvwxyz0123456789";
415        let stderr = format!("HTTP 401: token {secret} is invalid");
416        let redacted = redact_gh_stderr(&stderr, Some(secret));
417        // Exact-output, not absence-only: guards the env-value masking
418        // layer — the token is replaced by its `$NAME` placeholder.
419        assert_eq!(redacted, "HTTP 401: token $GITHUB_TOKEN is invalid");
420    }
421
422    #[test]
423    fn redact_gh_stderr_with_no_token_still_strips_url_creds() {
424        // Inline URL credentials must be redacted even with no explicit
425        // token argument.
426        let stderr = "auth failed: https://user:secret-pw@github.com/o/r.git rejected";
427        let redacted = redact_gh_stderr(stderr, None);
428        // Exact-output, not absence-only: guards the inline URL-credential
429        // stripping layer — userinfo becomes `<redacted>`.
430        assert_eq!(
431            redacted,
432            "auth failed: https://<redacted>@github.com/o/r.git rejected"
433        );
434    }
435
436    #[test]
437    fn redact_gh_stderr_empty_token_is_noop_on_token_field() {
438        // An empty Some("") token must not pollute the env vector with a
439        // zero-length value (that would match every position in the string).
440        let stderr = "plain error message without credentials";
441        let redacted = redact_gh_stderr(stderr, Some(""));
442        assert_eq!(redacted, stderr);
443    }
444
445    /// A missing `gh` binary must degrade to `None` (never an error/panic):
446    /// the changelog pipeline keeps name-based rendering. The failure is
447    /// memoized, so the second call returns the cached `None` without
448    /// re-attempting a spawn.
449    #[test]
450    fn commit_author_login_missing_binary_degrades_to_none_and_caches() {
451        let tmp = tempfile::tempdir().unwrap();
452        let missing = tmp.path().join("nonexistent-gh");
453        let first = commit_author_login_with_binary(
454            &missing,
455            "owner-cal-test",
456            "repo-cal-test",
457            "a@example.com",
458            "0123456789abcdef0123456789abcdef01234567",
459            None,
460        );
461        assert_eq!(first, None, "missing binary must yield None");
462        // Cached-failure path: same (owner, repo, email) key short-circuits
463        // before any spawn attempt.
464        let second = commit_author_login_with_binary(
465            &missing,
466            "owner-cal-test",
467            "repo-cal-test",
468            "a@example.com",
469            "fedcba9876543210fedcba9876543210fedcba98",
470            None,
471        );
472        assert_eq!(second, None);
473    }
474
475    /// Empty inputs short-circuit to `None` without touching the cache or
476    /// spawning anything.
477    #[test]
478    fn commit_author_login_empty_inputs_are_none() {
479        let gh = Path::new("gh");
480        assert_eq!(
481            commit_author_login_with_binary(gh, "", "r", "e", "s", None),
482            None
483        );
484        assert_eq!(
485            commit_author_login_with_binary(gh, "o", "", "e", "s", None),
486            None
487        );
488        assert_eq!(
489            commit_author_login_with_binary(gh, "o", "r", "", "s", None),
490            None
491        );
492        assert_eq!(
493            commit_author_login_with_binary(gh, "o", "r", "e", "", None),
494            None
495        );
496    }
497
498    /// Chain order: explicit beats `ANODIZER_GITHUB_TOKEN` beats
499    /// `GITHUB_TOKEN`; empty strings are absent at every link. Uses a
500    /// map-backed env closure — no process-env mutation, no network.
501    #[test]
502    fn resolve_github_token_chain_order_and_empty_filtering() {
503        let env_with = |pairs: &[(&str, &str)]| {
504            let map: HashMap<String, String> = pairs
505                .iter()
506                .map(|(k, v)| (k.to_string(), v.to_string()))
507                .collect();
508            move |key: &str| map.get(key).cloned()
509        };
510
511        let both = env_with(&[
512            ("ANODIZER_GITHUB_TOKEN", "anod-tok"),
513            ("GITHUB_TOKEN", "gh-tok"),
514        ]);
515        assert_eq!(
516            resolve_github_token_with_env(Some("explicit-tok"), &both),
517            Some("explicit-tok".to_string()),
518            "explicit token must win over both env vars"
519        );
520        assert_eq!(
521            resolve_github_token_with_env(None, &both),
522            Some("anod-tok".to_string()),
523            "ANODIZER_GITHUB_TOKEN must win over GITHUB_TOKEN"
524        );
525
526        let gh_only = env_with(&[("GITHUB_TOKEN", "gh-tok")]);
527        assert_eq!(
528            resolve_github_token_with_env(None, &gh_only),
529            Some("gh-tok".to_string()),
530            "GITHUB_TOKEN alone must resolve (the CI-gap pin)"
531        );
532        assert_eq!(
533            resolve_github_token_with_env(Some(""), &gh_only),
534            Some("gh-tok".to_string()),
535            "empty explicit token must fall through to env"
536        );
537
538        let empty_anod = env_with(&[("ANODIZER_GITHUB_TOKEN", ""), ("GITHUB_TOKEN", "gh-tok")]);
539        assert_eq!(
540            resolve_github_token_with_env(None, &empty_anod),
541            Some("gh-tok".to_string()),
542            "empty ANODIZER_GITHUB_TOKEN (GHA missing-secret materialization) must not short-circuit"
543        );
544
545        let none = env_with(&[]);
546        assert_eq!(resolve_github_token_with_env(None, &none), None);
547        assert_eq!(resolve_github_token_with_env(Some(""), &none), None);
548    }
549
550    /// `gh_api_get_with_binary` must surface a user-actionable spawn
551    /// failure when the binary path doesn't exist on disk.
552    ///
553    /// Drives the function with a temp-dir-relative path that points to
554    /// nothing, asserting the error mentions "spawn gh" so the operator
555    /// can correlate it with their missing-`gh` install state.
556    #[test]
557    fn gh_api_get_with_binary_bails_when_binary_missing() {
558        let tmp = tempfile::tempdir().unwrap();
559        let missing = tmp.path().join("nonexistent-gh");
560        let err = gh_api_get_with_binary(&missing, "/repos/x/y", None)
561            .expect_err("missing binary must error");
562        let msg = err.to_string();
563        assert!(
564            msg.contains("spawn gh") || msg.contains(&missing.display().to_string()),
565            "expected actionable error mentioning spawn gh or the binary path, got: {msg}"
566        );
567    }
568
569    /// Same guarantee for the paginated sibling.
570    #[test]
571    fn gh_api_get_paginated_with_binary_bails_when_binary_missing() {
572        let tmp = tempfile::tempdir().unwrap();
573        let missing = tmp.path().join("nonexistent-gh");
574        let err = gh_api_get_paginated_with_binary(&missing, "/repos/x/y", None)
575            .expect_err("missing binary must error");
576        let msg = err.to_string();
577        assert!(
578            msg.contains("spawn gh") || msg.contains(&missing.display().to_string()),
579            "expected actionable error mentioning spawn gh or the binary path, got: {msg}"
580        );
581    }
582
583    /// `create_tag_via_github_api_in` with `strict=true` must error
584    /// when `cwd` is not a git repo — the HEAD-sha resolver
585    /// (`get_head_commit_in`) drives `git rev-parse HEAD` and fails there.
586    ///
587    /// Skips when `git` isn't on PATH (mirrors `tool_on_path` patterns
588    /// elsewhere in the suite).
589    #[test]
590    fn create_tag_via_github_api_in_bails_when_not_a_git_repo() {
591        // spawn-retry-ok: this is a git *availability* probe — an Err means git
592        // is absent (skip the test via the success() guard), not a transient
593        // spawn-init failure to retry; routing it through the panicking helper
594        // would crash on a git-less host instead of skipping.
595        if !Command::new("git")
596            .arg("--version")
597            .output()
598            .map(|o| o.status.success())
599            .unwrap_or(false)
600        {
601            return;
602        }
603        let tmp = tempfile::tempdir().unwrap();
604        let log = crate::log::StageLogger::new("test", crate::log::Verbosity::Quiet);
605        let slug = RepoSlug::for_test("owner", "repo");
606        let err = create_tag_via_github_api_in(
607            tmp.path(),
608            Path::new("gh"),
609            &slug,
610            "v1.0.0",
611            "msg",
612            false,
613            &log,
614            true,
615        )
616        .expect_err("non-git cwd must error");
617        let msg = err.to_string();
618        assert!(
619            msg.contains("git") || msg.contains("remote"),
620            "expected error to mention git or remote, got: {msg}"
621        );
622    }
623
624    /// Dry-run short-circuit must also fire on the cwd-injectable entry
625    /// point — covers the new branch without re-hitting the inner
626    /// detection codepath.
627    #[test]
628    fn create_tag_via_github_api_in_dry_run_short_circuits() {
629        let tmp = tempfile::tempdir().unwrap();
630        let log = crate::log::StageLogger::new("test", crate::log::Verbosity::Quiet);
631        let slug = RepoSlug::for_test("owner", "repo");
632        let result = create_tag_via_github_api_in(
633            tmp.path(),
634            Path::new("gh"),
635            &slug,
636            "v1.0.0",
637            "msg",
638            true,
639            &log,
640            false,
641        );
642        assert!(result.is_ok(), "dry-run must succeed: {result:?}");
643    }
644}