forked from jk1/Gradle-License-Report
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement 'LicenseChecker' config option to change how dependency lic…
…enses are matched against allowedLicenses The default behavior is that a dependency is fine when any of its licenses are found inside allowedLicenses. This may miss dependencies, which contain multiple licenses. When 'AllRequiredLicenseChecker' is set, it will only approve a dependency when all of its discovered licenses are found in the allowedLicenses. This may report false-positives for dependencies which are dual-licensed. But in general I think a false-positive is better than missing a license violation. This fixes jk1#285
- Loading branch information
Showing
7 changed files
with
371 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/main/groovy/com/github/jk1/license/check/AllRequiredLicenseChecker.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright 2018 Evgeny Naumenko <[email protected]> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.github.jk1.license.check | ||
|
||
/** | ||
* All licenses of a dependency must be found inside allowedLicenses to pass. | ||
*/ | ||
class AllRequiredLicenseChecker implements LicenseChecker { | ||
@Override | ||
List<Tuple2<Dependency, List<ModuleLicense>>> checkAllDependencyLicensesAreAllowed(List<AllowedLicense> allowedLicenses, List<Dependency> allDependencies) { | ||
removeNullLicenses(allDependencies) | ||
List<Tuple2<Dependency, List<ModuleLicense>>> result = new ArrayList<>() | ||
for (Dependency dependency : (allDependencies)) { | ||
List<AllowedLicense> perDependencyAllowedLicenses = allowedLicenses.findAll { isDependencyNameMatchesAllowedLicense(dependency, it) && isDependencyVersionMatchesAllowedLicense(dependency, it) } | ||
// allowedLicense matches anything, so we don't need to further check | ||
if (perDependencyAllowedLicenses.any { it.moduleLicense == null || it.moduleLicense == ".*" }) { | ||
continue | ||
} | ||
List<ModuleLicense> notAllowedLicenses = dependency.moduleLicenses.findAll { !isDependencyLicenseMatchesAllowedLicense(it, perDependencyAllowedLicenses) } | ||
if (!notAllowedLicenses.isEmpty()) { | ||
result.add(new Tuple2(dependency, notAllowedLicenses)) | ||
} | ||
} | ||
return result | ||
} | ||
|
||
private static boolean isDependencyNameMatchesAllowedLicense(Dependency dependency, AllowedLicense allowedLicense) { | ||
return dependency.moduleName ==~ allowedLicense.moduleName || allowedLicense.moduleName == null || dependency.moduleName == allowedLicense.moduleName | ||
} | ||
|
||
private static boolean isDependencyVersionMatchesAllowedLicense(Dependency dependency, AllowedLicense allowedLicense) { | ||
return dependency.moduleVersion ==~ allowedLicense.moduleVersion || allowedLicense.moduleVersion == null || dependency.moduleVersion == allowedLicense.moduleVersion | ||
} | ||
|
||
private static boolean isDependencyLicenseMatchesAllowedLicense(ModuleLicense moduleLicense, List<AllowedLicense> allowedLicenses) { | ||
for (AllowedLicense allowedLicense : allowedLicenses) { | ||
if (allowedLicense.moduleLicense == null || allowedLicense.moduleLicense == ".*") return true | ||
|
||
if (moduleLicense.moduleLicense ==~ allowedLicense.moduleLicense || moduleLicense.moduleLicense == allowedLicense.moduleLicense) return true | ||
} | ||
return false | ||
} | ||
|
||
/** | ||
* removes 'null'-licenses from dependencies which have at least one more license | ||
*/ | ||
private static void removeNullLicenses(List<Dependency> dependencies) { | ||
for (Dependency dependency : dependencies) { | ||
if (dependency.moduleLicenses.any { it.moduleLicense == null } && !dependency.moduleLicenses.every { | ||
it.moduleLicense == null | ||
}) { | ||
dependency.moduleLicenses = dependency.moduleLicenses.findAll { it.moduleLicense != null } | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/main/groovy/com/github/jk1/license/check/OneRequiredLicenseChecker.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright 2018 Evgeny Naumenko <[email protected]> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.github.jk1.license.check | ||
|
||
/** | ||
* A Dependency, which has at least one license inside allowedLicenses, will pass. | ||
*/ | ||
class OneRequiredLicenseChecker implements LicenseChecker { | ||
|
||
@Override | ||
List<Tuple2<Dependency, List<ModuleLicense>>> checkAllDependencyLicensesAreAllowed(List<AllowedLicense> allowedLicenses, List<Dependency> allDependencies) { | ||
List<Dependency> notPassedDependencies = allDependencies.findAll { !isDependencyHasAllowedLicense(it, allowedLicenses) } | ||
return notPassedDependencies.collect { new Tuple2(it, it.moduleLicenses.isEmpty() ? null : it.moduleLicenses) } | ||
} | ||
|
||
private boolean isDependencyHasAllowedLicense(Dependency dependency, List<AllowedLicense> allowedLicenses) { | ||
for (allowedLicense in allowedLicenses) { | ||
if (isDependencyMatchesAllowedLicense(dependency, allowedLicense)) return true | ||
} | ||
return false | ||
} | ||
|
||
private boolean isDependencyMatchesAllowedLicense(Dependency dependency, AllowedLicense allowedLicense) { | ||
return isDependencyNameMatchesAllowedLicense(dependency, allowedLicense) && | ||
isDependencyLicenseMatchesAllowedLicense(dependency, allowedLicense) && | ||
isDependencyVersionMatchesAllowedLicense(dependency, allowedLicense) | ||
} | ||
|
||
private boolean isDependencyNameMatchesAllowedLicense(Dependency dependency, AllowedLicense allowedLicense) { | ||
return dependency.moduleName ==~ allowedLicense.moduleName || allowedLicense.moduleName == null || | ||
dependency.moduleName == allowedLicense.moduleName | ||
} | ||
|
||
private boolean isDependencyVersionMatchesAllowedLicense(Dependency dependency, AllowedLicense allowedLicense) { | ||
return dependency.moduleVersion ==~ allowedLicense.moduleVersion || allowedLicense.moduleVersion == null || | ||
dependency.moduleVersion == allowedLicense.moduleVersion | ||
} | ||
|
||
private boolean isDependencyLicenseMatchesAllowedLicense(Dependency dependency, AllowedLicense allowedLicense) { | ||
if (allowedLicense.moduleLicense == null || allowedLicense.moduleLicense == ".*") return true | ||
|
||
for (moduleLicenses in dependency.moduleLicenses) | ||
if (moduleLicenses.moduleLicense ==~ allowedLicense.moduleLicense || | ||
moduleLicenses.moduleLicense == allowedLicense.moduleLicense) return true | ||
return false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.