Skip to main content

gr/cli/
browse.rs

1use clap::Parser;
2
3#[derive(Parser)]
4pub struct BrowseCommand {
5    #[clap(subcommand)]
6    subcommand: Option<BrowseSubcommand>,
7}
8
9#[derive(Parser)]
10enum BrowseSubcommand {
11    #[clap(about = "Open the repo using your browser")]
12    Repo,
13    #[clap(name = "mr", about = "Open the merge requests using your browser")]
14    MergeRequest(MergeRequestBrowse),
15    #[clap(name = "pp", about = "Open the ci/cd pipelines using your browser")]
16    Pipelines(PipelineBrowse),
17    #[clap(name = "rl", about = "Open the releases page using your browser")]
18    Release,
19}
20
21impl From<MergeRequestBrowse> for BrowseOptions {
22    fn from(options: MergeRequestBrowse) -> Self {
23        match options.id {
24            Some(id) => BrowseOptions::MergeRequestId(id),
25            None => BrowseOptions::MergeRequests,
26        }
27    }
28}
29
30impl From<PipelineBrowse> for BrowseOptions {
31    fn from(options: PipelineBrowse) -> Self {
32        match options.id {
33            Some(id) => BrowseOptions::PipelineId(id),
34            None => BrowseOptions::Pipelines,
35        }
36    }
37}
38
39impl From<BrowseCommand> for BrowseOptions {
40    fn from(options: BrowseCommand) -> Self {
41        match options.subcommand {
42            Some(BrowseSubcommand::Repo) => BrowseOptions::Repo,
43            Some(BrowseSubcommand::MergeRequest(options)) => options.into(),
44            Some(BrowseSubcommand::Pipelines(options)) => options.into(),
45            Some(BrowseSubcommand::Release) => BrowseOptions::Releases,
46            // defaults to open repo in browser
47            None => BrowseOptions::Repo,
48        }
49    }
50}
51
52#[derive(Debug, PartialEq)]
53pub enum BrowseOptions {
54    // defaults to open repo in browser
55    Repo,
56    MergeRequests,
57    MergeRequestId(i64),
58    Pipelines,
59    PipelineId(i64),
60    Releases,
61    Manual,
62}
63
64#[derive(Parser)]
65struct MergeRequestBrowse {
66    /// Open merge/pull request id in the browser
67    #[clap()]
68    pub id: Option<i64>,
69}
70
71#[derive(Parser)]
72struct PipelineBrowse {
73    /// Open pipeline id in the browser
74    #[clap()]
75    pub id: Option<i64>,
76}
77
78#[cfg(test)]
79mod test {
80
81    use super::*;
82    use crate::cli::{Args, Command};
83
84    #[test]
85    fn test_browse_command_repo() {
86        let args = Args::parse_from(vec!["gr", "br", "repo"]);
87        match args.command {
88            Command::Browse(BrowseCommand {
89                subcommand: Some(BrowseSubcommand::Repo),
90            }) => {}
91            _ => panic!("Expected Repo BrowseCommand"),
92        }
93    }
94
95    #[test]
96    fn test_browse_command_mr() {
97        let args = Args::parse_from(vec!["gr", "br", "mr"]);
98        let mr_browse = match args.command {
99            Command::Browse(BrowseCommand {
100                subcommand: Some(BrowseSubcommand::MergeRequest(options)),
101            }) => {
102                assert_eq!(options.id, None);
103                options
104            }
105            _ => panic!("Expected MergeRequest BrowseCommand"),
106        };
107        let options: BrowseOptions = mr_browse.into();
108        assert_eq!(options, BrowseOptions::MergeRequests);
109    }
110
111    #[test]
112    fn test_browse_command_mr_id() {
113        let args = Args::parse_from(vec!["gr", "br", "mr", "1"]);
114        let mr_browse = match args.command {
115            Command::Browse(BrowseCommand {
116                subcommand: Some(BrowseSubcommand::MergeRequest(options)),
117            }) => {
118                assert_eq!(options.id, Some(1));
119                options
120            }
121            _ => panic!("Expected MergeRequest BrowseCommand"),
122        };
123        let options: BrowseOptions = mr_browse.into();
124        assert_eq!(options, BrowseOptions::MergeRequestId(1));
125    }
126
127    #[test]
128    fn test_browse_command_pipelines() {
129        let args = Args::parse_from(vec!["gr", "br", "pp"]);
130        let pp_browse = match args.command {
131            Command::Browse(BrowseCommand {
132                subcommand: Some(BrowseSubcommand::Pipelines(options)),
133            }) => {
134                assert_eq!(options.id, None);
135                options
136            }
137            _ => panic!("Expected Pipelines BrowseCommand"),
138        };
139        let options: BrowseOptions = pp_browse.into();
140        assert_eq!(options, BrowseOptions::Pipelines);
141    }
142
143    #[test]
144    fn test_browse_command_pp_id() {
145        let args = Args::parse_from(vec!["gr", "br", "pp", "1"]);
146        let mr_browse = match args.command {
147            Command::Browse(BrowseCommand {
148                subcommand: Some(BrowseSubcommand::Pipelines(options)),
149            }) => {
150                assert_eq!(options.id, Some(1));
151                options
152            }
153            _ => panic!("Expected Pipeline BrowseCommand"),
154        };
155        let options: BrowseOptions = mr_browse.into();
156        assert_eq!(options, BrowseOptions::PipelineId(1));
157    }
158}