Skip to content

Commit 2235c00

Browse files
committed
feat(nuget)!: Split the (technology) namespace from the name
Use up to two namespace nodes from the name as defined at [1] as the package namespace. [1]: https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/names-of-namespaces Signed-off-by: Sebastian Schuberth <[email protected]>
1 parent f454039 commit 2235c00

File tree

2 files changed

+60
-6
lines changed

2 files changed

+60
-6
lines changed

plugins/package-managers/nuget/src/main/kotlin/utils/NuGetInspector.kt

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,13 @@ private fun List<NuGetInspector.PackageData>.toPackageReferences(): Set<PackageR
197197
)
198198
}
199199

200+
internal fun getIdentifierWithNamespace(name: String, version: String): Identifier {
201+
val namespace = name.split('.', limit = 3).toMutableList()
202+
val nameWithoutNamespace = namespace.removeLast()
203+
val namespaceWithoutName = namespace.joinToString(".")
204+
return Identifier(type = TYPE, namespace = namespaceWithoutName, name = nameWithoutNamespace, version = version)
205+
}
206+
200207
internal fun Collection<NuGetInspector.PackageData>.toOrtPackages(): Set<Package> =
201208
groupBy { "${it.name}:${it.version}" }.mapTo(mutableSetOf()) { (_, packages) ->
202209
val pkg = packages.first()
@@ -206,12 +213,16 @@ internal fun Collection<NuGetInspector.PackageData>.toOrtPackages(): Set<Package
206213
(sha512 ?: sha256 ?: sha1 ?: md5 ?: "").lowercase()
207214
)
208215

209-
val id = Identifier(
210-
type = TYPE,
211-
namespace = pkg.namespace.orEmpty(),
212-
name = pkg.name,
213-
version = pkg.version.orEmpty()
214-
)
216+
val id = if (pkg.namespace.isNullOrEmpty()) {
217+
getIdentifierWithNamespace(pkg.name, pkg.version.orEmpty())
218+
} else {
219+
Identifier(
220+
type = TYPE,
221+
namespace = pkg.namespace,
222+
name = pkg.name,
223+
version = pkg.version.orEmpty()
224+
)
225+
}
215226

216227
val declaredLicenses = mutableSetOf<String>()
217228
val pkgDeclaredLicense = pkg.declaredLicense.orEmpty()
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (C) 2020 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+
20+
package org.ossreviewtoolkit.plugins.packagemanagers.nuget.utils
21+
22+
import io.kotest.assertions.assertSoftly
23+
import io.kotest.core.spec.style.WordSpec
24+
import io.kotest.matchers.shouldBe
25+
26+
import org.ossreviewtoolkit.model.Identifier
27+
28+
class NuGetInspectorTest : WordSpec({
29+
"getIdentifierWithNamespace()" should {
30+
"split the namespace from the name" {
31+
assertSoftly {
32+
getIdentifierWithNamespace("SharpCompress", "0.23.0") shouldBe
33+
Identifier("NuGet::SharpCompress:0.23.0")
34+
getIdentifierWithNamespace("System.IO", "4.1.0") shouldBe
35+
Identifier("NuGet:System:IO:4.1.0")
36+
getIdentifierWithNamespace("System.IO.Compression", "4.3.0") shouldBe
37+
Identifier("NuGet:System.IO:Compression:4.3.0")
38+
getIdentifierWithNamespace("System.IO.Compression.ZipFile", "4.0.1") shouldBe
39+
Identifier("NuGet:System.IO:Compression.ZipFile:4.0.1")
40+
}
41+
}
42+
}
43+
})

0 commit comments

Comments
 (0)