prefetch-npm-deps: throw better error when unsupported git service is used

See https://github.com/NixOS/nixpkgs/pull/206476#discussion_r1058689383.
This commit is contained in:
Winter 2023-01-28 14:17:27 -05:00 committed by Lily Foster
parent 7e247e6fec
commit e1d64c1941
No known key found for this signature in database
GPG Key ID: 49340081E484C893

View File

@ -101,7 +101,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();
@ -190,11 +190,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()?;
@ -243,7 +245,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;
@ -263,7 +265,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()?;
@ -288,9 +293,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)
}
}
@ -322,32 +332,26 @@ mod tests {
"git+ssh://git@github.com/castlabs/electron-releases.git#fc5f78d046e8d7cdeb66345a2633c383ab41f525",
Some("https://codeload.github.com/castlabs/electron-releases/tar.gz/fc5f78d046e8d7cdeb66345a2633c383ab41f525"),
),
(
"https://user@github.com/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://git@gitlab.com/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://git@gitlab.com/foo/bar.git#fix/bug").unwrap())
.is_err(),
"GitLab URLs should be marked as invalid (lol)"
);
}
#[test]