Skip to content

Commit

Permalink
feat(fossid)!: Remove support for custom naming variables
Browse files Browse the repository at this point in the history
Remove support for adding custom naming variables prefixed with
`namingVariable`. Instead, custom values can be written directly into
the `namingScanPattern`.

Signed-off-by: Martin Nonnenmacher <[email protected]>
  • Loading branch information
mnonnenmacher committed Jan 15, 2025
1 parent a5abd0a commit 716e3b8
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 48 deletions.
5 changes: 1 addition & 4 deletions model/src/main/resources/reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,7 @@ ort:
serverUrl: 'https://fossid.example.com/instance/'

projectName: 'My Project'
namingScanPattern: '$Var1_#projectBaseCode_$Var3'
namingVariableVar1: myOrg
namingVariableVar2: myTeam
namingVariableVar3: myUnit
namingScanPattern: '#projectName_#repositoryName_#currentTimestamp_#deltaTag_#branch'

waitForResult: false

Expand Down
5 changes: 1 addition & 4 deletions model/src/test/kotlin/config/OrtConfigurationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,7 @@ class OrtConfigurationTest : WordSpec({
options shouldContainExactly mapOf(
"serverUrl" to "https://fossid.example.com/instance/",
"projectName" to "My Project",
"namingScanPattern" to "\$Var1_#projectBaseCode_\$Var3",
"namingVariableVar1" to "myOrg",
"namingVariableVar2" to "myTeam",
"namingVariableVar3" to "myUnit",
"namingScanPattern" to "#projectName_#repositoryName_#currentTimestamp_#deltaTag_#branch",
"waitForResult" to "false",
"keepFailedScans" to "false",
"deltaScans" to "true",
Expand Down
19 changes: 3 additions & 16 deletions plugins/scanners/fossid/src/main/kotlin/FossIdConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,8 @@ import org.ossreviewtoolkit.utils.common.Options
* license declarations.
* * **"options.detectCopyrightStatements":** When set, the FossID scan is configured to automatically detect copyright
* statements.
*
* Naming conventions options. If they are not set, default naming conventions are used.
* * **"options.namingProjectPattern":** A pattern for project names when projects are created on the FossID instance.
* Contains variables prefixed by "$" e.g. "$Var1_$Var2". Variables are also passed as options and are prefixed by
* [NAMING_CONVENTION_VARIABLE_PREFIX] e.g. namingVariableVar1 = "foo".
* * **"options.namingScanPattern":** A pattern for scan names when scans are created on the FossID instance.
* * **"options.namingScanPattern":** A pattern for scan names when scans are created on the FossID instance. If not
* set, a default pattern is used.
*
* URL mapping options. These options allow transforming the URLs of specific repositories before they are passed to
* the FossID service. This may be necessary if FossID uses a different mechanism to clone a repository, e.g. via SSH
Expand Down Expand Up @@ -166,11 +162,6 @@ data class FossIdConfig(
/** Name of the configuration property defining the sensitivity of the scan. */
private const val PROP_SENSITIVITY = "sensitivity"

/**
* The scanner options beginning with this prefix will be used to parameterize project and scan names.
*/
private const val NAMING_CONVENTION_VARIABLE_PREFIX = "namingVariable"

/**
* Default timeout in minutes for communication with FossID.
*/
Expand Down Expand Up @@ -255,11 +246,7 @@ data class FossIdConfig(
logger.info { "Naming pattern for scans is $it." }
}

val namingConventionVariables = options
.filterKeys { it.startsWith(NAMING_CONVENTION_VARIABLE_PREFIX) }
.mapKeys { it.key.substringAfter(NAMING_CONVENTION_VARIABLE_PREFIX) }

return FossIdNamingProvider(namingScanPattern, namingConventionVariables, projectName)
return FossIdNamingProvider(namingScanPattern, projectName)
}

/**
Expand Down
21 changes: 7 additions & 14 deletions plugins/scanners/fossid/src/main/kotlin/FossIdNamingProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,18 @@ import org.apache.logging.log4j.kotlin.logger
* This class provides names for scans when the FossID scanner creates them, based on the provided [namingScanPattern].
* If the pattern is null, a default pattern is used.
*
* The pattern can contain variables prefixed with "$", for example, "$var1_$var2".
* Variable values are given in the map [namingConventionVariables].
* The pattern can include built-in variables which are prefixed in the pattern with `#`, e.g., `#projectName_#branch`.
*
* There are also built-in variables. Built-in variables are prefixed in the pattern with "#" e.g. "$var1_#builtin".
* Available built-in variables:
* The following built-in variables are available:
* * **projectName**: The project name, if configured in the scanner options.
* * **repositoryName**: The name of the repository (i.e., the part of the URL before .git).
* * **currentTimestamp**: The current time.
* * **deltaTag** (scan code only): If delta scans are enabled, this qualifies the scan as an *origin* scan or a *delta*
* scan.
* * **deltaTag**: If delta scans are enabled, this qualifies the scan as an `origin` scan or a `delta` scan.
* * **branch**: The branch name (revision) to scan. FossID only allows alphanumeric characters and '-' in names, all
* other characters are replaced with underscores. Might be shortened to fit the scan code length limit.
*/
class FossIdNamingProvider(
private val namingScanPattern: String?,
private val namingConventionVariables: Map<String, String>,
private val projectName: String?
) {
companion object {
Expand All @@ -70,7 +66,7 @@ class FossIdNamingProvider(

builtins += "#branch" to normalizeBranchName(branch, pattern, builtins)

return replaceNamingConventionVariables(pattern, builtins, namingConventionVariables)
return replaceNamingConventionVariables(pattern, builtins)
}

/**
Expand All @@ -85,8 +81,7 @@ class FossIdNamingProvider(
val noBranchScanCode =
replaceNamingConventionVariables(
scanCodeNamingPattern.replace("#branch", ""),
scanCodeVariables,
namingConventionVariables
scanCodeVariables
)

require(noBranchScanCode.length < MAX_SCAN_CODE_LEN) {
Expand All @@ -105,14 +100,12 @@ class FossIdNamingProvider(
*/
private fun replaceNamingConventionVariables(
namingConventionPattern: String,
builtins: Map<String, String>,
namingConventionVariables: Map<String, String>
builtins: Map<String, String>
): String {
logger.info { "Parameterizing the name with pattern '$namingConventionPattern'." }
val currentTimestamp = FORMATTER.format(LocalDateTime.now())

val allVariables =
namingConventionVariables.mapKeys { "\$${it.key}" } + builtins + ("#currentTimestamp" to currentTimestamp)
val allVariables = builtins + ("#currentTimestamp" to currentTimestamp)

return allVariables.entries.fold(namingConventionPattern) { acc, entry ->
acc.replace(entry.key, entry.value)
Expand Down
6 changes: 2 additions & 4 deletions plugins/scanners/fossid/src/test/kotlin/FossIdConfigTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,7 @@ class FossIdConfigTest : WordSpec({
"create a naming provider with a correct scan naming convention" {
val options = mapOf(
"serverUrl" to SERVER_URL,
"namingScanPattern" to "#repositoryName_\$Org_\$Unit_#deltaTag",
"namingVariableOrg" to "TestOrganization",
"namingVariableUnit" to "TestUnit"
"namingScanPattern" to "#repositoryName_#deltaTag"
)

val secrets = mapOf(
Expand All @@ -181,7 +179,7 @@ class FossIdConfigTest : WordSpec({

val scanCode = namingProvider.createScanCode("TestProject", FossId.DeltaTag.DELTA)

scanCode shouldBe "TestProject_TestOrganization_TestUnit_delta"
scanCode shouldBe "TestProject_delta"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class FossIdNamingProviderTest : WordSpec() {

init {
"createScanCode" should {
val namingProvider = FossIdNamingProvider(null, emptyMap(), null)
val namingProvider = FossIdNamingProvider(null, null)

val mockedDateTime = LocalDateTime.of(2024, 4, 1, 10, 0)
val expectedTimestamp = "20240401_100000"
Expand Down Expand Up @@ -116,7 +116,7 @@ class FossIdNamingProviderTest : WordSpec() {

"replace all built-in variables" {
val customScanPattern = "#projectName_#repositoryName_#currentTimestamp_#deltaTag_#branch"
val customNamingProvider = FossIdNamingProvider(customScanPattern, emptyMap(), "projectName")
val customNamingProvider = FossIdNamingProvider(customScanPattern, "projectName")

mockkStatic(LocalDateTime::class) {
every { LocalDateTime.now() } returns mockedDateTime
Expand All @@ -130,7 +130,7 @@ class FossIdNamingProviderTest : WordSpec() {
"create code without branch name form custom naming pattern" {
val customScanPattern = "#repositoryName_#currentTimestamp"

val namingProviderWithLongScanPattern = FossIdNamingProvider(customScanPattern, emptyMap(), null)
val namingProviderWithLongScanPattern = FossIdNamingProvider(customScanPattern, null)
mockkStatic(LocalDateTime::class) {
every { LocalDateTime.now() } returns mockedDateTime

Expand All @@ -143,7 +143,7 @@ class FossIdNamingProviderTest : WordSpec() {
"create code without branch name form custom naming pattern when branch name is provided" {
val customScanPattern = "#repositoryName_#currentTimestamp"

val namingProviderWithLongScanPattern = FossIdNamingProvider(customScanPattern, emptyMap(), null)
val namingProviderWithLongScanPattern = FossIdNamingProvider(customScanPattern, null)
mockkStatic(LocalDateTime::class) {
every { LocalDateTime.now() } returns mockedDateTime

Expand All @@ -155,7 +155,7 @@ class FossIdNamingProviderTest : WordSpec() {

"create code without branch name form custom naming pattern when too long branch name is provided" {
val customScanPattern = "#repositoryName_#currentTimestamp_#branch"
val namingProviderWithLongScanPattern = FossIdNamingProvider(customScanPattern, emptyMap(), null)
val namingProviderWithLongScanPattern = FossIdNamingProvider(customScanPattern, null)
mockkStatic(LocalDateTime::class) {
every { LocalDateTime.now() } returns mockedDateTime

Expand All @@ -166,7 +166,7 @@ class FossIdNamingProviderTest : WordSpec() {
}

"throw an exception if scan code pattern is too long" {
val namingProviderWithLongScanPattern = FossIdNamingProvider(longScanPattern, emptyMap(), null)
val namingProviderWithLongScanPattern = FossIdNamingProvider(longScanPattern, null)
mockkStatic(LocalDateTime::class) {
every { LocalDateTime.now() } returns mockedDateTime

Expand Down

0 comments on commit 716e3b8

Please sign in to comment.