1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use clap::Parser;

#[derive(Parser)]
pub struct BrowseCommand {
    #[clap(subcommand)]
    subcommand: Option<BrowseSubcommand>,
}

#[derive(Parser)]
enum BrowseSubcommand {
    #[clap(about = "Open the repo using your browser")]
    Repo,
    #[clap(name = "mr", about = "Open the merge requests using your browser")]
    MergeRequest(MergeRequestBrowse),
    #[clap(name = "pp", about = "Open the ci/cd pipelines using your browser")]
    Pipelines,
}

impl From<MergeRequestBrowse> for BrowseOptions {
    fn from(options: MergeRequestBrowse) -> Self {
        match options.id {
            Some(id) => BrowseOptions::MergeRequestId(id),
            None => BrowseOptions::MergeRequests,
        }
    }
}

impl From<BrowseCommand> for BrowseOptions {
    fn from(options: BrowseCommand) -> Self {
        match options.subcommand {
            Some(BrowseSubcommand::Repo) => BrowseOptions::Repo,
            Some(BrowseSubcommand::MergeRequest(options)) => options.into(),
            Some(BrowseSubcommand::Pipelines) => BrowseOptions::Pipelines,
            // defaults to open repo in browser
            None => BrowseOptions::Repo,
        }
    }
}

#[derive(Debug, PartialEq)]
pub enum BrowseOptions {
    // defaults to open repo in browser
    Repo,
    MergeRequests,
    MergeRequestId(i64),
    Pipelines,
    Manual,
}

#[derive(Parser)]
struct MergeRequestBrowse {
    /// Open merge/pull request id in the browser
    #[clap()]
    pub id: Option<i64>,
}

#[cfg(test)]
mod test {

    use super::*;
    use crate::cli::{Args, Command};

    #[test]
    fn test_browse_command_repo() {
        let args = Args::parse_from(vec!["gr", "br", "repo"]);
        match args.command {
            Command::Browse(BrowseCommand {
                subcommand: Some(BrowseSubcommand::Repo),
            }) => {}
            _ => panic!("Expected Repo BrowseCommand"),
        }
    }

    #[test]
    fn test_browse_command_mr() {
        let args = Args::parse_from(vec!["gr", "br", "mr"]);
        let mr_browse = match args.command {
            Command::Browse(BrowseCommand {
                subcommand: Some(BrowseSubcommand::MergeRequest(options)),
            }) => {
                assert_eq!(options.id, None);
                options
            }
            _ => panic!("Expected MergeRequest BrowseCommand"),
        };
        let options: BrowseOptions = mr_browse.into();
        assert_eq!(options, BrowseOptions::MergeRequests);
    }

    #[test]
    fn test_browse_command_mr_id() {
        let args = Args::parse_from(vec!["gr", "br", "mr", "1"]);
        let mr_browse = match args.command {
            Command::Browse(BrowseCommand {
                subcommand: Some(BrowseSubcommand::MergeRequest(options)),
            }) => {
                assert_eq!(options.id, Some(1));
                options
            }
            _ => panic!("Expected MergeRequest BrowseCommand"),
        };
        let options: BrowseOptions = mr_browse.into();
        assert_eq!(options, BrowseOptions::MergeRequestId(1));
    }

    #[test]
    fn test_browse_command_pipelines() {
        let args = Args::parse_from(vec!["gr", "br", "pp"]);
        match args.command {
            Command::Browse(BrowseCommand {
                subcommand: Some(BrowseSubcommand::Pipelines),
            }) => {}
            _ => panic!("Expected Pipelines BrowseCommand"),
        }
    }
}