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

License check does not support the format ssh://git@github.com/{username}. #207

Open
mizar opened this issue Sep 6, 2024 · 0 comments
Open

Comments

@mizar
Copy link

mizar commented Sep 6, 2024

--mine github.com/{username} のオプションでのライセンス表示除外ですが、 ssh://git@github.com/{username} のようなssh接続でのURLが考慮されていないため、機能しないようです。

fn users(package: &cm::Package, cache_dir: &Path) -> anyhow::Result<BTreeSet<User>> {
let path = &cache_dir.join("owners.json");
let cur_cache = if path.exists() {
serde_json::from_str(&cargo_util::paths::read(path)?)?
} else {
CachedUsers::default()
};
let mut cache = cur_cache.clone();
let mut users = if let Some(source) = &package.source {
if source.is_crates_io() {
match cache
.crates_io
.entry(package.name.clone())
.or_default()
.entry(package.version.clone())
{
btree_map::Entry::Vacant(entry) => {
let owners = retrieve_owner_urls(&package.name, cache_dir)?
.flat_map(|url| {
url.strip_prefix("https://github.com/")
.map(ToOwned::to_owned)
})
.collect();
github_users(entry.insert(owners))
}
entry @ btree_map::Entry::Occupied(_) => {
github_users(entry.or_insert_with(|| unreachable!()))
}
}
} else if let Some([username, _, rev]) = source
.repr
.strip_prefix("git+https://github.com/")
.map(|s| s.split(|c| ['/', '#'].contains(&c)).collect::<Vec<_>>())
.as_deref()
{
cache
.github_com
.entry(package.name.clone())
.or_default()
.entry((*rev).to_owned())
.or_default()
.insert((*username).to_owned());
btreeset!(User::Github((*username).to_owned()))
} else if let Some([username, _, rev]) = source
.repr
.strip_prefix("git+https://gitlab.com/")
.map(|s| s.split(|c| ['/', '#'].contains(&c)).collect::<Vec<_>>())
.as_deref()
{
cache
.gitlab_com
.entry(package.name.clone())
.or_default()
.entry((*rev).to_owned())
.or_default()
.insert((*username).to_owned());
btreeset!(User::GitlabCom((*username).to_owned()))
} else {
btreeset!()
}
} else {
btreeset!()
};
if cache != cur_cache {
cargo_util::paths::create_dir_all(cache_dir)?;
cargo_util::paths::write(path, cache.to_json())?;
}
if users.is_empty() {
if let Some(repository) = &package.repository {
if let Some(username) = repository.strip_prefix("https://github.com/") {
users.insert(User::Github(username.to_owned()));
} else if let Some(username) = repository.strip_prefix("https://gitlab.com/") {
users.insert(User::GitlabCom(username.to_owned()));
}
}
}
return Ok(users);
#[derive(Default, Deserialize, Serialize, Clone, PartialEq)]
struct CachedUsers {
#[serde(rename = "crates.io")]
crates_io: BTreeMap<String, BTreeMap<Version, BTreeSet<String>>>,
#[serde(rename = "github.com")]
github_com: BTreeMap<String, BTreeMap<String, BTreeSet<String>>>,
#[serde(rename = "gitlab.com")]
gitlab_com: BTreeMap<String, BTreeMap<String, BTreeSet<String>>>,
}
impl CachedUsers {
fn to_json(&self) -> String {
serde_json::to_string(self).expect("should not fail")
}
}
fn github_users(names: &BTreeSet<String>) -> BTreeSet<User> {
names.iter().cloned().map(User::Github).collect()
}
fn retrieve_owner_urls(
package_name: &str,
cwd: &Path,
) -> anyhow::Result<impl Iterator<Item = String>> {
let url = &format!("https://crates.io/api/v1/crates/{}/owners", package_name);
let res = &curl(url, cwd)?;
let KrateOwnersOwners { users } = serde_json::from_str(res)
.with_context(|| format!("could not parse the output from {}", url))?;
return Ok(users.into_iter().flat_map(|EncodableOwner { url }| url));
#[derive(Deserialize)]
struct KrateOwnersOwners {
users: Vec<EncodableOwner>,
}
#[derive(Deserialize)]
struct EncodableOwner {
url: Option<String>,
}
fn curl(url: &str, cwd: &Path) -> anyhow::Result<String> {
let curl_exe = which::which("curl").map_err(|_| anyhow!("command not found: curl"))?;
ProcessBuilder::new(curl_exe)
.args(&[url, "-L"])
.cwd(cwd)
.read_stdout()
}
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant