forked from oss-review-toolkit/ort
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PackageRule.kt
237 lines (195 loc) · 8.15 KB
/
PackageRule.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
* Copyright (C) 2017-2019 HERE Europe B.V.
*
* 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.
*
* SPDX-License-Identifier: Apache-2.0
* License-Filename: LICENSE
*/
package com.here.ort.evaluator
import com.here.ort.model.CuratedPackage
import com.here.ort.model.Identifier
import com.here.ort.model.LicenseFindings
import com.here.ort.model.LicenseSource
import com.here.ort.model.Package
import com.here.ort.model.PackageCurationResult
import com.here.ort.model.Project
import com.here.ort.model.Severity
import com.here.ort.model.config.Excludes
import com.here.ort.model.config.PathExclude
import com.here.ort.spdx.SpdxLicense
/**
* A [Rule] to check a single [Package].
*/
open class PackageRule(
ruleSet: RuleSet,
name: String,
/**
* The [Package] to check.
*/
val pkg: Package,
/**
* The list of curations applied to the [package][pkg].
*/
val curations: List<PackageCurationResult>,
/**
* The detected licenses for the [Package].
*/
val detectedLicenses: List<LicenseFindings>
) : Rule(ruleSet, name) {
private val licenseRules = mutableListOf<LicenseRule>()
@Suppress("UNUSED") // This is intended to be used by rule implementations.
val uncuratedPkg by lazy { CuratedPackage(pkg, curations).toUncuratedPackage() }
override val description = "Evaluating rule '$name' for package '${pkg.id.toCoordinates()}'."
override fun issueSource() = "$name - ${pkg.id.toCoordinates()}"
override fun runInternal() {
licenseRules.forEach { it.evaluate() }
}
/**
* A [RuleMatcher] that checks if the [package][pkg] has any concluded, declared, or detected license.
*/
fun hasLicense() =
object : RuleMatcher {
override val description = "hasLicense()"
override fun matches() =
pkg.concludedLicense?.licenses()?.isNotEmpty() == true
|| pkg.declaredLicenses.isNotEmpty()
|| detectedLicenses.isNotEmpty()
}
/**
* A [RuleMatcher] that checks if the [package][pkg] is [excluded][Excludes].
*/
fun isExcluded() =
object : RuleMatcher {
override val description = "isExcluded()"
override fun matches() = ruleSet.ortResult.isExcluded(pkg.id)
}
/**
* A [RuleMatcher] that checks if the [identifier][Package.id] of the [package][pkg] belongs to one of the provided
* [orgs][Identifier.isFromOrg].
*/
fun isFromOrg(vararg names: String) =
object : RuleMatcher {
override val description = "isFromOrg(${names.joinToString()})"
override fun matches() = pkg.id.isFromOrg(*names)
}
/**
* A [RuleMatcher] that checks if the [package][pkg] was created from a [Project].
*/
fun isProject() =
object : RuleMatcher {
override val description = "isProject()"
override fun matches() = ruleSet.ortResult.analyzer?.result?.projects?.find { it.id == pkg.id } != null
}
/**
* A [RuleMatcher] that checks if the [identifier type][Identifier.type] of the [package][pkg] equals [type].
*/
fun isType(type: String) =
object : RuleMatcher {
override val description = "isType($type)"
override fun matches() = pkg.id.type == type
}
/**
* A DSL function to configure a [LicenseRule] and add it to this rule.
*/
fun licenseRule(name: String, licenseView: LicenseView, block: LicenseRule.() -> Unit) {
val licenses = licenseView.licenses(pkg, detectedLicenses.map { it.license })
licenses.forEach { (license, licenseSource) ->
val findings = if (licenseSource == LicenseSource.DETECTED) {
ruleSet.licenseFindings[pkg.id].orEmpty()
} else {
emptyMap()
}.filter { (finding, _) -> finding.license == license }
licenseRules += LicenseRule(name, license, licenseSource, findings).apply(block)
}
}
fun issue(severity: Severity, message: String, howToFix: String) =
issue(severity, pkg.id, null, null, message, howToFix)
/**
* Add a [hint][Severity.HINT] to the list of [violations].
*/
fun hint(message: String, howToFix: String) = hint(pkg.id, null, null, message, howToFix)
/**
* Add a [warning][Severity.WARNING] to the list of [violations].
*/
fun warning(message: String, howToFix: String) = warning(pkg.id, null, null, message, howToFix)
/**
* Add an [error][Severity.ERROR] to the list of [violations].
*/
fun error(message: String, howToFix: String) = error(pkg.id, null, null, message, howToFix)
/**
* A [Rule] to check a single license of the [package][pkg].
*/
inner class LicenseRule(
name: String,
/**
* The license to check.
*/
val license: String,
/**
* The source of the license.
*/
val licenseSource: LicenseSource,
/**
* The associated [LicenseFindings]. Only used if [licenseSource] is [LicenseSource.DETECTED].
*/
val licenseFindings: Map<LicenseFindings, List<PathExclude>> = emptyMap()
) : Rule(ruleSet, name) {
/**
* A helper function to access [PackageRule.pkg] in extension functions for [LicenseRule], required because the
* properties of the outer class [PackageRule] cannot be accessed from an extension function.
*/
fun pkg() = pkg
/**
* A helper function to access [PackageRule.detectedLicenses] in extension functions for [LicenseRule], required
* because the properties of the outer class [PackageRule] cannot be accessed from an extension function.
*/
fun detectedLicenses() = detectedLicenses
override val description = "\tEvaluating license rule '$name' for $licenseSource license '$license'."
override fun issueSource() = "$name - ${pkg.id.toCoordinates()} - $license ($licenseSource)"
/**
* A [RuleMatcher] that checks if a [detected][LicenseSource.DETECTED] license is excluded. This is the case if
* all keys of [licenseFindings] are associated to at least one [PathExclude].
*/
fun isExcluded() =
object : RuleMatcher {
override val description = "isExcluded($license)"
override fun matches() =
licenseSource == LicenseSource.DETECTED
&& licenseFindings.isNotEmpty()
&& licenseFindings.values.all { it.isNotEmpty() }
}
/**
* A [RuleMatcher] that checks if the [license] is a valid [SpdxLicense].
*/
fun isSpdxLicense() =
object : RuleMatcher {
override val description = "isSpdxLicense($license)"
override fun matches() = SpdxLicense.forId(license) != null
}
fun issue(severity: Severity, message: String, howToFix: String) =
issue(severity, pkg.id, license, licenseSource, message, howToFix)
/**
* Add a [hint][Severity.HINT] to the list of [violations].
*/
fun hint(message: String, howToFix: String) = hint(pkg.id, license, licenseSource, message, howToFix)
/**
* Add a [warning][Severity.WARNING] to the list of [violations].
*/
fun warning(message: String, howToFix: String) = warning(pkg.id, license, licenseSource, message, howToFix)
/**
* Add an [error][Severity.ERROR] to the list of [violations].
*/
fun error(message: String, howToFix: String) = error(pkg.id, license, licenseSource, message, howToFix)
}
}