Skip to main content

release_kit/commands/
issue.rs

1//! `rk issue start`: one command from an issue to the branch the forge
2//! named, seated the way the recorded mode says.
3//!
4//! Not a worktree verb, because the worktree verbs are mode-free by
5//! design and starting from an issue is not: branches mode has no
6//! worktree to add. The order of the run is what makes the refusals
7//! cheap — the local reads and the reference check come first, the
8//! forge-CLI gate next, and the network last — so every failure but a
9//! forge outage costs one local process and leaves the clone untouched.
10
11use camino::{Utf8Path, Utf8PathBuf};
12use serde::Serialize;
13
14use crate::cli::issue::{IssueAction, IssueArgs};
15use crate::detect::{self, Forge};
16use crate::diagnostic::{Diagnostic, Reason};
17use crate::error::RkError;
18use crate::issue::{self, Resolved};
19use crate::landing::manifest::{self, Workflow};
20use crate::output::Output;
21use crate::probes;
22use crate::setup::context::resolve_cli;
23
24/// One `rk issue start` report.
25#[derive(Debug, Serialize)]
26struct StartReport {
27    /// The shape version of this JSON.
28    schema: &'static str,
29    /// `preview` or `apply`.
30    mode: &'static str,
31    /// The forge acted on.
32    forge: &'static str,
33    /// The project path.
34    repo: String,
35    /// The issue, as the forge numbers it.
36    issue: u64,
37    /// The issue's title.
38    title: String,
39    /// The branch, where one is known.
40    #[serde(skip_serializing_if = "Option::is_none")]
41    branch: Option<String>,
42    /// Where the name came from: `already`, `forge`, or `pending`.
43    origin: &'static str,
44    /// The recorded workflow mode this run seated by.
45    workflow: &'static str,
46    /// The worktree path, under worktree mode.
47    #[serde(skip_serializing_if = "Option::is_none")]
48    path: Option<String>,
49    /// The branch checked out in place, under branches mode.
50    #[serde(skip_serializing_if = "Option::is_none")]
51    checkout: Option<String>,
52    /// Every other branch the forge links to this issue.
53    #[serde(skip_serializing_if = "Vec::is_empty")]
54    others: Vec<String>,
55    /// A state the operator must see rather than one rk decided quietly.
56    #[serde(skip_serializing_if = "Option::is_none")]
57    detail: Option<String>,
58    /// What to run next.
59    next: Vec<String>,
60}
61
62/// Dispatch the issue surface.
63///
64/// # Errors
65///
66/// Refuses a target that is not a repository, a reference that names
67/// another project, an undetected forge, a forge CLI below its floor, a
68/// GitLab template rendering a name the landed grammar refuses, and every
69/// seating refusal `rk worktree add` already carries.
70pub fn run(args: &IssueArgs) -> Result<(), RkError> {
71    match &args.action {
72        IssueAction::Start {
73            issue,
74            target,
75            forge,
76            repo,
77            workflow,
78            base,
79            apply,
80            json,
81        } => start(
82            target,
83            issue,
84            &Overrides {
85                forge: forge.as_deref(),
86                repo: repo.as_deref(),
87                workflow: workflow.as_deref(),
88                base: base.as_deref(),
89            },
90            *apply,
91            Output::new(*json),
92        ),
93    }
94}
95
96/// What the operator overrode, where detection or the record would
97/// otherwise decide.
98struct Overrides<'a> {
99    /// The forge, over the detected one.
100    forge: Option<&'a str>,
101    /// The project path, over the detected one.
102    repo: Option<&'a str>,
103    /// The workflow mode, over the recorded one.
104    workflow: Option<&'a str>,
105    /// The commit-ish a new branch starts from.
106    base: Option<&'a str>,
107}
108
109/// Everything the local reads settled, before the first forge call.
110struct Ground {
111    /// The forge to act on.
112    forge: Forge,
113    /// The project path.
114    repo: String,
115    /// The API host to name explicitly, where one has to be named.
116    ///
117    /// The reference's host alone, because an issue URL is a web address
118    /// and its host is the instance. A remote's host is a transport
119    /// host, which a self-managed instance may serve under a separate
120    /// name — `git@ssh.example.com` for `example.com` — so naming it
121    /// would send the API calls somewhere the forge CLI already resolves
122    /// correctly from the working directory.
123    api_host: Option<String>,
124    /// The mode the seat follows.
125    workflow: Workflow,
126    /// Where the mode came from, for the report.
127    workflow_source: &'static str,
128}
129
130/// Refuse a coordinate that disagrees with one the clone already knows.
131///
132/// A coordinate the clone does not know is absent rather than
133/// contradicted, which is what leaves an override its real job.
134fn contradicts(what: &str, chosen: Option<&str>, known: Option<&str>) -> Result<(), RkError> {
135    let (Some(chosen), Some(known)) = (chosen, known) else {
136        return Ok(());
137    };
138    if chosen == known {
139        return Ok(());
140    }
141    Err(RkError::Usage(format!(
142        "the {what} to act on is {chosen} and this clone's is {known}; the branch would be minted on one project and seated in another"
143    )))
144}
145
146/// The mode the seat follows, and what decided it.
147///
148/// The mode is a landing parameter, changed through the landing verbs
149/// alone, so a runtime flag states it rather than sets it. Where it
150/// disagrees with the record, one clone would work in a mode the
151/// committed project policy does not carry.
152fn mode_of(target: &Utf8Path, named: Option<&str>) -> Result<(Workflow, &'static str), RkError> {
153    let recorded = manifest::load(target)?.map(|held| held.parameters.workflow);
154    match (named, recorded) {
155        (Some(raw), Some(held)) => {
156            if Workflow::parse(raw)? != held {
157                return Err(RkError::refusal(
158                    Diagnostic::new(
159                        Reason::StateDrift,
160                        format!(
161                            "--workflow {raw} disagrees with the landing record, which states {}",
162                            held.as_str()
163                        ),
164                    )
165                    .expected("a flag that states the recorded mode, or no flag at all")
166                    .action("rk upgrade --workflow <mode> --apply changes the recorded mode")
167                    .target_state("unchanged"),
168                ));
169            }
170            Ok((held, "the landing record, restated by --workflow"))
171        }
172        (Some(raw), None) => Ok((Workflow::parse(raw)?, "the --workflow flag")),
173        (None, Some(held)) => Ok((held, "the landing record")),
174        // A target with no record is treated as the convention's own
175        // mode rather than as branches: the record's serde default exists
176        // for records written before the parameter, not for targets that
177        // never landed.
178        (None, None) => Ok((Workflow::Worktree, "the default, with no landing record")),
179    }
180}
181
182/// Refuse a forge on a host this verb's calls would not reach.
183///
184/// Every GitHub call here goes to the CLI's default host. An enterprise
185/// remote reaches this verb only through `--forge`, and acting on the
186/// wrong host is worse than saying plainly that this verb carries one.
187fn reachable(forge: Forge, host: Option<&str>) -> Result<(), RkError> {
188    let Some(host) = host else { return Ok(()) };
189    if forge != Forge::Github || host.eq_ignore_ascii_case("github.com") {
190        return Ok(());
191    }
192    Err(RkError::refusal(
193        Diagnostic::new(
194            Reason::ForgeUnsupported,
195            format!("this clone's origin is {host}, and rk issue start reaches github.com alone"),
196        )
197        .expected("a github.com remote, or a GitLab project")
198        .action(
199            "start the branch with gh issue develop --repo <host>/<owner>/<name>, then rk worktree add it",
200        )
201        .target_state("unchanged"),
202    ))
203}
204
205/// Read the target, the reference, and the recorded mode. Nothing here
206/// touches the network, so every refusal below costs one local read.
207fn ground(
208    target: &Utf8Path,
209    reference: &issue::Reference,
210    overrides: &Overrides<'_>,
211) -> Result<Ground, RkError> {
212    if !target.is_dir() {
213        return Err(RkError::missing(
214            Diagnostic::new(
215                Reason::TargetNotFound,
216                format!("target {target} is not a directory"),
217            )
218            .expected("an existing repository to act on"),
219        ));
220    }
221    let named = overrides
222        .forge
223        .map(|name| {
224            Forge::parse(name).ok_or_else(|| {
225                RkError::Usage(format!(
226                    "unknown forge '{name}'; the forges are: github, gitlab"
227                ))
228            })
229        })
230        .transpose()?;
231    let detected = detect::detect(target.as_std_path());
232    // The reference is held to the clone before anything else: an agent
233    // pasting a URL while sitting in another checkout would otherwise
234    // mint on one project and seat in another.
235    issue::agrees(reference, &detected).map_err(RkError::Usage)?;
236    let Some(forge) = named.or(detected.forge) else {
237        let diagnostic = detected
238            .host
239            .as_ref()
240            .map_or_else(
241                || {
242                    Diagnostic::new(
243                        Reason::ForgeUndetected,
244                        "no forge detected: the target has no origin remote",
245                    )
246                },
247                |host| {
248                    Diagnostic::new(
249                        Reason::ForgeUndetected,
250                        format!("no forge detected: the host {host} is not recognized"),
251                    )
252                },
253            )
254            .expected("a github.com or gitlab remote, or an override")
255            .action("pass --forge <github|gitlab>, and --repo <path> if the remote is absent");
256        return Err(if detected.host.is_some() {
257            RkError::refusal(diagnostic)
258        } else {
259            RkError::missing(diagnostic)
260        });
261    };
262    // The reference names a host too, and it is authoritative where the
263    // clone has none: an issue URL for another host must not be acted on
264    // at the CLI's default one.
265    reachable(
266        forge,
267        detected.host.as_deref().or(reference.host.as_deref()),
268    )?;
269    // An override supplies a coordinate detection could not, and never
270    // replaces one it could: minting on the project the operator named
271    // and seating the branch in the clone they are standing in is the
272    // same cross-project mistake the reference check refuses.
273    contradicts(
274        "forge",
275        named.map(Forge::as_str),
276        detected.forge.map(Forge::as_str),
277    )?;
278    let Some(repo) = overrides
279        .repo
280        .map(str::to_owned)
281        .or_else(|| reference.repo.clone())
282        .or_else(|| detected.repo.clone())
283    else {
284        return Err(RkError::missing(
285            Diagnostic::new(
286                Reason::ForgeUndetected,
287                "no repository detected: the target has no origin remote",
288            )
289            .expected("an origin remote naming the project")
290            .action("pass --repo <owner/name>"),
291        ));
292    };
293    contradicts("repository", Some(repo.as_str()), detected.repo.as_deref())?;
294    contradicts("repository", Some(repo.as_str()), reference.repo.as_deref())?;
295    let (workflow, workflow_source) = mode_of(target, overrides.workflow)?;
296    Ok(Ground {
297        forge,
298        repo,
299        api_host: reference.host.clone(),
300        workflow,
301        workflow_source,
302    })
303}
304
305/// Mint the issue's branch at the forge and seat it.
306fn start(
307    target: &Utf8Path,
308    reference: &str,
309    overrides: &Overrides<'_>,
310    apply: bool,
311    out: Output,
312) -> Result<(), RkError> {
313    let reference = issue::parse_reference(reference).map_err(RkError::Usage)?;
314    let ground = ground(target, &reference, overrides)?;
315    // The target is a repository before anything reaches the network.
316    // Without this, a mint could succeed and the run then fail on a
317    // local prerequisite, leaving a remote branch no report accounts for.
318    let main = crate::commands::worktree::main_checkout(target)?;
319    // The gate before the first forge call, so a stale CLI costs one
320    // local process rather than a half-finished remote change.
321    probes::require_forge_cli(ground.forge)?;
322    let cli = resolve_cli(ground.forge)?;
323    // Where the forge lets rk know the name before it writes, every
324    // local refusal the seat carries runs first.
325    let seatable = |branch: &str| -> Result<(), RkError> {
326        match ground.workflow {
327            Workflow::Worktree => {
328                crate::commands::worktree::plan_seat(target, branch, overrides.base, false)
329                    .map(|_| ())
330            }
331            Workflow::Branches => branch_seatable(&main, branch),
332        }
333    };
334    let resolved = issue::resolve(
335        &cli,
336        target.as_std_path(),
337        &issue::Ask {
338            forge: ground.forge,
339            repo: &ground.repo,
340            reference: &reference,
341            host: ground.api_host.as_deref(),
342            base: overrides.base,
343            apply,
344            seatable: &seatable,
345        },
346    )?;
347    match ground.workflow {
348        Workflow::Worktree => seat_worktree(target, &ground, &resolved, overrides.base, apply, out),
349        Workflow::Branches => seat_branch(&main, &ground, &resolved, apply, out),
350    }
351}
352
353/// Worktree mode: the derived sibling path, through the same planning and
354/// the same refusals `rk worktree add` uses.
355fn seat_worktree(
356    target: &Utf8Path,
357    ground: &Ground,
358    resolved: &Resolved,
359    base: Option<&str>,
360    apply: bool,
361    out: Output,
362) -> Result<(), RkError> {
363    let Some(branch) = resolved.branch.as_deref() else {
364        return report(out, ground, resolved, None, None, apply);
365    };
366    let seat = crate::commands::worktree::plan_seat(target, branch, base, apply)?;
367    let mut note = None;
368    let path = match seat {
369        crate::commands::worktree::Seat::Satisfied { path } => path,
370        crate::commands::worktree::Seat::Fresh {
371            path,
372            source,
373            detail,
374        } => {
375            // An apply refreshes first, and `detail` is set only where
376            // that refresh failed. A remote-tracking ref left over from
377            // an older fetch is not the tip the forge holds now, so it
378            // is not something to seat from and call success.
379            if apply {
380                if let Some(why) = detail {
381                    return Err(stale_refs(branch, resolved, &why));
382                }
383            }
384            // The forge holds this branch, so the seat comes from its
385            // real tip — an adopted local branch, or the remote-tracking
386            // ref. Anything else would build a same-named branch sharing
387            // none of the forge's history. A preview says so, because it
388            // does not fetch and the refs it reads may simply be stale;
389            // an apply fetched first, so there it is a refusal.
390            if !matches!(source.kind, "adopted" | "remote") {
391                if apply {
392                    return Err(unreachable_tip(branch, resolved));
393                }
394                note = Some(format!(
395                    "origin/{branch} is not in this clone yet; the apply fetches first, and refuses rather than seat a branch from the trunk"
396                ));
397            }
398            if apply {
399                crate::commands::worktree::create_seat(target, &source)?;
400            }
401            path
402        }
403    };
404    report_with(out, ground, resolved, Some(path), None, apply, note)
405}
406
407/// The branch the forge holds is not reachable locally, so no seat is
408/// made from something else that happens to share its name.
409fn unreachable_tip(branch: &str, resolved: &Resolved) -> RkError {
410    RkError::refusal(
411        Diagnostic::new(
412            Reason::StateDrift,
413            format!("the forge carries {branch} and this clone cannot reach its tip"),
414        )
415        .expected(format!(
416            "origin/{branch} present, or {branch} already local"
417        ))
418        .action("git fetch origin, then rerun")
419        .target_state(format!(
420            "unchanged; issue #{} keeps its branch at the forge",
421            resolved.number
422        )),
423    )
424}
425
426/// What the branches-mode checkout needs, checked before the forge is
427/// written to.
428///
429/// `git switch` refuses a branch another worktree has checked out, and it
430/// refuses to move a working tree whose changes it would lose. Both are
431/// knowable here, and a branch created at the forge and then refused
432/// locally is a remote change no report accounts for.
433///
434/// The second check is deliberately stricter than git, which permits a
435/// switch whose changes do not conflict. Whether they conflict is not
436/// knowable without doing the switch, and this mode seats a branch the
437/// operator is about to start work on: a clean checkout is what that
438/// asks for, and the remedy is one command.
439fn branch_seatable(main: &Utf8Path, branch: &str) -> Result<(), RkError> {
440    let trunk = crate::config::trunk_of(main.as_std_path())?;
441    if let Some(seat) = crate::commands::worktree::seat_of(main, branch)? {
442        if seat != main {
443            return Err(RkError::refusal(
444                Diagnostic::new(
445                    Reason::StateDrift,
446                    format!(
447                        "branch {branch} is checked out at {seat}, and one branch has one seat"
448                    ),
449                )
450                .expected("the branch free, or already in the main checkout")
451                .action(format!("git -C {seat} switch {trunk}, then rerun"))
452                .target_state("unchanged"),
453            ));
454        }
455        // The branch is already seated here, so nothing is checked out
456        // over anything: the switch is a no-op and carries no risk.
457        return Ok(());
458    }
459    let held = crate::commands::worktree::git(main, &["status", "--porcelain"])?;
460    // A probe that cannot answer counts as dirty: this runs before a
461    // remote write, so the closed direction is the safe one.
462    if !held.status.success() || !held.stdout.is_empty() {
463        return Err(RkError::refusal(
464            Diagnostic::new(
465                Reason::StateDrift,
466                format!("{main} carries uncommitted work, and this mode checks {branch} out there"),
467            )
468            .expected("a clean main checkout to seat the branch in")
469            .action("commit or stash the work, then rerun")
470            .target_state("unchanged"),
471        ));
472    }
473    Ok(())
474}
475
476/// The refresh failed, so no local ref is proof of what the forge holds.
477fn stale_refs(branch: &str, resolved: &Resolved, why: &str) -> RkError {
478    RkError::refusal(
479        Diagnostic::new(
480            Reason::StateDrift,
481            format!(
482                "this clone could not refresh from the forge, so its {branch} may be stale: {why}"
483            ),
484        )
485        .expected("a fetch that answered, so the seat starts from the tip the forge holds")
486        .action("git fetch origin, then rerun")
487        .target_state(format!(
488            "unchanged; issue #{} keeps its branch at the forge",
489            resolved.number
490        )),
491    )
492}
493
494/// Branches mode: the branch checked out in the main checkout.
495///
496/// The main checkout is where this mode works branches, so the switch
497/// runs there whichever of the repository's worktrees `--target` named.
498///
499/// Both forges create the branch on the remote, so an apply always sees
500/// the same case — a remote tip with no local branch — unless a previous
501/// run already made one.
502fn seat_branch(
503    main: &Utf8Path,
504    ground: &Ground,
505    resolved: &Resolved,
506    apply: bool,
507    out: Output,
508) -> Result<(), RkError> {
509    let Some(branch) = resolved.branch.as_deref() else {
510        return report(out, ground, resolved, None, None, apply);
511    };
512    if !apply {
513        return report(out, ground, resolved, None, Some(branch.to_owned()), false);
514    }
515    // Through the shared runner, which scrubs the hook variables: a run
516    // from inside a git hook must act on the named checkout and never on
517    // the hook's own repository.
518    let git = |args: &[&str]| crate::commands::worktree::git(main, args);
519    let fetched = git(&["fetch", "origin"])?;
520    if !fetched.status.success() {
521        return Err(stale_refs(branch, resolved, &last_line(&fetched.stderr)));
522    }
523    let local = git(&[
524        "rev-parse",
525        "--verify",
526        "--quiet",
527        "--end-of-options",
528        &format!("refs/heads/{branch}^{{commit}}"),
529    ])?;
530    let switched = if local.status.success() {
531        git(&["switch", branch])?
532    } else {
533        git(&[
534            "switch",
535            "--track",
536            "-c",
537            branch,
538            &format!("refs/remotes/origin/{branch}"),
539        ])?
540    };
541    if !switched.status.success() {
542        // git refuses a dirty switch itself, so its own last line is the
543        // honest reason rather than one rk invents.
544        return Err(RkError::subprocess(
545            Diagnostic::new(
546                Reason::SubprocessFailed,
547                format!(
548                    "git refused to check out {branch}: {}",
549                    last_line(&switched.stderr)
550                ),
551            )
552            .expected("a working tree the checkout can move")
553            .target_state("the branch exists on the forge and is not checked out here"),
554        ));
555    }
556    report(out, ground, resolved, None, Some(branch.to_owned()), true)
557}
558
559/// One report, in either mode.
560fn report(
561    out: Output,
562    ground: &Ground,
563    resolved: &Resolved,
564    path: Option<Utf8PathBuf>,
565    checkout: Option<String>,
566    apply: bool,
567) -> Result<(), RkError> {
568    report_with(out, ground, resolved, path, checkout, apply, None)
569}
570
571/// [`report`], carrying a note the seating step raised.
572fn report_with(
573    out: Output,
574    ground: &Ground,
575    resolved: &Resolved,
576    path: Option<Utf8PathBuf>,
577    checkout: Option<String>,
578    apply: bool,
579    note: Option<String>,
580) -> Result<(), RkError> {
581    let mode = if apply { "apply" } else { "preview" };
582    out.result_line(format!("issue:  #{} {}", resolved.number, resolved.title));
583    out.result_line(format!(
584        "branch: {}  ({})",
585        resolved.branch.as_deref().unwrap_or("named by the forge"),
586        match resolved.origin {
587            "already" => "already linked at the forge",
588            "forge" => "minted at the forge",
589            _ => "not minted yet",
590        }
591    ));
592    out.result_line(format!(
593        "seat:   {} ({} says so)",
594        path.as_ref().map_or_else(
595            || checkout.as_deref().map_or_else(
596                || "unknown".to_owned(),
597                |branch| format!("checkout {branch}")
598            ),
599            ToString::to_string
600        ),
601        ground.workflow_source
602    ));
603    if !resolved.others.is_empty() {
604        out.warn(format!(
605            "the issue carries other linked branches, and the first was taken: {}",
606            resolved.others.join(", ")
607        ));
608    }
609    let detail = match (resolved.detail.clone(), note) {
610        (Some(had), Some(note)) => Some(format!("{had}; {note}")),
611        (Some(one), None) | (None, Some(one)) => Some(one),
612        (None, None) => None,
613    };
614    if let Some(detail) = &detail {
615        out.warn(detail);
616    }
617    let next = next_lines(ground, resolved, path.as_ref(), apply);
618    out.next(&next);
619    out.emit(&StartReport {
620        schema: "rk.issue-start/1",
621        mode,
622        forge: ground.forge.as_str(),
623        repo: ground.repo.clone(),
624        issue: resolved.number,
625        title: resolved.title.clone(),
626        branch: resolved.branch.clone(),
627        origin: resolved.origin,
628        workflow: ground.workflow.as_str(),
629        path: path.map(|path| path.to_string()),
630        checkout,
631        others: resolved.others.clone(),
632        detail,
633        next,
634    })
635}
636
637/// What to run next, which differs by mode and by whether this ran.
638fn next_lines(
639    ground: &Ground,
640    resolved: &Resolved,
641    path: Option<&Utf8PathBuf>,
642    apply: bool,
643) -> Vec<String> {
644    if !apply {
645        return vec![format!(
646            "rk issue start {} --apply mints the branch and seats it",
647            resolved.number
648        )];
649    }
650    match (ground.workflow, path) {
651        (Workflow::Worktree, Some(path)) => vec![
652            format!("cd {path}"),
653            "rk worktree list reports every seat".to_owned(),
654        ],
655        _ => vec!["rk status reports what this target carries".to_owned()],
656    }
657}
658
659/// The last non-empty stderr line, for a one-line reason.
660fn last_line(bytes: &[u8]) -> String {
661    String::from_utf8_lossy(bytes)
662        .lines()
663        .rev()
664        .find(|line| !line.trim().is_empty())
665        .unwrap_or("no output")
666        .to_owned()
667}