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
// Copyright (c) 2018-2019, Wayfair LLC
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
// following conditions are met:
//
//  * Redistributions of source code must retain the above copyright notice, this list of conditions and the following
//    disclaimer.
//  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
//    following disclaimer in the documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
// BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
// OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

//! `what_git` provides an easy mechanism for associating a given Git repository URL with its source. It supports
//! either GitHub, GitHub Enterprise, Gitlab, or Gitlab Enterprise repositories. Use this crate to structure
//! SCM-agnostic code with minimal branching.
//!
//! # About
//!
//! `what_git` associates a repository URL to a known repository source. All you need is an HTTP or Git URL,
//! and a personal access token to the API service your repository is associated with. Provide each of those to the
//! [`what_git::what_git`] function, and that's it.
//!
//!
//! [`what_git::what_git`]: ./fn.what_git.html

use reqwest::header;
use reqwest::{Client, Url};
use std::env;
use std::error;
use std::fmt;
use std::result;

/// Determines what source control management (SCM) solution a repository URL belongs to. Returns a
/// [`what_git::Result`] type describing the structure of the URL and the associated [`what_git::SCMKind`], or some
/// error of type [`what_git::Error`].
///
/// # Examples
///
/// ```no_run
/// use what_git::{what_git, SCMKind};
///
/// async {
///     let scm = what_git("https://github.com/rust-lang/rust", "<PERSONAL ACCESS TOKEN>")
///         .await
///         .map(|scm| match scm.kind {
///             SCMKind::GitHub => println!("Do something with GitHub..."),
///             _ => unimplemented!(),
///         });
/// };
/// ```
/// [`what_git::Result`]: ./type.Result.html
/// [`what_git::SCMKind`]: ./enum.SCMKind.html
/// [`what_git::Error`]: ./enum.Error.html
pub async fn what_git(repository: &str, token: &str) -> Result {
    let url_str = scrub_git_url_if_needed(repository);
    let url = Url::parse(&url_str).map_err(|_| Error::UnknownProvider(url_str.to_string()))?;
    metadata_for_url(&url, token).await
}

/// Remove various non-standard decorations, such as SSH decorations, from a URL string to get a string conforming to
/// the [URL Standard](http://url.spec.whatwg.org/).
fn scrub_git_url_if_needed(repository: &str) -> String {
    if repository.starts_with("git@") {
        repository
            .replacen(":", "/", 1)
            .replacen("git@", "git://", 1)
    } else {
        repository.to_string()
    }
}

/// Determines what source control management (SCM) solution a repository URL belongs to. Returns a [`what_git::Result`]
/// type describing the structure of the URL and the associated [`what_git::SCMKind`], or some error of type
/// [`what_git::Error`].
async fn metadata_for_url(url: &Url, token: &str) -> Result {
    // Extract the first two path components in the URL to guess at the repository owner and name.
    let path_components = url
        .path_segments()
        .expect(
            "URL path components could not be represented.
    This is likely because it is not a valid URL for this tool.",
        )
        .take(2)
        .collect::<Vec<&str>>();
    let (owner, mut repo) = if let [own, rep] = path_components[..] {
        (own, rep)
    } else {
        return Err(Error::UnknownProvider(url.to_string()));
    };
    if let Some(idx) = repo.rfind(".git") {
        repo = &repo[..idx];
    }

    // Extract the hostname
    let hostname = url
        .domain()
        .ok_or_else(|| Error::UnknownProvider(url.to_string()))?;

    let base_url: String;
    let kind: SCMKind;

    if hostname == "github.com" || hostname == "www.github.com" {
        // 1. If the repository is located on GitHub.com, proceed
        base_url = "https://api.github.com".to_string();
        kind = SCMKind::GitHub;
    } else if hostname == "gitlab.com" || hostname == "www.gitlab.com" {
        // 2. If the repository is located on Gitlab.com, proceed
        base_url = "https://gitlab.com".to_string();
        kind = SCMKind::Gitlab;
    } else if let Ok(base) = env::var("GITHUB_BASE_URL") {
        // 3. If the user has manually specified an API base URL for a GitHub repository, proceed
        base_url = base;
        kind = SCMKind::GitHub;
    } else if let Ok(base) = env::var("GITLAB_BASE_URL") {
        // 4. If the user has manually specified an API base URL for a Gitlab repository, proceed
        base_url = base;
        kind = SCMKind::GitHub;
    } else {
        // 5. Attempt to connect to an SCM's API using known unique endpoints, and match on the possible successes.
        let base_url_candidate = format!("https://{}", hostname);
        let github_result = verify_github(&base_url_candidate, token).await;
        let gitlab_result = verify_gitlab(&base_url_candidate, token).await;
        match (github_result, gitlab_result) {
            (Ok(true), _) => {
                base_url = format!("{}/api/v3", base_url_candidate);
                kind = SCMKind::GitHub;
            }
            (_, Ok(true)) => {
                base_url = base_url_candidate;
                kind = SCMKind::Gitlab;
            }
            _ => return Err(Error::UnknownProvider(url.to_string())),
        };
    }
    Ok(SCM {
        base_url,
        kind,
        owner: owner.to_string(),
        repo: repo.to_string(),
    })
}

