Skip to content

Commit

Permalink
Merge pull request #501 from codacy/improve-path-fixing-algo
Browse files Browse the repository at this point in the history
fix: Improve algorithm of path fixing ALA-975
  • Loading branch information
lventura-codacy authored Apr 9, 2024
2 parents 4f67ee3 + f7a9510 commit 47e73b6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
26 changes: 17 additions & 9 deletions src/main/scala/com/codacy/rules/ReportRules.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.codacy.parsers.CoverageParser
import com.codacy.plugins.api.languages.Languages
import com.codacy.rules.commituuid.CommitUUIDProvider
import com.codacy.rules.file.GitFileFetcher
import com.codacy.transformation.FileNameMatcher.getFilenameFromPath
import com.codacy.transformation.{GitFileNameUpdaterAndFilter, PathPrefixer, Transformation}
import wvlet.log.LogSupport

Expand All @@ -33,6 +34,7 @@ class ReportRules(coverageServices: => CoverageServices, gitFileFetcher: GitFile
commitUUID: String
): Either[String, String] = {
val finalConfig = config.copy(partial = partial)
val acceptableFileNames = gitFileFetcher.forCommit(commitUUID)

files
.map { file =>
Expand All @@ -42,7 +44,7 @@ class ReportRules(coverageServices: => CoverageServices, gitFileFetcher: GitFile
report <- CoverageParser.parse(rootProjectDir, file, forceParser = config.forceCoverageParser).map {
coverageResult =>
logger.info(s"Coverage parser used is ${coverageResult.parser}")
transform(coverageResult.report)(finalConfig, commitUUID)
transform(coverageResult.report)(finalConfig, commitUUID, acceptableFileNames)
}
_ <- storeReport(report, file)
language <- guessReportLanguage(finalConfig.languageOpt, report, file.getAbsolutePath)
Expand Down Expand Up @@ -323,15 +325,21 @@ class ReportRules(coverageServices: => CoverageServices, gitFileFetcher: GitFile
}
}

private def transform(report: CoverageReport)(config: ReportConfig, commitUUID: String): CoverageReport = {

val acceptableFileNames = gitFileFetcher.forCommit(commitUUID)

private def transform(
report: CoverageReport
)(config: ReportConfig, commitUUID: String, acceptableFileNames: Either[String, Seq[String]]): CoverageReport = {
val transformations: Seq[Transformation] = acceptableFileNames
.fold(error => {
logger.warn(s"Report files will not be matched against git files, reason: $error")
Seq(new PathPrefixer(config.prefix))
}, filenames => Seq(new PathPrefixer(config.prefix), new GitFileNameUpdaterAndFilter(filenames)))
.fold(
error => {
logger.warn(s"Report files will not be matched against git files, reason: $error")
Seq(new PathPrefixer(config.prefix))
},
filenames =>
Seq(new PathPrefixer(config.prefix), {
val acceptableFileNamesMap = filenames.groupBy(getFilenameFromPath).view.toMap
new GitFileNameUpdaterAndFilter(acceptableFileNamesMap)
})
)

transformations.foldLeft(report) { (report, transformation) =>
transformation.execute(report)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import scala.util.Try
object FileNameMatcher {

def matchAndReturnName(filename: String, fileNames: Seq[String]): Option[String] = {

fileNames
.filter(name => isTheSameFile(filename, name))
.sortBy(name => Math.abs(filename.length - name.length))
.headOption
}

private def getFilenameFromPath(filename: String): String = {
def getFilenameFromPath(filename: String): String = {
Try(Paths.get(filename).getFileName.toString).getOrElse(filename)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.codacy.transformation
import com.codacy.api.CoverageReport
import com.codacy.transformation.FileNameMatcher.getFilenameFromPath
import wvlet.log.LogSupport

class GitFileNameUpdaterAndFilter(acceptableFileNames: Seq[String]) extends Transformation with LogSupport {
class GitFileNameUpdaterAndFilter(acceptableFileNamesMap: Map[String, Seq[String]])
extends Transformation
with LogSupport {
override def execute(report: CoverageReport): CoverageReport = {
val fileReports = for {
fileReport <- report.fileReports
Expand All @@ -15,7 +18,7 @@ class GitFileNameUpdaterAndFilter(acceptableFileNames: Seq[String]) extends Tran

private def matchAndReturnName(filename: String): Option[String] = {
val maybeFilename = FileNameMatcher
.matchAndReturnName(filename, acceptableFileNames)
.matchAndReturnName(filename, acceptableFileNamesMap.getOrElse(getFilenameFromPath(filename), Seq.empty))

if (maybeFilename.isEmpty)
logger
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.codacy.transformation

import com.codacy.api.{CoverageFileReport, CoverageReport}
import com.codacy.transformation.FileNameMatcher.getFilenameFromPath
import org.scalatest.{Matchers, WordSpec}

class GitFileNameUpdaterAndFilterSpec extends WordSpec with Matchers {

private val acceptableFilenames =
Seq("src/folder/file1.txt", "src/another-folder/file1.txt", "src/folder/file2.txt", "src/folder/file3.txt")
private val updaterAndFilter = new GitFileNameUpdaterAndFilter(acceptableFilenames)
private val acceptableFilenamesMap = acceptableFilenames.groupBy(getFilenameFromPath).view.toMap
private val updaterAndFilter = new GitFileNameUpdaterAndFilter(acceptableFilenamesMap)

"execute" should {
"update and match filename" in {
Expand Down

0 comments on commit 47e73b6

Please sign in to comment.