Skip to content

Commit

Permalink
fix(fossid-webapp): Replace version comparison with Semver
Browse files Browse the repository at this point in the history
`coerce` needs to be used because FossID versions are not valid semantic
versions e.g. "23.3".

Signed-off-by: Nicolas Nobelis <[email protected]>
  • Loading branch information
nnobelis authored and sschuberth committed Feb 29, 2024
1 parent bcb2d33 commit eed25cf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
12 changes: 10 additions & 2 deletions plugins/scanners/fossid/src/main/kotlin/FossId.kt
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ import org.ossreviewtoolkit.utils.common.enumSetOf
import org.ossreviewtoolkit.utils.common.replaceCredentialsInUri
import org.ossreviewtoolkit.utils.ort.showStackTrace

import org.semver4j.Semver

/**
* A wrapper for [FossID](https://fossid.com/).
*
Expand Down Expand Up @@ -700,7 +702,9 @@ class FossId internal constructor(
// Scans that were added to the queue are interpreted as an error by FossID before version 2021.2.
// For older versions, `waitScanComplete()` is able to deal with queued scans. Therefore, not checking the
// response of queued scans.
if (version >= "2021.2" || scanResult.error != "Scan was added to queue.") {
val currentVersion = checkNotNull(Semver.coerce(version))
val minVersion = checkNotNull(Semver.coerce("2021.2"))
if (currentVersion >= minVersion || scanResult.error != "Scan was added to queue.") {
scanResult.checkResponse("trigger scan", false)
}

Expand Down Expand Up @@ -739,7 +743,11 @@ class FossId internal constructor(
// stays in state "NOT FINISHED". Therefore, we check the output of the Git fetch to find out
// whether the download is actually done.
val message = response.message
if (version >= "20.2" || message == null || !GIT_FETCH_DONE_REGEX.containsMatchIn(message)) {
val currentVersion = checkNotNull(Semver.coerce(version))
val minVersion = checkNotNull(Semver.coerce("20.2"))
if (currentVersion >= minVersion || message == null
|| !GIT_FETCH_DONE_REGEX.containsMatchIn(message)
) {
return@wait false
}

Expand Down
12 changes: 12 additions & 0 deletions plugins/scanners/fossid/src/test/kotlin/FossIdTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ import org.ossreviewtoolkit.plugins.scanners.fossid.FossId.Companion.SCAN_ID_KEY
import org.ossreviewtoolkit.plugins.scanners.fossid.FossId.Companion.SERVER_URL_KEY
import org.ossreviewtoolkit.plugins.scanners.fossid.FossId.Companion.convertGitUrlToProjectName

import org.semver4j.Semver

@Suppress("LargeClass")
class FossIdTest : WordSpec({

Check warning on line 74 in plugins/scanners/fossid/src/test/kotlin/FossIdTest.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused symbol

Class "FossIdTest" is never used
beforeSpec {
Expand Down Expand Up @@ -95,6 +97,16 @@ class FossIdTest : WordSpec({

fossId.version shouldBe FOSSID_VERSION
}

"return a comparable version" {
val fossId = createFossId(createConfig())

val currentVersion = checkNotNull(Semver.coerce(fossId.version))
val minVersion = checkNotNull(Semver.coerce("2020.2"))
(currentVersion >= minVersion) shouldBe true
val minVersion2 = checkNotNull(Semver.coerce("2023.3"))
(currentVersion <= minVersion2) shouldBe true
}
}

"convertGitUrlToProjectName()" should {
Expand Down

0 comments on commit eed25cf

Please sign in to comment.