Skip to content

Commit bf1b1e3

Browse files
committed
refactor(Repository): Remove vcs attributes
Remove `vcs` and `vcsProcessed` attributes from `Repository`, since the `RepositoryProvenance` `provenance` is now the main attribute. Add `RepositoryDeserializer` to support parsing of legacy `OrtResult` files containing the previous attributes `vcs` and `vcsProcessed`, as well as the new format. It also needs to handle the `nestedRepositories` and `config` attributes for both legacy and recent `OrtResult` files. Signed-off-by: Jens Keim <[email protected]>
1 parent fabe966 commit bf1b1e3

File tree

10 files changed

+74
-27
lines changed

10 files changed

+74
-27
lines changed

cli-helper/src/main/kotlin/utils/Extensions.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ internal fun OrtResult.getScanResultFor(packageConfiguration: PackageConfigurati
716716
* tree.
717717
*/
718718
internal fun OrtResult.getRepositoryPaths(): Map<String, Set<String>> {
719-
val result = mutableMapOf(repository.vcsProcessed.url to mutableSetOf(""))
719+
val result = mutableMapOf(repository.provenance.vcsInfo.normalize().url to mutableSetOf(""))
720720

721721
repository.nestedRepositories.mapValues { (path, vcsInfo) ->
722722
result.getOrPut(vcsInfo.url) { mutableSetOf() } += path

evaluator/src/main/kotlin/ProjectSourceRule.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ open class ProjectSourceRule(
9393
/**
9494
* Return the [VcsType] of the project's code repository.
9595
*/
96-
fun projectSourceGetVcsType(): VcsType = ortResult.repository.vcsProcessed.type
96+
fun projectSourceGetVcsType(): VcsType = ortResult.repository.provenance.vcsInfo.normalize().type
9797

9898
/**
9999
* Return the file paths matching any of the given [glob expressions][patterns] with its file content matching

evaluator/src/main/kotlin/RuleSet.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ fun ruleSet(
159159
licenseInfoResolver: LicenseInfoResolver = ortResult.createLicenseInfoResolver(),
160160
resolutionProvider: ResolutionProvider = DefaultResolutionProvider.create(),
161161
projectSourceResolver: SourceTreeResolver = SourceTreeResolver.forRemoteRepository(
162-
ortResult.repository.vcsProcessed
162+
ortResult.repository.provenance.vcsInfo.normalize()
163163
),
164164
configure: RuleSet.() -> Unit = {}
165165
) = RuleSet(ortResult, licenseInfoResolver, resolutionProvider, projectSourceResolver).apply(configure)

model/src/main/kotlin/Repository.kt

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,26 @@
2020
package org.ossreviewtoolkit.model
2121

2222
import com.fasterxml.jackson.annotation.JsonInclude
23+
import com.fasterxml.jackson.core.JsonParser
24+
import com.fasterxml.jackson.databind.DeserializationContext
25+
import com.fasterxml.jackson.databind.JsonNode
26+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
27+
import com.fasterxml.jackson.databind.deser.std.StdDeserializer
28+
import com.fasterxml.jackson.module.kotlin.treeToValue
2329

2430
import org.ossreviewtoolkit.model.config.RepositoryConfiguration
2531
import org.ossreviewtoolkit.utils.ort.ORT_REPO_CONFIG_FILENAME
2632

2733
/**
2834
* A description of the source code repository that was used as input for ORT.
2935
*/
36+
@JsonDeserialize(using = RepositoryDeserializer::class)
3037
data class Repository(
3138
/**
3239
* Provenance wrapper for original VCS information, if present.
3340
*/
3441
val provenance: RepositoryProvenance,
3542

36-
/**
37-
* Original VCS-related information from the working tree containing the analyzer root.
38-
*/
39-
val vcs: VcsInfo = provenance.vcsInfo,
40-
41-
/**
42-
* Processed VCS-related information from the working tree containing the analyzer root that has e.g. common
43-
* mistakes corrected.
44-
*/
45-
val vcsProcessed: VcsInfo = vcs.normalize(),
46-
4743
/**
4844
* A map of nested repositories, for example Git submodules or Git-Repo modules. The key is the path to the
4945
* nested repository relative to the root of the main repository.
@@ -66,8 +62,6 @@ data class Repository(
6662
vcsInfo = VcsInfo.EMPTY,
6763
resolvedRevision = HashAlgorithm.SHA1.emptyValue
6864
),
69-
vcs = VcsInfo.EMPTY,
70-
vcsProcessed = VcsInfo.EMPTY,
7165
nestedRepositories = emptyMap(),
7266
config = RepositoryConfiguration()
7367
)
@@ -82,8 +76,57 @@ data class Repository(
8276

8377
val normalizedVcs = vcs.normalize()
8478

85-
if (vcsProcessed.matches(normalizedVcs)) return ""
79+
if (provenance.vcsInfo.normalize().matches(normalizedVcs)) return ""
8680

8781
return nestedRepositories.entries.find { (_, nestedVcs) -> nestedVcs.normalize().matches(normalizedVcs) }?.key
8882
}
8983
}
84+
85+
/**
86+
* A custom deserializer for [Repository] to support the legacy [vcs] and [vcsProcessed] attributes.
87+
*/
88+
private class RepositoryDeserializer : StdDeserializer<Repository>(Repository::class.java) {
89+
override fun deserialize(p: JsonParser, ctxt: DeserializationContext): Repository {
90+
val node = p.codec.readTree<JsonNode>(p)
91+
val parsedProvenance = when {
92+
node.has("vcs") -> {
93+
// Parse [vcs] and [vcsProcessed] attributes.
94+
val vcs = jsonMapper.treeToValue<VcsInfo>(node["vcs"])
95+
val vcsProcess = jsonMapper.treeToValue<VcsInfo>(node["vcs_processed"])
96+
97+
// Fall back to [vcsProcessed], if [vcs] is empty.
98+
val vcsInfo = if (vcs != VcsInfo.EMPTY) vcs else vcsProcess
99+
100+
// Get the [vcs]'s revision.
101+
// Fall back to [vcsProcessed], if [vcs] has empty revision.
102+
val resolvedRevision = vcs.revision.ifEmpty {
103+
vcsProcess.revision.ifEmpty {
104+
HashAlgorithm.SHA1.emptyValue
105+
}
106+
}
107+
108+
// Build a RepositoryProvenance from the parsed VcsInfo fields.
109+
RepositoryProvenance(vcsInfo, resolvedRevision)
110+
}
111+
112+
else -> {
113+
// Parse the [provenance], if no legacy fields are present.
114+
jsonMapper.treeToValue<RepositoryProvenance>(node["provenance"])
115+
}
116+
}
117+
118+
val nestedRepositories = if (node.has("nested_repositories")) {
119+
jsonMapper.treeToValue<Map<String, VcsInfo>>(node["nested_repositories"])
120+
} else {
121+
emptyMap()
122+
}
123+
124+
val config = if (node.has("config")) {
125+
jsonMapper.treeToValue<RepositoryConfiguration>(node["config"])
126+
} else {
127+
RepositoryConfiguration()
128+
}
129+
130+
return Repository(provenance = parsedProvenance, nestedRepositories, config)
131+
}
132+
}

model/src/test/kotlin/OrtResultTest.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,10 @@ class OrtResultTest : WordSpec({
379379

380380
val ortResult = OrtResult.EMPTY.copy(
381381
repository = Repository.EMPTY.copy(
382-
vcs = vcs,
383-
vcsProcessed = vcs,
382+
provenance = RepositoryProvenance(
383+
vcsInfo = vcs,
384+
resolvedRevision = vcs.revision
385+
),
384386
config = RepositoryConfiguration(
385387
excludes = Excludes(
386388
paths = listOf(

plugins/reporters/cyclonedx/src/main/kotlin/CycloneDxReporter.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ class CycloneDxReporter(
154154
// There is no component type for repositories.
155155
type = Component.Type.FILE
156156

157-
with(input.ortResult.repository.vcsProcessed) {
157+
with(input.ortResult.repository.provenance.vcsInfo.normalize()) {
158158
bomRef = "$url@$revision"
159159

160160
name = url
@@ -178,7 +178,7 @@ class CycloneDxReporter(
178178
// distributable), just create a single BOM for all projects in that case for now. As there also is no
179179
// single correct project to pick for adding external references in that case, simply only use the global
180180
// repository VCS information here.
181-
val vcs = input.ortResult.repository.vcsProcessed
181+
val vcs = input.ortResult.repository.provenance.vcsInfo.normalize()
182182
bom.addExternalReference(
183183
ExternalReference.Type.VCS,
184184
vcs.url,

plugins/reporters/opossum/src/main/kotlin/OpossumReporter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ class OpossumReporter(
127127
private val externalAttributionSources: MutableMap<String, OpossumExternalAttributionSource> = mutableMapOf()
128128

129129
internal fun create(input: ReporterInput, maxDepth: Int = Int.MAX_VALUE): OpossumInput {
130-
addBaseUrl("/", input.ortResult.repository.vcs)
130+
addBaseUrl("/", input.ortResult.repository.provenance.vcsInfo)
131131

132132
SpdxLicense.entries.forEach {
133133
val licenseText = input.licenseTextProvider.getLicenseText(it.id)

plugins/reporters/spdx/src/testFixtures/kotlin/TestData.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ val ORT_RESULT = OrtResult(
7171
)
7272
)
7373
),
74-
provenance = ANALYZED_PROVENANCE,
75-
vcsProcessed = ANALYZED_VCS
74+
provenance = ANALYZED_PROVENANCE
7675
),
7776
analyzer = AnalyzerRun.EMPTY.copy(
7877
result = AnalyzerResult(

plugins/reporters/static-html/src/main/kotlin/TablesReportModelMapper.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ internal object TablesReportModelMapper {
4848
.sortedBy { it.id }
4949

5050
return TablesReport(
51-
input.ortResult.repository.vcsProcessed,
51+
input.ortResult.repository.provenance.vcsInfo.normalize(),
5252
input.ortResult.repository.config,
5353
ruleViolations,
5454
getAnalyzerIssueSummaryTable(input),

scanner/src/funTest/kotlin/scanners/ScannerIntegrationFunTest.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import org.ossreviewtoolkit.model.PackageReference
3636
import org.ossreviewtoolkit.model.PackageType
3737
import org.ossreviewtoolkit.model.Project
3838
import org.ossreviewtoolkit.model.Repository
39+
import org.ossreviewtoolkit.model.RepositoryProvenance
3940
import org.ossreviewtoolkit.model.ScanSummary
4041
import org.ossreviewtoolkit.model.Scope
4142
import org.ossreviewtoolkit.model.TextLocation
@@ -167,8 +168,10 @@ private fun createAnalyzerResultWithProject(project: Project, vararg packages: P
167168
return OrtResult.EMPTY.copy(
168169
analyzer = analyzerRun,
169170
repository = Repository.EMPTY.copy(
170-
vcsProcessed = projectWithScope.vcsProcessed,
171-
vcs = projectWithScope.vcs
171+
provenance = RepositoryProvenance(
172+
vcsInfo = projectWithScope.vcs,
173+
resolvedRevision = projectWithScope.vcs.revision
174+
)
172175
)
173176
)
174177
}

0 commit comments

Comments
 (0)