Skip to content

Commit

Permalink
clean up scapegoat warnings and reduce instance creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Johnnei committed Nov 26, 2023
1 parent 3191d1f commit 055d6a1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,26 @@ object GitlabCodeQualityReportWriter extends ReportWriter {

override protected def fileName: String = "scapegoat-gitlab.json"

override protected def generate(feedback: Feedback): String =
toCodeQualityElements(feedback.warningsWithMinimalLevel, sys.env.get("CI_PROJECT_DIR"))
override protected def generate(feedback: Feedback): String = {
val md5Digest = MessageDigest.getInstance("MD5")
toCodeQualityElements(feedback.warningsWithMinimalLevel, sys.env.get("CI_PROJECT_DIR"), md5Digest)
.map(_.toJsonArrayElement)
.mkString("[", ",", "]")
}

def toCodeQualityElements(
private[io] def toCodeQualityElements(
warnings: Seq[Warning],
gitlabBuildDir: Option[String]
gitlabBuildDir: Option[String],
messageDigest: MessageDigest
): Seq[CodeQualityReportElement] = warnings.map { warning =>
// Stable hash for the same warning.
// Avoids moving code blocks around from causing "new" detecions.
val fingerprintRaw = warning.sourceFileNormalized + warning.snippet.getOrElse(warning.line.toString)
val fingerprint = MessageDigest
.getInstance("MD5")
.digest(fingerprintRaw.getBytes(StandardCharsets.UTF_8))

messageDigest.reset()
messageDigest.update(fingerprintRaw.getBytes(StandardCharsets.UTF_8))
val fingerprint = messageDigest
.digest()
.map("%02x".format(_))
.mkString

Expand Down Expand Up @@ -84,11 +89,11 @@ case object CriticalSeverity extends CodeClimateSeverity {
override val name: String = "critical"
}

case class Location(path: String, lines: Lines)
final case class Location(path: String, lines: Lines)

case class Lines(begin: Int)
final case class Lines(begin: Int)

case class CodeQualityReportElement(
final case class CodeQualityReportElement(
description: String,
checkName: String,
severity: CodeClimateSeverity,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.sksamuel.scapegoat.io

import java.security.MessageDigest

import com.sksamuel.scapegoat.{Levels, Warning}
import org.scalatest.freespec.AnyFreeSpec
import org.scalatest.matchers.should.Matchers
Expand All @@ -19,26 +21,7 @@ class GitlabCodeQualityReportWriterTest extends AnyFreeSpec with Matchers {
Some("File.this.d.get"),
"Using Option.get defeats the purpose",
"com.sksamuel.scapegoat.inspections.option.OptionGet"
)
)

val report = GitlabCodeQualityReportWriter
.toCodeQualityElements(warnings, Some("/home/johnnei/git/scapegoat"))
report should be(
Seq(
CodeQualityReportElement(
"Use of Option.get. Using Option.get defeats the purpose",
"com.sksamuel.scapegoat.inspections.option.OptionGet",
CriticalSeverity,
Location("src/main/scala/com/sksamuel/File.scala", Lines(13)),
"909b14c15a3a3891659251f133058264"
)
)
)
}

"should transform feedback without duplicate text" in {
val warnings = Seq(
),
Warning(
"List.size is O(n)",
13,
Expand All @@ -52,10 +35,23 @@ class GitlabCodeQualityReportWriterTest extends AnyFreeSpec with Matchers {
)

val report = GitlabCodeQualityReportWriter
.toCodeQualityElements(warnings, Some("/home/johnnei/git/scapegoat"))
.toCodeQualityElements(
warnings,
Some("/home/johnnei/git/scapegoat"),
MessageDigest.getInstance("MD5")
)
report should be(
Seq(
CodeQualityReportElement(
// Extra dot after warning text to improve readability
"Use of Option.get. Using Option.get defeats the purpose",
"com.sksamuel.scapegoat.inspections.option.OptionGet",
CriticalSeverity,
Location("src/main/scala/com/sksamuel/File.scala", Lines(13)),
"909b14c15a3a3891659251f133058264"
),
CodeQualityReportElement(
// Warning text is trimmed to avoid duplicate text
"List.size is O(n). Consider using...",
"com.sksamuel.scapegoat.inspections.collections.ListSize",
InfoSeverity,
Expand Down

0 comments on commit 055d6a1

Please sign in to comment.