Skip to content

Commit

Permalink
Merge pull request #76 from ybasket/license-check-exclusions
Browse files Browse the repository at this point in the history
Add license check exclusions
  • Loading branch information
eed3si9n authored Jul 28, 2023
2 parents a7e7426 + d0d842f commit 0c69634
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 7 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ If you want to check that only certain licenses are used in a project, you can u
> licenseCheck

This ensures all licenses fall into one of the categories given by `licenseCheckAllow` which defaults
to a set of commonly allowed [OSS licenses](./src/main/scala/sbtlicensereport/SbtLicenseReport.scala#L173).
to a set of commonly allowed [OSS licenses](./src/main/scala/sbtlicensereport/SbtLicenseReport.scala#L173). You can also exclude dependencies from the license check by
providing `licenseCheckExclusions`.

## Configuration

Expand Down Expand Up @@ -66,6 +67,11 @@ can be controlled via the following keys:
case DepModuleInfo("org.scala-lang", _, _) => true
}

// Want to exclude any artifact named icu4j from the license check, but keep it in the reports.
licenseCheckExclusions := {
case DepModuleInfo(_, "icu4j", _) => true
}

# Releasing

This plugin uses [sbt-ci-release](https://github.com/sbt/sbt-ci-release). To
Expand Down
9 changes: 7 additions & 2 deletions src/main/scala/sbtlicensereport/SbtLicenseReport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ object SbtLicenseReport extends AutoPlugin {
val licenseFilter =
settingKey[LicenseCategory => Boolean]("Configuration for what licenses to include in the report, by default.")
val licenseCheckAllow = settingKey[Seq[LicenseCategory]]("Licenses that are allowed to pass in checkLicenses.")
val licenseCheckExclusions = settingKey[PartialFunction[DepModuleInfo, Boolean]](
"A partial function of which dependencies you want to exclude in license checks"
)
}
// Workaround for broken autoImport in sbt 0.13.5
val autoImport = autoImportImpl
Expand Down Expand Up @@ -139,7 +142,8 @@ object SbtLicenseReport extends AutoPlugin {
val log = streams.value.log
val report = updateLicenses.value
val allowed = licenseCheckAllow.value
LicenseReport.checkLicenses(report.licenses, allowed, log)
val exclusions = licenseCheckExclusions.value
LicenseReport.checkLicenses(report.licenses, exclusions, allowed, log)
}
)

Expand All @@ -165,6 +169,7 @@ object SbtLicenseReport extends AutoPlugin {
LicenseCategory.PublicDomain,
LicenseCategory.JSON,
LicenseCategory.Unicode
)
),
licenseCheckExclusions := PartialFunction.empty
)
}
15 changes: 11 additions & 4 deletions src/main/scala/sbtlicensereport/license/LicenseReport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,17 @@ object LicenseReport {
}
}

def checkLicenses(reportLicenses: Seq[DepLicense], allowed: Seq[LicenseCategory], log: Logger): Unit = {
val violators = reportLicenses.collect {
case dep if !allowed.contains(dep.license.category) => dep
}
def checkLicenses(
reportLicenses: Seq[DepLicense],
exclude: PartialFunction[DepModuleInfo, Boolean],
allowed: Seq[LicenseCategory],
log: Logger
): Unit = {
val violators =
reportLicenses.filterNot(dl => exclude.applyOrElse(dl.module, (_: DepModuleInfo) => false)).collect {
case dep if !allowed.contains(dep.license.category) => dep
}

if (violators.nonEmpty) {
log.error(
violators.sorted
Expand Down
8 changes: 8 additions & 0 deletions src/sbt-test/checkLicense/customized-check/example.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name := "example"

libraryDependencies += "com.fasterxml.jackson.core" % "jackson-databind" % "2.5.4"
libraryDependencies += "junit" % "junit" % "4.12" % "test"

licenseCheckAllow := Seq(LicenseCategory.Apache, LicenseCategory.BSD) // covers only jackson & scala

licenseCheckExclusions := { case DepModuleInfo("junit", _, _) => true }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("com.github.sbt" % "sbt-license-report" % sys.props("plugin.version"))
1 change: 1 addition & 0 deletions src/sbt-test/checkLicense/customized-check/test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
> licenseCheck
6 changes: 6 additions & 0 deletions src/sbt-test/checkLicense/default-check/example.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name := "example"

libraryDependencies += "com.fasterxml.jackson.core" % "jackson-databind" % "2.5.4"
libraryDependencies += "junit" % "junit" % "4.12" % "test"

licenseCheckAllow := Seq(LicenseCategory.Apache, LicenseCategory.BSD, LicenseCategory.EPL)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("com.github.sbt" % "sbt-license-report" % sys.props("plugin.version"))
1 change: 1 addition & 0 deletions src/sbt-test/checkLicense/default-check/test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
> licenseCheck
15 changes: 15 additions & 0 deletions src/sbt-test/checkLicense/failing-check/example.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name := "example"

libraryDependencies += "com.fasterxml.jackson.core" % "jackson-databind" % "2.5.4"
libraryDependencies += "junit" % "junit" % "4.12" % "test"

licenseCheckAllow := Nil

TaskKey[Unit]("check") := {
licenseCheck.result.value match {
case Inc(inc: Incomplete) =>
println("licenseCheck failed as expected")
case Value(_) =>
sys.error("Expect licenseCheck to fail")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("com.github.sbt" % "sbt-license-report" % sys.props("plugin.version"))
1 change: 1 addition & 0 deletions src/sbt-test/checkLicense/failing-check/test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
> check

0 comments on commit 0c69634

Please sign in to comment.