Skip to content

Commit

Permalink
Ignore licenses for listed private sources (#391)
Browse files Browse the repository at this point in the history
* Ignore licenses for listed private sources

* Clippy fix and better comments
  • Loading branch information
ShellWowza authored Jan 27, 2022
1 parent 927c9d1 commit a912500
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
11 changes: 11 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,17 @@ impl Krate {
}
})
}

/// Returns the normalized source URL
pub(crate) fn normalized_source_url(&self) -> Option<url::Url> {
self.source.as_ref().map(|source| {
let mut url = source.url().clone();
url.set_query(None);
url.set_fragment(None);
crate::sources::normalize_url(&mut url);
url
})
}
}

impl fmt::Display for Krate {
Expand Down
17 changes: 11 additions & 6 deletions src/licenses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,13 +282,18 @@ pub fn check(
let mut pack = Pack::with_kid(Check::Licenses, krate_lic_nfo.krate.id.clone());

// If the user has set this, check if it's a private workspace
// crate and just print out a help message that we skipped it
// crate or a crate from a private registry and just print out
// a help message that we skipped it
if ctx.cfg.private.ignore
&& ctx
.krates
.workspace_members()
.any(|wm| wm.id == krate_lic_nfo.krate.id)
&& krate_lic_nfo.krate.is_private(&private_registries)
&& (krate_lic_nfo
.krate
.normalized_source_url()
.map_or(false, |source| ctx.cfg.ignore_sources.contains(&source))
|| (ctx
.krates
.workspace_members()
.any(|wm| wm.id == krate_lic_nfo.krate.id)
&& krate_lic_nfo.krate.is_private(&private_registries)))
{
pack.push(diags::SkippedPrivateWorkspaceCrate {
krate: krate_lic_nfo.krate,
Expand Down
24 changes: 24 additions & 0 deletions src/licenses/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ pub struct Private {
/// only published to private registries
#[serde(default)]
pub ignore: bool,
/// One or more URLs to private registries, if a crate comes from one
/// of these registries, the crate will not have its license checked
#[serde(default)]
pub ignore_sources: Vec<Spanned<String>>,
/// One or more private registries that you might publish crates to, if
/// a crate is only published to private registries, and ignore is true
/// the crate will not have its license checked
Expand Down Expand Up @@ -198,6 +202,24 @@ impl crate::cfg::UnvalidatedConfig for Config {
fn validate(self, cfg_file: FileId, diags: &mut Vec<Diagnostic>) -> Self::ValidCfg {
use rayon::prelude::*;

let mut ignore_sources = Vec::with_capacity(self.private.ignore_sources.len());
for aurl in &self.private.ignore_sources {
match url::Url::parse(aurl.as_ref()) {
Ok(mut url) => {
crate::sources::normalize_url(&mut url);
ignore_sources.push(url);
}
Err(pe) => {
diags.push(
Diagnostic::error()
.with_message("failed to parse url")
.with_labels(vec![Label::primary(cfg_file, aurl.span.clone())
.with_message(pe.to_string())]),
);
}
}
}

let mut parse_license = |ls: &Spanned<String>, v: &mut Vec<Licensee>| {
match spdx::Licensee::parse(ls.as_ref()) {
Ok(licensee) => {
Expand Down Expand Up @@ -306,6 +328,7 @@ impl crate::cfg::UnvalidatedConfig for Config {
exceptions,
denied,
allowed,
ignore_sources,
}
}
}
Expand Down Expand Up @@ -344,6 +367,7 @@ pub struct ValidConfig {
pub allowed: Vec<Licensee>,
pub clarifications: Vec<ValidClarification>,
pub exceptions: Vec<ValidException>,
pub ignore_sources: Vec<url::Url>,
}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion src/sources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ pub fn check(ctx: crate::CheckCtx<'_, ValidConfig>, mut sink: ErrorSink) {
}
}

fn normalize_url(url: &mut Url) {
pub(crate) fn normalize_url(url: &mut Url) {
// Normalizes the URL so that different representations can be compared to each other.
// At the moment we just remove a tailing `.git` but there are more possible optimisations.
// See https://github.com/rust-lang/cargo/blob/1f6c6bd5e7bbdf596f7e88e6db347af5268ab113/src/cargo/util/canonical_url.rs#L31-L57
Expand Down

0 comments on commit a912500

Please sign in to comment.