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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
use git_url_parse::GitUrl;
use thiserror::Error;

use crate::options::Opt;

#[macro_use]
extern crate log;

mod git;
pub mod options;

const BITBUCKET_HOSTNAME: &str = "bitbucket.org";
const GITHUB_HOSTNAME: &str = "github.com";
const GITLAB_HOSTNAME: &str = "gitlab.com";
const GITEA_HOSTNAME: &str = "gitea.io";

#[derive(Debug, Eq, Error, PartialEq, Clone)]
pub enum Issue {
    #[error("Command failed, please run command inside a git directory")]
    NotInAGitRepository,
    #[error("No matching remote url found for '{0}' remote name")]
    NoRemoteMatching(String),
    #[error("No remote available")]
    NoRemoteAvailable,
    #[error("Not able to open system browser")]
    NotAbleToOpenSystemBrowser,
    #[error("Unable to open browser '{0}'")]
    BrowserNotAvailable(String),
    #[error("Unable to get remote parts, please open an issue as it might come from the code")]
    UnableToGetRemoteParts,
    #[error("Unknown provider")]
    UnknownProvider,
}

pub struct Success;
type Result = core::result::Result<Success, Issue>;

impl Issue {
    pub fn exit_code(&self) -> i32 {
        match self {
            Self::NotInAGitRepository => 1,
            Self::NoRemoteMatching(..) => 2,
            Self::NoRemoteAvailable => 3,
            Self::NotAbleToOpenSystemBrowser => 4,
            Self::BrowserNotAvailable(..) => 5,
            Self::UnableToGetRemoteParts => 6,
            Self::UnknownProvider => 7,
        }
    }
}

enum GitProvider {
    GitHub,
    GitLab,
    Bitbucket,
    Gitea,
}

impl Default for GitProvider {
    fn default() -> Self {
        Self::GitHub
    }
}

impl GitProvider {
    fn hostname(&self) -> String {
        match self {
            Self::GitHub => GITHUB_HOSTNAME,
            Self::GitLab => GITLAB_HOSTNAME,
            Self::Bitbucket => BITBUCKET_HOSTNAME,
            Self::Gitea => GITEA_HOSTNAME,
        }
        .to_string()
    }
}

pub struct RemoteParts {
    domain: String,
    repository: String,
}

struct MergeRequestParts {
    path: String,
    tail: String,
}

const DEFAULT_REMOTE_ORIGIN: &str = "origin";

fn get_remote_parts(url: &str) -> anyhow::Result<RemoteParts> {
    let giturl = GitUrl::parse(url).map_err(|_| Issue::UnableToGetRemoteParts)?;

    let domain = giturl
        .host
        .map_or(GitProvider::GitHub.hostname(), |m| m.as_str().to_string());

    let repository = giturl
        .path
        .replace(".git", "") // don't want the .git part
        .split('/')
        .filter(|s| !s.is_empty())
        .collect::<Vec<&str>>()
        .join("/");

    Ok(RemoteParts { domain, repository })
}

fn get_merge_request_parts(domain: &str) -> anyhow::Result<MergeRequestParts, Issue> {
    match domain {
        GITHUB_HOSTNAME => Ok(MergeRequestParts {
            path: "pulls".to_string(),
            tail: "".to_string(),
        }),
        GITLAB_HOSTNAME => Ok(MergeRequestParts {
            path: "-/merge_requests".to_string(),
            tail: "".to_string(),
        }),
        BITBUCKET_HOSTNAME => Ok(MergeRequestParts {
            path: "pull-requests".to_string(),
            tail: "".to_string(),
        }),
        GITEA_HOSTNAME => Ok(MergeRequestParts {
            path: "pulls".to_string(),
            tail: "".to_string(),
        }),
        _ => Err(Issue::UnknownProvider),
    }
}

pub fn run(opt: Opt) -> Result {
    // let logger = logger::Logger::new(opt.verbose);
    debug!("Verbose mode is active");

    let repo = git::get_repo()?;

    // Get the tag to show in the browser. If the option is given, then the value
    // will be used as it is an alias for branch.
    let reference = if let Some(tag) = opt.tag {
        tag
    } else {
        // Get the branch to show in the browser. If the option is given, then, the
        // value will be used, else, the current branch is given, or master if
        // something went wrong.
        opt.branch.unwrap_or_else(|| {
            debug!("No branch given, getting current one");
            git::get_branch(&repo)
        })
    };

    let remote_name = &opt
        .remote
        .unwrap_or_else(|| String::from(DEFAULT_REMOTE_ORIGIN));

    debug!("Getting remote url for '{}' remote name", remote_name);

    let optional_remote = repo
        .find_remote(remote_name)
        .map_err(|_| Issue::NoRemoteMatching(remote_name.clone()))?;

    let remote_url = optional_remote
        .url()
        .ok_or(())
        .map_err(|_| Issue::NoRemoteAvailable)?;

    let RemoteParts { domain, repository } = get_remote_parts(remote_url).unwrap();

    let (path, tail) = if let Some(commit) = opt.commit {
        let path = if domain == GitProvider::Bitbucket.hostname() {
            "commits"
        } else {
            "commit"
        };

        (path, commit)
    } else {
        let path = if domain == GitProvider::Bitbucket.hostname() {
            "src"
        } else {
            "tree"
        };

        (path, reference)
    };

    let (path, tail) = if opt.merge_request {
        debug!("Getting merge request parts for domain '{}'", domain);
        let MergeRequestParts { path, tail } = get_merge_request_parts(&domain).unwrap();
        (path, tail)
    } else {
        (path.to_owned(), tail)
    };

    // Generate the requested url that has to be opened in the browser
    let url = generate_url(&domain, &repository, &path, &tail);

    // If the option is available through the command line, open the given one
    match opt.browser {
        Some(option_browser) => {
            debug!("Browser '{}' given as option", option_browser);

            if option_browser == *"" {
                println!("{}", url);
            }

            open::with(&url, &option_browser)
                .map_err(|_| Issue::BrowserNotAvailable(option_browser))?;

            Ok(Success)
        }
        None => {
            // Open the default web browser on the current system.
            match open::that(&url) {
                Ok(_) => {
                    debug!("Default browser is now opened");
                    Ok(Success)
                }
                Err(_) => Err(Issue::NotAbleToOpenSystemBrowser),
            }
        }
    }
}

fn generate_url(domain: &str, repository: &str, path: &str, tail: &str) -> String {
    format!(
        "https://{domain}/{repository}/{path}/{tail}",
        domain = domain,
        path = path,
        repository = repository,
        tail = tail
    )
}

#[cfg(test)]
mod tests {
    // Note this useful idiom: importing names from outer (for mod tests) scope.
    use super::*;

    #[test]
    fn test_without_ssh_git_and_without_extension_url_parts() {
        let RemoteParts { domain, repository } =
            get_remote_parts("git@github.com:yoannfleurydev/gitweb").unwrap();

        assert_eq!(domain, "github.com");
        assert_eq!(repository, "yoannfleurydev/gitweb");
    }

    #[test]
    fn test_without_ssh_git_url_parts() {
        let RemoteParts { domain, repository } =
            get_remote_parts("git@github.com:yoannfleurydev/gitweb.git").unwrap();

        assert_eq!(domain, "github.com");
        assert_eq!(repository, "yoannfleurydev/gitweb");
    }

    #[test]
    fn test_with_ssh_and_multiple_subgroups_git_url_parts() {
        let RemoteParts { domain, repository } =
            get_remote_parts("ssh://git@gitlab.com/group/subgroup/subsubgroup/design-system.git")
                .unwrap();

        assert_eq!(domain, "gitlab.com");
        assert_eq!(repository, "group/subgroup/subsubgroup/design-system");
    }

    #[test]
    fn test_with_ssh_and_port_git_url_parts() {
        let RemoteParts { domain, repository } =
            get_remote_parts("ssh://user@host.xz:22/path/to/repo.git/").unwrap();

        assert_eq!(domain, "host.xz");
        assert_eq!(repository, "path/to/repo");
    }

    #[test]
    fn test_with_http_and_port_git_url_parts() {
        let RemoteParts { domain, repository } =
            get_remote_parts("http://host.xz:80/path/to/repo.git/").unwrap();

        assert_eq!(domain, "host.xz");
        assert_eq!(repository, "path/to/repo");
    }

    #[test]
    fn test_with_http_dash_and_port_git_url_parts() {
        let RemoteParts { domain, repository } =
            get_remote_parts("http://host-dash.xz:80/path/to/repo.git/").unwrap();

        assert_eq!(domain, "host-dash.xz");
        assert_eq!(repository, "path/to/repo");
    }

    #[test]
    fn test_with_http_git_url_parts() {
        let RemoteParts { domain, repository } =
            get_remote_parts("https://host.xz/path/to/repo.git/").unwrap();

        assert_eq!(domain, "host.xz");
        assert_eq!(repository, "path/to/repo");
    }

    #[test]
    fn test_get_merge_request_parts_with_github() {
        let MergeRequestParts { path, tail } = get_merge_request_parts(GITHUB_HOSTNAME).unwrap();

        assert_eq!(path, "pulls");
        assert_eq!(tail, "");
    }

    #[test]
    fn test_get_merge_request_parts_with_gitlab() {
        let MergeRequestParts { path, tail } = get_merge_request_parts(GITLAB_HOSTNAME).unwrap();

        assert_eq!(path, "-/merge_requests");
        assert_eq!(tail, "");
    }

    #[test]
    fn test_get_merge_request_parts_with_bitbucket() {
        let MergeRequestParts { path, tail } = get_merge_request_parts(BITBUCKET_HOSTNAME).unwrap();

        assert_eq!(path, "pull-requests");
        assert_eq!(tail, "");
    }

    #[test]
    fn test_get_merge_request_parts_with_gitea() {
        let MergeRequestParts { path, tail } = get_merge_request_parts(GITEA_HOSTNAME).unwrap();

        assert_eq!(path, "pulls");
        assert_eq!(tail, "");
    }

    #[test]
    fn test_get_merge_request_parts_with_unknown_provider() {
        let result = get_merge_request_parts("host.xz");

        assert_eq!(result.err(), Some(Issue::UnknownProvider));
    }
}