|
| 1 | +/* |
| 2 | + * Copyright (C) 2026 The ORT Project Copyright Holders <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.packagecurationproviders.ortconfig |
| 21 | + |
| 22 | +import java.io.File |
| 23 | +import java.io.IOException |
| 24 | + |
| 25 | +import org.apache.logging.log4j.kotlin.logger |
| 26 | + |
| 27 | +import org.ossreviewtoolkit.model.Identifier |
| 28 | +import org.ossreviewtoolkit.model.Package |
| 29 | +import org.ossreviewtoolkit.model.PackageCuration |
| 30 | +import org.ossreviewtoolkit.model.VcsInfo |
| 31 | +import org.ossreviewtoolkit.model.VcsType |
| 32 | +import org.ossreviewtoolkit.model.readValue |
| 33 | +import org.ossreviewtoolkit.plugins.api.OrtPlugin |
| 34 | +import org.ossreviewtoolkit.plugins.api.OrtPluginOption |
| 35 | +import org.ossreviewtoolkit.plugins.api.PluginDescriptor |
| 36 | +import org.ossreviewtoolkit.plugins.packagecurationproviders.api.PackageCurationProvider |
| 37 | +import org.ossreviewtoolkit.plugins.packagecurationproviders.api.PackageCurationProviderFactory |
| 38 | +import org.ossreviewtoolkit.plugins.versioncontrolsystems.git.GitFactory |
| 39 | +import org.ossreviewtoolkit.utils.common.div |
| 40 | +import org.ossreviewtoolkit.utils.common.encodeOr |
| 41 | +import org.ossreviewtoolkit.utils.common.fileSystemEncode |
| 42 | +import org.ossreviewtoolkit.utils.common.safeMkdirs |
| 43 | +import org.ossreviewtoolkit.utils.ort.ortDataDirectory |
| 44 | + |
| 45 | +data class GitPackageCurationProviderConfig( |
| 46 | + /** The URL of the repository containing the curations. */ |
| 47 | + val repositoryUrl: String, |
| 48 | + |
| 49 | + /** The optional revision to use. If not specified, the default branch is used. */ |
| 50 | + val revision: String?, |
| 51 | + |
| 52 | + /** The path that contains the package curations. */ |
| 53 | + @OrtPluginOption(defaultValue = "curations") |
| 54 | + val path: String |
| 55 | +) |
| 56 | + |
| 57 | +/** |
| 58 | + * A [PackageCurationProvider] that loads [PackageCuration]s from the configured Git repository. |
| 59 | + * |
| 60 | + * ### Path layout |
| 61 | + * |
| 62 | + * The provider requires that the curation files follow the same path layout as in the |
| 63 | + * [ort-config repository](https://github.com/oss-review-toolkit/ort-config#curations): |
| 64 | + * |
| 65 | + * * Files with curations for a specific package must be located at `[type]/[namespace]/[name].yml`, based on the |
| 66 | + * identifier of the package. If a component of the identifier is empty, `_` is used as a placeholder. For example, |
| 67 | + * for the package `NuGet::Azure.Core:1.2.0`, the curation file must be located at `NuGet/_/Azure.Core.yml`. |
| 68 | + * * Files with curations that match all packages within a namespace must be located at `[type]/[namespace]/_.yml`. |
| 69 | + * |
| 70 | + * Namespace-scoped curations are loaded before package-scoped curations, so that the latter can override the former. |
| 71 | + */ |
| 72 | +@OrtPlugin( |
| 73 | + displayName = "Git Repository", |
| 74 | + summary = "A package curation provider that loads package curations from a Git repository.", |
| 75 | + factory = PackageCurationProviderFactory::class |
| 76 | +) |
| 77 | +open class GitPackageCurationProvider( |
| 78 | + override val descriptor: PluginDescriptor = GitPackageCurationProviderFactory.descriptor, |
| 79 | + private val config: GitPackageCurationProviderConfig |
| 80 | +) : PackageCurationProvider { |
| 81 | + init { |
| 82 | + require(config.repositoryUrl.isNotBlank()) { "The repository URL must not be blank." } |
| 83 | + } |
| 84 | + |
| 85 | + private val repositoryDir by lazy { |
| 86 | + // Use a stable cache path to clone the repository to speed up subsequent runs. |
| 87 | + (ortDataDirectory / "cache" / "git-package-curation-provider" / config.repositoryUrl.fileSystemEncode()).also { |
| 88 | + updateRepository(it) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private val curationsDir by lazy { repositoryDir / config.path } |
| 93 | + |
| 94 | + override fun getCurationsFor(packages: Collection<Package>): Set<PackageCuration> = |
| 95 | + packages.flatMapTo(mutableSetOf()) { pkg -> getCurationsFor(pkg.id) } |
| 96 | + |
| 97 | + private fun getCurationsFor(pkgId: Identifier): List<PackageCuration> { |
| 98 | + // The Git repository has to follow path layout conventions, so curations can be looked up directly. |
| 99 | + val packageCurationsFile = curationsDir / pkgId.toCurationPath() |
| 100 | + |
| 101 | + // Also consider curations for all packages in a namespace. |
| 102 | + val namespaceCurationsFile = packageCurationsFile.resolveSibling("_.yml") |
| 103 | + |
| 104 | + // Return namespace-level curations before package-level curations to allow overriding the former. |
| 105 | + return listOf(namespaceCurationsFile, packageCurationsFile).filter { it.isFile }.flatMap { file -> |
| 106 | + runCatching { |
| 107 | + file.readValue<List<PackageCuration>>().filter { it.isApplicable(pkgId) } |
| 108 | + }.getOrElse { |
| 109 | + throw IOException("Failed parsing package curation from '${file.absolutePath}'.", it) |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private fun updateRepository(dir: File) { |
| 115 | + val vcsInfo = VcsInfo.EMPTY.copy(type = VcsType.GIT, url = config.repositoryUrl) |
| 116 | + dir.safeMkdirs() |
| 117 | + |
| 118 | + GitFactory.create().apply { |
| 119 | + val workingTree = initWorkingTree(dir, vcsInfo) |
| 120 | + val revision = config.revision ?: getDefaultBranchName(config.repositoryUrl) |
| 121 | + val clonedRevision = updateWorkingTree(workingTree, revision).getOrThrow() |
| 122 | + |
| 123 | + logger.info { |
| 124 | + buildString { |
| 125 | + append("Successfully cloned revision $clonedRevision ") |
| 126 | + if (revision != clonedRevision) append("(from $revision) ") |
| 127 | + append("of ${config.repositoryUrl}.") |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +/** |
| 135 | + * The path must be aligned with the |
| 136 | + * [conventions for the ort-config repository](https://github.com/oss-review-toolkit/ort-config#curations). |
| 137 | + */ |
| 138 | +fun Identifier.toCurationPath() = "${type.encodeOr("_")}/${namespace.encodeOr("_")}/${name.encodeOr("_")}.yml" |
0 commit comments