Skip to content

Commit

Permalink
fix: don't panic on git remotes w/no apparent owner
Browse files Browse the repository at this point in the history
  • Loading branch information
EverlastingBugstopper committed Aug 13, 2021
1 parent 50181e2 commit a95f998
Showing 1 changed file with 42 additions and 28 deletions.
70 changes: 42 additions & 28 deletions crates/rover-client/src/shared/git_context.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::env;
use std::{env, panic};

use git2::{Reference, Repository};
use git_url_parse::GitUrl;
Expand Down Expand Up @@ -103,35 +103,42 @@ impl GitContext {
// will return None
fn sanitize_remote_url(remote_url: &str) -> Option<String> {
// try to parse url into git info
let mut parsed_remote_url = match GitUrl::parse(remote_url) {
Ok(parsed_remote_url) => parsed_remote_url,
Err(_) => return None,
};

// return None for any remote that is not a supported host
if let Some(host) = &parsed_remote_url.host {
match host.as_str() {
"github.com" | "gitlab.com" | "bitbucket.org" => {}
_ => return None,
}
} else {
return None;
};

let optional_user = parsed_remote_url.user.clone();
parsed_remote_url = parsed_remote_url.trim_auth();

// if the user is "git" we can add back in the user. Otherwise, return
// the clean remote url
// this is done previously here:
// https://github.com/apollographql/apollo-tooling/blob/fd642ab59620cd836651dcab4c3ecbcbcca3f780/packages/apollo/src/git.ts#L49
if let Some(user) = &optional_user {
if user == "git" {
parsed_remote_url.user = optional_user;
}
};
// GitUrl::parse can panic, so we attempt to catch it and
// just return None if the parsing fails.

Some(parsed_remote_url.to_string())
let parsed_remote_url = panic::catch_unwind(|| GitUrl::parse(remote_url).ok())
.ok()
.flatten();

if let Some(mut parsed_remote_url) = parsed_remote_url {
// return None for any remote that is not a supported host
if let Some(host) = &parsed_remote_url.host {
match host.as_str() {
"github.com" | "gitlab.com" | "bitbucket.org" => {}
_ => return None,
}
} else {
return None;
};

let optional_user = parsed_remote_url.user.clone();
parsed_remote_url = parsed_remote_url.trim_auth();

// if the user is "git" we can add back in the user. Otherwise, return
// the clean remote url
// this is done previously here:
// https://github.com/apollographql/apollo-tooling/blob/fd642ab59620cd836651dcab4c3ecbcbcca3f780/packages/apollo/src/git.ts#L49
if let Some(user) = &optional_user {
if user == "git" {
parsed_remote_url.user = optional_user;
}
};

Some(parsed_remote_url.to_string())
} else {
None
}
}
}

Expand Down Expand Up @@ -299,4 +306,11 @@ mod tests {
panic!("GitContext could not find the remote url");
}
}

#[test]
// regression test for https://github.com/apollographql/rover/issues/670
fn it_does_not_panic_on_remote_urls_with_no_apparent_owner() {
let clean = GitContext::sanitize_remote_url("ssh://[email protected]:4000/repo-name");
assert_eq!(clean, None);
}
}

0 comments on commit a95f998

Please sign in to comment.