// Attempt to connect to the GitHub `/zen` endpoint, which is unique to GitHub's API.
async fn verify_github(base_url: &str, token: &str) -> result::Result<bool, reqwest::Error> {
    let url = format!("{}/api/v3/zen", base_url);

    Client::new()
        .get(&*url)
        .header(header::ACCEPT, "application/vnd.github.v3+json")
        .header(header::AUTHORIZATION, format!("Bearer {}", token))
        .header(header::USER_AGENT, "com.wayfair.what_gitjson")
        .send()
        .await
        .map(|res| res.status().is_success())
}

// Attempt to connect to the Gitlab `/version` endpoint, which is unique to Gitlab's API.
async fn verify_gitlab(base_url: &str, token: &str) -> result::Result<bool, reqwest::Error> {
    let url = format!("{}/api/v4/version", base_url);

    Client::new()
        .get(&*url)
        .header("private-token", token)
        .send()
        .await
        .map(|res| res.status().is_success())
}

/// Used to describe the structure of a repository on a supported source control management (SCM) solution.
#[derive(Debug)]
pub struct SCM {
    pub kind: SCMKind,
    /// The base URL used in API calls
    pub base_url: String,
    /// The user or organization space that owns the repository
    pub owner: String,
    /// The name of the repository
    pub repo: String,
}

/// Supported SCMs. Currently, `what_git` only supports GitHub and Gitlab.
#[derive(Debug, PartialEq)]
pub enum SCMKind {
    Unsupported,
    GitHub,
    Gitlab,
}

pub type Result = result::Result<SCM, Error>;

#[derive(Debug)]
pub enum Error {
    UnknownProvider(String),
}

impl error::Error for Error {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        match *self {
            Error::UnknownProvider(_) => None,
        }
    }
}
impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Error::UnknownProvider(ref url) => write!(f, "Unknown provider for url {}", url),
        }
    }
}

mod tests {

    #[test]
    fn test_scrub_git_url() {
        assert_eq!(
            super::scrub_git_url_if_needed("file:///Users/wayfair/foxdie.git"),
            "file:///Users/wayfair/foxdie.git"
        );
        assert_eq!(
            super::scrub_git_url_if_needed("https://github.com/wayfair/foxdie"),
            "https://github.com/wayfair/foxdie"
        );
        assert_eq!(
            super::scrub_git_url_if_needed("https://github.com/wayfair/foxdie.git"),
            "https://github.com/wayfair/foxdie.git"
        );
        assert_eq!(
            super::scrub_git_url_if_needed("git@github.com:wayfair/foxdie"),
            "git://github.com/wayfair/foxdie"
        );
        assert_eq!(
            super::scrub_git_url_if_needed("git@github.com:wayfair/foxdie.git"),
            "git://github.com/wayfair/foxdie.git"
        );
    }
}