Skip to content

Commit

Permalink
prefetch-npm-deps: throw better error when unsupported git service is…
Browse files Browse the repository at this point in the history
  • Loading branch information
winterqt committed Jan 31, 2023
1 parent 60bb96f commit 208a4d0
Showing 1 changed file with 25 additions and 21 deletions.
46 changes: 25 additions & 21 deletions pkgs/build-support/node/fetch-npm-deps/src/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl Package {
UrlOrString::String(_) => panic!("at this point, all packages should have URLs"),
};

let specifics = match get_hosted_git_url(&resolved) {
let specifics = match get_hosted_git_url(&resolved)? {
Some(hosted) => {
let mut body = ureq::get(hosted.as_str()).call()?.into_reader();

Expand Down Expand Up @@ -188,11 +188,13 @@ impl Package {
}

#[allow(clippy::case_sensitive_file_extension_comparisons)]
fn get_hosted_git_url(url: &Url) -> Option<Url> {
if ["git", "http", "git+ssh", "git+https", "ssh", "https"].contains(&url.scheme()) {
let mut s = url.path_segments()?;
fn get_hosted_git_url(url: &Url) -> anyhow::Result<Option<Url>> {
if ["git", "git+ssh", "git+https", "ssh"].contains(&url.scheme()) {
let mut s = url
.path_segments()
.ok_or_else(|| anyhow!("bad URL: {url}"))?;

match url.host_str()? {
let mut get_url = || match url.host_str()? {
"github.com" => {
let user = s.next()?;
let mut project = s.next()?;
Expand Down Expand Up @@ -241,7 +243,7 @@ fn get_hosted_git_url(url: &Url) -> Option<Url> {
)
}
"gitlab.com" => {
let path = &url.path()[1..];
/* let path = &url.path()[1..];
if path.contains("/~/") || path.contains("/archive.tar.gz") {
return None;
Expand All @@ -261,7 +263,10 @@ fn get_hosted_git_url(url: &Url) -> Option<Url> {
"https://gitlab.com/{user}/{project}/repository/archive.tar.gz?ref={commit}"
))
.ok()?,
)
) */

// lmao: https://github.com/npm/hosted-git-info/pull/109
None
}
"git.sr.ht" => {
let user = s.next()?;
Expand All @@ -286,9 +291,14 @@ fn get_hosted_git_url(url: &Url) -> Option<Url> {
)
}
_ => None,
};

match get_url() {
Some(u) => Ok(Some(u)),
None => Err(anyhow!("This lockfile either contains a Git dependency with an unsupported host, or a malformed URL in the lockfile: {url}"))
}
} else {
None
Ok(None)
}
}

Expand Down Expand Up @@ -320,32 +330,26 @@ mod tests {
"git+ssh://[email protected]/castlabs/electron-releases.git#fc5f78d046e8d7cdeb66345a2633c383ab41f525",
Some("https://codeload.github.com/castlabs/electron-releases/tar.gz/fc5f78d046e8d7cdeb66345a2633c383ab41f525"),
),
(
"https://[email protected]/foo/bar#fix/bug",
Some("https://codeload.github.com/foo/bar/tar.gz/fix/bug")
),
(
"https://github.com/eligrey/classList.js/archive/1.2.20180112.tar.gz",
None
),
(
"git+ssh://bitbucket.org/foo/bar#branch",
Some("https://bitbucket.org/foo/bar/get/branch.tar.gz")
),
(
"ssh://[email protected]/foo/bar.git#fix/bug",
Some("https://gitlab.com/foo/bar/repository/archive.tar.gz?ref=fix/bug")
),
(
"git+ssh://git.sr.ht/~foo/bar#branch",
Some("https://git.sr.ht/~foo/bar/archive/branch.tar.gz")
),
] {
assert_eq!(
get_hosted_git_url(&Url::parse(input).unwrap()),
get_hosted_git_url(&Url::parse(input).unwrap()).unwrap(),
expected.map(|u| Url::parse(u).unwrap())
);
}

assert!(
get_hosted_git_url(&Url::parse("ssh://[email protected]/foo/bar.git#fix/bug").unwrap())
.is_err(),
"GitLab URLs should be marked as invalid (lol)"
);
}

#[test]
Expand Down

0 comments on commit 208a4d0

Please sign in to comment.