Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't panic on git remotes w/no apparent owner #731

Merged
merged 1 commit into from
Aug 16, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}