Skip to content

Commit

Permalink
Merge pull request #174 from patst/master
Browse files Browse the repository at this point in the history
Add support for remote allowedLicenses file
  • Loading branch information
jk1 authored Jan 26, 2020
2 parents e7ed199 + d34499e commit 0ad58d4
Show file tree
Hide file tree
Showing 7 changed files with 296 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class LicenseReportExtension {
public boolean excludeOwnGroup
public String[] excludeGroups
public String[] excludes
public File allowedLicensesFile
public Object allowedLicensesFile

LicenseReportExtension(Project project) {
outputDir = "${project.buildDir}/reports/dependency-license"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import org.gradle.api.GradleException
class LicenseChecker {

void checkAllDependencyLicensesAreAllowed(
File allowedLicensesFile, File projectLicensesDataFile, File notPassedDependenciesOutputFile) {
Object allowedLicensesFile, File projectLicensesDataFile, File notPassedDependenciesOutputFile) {
List<Dependency> allDependencies = LicenseCheckerFileReader.importDependencies(projectLicensesDataFile)
List<AllowedLicense> allowedLicenses = LicenseCheckerFileReader.importAllowedLicenses(allowedLicensesFile)
List<Dependency> notPassedDependencies = searchForNotAllowedDependencies(allDependencies, allowedLicenses)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,27 @@ package com.github.jk1.license.check

import groovy.json.JsonParserType
import groovy.json.JsonSlurper
import org.gradle.api.InvalidUserDataException

class LicenseCheckerFileReader {

static List<AllowedLicense> importAllowedLicenses(File allowedLicensesFile) {
def slurpResult = new JsonSlurper().setType(JsonParserType.LAX).parse(allowedLicensesFile)
static List<AllowedLicense> importAllowedLicenses(Object allowedLicensesFile) {
def slurpResult
if(allowedLicensesFile instanceof File ) {
slurpResult = new JsonSlurper().setType(JsonParserType.LAX).parse(allowedLicensesFile)
} else if(allowedLicensesFile instanceof URL) {
slurpResult = new JsonSlurper().setType(JsonParserType.LAX).parse(allowedLicensesFile)
} else if(allowedLicensesFile instanceof String) {
def source
try {
source = new URL(allowedLicensesFile)
} catch (MalformedURLException ignored) {
source = new File(allowedLicensesFile)
}
return importAllowedLicenses(source)
} else {
throw new InvalidUserDataException("Unknown type for allowedLicensesFile: " + allowedLicensesFile.getClass())
}
return slurpResult.allowedLicenses.collect { new AllowedLicense(it.moduleName, it.moduleVersion, it.moduleLicense) }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import org.gradle.api.DefaultTask
import org.gradle.api.logging.Logger
import org.gradle.api.logging.Logging
import org.gradle.api.tasks.CacheableTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.PathSensitive
Expand All @@ -41,9 +42,8 @@ class CheckLicenseTask extends DefaultTask {
description = 'Check if License could be used'
}

@InputFile
@PathSensitive(PathSensitivity.NAME_ONLY)
File getAllowedLicenseFile() {
@Input
Object getAllowedLicenseFile() {
return config.allowedLicensesFile
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -547,4 +547,130 @@ class CheckLicenseTaskSpec extends Specification {
then:
buildResult.task(":checkLicense").outcome == TaskOutcome.UP_TO_DATE
}

def "it should pass when only containing included licenses which are referenced by url"() {
given:
buildFile << """
import com.github.jk1.license.filter.*
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.3.50'
id 'com.github.jk1.dependency-license-report' version '1.2'
}
apply plugin: 'java'
group 'greeting'
version '0.0.1'
repositories {
mavenCentral()
}
dependencies {
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25'
compile group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.25'
compile "org.jetbrains.kotlin:kotlin-reflect"
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
licenseReport {
filters = new LicenseBundleNormalizer()
allowedLicensesFile = new File("${StringEscapeUtils.escapeJava(allowed.path)}").toURI().toURL()
}
"""

when:
BuildResult buildResult = result("--build-cache", "checkLicense")

then:
buildResult.task(":checkLicense").outcome == TaskOutcome.SUCCESS

when:
buildResult = result("--build-cache", "checkLicense")

then:
buildResult.task(":checkLicense").outcome == TaskOutcome.UP_TO_DATE

when:
buildResult = result("--build-cache", "clean", "checkLicense")

then:
buildResult.task(":checkLicense").outcome == TaskOutcome.FROM_CACHE

when:
buildResult = result("--build-cache", "checkLicense")

then:
buildResult.task(":checkLicense").outcome == TaskOutcome.UP_TO_DATE
}

def "it should pass when only containing included licenses which are referenced by file name"() {
given:
buildFile << """
import com.github.jk1.license.filter.*
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.3.50'
id 'com.github.jk1.dependency-license-report' version '1.2'
}
apply plugin: 'java'
group 'greeting'
version '0.0.1'
repositories {
mavenCentral()
}
dependencies {
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25'
compile group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.25'
compile "org.jetbrains.kotlin:kotlin-reflect"
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
licenseReport {
filters = new LicenseBundleNormalizer()
allowedLicensesFile = "${StringEscapeUtils.escapeJava(allowed.path)}"
}
"""

when:
BuildResult buildResult = result("--build-cache", "checkLicense")

then:
buildResult.task(":checkLicense").outcome == TaskOutcome.SUCCESS

when:
buildResult = result("--build-cache", "checkLicense")

then:
buildResult.task(":checkLicense").outcome == TaskOutcome.UP_TO_DATE

when:
buildResult = result("--build-cache", "clean", "checkLicense")

then:
buildResult.task(":checkLicense").outcome == TaskOutcome.FROM_CACHE

when:
buildResult = result("--build-cache", "checkLicense")

then:
buildResult.task(":checkLicense").outcome == TaskOutcome.UP_TO_DATE
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class LicenseCheckerFileReaderSpec extends Specification {
TemporaryFolder testProjectDir = new TemporaryFolder()

File allowedLicenseFile
URL allowedLicenseUrl
File projectDataFile

def setup() {
Expand All @@ -45,11 +46,12 @@ class LicenseCheckerFileReaderSpec extends Specification {
}
]
}"""
allowedLicenseUrl = allowedLicenseFile.toURI().toURL()
}

def "it reads out all the allowed licenses"() {
when:
List<AllowedLicense> allowedLicenses = LicenseCheckerFileReader.importAllowedLicenses(allowedLicenseFile)
List<AllowedLicense> allowedLicenses = LicenseCheckerFileReader.importAllowedLicenses(allowedLicenseUrl)

then:
allowedLicenses.collect { it.moduleLicense } == ["License1", "License2", "License3"]
Expand All @@ -61,7 +63,7 @@ class LicenseCheckerFileReaderSpec extends Specification {
allowedLicenseFile.text = """{"allowedLicenses":[]}"""

when:
List<AllowedLicense> allowedLicenses = LicenseCheckerFileReader.importAllowedLicenses(allowedLicenseFile)
List<AllowedLicense> allowedLicenses = LicenseCheckerFileReader.importAllowedLicenses(allowedLicenseUrl)

then:
allowedLicenses == []
Expand All @@ -85,7 +87,7 @@ class LicenseCheckerFileReaderSpec extends Specification {
}"""

when:
List<AllowedLicense> allowedLicenses = LicenseCheckerFileReader.importAllowedLicenses(allowedLicenseFile)
List<AllowedLicense> allowedLicenses = LicenseCheckerFileReader.importAllowedLicenses(allowedLicenseUrl)

then:
allowedLicenses.moduleLicense == [null, null, null]
Expand Down Expand Up @@ -562,4 +564,22 @@ class LicenseCheckerFileReaderSpec extends Specification {
then:
dependencies == []
}

def "it reads out all the allowed licenses from an url"() {
when:
List<AllowedLicense> allowedLicenses = LicenseCheckerFileReader.importAllowedLicenses(allowedLicenseUrl)

then:
allowedLicenses.collect { it.moduleLicense } == ["License1", "License2", "License3"]
allowedLicenses.collect { it.moduleName } == [null, null, null]
}

def "it reads out all the allowed licenses from a file reference by string"() {
when:
List<AllowedLicense> allowedLicenses = LicenseCheckerFileReader.importAllowedLicenses(allowedLicenseFile.path)

then:
allowedLicenses.collect { it.moduleLicense } == ["License1", "License2", "License3"]
allowedLicenses.collect { it.moduleName } == [null, null, null]
}
}
Loading

0 comments on commit 0ad58d4

Please sign in to comment.