Skip to main content

git_stk/commands/
view.rs

1use anyhow::{Result, bail};
2use clap_complete::engine::ArgValueCompleter;
3
4use crate::commands::Run;
5use crate::completions;
6use crate::git;
7use crate::providers::detect_review_provider;
8use crate::style;
9
10/// Open a branch's review in the browser.
11#[derive(Debug, clap::Args)]
12pub struct View {
13    #[arg(add = ArgValueCompleter::new(completions::branch_candidates))]
14    branch: Option<String>,
15}
16
17impl Run for View {
18    fn run(self) -> Result<()> {
19        view(self.branch.as_deref())
20    }
21}
22
23fn view(branch: Option<&str>) -> Result<()> {
24    let branch = branch
25        .map(str::to_owned)
26        .map_or_else(git::current_branch, Ok)?;
27    let (provider, review_provider) = detect_review_provider()?;
28
29    // Closed-inclusive: opening a merged or closed review is still useful.
30    let Some(review) = review_provider.review_for_branch_including_closed(&branch)? else {
31        bail!(
32            "no {} review found for {branch}; submit it first with `git stk submit`",
33            provider.kind
34        );
35    };
36
37    anstream::println!(
38        "opening {} {} {}",
39        review.id,
40        style::state(&review.state),
41        style::dim(&review.url)
42    );
43    let output = review_provider.open_review(&review)?;
44    if !output.is_empty() {
45        println!("{output}");
46    }
47    Ok(())
48}