Skip to content

Commit

Permalink
ProjectSourceRule: Add a matcher for project VCS type
Browse files Browse the repository at this point in the history
The matcher can be used to enable a rule only for certain VCS types.

Signed-off-by: Frank Viernau <frank_viernau@epam.com>
  • Loading branch information
fviernau authored and tsteenbe committed Sep 19, 2022
1 parent 446d55a commit 23c4ae8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
21 changes: 21 additions & 0 deletions evaluator/src/main/kotlin/ProjectSourceRule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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].
Expand Down Expand Up @@ -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() =
Expand Down
18 changes: 16 additions & 2 deletions evaluator/src/test/kotlin/ProjectSourceRuleTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) =
Expand Down Expand Up @@ -182,11 +195,12 @@ private fun ortResultWithDetectedLicenses(vararg detectedLicensesForFilePath: Pa
createOrtResult(detectedLicensesForFilePath.toMap())

private fun createOrtResult(
detectedLicensesForFilePath: Map<String, Set<String>> = emptyMap()
detectedLicensesForFilePath: Map<String, Set<String>> = 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"
)
Expand Down

0 comments on commit 23c4ae8

Please sign in to comment.