Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions api/v1/mapping/src/commonMain/kotlin/ApiMappings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,9 @@ fun Package.mapToApi(
shortestDependencyPaths = shortestDependencyPaths.map { it.mapToApi() },
curations = curations,
sourceCodeOrigins = sourceCodeOrigins?.map { it.mapToApi() },
labels = labels
labels = labels,
detectedLicenses = detectedLicenses,
effectiveLicense = effectiveLicense
)

fun PackageCurationData.mapToApi() = ApiPackageCurationData(
Expand Down Expand Up @@ -864,7 +866,9 @@ fun Project.mapToApi() = ApiProject(
vcsProcessed = vcsProcessed.mapToApi(),
description = description,
homepageUrl = homepageUrl,
scopeNames = scopeNames
scopeNames = scopeNames,
detectedLicenses = detectedLicenses,
effectiveLicense = effectiveLicense
)

fun UserDisplayName.mapToApi() = ApiUserDisplayName(username = username, fullName = fullName)
Expand Down
4 changes: 3 additions & 1 deletion api/v1/model/src/commonMain/kotlin/Package.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ data class Package(
val shortestDependencyPaths: List<ShortestDependencyPath>,
val curations: List<PackageCuration>,
val sourceCodeOrigins: List<SourceCodeOrigin>? = null,
val labels: Map<String, String> = emptyMap()
val labels: Map<String, String> = emptyMap(),
val detectedLicenses: Set<String> = emptySet(),
val effectiveLicense: String? = null
)

/**
Expand Down
4 changes: 3 additions & 1 deletion api/v1/model/src/commonMain/kotlin/Project.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,7 @@ data class Project(
val vcsProcessed: VcsInfo,
val description: String,
val homepageUrl: String,
val scopeNames: Set<String>
val scopeNames: Set<String>,
val detectedLicenses: Set<String> = emptySet(),
val effectiveLicense: String? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,13 @@ class GetPackagesForAnalyzerRunQuery(
vcs = vcs,
vcsProcessed = vcsProcessed,
isMetadataOnly = resultRow[PackagesTable.isMetadataOnly],
isModified = resultRow[PackagesTable.isModified]
isModified = resultRow[PackagesTable.isModified],
detectedLicenses = resultRow[PackagesTable.detectedLicenses]
?.split(',')
?.filterNot { it.isEmpty() }
?.toSet()
.orEmpty(),
effectiveLicense = resultRow[PackagesTable.effectiveLicense]
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,13 @@ class GetProjectsForAnalyzerRunQuery(
vcsProcessed = vcsProcessed,
description = resultRow[ProjectsTable.description],
homepageUrl = resultRow[ProjectsTable.homepageUrl],
scopeNames = scopeNamesByProjectId[projectId].orEmpty()
scopeNames = scopeNamesByProjectId[projectId].orEmpty(),
detectedLicenses = resultRow[ProjectsTable.detectedLicenses]
?.split(',')
?.filterNot { it.isEmpty() }
?.toSet()
.orEmpty(),
effectiveLicense = resultRow[ProjectsTable.effectiveLicense]
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ private fun insertProject(

createProcessedDeclaredLicense(project.processedDeclaredLicense, projectDao = projectDao)

projectDao.detectedLicenses = project.detectedLicenses.joinToString(",").takeIf { it.isNotEmpty() }
projectDao.effectiveLicense = project.effectiveLicense

return projectDao
}

Expand Down Expand Up @@ -284,6 +287,9 @@ private fun insertPackage(

createProcessedDeclaredLicense(pkg.processedDeclaredLicense, pkgDao = pkgDao)

pkgDao.detectedLicenses = pkg.detectedLicenses.joinToString(",").takeIf { it.isNotEmpty() }
pkgDao.effectiveLicense = pkg.effectiveLicense

return pkgDao
}

Expand Down
12 changes: 11 additions & 1 deletion dao/src/main/kotlin/repositories/analyzerrun/PackagesTable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ object PackagesTable : SortableTable("packages") {
val isMetadataOnly = bool("is_metadata_only").default(false)
val isModified = bool("is_modified").default(false)
val sourceCodeOrigins = text("source_code_origins").nullable()
val detectedLicenses = text("detected_licenses").nullable()
val effectiveLicense = text("effective_license").nullable()
}

class PackageDao(id: EntityID<Long>) : LongEntity(id) {
Expand Down Expand Up @@ -185,6 +187,8 @@ class PackageDao(id: EntityID<Long>) : LongEntity(id) {
ProcessedDeclaredLicensesTable.packageId

var sourceCodeOrigins by PackagesTable.sourceCodeOrigins
var detectedLicenses by PackagesTable.detectedLicenses
var effectiveLicense by PackagesTable.effectiveLicense
val labels by PackageLabelDao referrersOn PackageLabelsTable.packageId

fun mapToModel() = Package(
Expand All @@ -206,6 +210,12 @@ class PackageDao(id: EntityID<Long>) : LongEntity(id) {
?.split(',')
?.filterNot { it.isEmpty() }
?.map { SourceCodeOrigin.valueOf(it) },
labels = labels.associate { it.key to it.value }
labels = labels.associate { it.key to it.value },
detectedLicenses = detectedLicenses
?.split(',')
?.filterNot { it.isEmpty() }
?.toSet()
.orEmpty(),
effectiveLicense = effectiveLicense
)
}
13 changes: 12 additions & 1 deletion dao/src/main/kotlin/repositories/analyzerrun/ProjectsTable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ object ProjectsTable : SortableTable("projects") {
val definitionFilePath = text("definition_file_path")
val description = text("description")
val homepageUrl = text("homepage_url")
val detectedLicenses = text("detected_licenses").nullable()
val effectiveLicense = text("effective_license").nullable()
}

class ProjectDao(id: EntityID<Long>) : LongEntity(id) {
Expand Down Expand Up @@ -134,6 +136,9 @@ class ProjectDao(id: EntityID<Long>) : LongEntity(id) {
val processedDeclaredLicense by ProcessedDeclaredLicenseDao backReferencedOn
ProcessedDeclaredLicensesTable.projectId

var detectedLicenses by ProjectsTable.detectedLicenses
var effectiveLicense by ProjectsTable.effectiveLicense

fun mapToModel() = Project(
identifier = identifier.mapToModel(),
cpe = cpe,
Expand All @@ -145,6 +150,12 @@ class ProjectDao(id: EntityID<Long>) : LongEntity(id) {
vcsProcessed = vcsProcessed.mapToModel(),
description = description,
homepageUrl = homepageUrl,
scopeNames = scopeNames.mapTo(mutableSetOf(), ProjectScopeDao::name)
scopeNames = scopeNames.mapTo(mutableSetOf(), ProjectScopeDao::name),
detectedLicenses = detectedLicenses
?.split(',')
?.filterNot { it.isEmpty() }
?.toSet()
.orEmpty(),
effectiveLicense = effectiveLicense
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ALTER TABLE packages
ADD COLUMN detected_licenses TEXT,
ADD COLUMN effective_license TEXT;

ALTER TABLE projects
ADD COLUMN detected_licenses TEXT,
ADD COLUMN effective_license TEXT;
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,38 @@ class DaoAnalyzerRunRepositoryTest : StringSpec({
dbExtension.db.dbQuery { ProjectsTable.selectAll().count() } shouldBe 1
}

"create should deduplicate packages when license fields differ" {
val pkg1 = createPackage(1).copy(
detectedLicenses = setOf("MIT"),
effectiveLicense = "MIT"
)
val pkg2 = createPackage(1).copy(
detectedLicenses = setOf("Apache-2.0"),
effectiveLicense = "Apache-2.0"
)

analyzerRunRepository.create(analyzerJobId, analyzerRun.copy(packages = setOf(pkg1)))
analyzerRunRepository.create(analyzerJobId, analyzerRun.copy(packages = setOf(pkg2)))

dbExtension.db.dbQuery { PackagesTable.selectAll().count() } shouldBe 1
}

"create should deduplicate projects when license fields differ" {
val project1 = project.copy(
detectedLicenses = setOf("MIT"),
effectiveLicense = "MIT"
)
val project2 = project.copy(
detectedLicenses = setOf("Apache-2.0"),
effectiveLicense = "Apache-2.0"
)

analyzerRunRepository.create(analyzerJobId, analyzerRun.copy(projects = setOf(project1)))
analyzerRunRepository.create(analyzerJobId, analyzerRun.copy(projects = setOf(project2)))

dbExtension.db.dbQuery { ProjectsTable.selectAll().count() } shouldBe 1
}

"create should handle unique constraint violations" {
val txCount = 64
withContext(Dispatchers.IO) {
Expand Down
13 changes: 10 additions & 3 deletions dao/src/testFixtures/kotlin/Fixtures.kt
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,9 @@ class Fixtures(private val db: Database) {
vcsProcessed = VcsInfo(RepositoryType.GIT, "https://example.com/git", "revision", ""),
description = "",
homepageUrl = "https://example.com",
scopeNames = setOf("compileClasspath", "runtimeClasspath")
scopeNames = setOf("compileClasspath", "runtimeClasspath"),
detectedLicenses = emptySet(),
effectiveLicense = null
)

fun createAnalyzerRun(
Expand Down Expand Up @@ -324,6 +326,7 @@ class Fixtures(private val db: Database) {
results = results
)

@Suppress("LongParameterList")
fun generatePackage(
identifier: Identifier,
authors: Set<String> = emptySet(),
Expand Down Expand Up @@ -356,7 +359,9 @@ class Fixtures(private val db: Database) {
"https://example.com/git",
"revision",
"path"
)
),
detectedLicenses: Set<String> = emptySet(),
effectiveLicense: String? = null
) = Package(
identifier = identifier,
purl = "pkg:${identifier.type}/${identifier.namespace}/${identifier.name}@${identifier.version}",
Expand All @@ -371,6 +376,8 @@ class Fixtures(private val db: Database) {
vcs = vcs,
vcsProcessed = vcsProcessed,
isMetadataOnly = false,
isModified = false
isModified = false,
detectedLicenses = detectedLicenses,
effectiveLicense = effectiveLicense
)
}
4 changes: 3 additions & 1 deletion model/src/commonMain/kotlin/runs/Package.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ data class Package(
val isMetadataOnly: Boolean = false,
val isModified: Boolean = false,
val sourceCodeOrigins: List<SourceCodeOrigin>? = null,
val labels: Map<String, String> = emptyMap()
val labels: Map<String, String> = emptyMap(),
val detectedLicenses: Set<String> = emptySet(),
val effectiveLicense: String? = null
)

/**
Expand Down
4 changes: 3 additions & 1 deletion model/src/commonMain/kotlin/runs/Project.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,7 @@ data class Project(
val vcsProcessed: VcsInfo,
val description: String,
val homepageUrl: String,
val scopeNames: Set<String>
val scopeNames: Set<String>,
val detectedLicenses: Set<String> = emptySet(),
val effectiveLicense: String? = null
)
Loading
Loading