From 23c4ae8b7802e5ee262717ee31b98dbc3de52649 Mon Sep 17 00:00:00 2001 From: Frank Viernau Date: Mon, 19 Sep 2022 12:32:13 +0200 Subject: [PATCH] ProjectSourceRule: Add a matcher for project VCS type The matcher can be used to enable a rule only for certain VCS types. Signed-off-by: Frank Viernau --- .../src/main/kotlin/ProjectSourceRule.kt | 21 +++++++++++++++++++ .../src/test/kotlin/ProjectSourceRuleTest.kt | 18 ++++++++++++++-- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/evaluator/src/main/kotlin/ProjectSourceRule.kt b/evaluator/src/main/kotlin/ProjectSourceRule.kt index 2db39da398b7..14b4fdd9c781 100644 --- a/evaluator/src/main/kotlin/ProjectSourceRule.kt +++ b/evaluator/src/main/kotlin/ProjectSourceRule.kt @@ -24,6 +24,7 @@ import java.io.File import org.ossreviewtoolkit.model.LicenseSource import org.ossreviewtoolkit.model.OrtResult import org.ossreviewtoolkit.model.RepositoryProvenance +import org.ossreviewtoolkit.model.VcsType import org.ossreviewtoolkit.model.utils.getRepositoryPath import org.ossreviewtoolkit.utils.common.FileMatcher @@ -91,6 +92,11 @@ open class ProjectSourceRule( FileMatcher.match(patterns.toList(), filepath) } + /** + * Return the [VcsType] of the project's code repository. + */ + fun projectSourceGetVcsType(): VcsType = ortResult.repository.vcsProcessed.type + /** * Return the file paths matching any of the given [glob expressions][patterns] with its file content matching * [contentPattern]. @@ -138,6 +144,21 @@ open class ProjectSourceRule( override fun matches(): Boolean = projectSourceFindFilesWithContent(contentPattern, *patterns).isNotEmpty() } + + /** + * A [RuleMatcher] that checks whether the [VcsType] of the project's code repository is contained in the given + * [vcsTypes]. + */ + fun projectSourceHasVcsType(vararg vcsTypes: VcsType): RuleMatcher = + object : RuleMatcher { + private val types = vcsTypes.toSet() + + override val description = + "projectSourceHasVcsType('${vcsTypes.joinToString()}')" + + override fun matches(): Boolean = + projectSourceGetVcsType() in types + } } private fun OrtResult.createResolver() = diff --git a/evaluator/src/test/kotlin/ProjectSourceRuleTest.kt b/evaluator/src/test/kotlin/ProjectSourceRuleTest.kt index 2e9fe808a643..40872016e455 100644 --- a/evaluator/src/test/kotlin/ProjectSourceRuleTest.kt +++ b/evaluator/src/test/kotlin/ProjectSourceRuleTest.kt @@ -149,6 +149,19 @@ class ProjectSourceRuleTest : WordSpec({ ) } } + + "projectSourceHasVcsType" should { + "return true if and only if any of the given VCS types match the VCS type of the project's code repository" { + val rule = createRule( + createSpecTempDir(), + createOrtResult(projectVcsType = VcsType.GIT) + ) + + rule.projectSourceHasVcsType(VcsType.GIT).matches() shouldBe true + rule.projectSourceHasVcsType(VcsType.GIT_REPO).matches() shouldBe false + rule.projectSourceHasVcsType(VcsType.GIT, VcsType.GIT_REPO).matches() shouldBe true + } + } }) private fun createRule(projectSourcesDir: File, ortResult: OrtResult = OrtResult.EMPTY) = @@ -182,11 +195,12 @@ private fun ortResultWithDetectedLicenses(vararg detectedLicensesForFilePath: Pa createOrtResult(detectedLicensesForFilePath.toMap()) private fun createOrtResult( - detectedLicensesForFilePath: Map> = emptyMap() + detectedLicensesForFilePath: Map> = emptyMap(), + projectVcsType: VcsType = VcsType.GIT ): OrtResult { val id = Identifier("Maven:org.oss-review-toolkit:example:1.0") val vcsInfo = VcsInfo( - type = VcsType.GIT, + type = projectVcsType, url = "https://github.com/oss-review-toolkit/example.git", revision = "0000000000000000000000000000000000000000" )