Skip to content

Commit 898ed7a

Browse files
committed
refactor(utils): Move the version handling functions to a utility class
These functions will be used in a future commit by package configurations. Signed-off-by: Nicolas Nobelis <[email protected]>
1 parent afd60b5 commit 898ed7a

File tree

2 files changed

+68
-43
lines changed

2 files changed

+68
-43
lines changed

model/src/main/kotlin/PackageCuration.kt

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,7 @@ package org.ossreviewtoolkit.model
2121

2222
import com.fasterxml.jackson.annotation.JsonProperty
2323

24-
import org.apache.logging.log4j.kotlin.logger
25-
26-
import org.ossreviewtoolkit.utils.ort.showStackTrace
27-
28-
import org.semver4j.RangesListFactory
29-
import org.semver4j.Semver
30-
31-
/**
32-
* A list of Strings that are used to identify a version string as a version range in the [PackageCuration]'s version.
33-
*/
34-
private val versionRangeIndicators = listOf(",", "~", "*", "+", ">", "<", "=", " - ", "^", ".x", "||")
24+
import org.ossreviewtoolkit.model.utils.isApplicableIvyVersion
3525

3626
/**
3727
* Return true if this string equals the [other] string, or if either string is blank.
@@ -62,45 +52,14 @@ data class PackageCuration(
6252
&& id.namespace == pkgId.namespace
6353
&& id.name.equalsOrIsBlank(pkgId.name)
6454

65-
/**
66-
* Return true if the version of this [PackageCuration] interpreted as an Ivy version matcher is applicable to the
67-
* package with the given [identifier][pkgId].
68-
*/
69-
private fun isApplicableIvyVersion(pkgId: Identifier) =
70-
runCatching {
71-
if (id.version == pkgId.version) return true
72-
73-
if (id.version.isVersionRange()) {
74-
// `Semver.satisfies(String)` requires a valid version range to work as expected, see:
75-
// https://github.com/semver4j/semver4j/issues/132.
76-
val range = RangesListFactory.create(id.version)
77-
require(range.get().isNotEmpty()) {
78-
"'${id.version}' is not a valid version range."
79-
}
80-
81-
return Semver.coerce(pkgId.version)?.satisfies(range) == true
82-
}
83-
84-
return false
85-
}.onFailure {
86-
logger.warn {
87-
"Failed to check if package curation version '${id.version}' is applicable to package version " +
88-
"'${pkgId.version}' of package '${pkgId.toCoordinates()}'."
89-
}
90-
91-
it.showStackTrace()
92-
}.getOrDefault(false)
93-
94-
private fun String.isVersionRange() = versionRangeIndicators.any { contains(it, ignoreCase = true) }
95-
9655
/**
9756
* Return true if this [PackageCuration] is applicable to the package with the given [identifier][pkgId]. The
9857
* curation's version may be an
9958
* [Ivy version matcher](http://ant.apache.org/ivy/history/2.4.0/settings/version-matchers.html).
10059
*/
10160
fun isApplicable(pkgId: Identifier): Boolean =
10261
isApplicableDisregardingVersion(pkgId)
103-
&& (id.version.equalsOrIsBlank(pkgId.version) || isApplicableIvyVersion(pkgId))
62+
&& (id.version.equalsOrIsBlank(pkgId.version) || id.isApplicableIvyVersion(pkgId))
10463

10564
/**
10665
* Apply the curation [data] to the provided [basePackage] by calling [PackageCurationData.apply], if applicable.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright (C) 2017 The ORT Project Authors (see <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
* License-Filename: LICENSE
18+
*/
19+
package org.ossreviewtoolkit.model.utils
20+
21+
import kotlin.collections.isNotEmpty
22+
23+
import org.apache.logging.log4j.kotlin.logger
24+
25+
import org.ossreviewtoolkit.model.Identifier
26+
import org.ossreviewtoolkit.model.PackageCuration
27+
import org.ossreviewtoolkit.utils.ort.showStackTrace
28+
29+
import org.semver4j.RangesListFactory
30+
import org.semver4j.Semver
31+
32+
/**
33+
* A list of Strings that are used to identify a version string as a version range in the [PackageCuration]'s version.
34+
*/
35+
private val versionRangeIndicators = listOf(",", "~", "*", "+", ">", "<", "=", " - ", "^", ".x", "||")
36+
37+
/**
38+
* Return true if the version of this [PackageCuration] interpreted as an Ivy version matcher is applicable to the
39+
* package with the given [identifier][pkgId].
40+
*/
41+
internal fun Identifier.isApplicableIvyVersion(pkgId: Identifier) =
42+
runCatching {
43+
if (version == pkgId.version) return true
44+
45+
if (version.isVersionRange()) {
46+
// `Semver.satisfies(String)` requires a valid version range to work as expected, see:
47+
// https://github.com/semver4j/semver4j/issues/132.
48+
val range = RangesListFactory.create(version)
49+
require(range.get().isNotEmpty()) {
50+
"'$version' is not a valid version range."
51+
}
52+
53+
return Semver.coerce(pkgId.version)?.satisfies(range) == true
54+
}
55+
56+
return false
57+
}.onFailure {
58+
logger.warn {
59+
"Failed to check if package curation version '$version' is applicable to package version " +
60+
"'${pkgId.version}' of package '${pkgId.toCoordinates()}'."
61+
}
62+
63+
it.showStackTrace()
64+
}.getOrDefault(false)
65+
66+
private fun String.isVersionRange() = versionRangeIndicators.any { contains(it, ignoreCase = true) }

0 commit comments

Comments
 (0)