From 8f0550a160c7e4efae2f98b95632caab4a7330da Mon Sep 17 00:00:00 2001 From: Pedro Amaral Date: Wed, 18 Aug 2021 15:25:34 +0100 Subject: [PATCH] feature: print version on the start of Text output --- .../cli/configuration/Environment.scala | 4 ++++ .../analysis/cli/formatter/Formatter.scala | 11 +++++++--- .../codacy/analysis/cli/formatter/Json.scala | 5 ++++- .../codacy/analysis/cli/formatter/Sarif.scala | 22 +++++++++++-------- .../codacy/analysis/cli/formatter/Text.scala | 10 ++++++--- project/Common.scala | 2 +- 6 files changed, 37 insertions(+), 17 deletions(-) diff --git a/cli/src/main/scala/com/codacy/analysis/cli/configuration/Environment.scala b/cli/src/main/scala/com/codacy/analysis/cli/configuration/Environment.scala index 73bab5bd..30fba70d 100644 --- a/cli/src/main/scala/com/codacy/analysis/cli/configuration/Environment.scala +++ b/cli/src/main/scala/com/codacy/analysis/cli/configuration/Environment.scala @@ -12,6 +12,10 @@ class Environment(variables: Map[String, String]) { private val logger: Logger = getLogger + def appVersionEnvironmentVariable(): Option[String] = { + validate("Application version", "environment variable", "APP_VERSION")(variables.get("APP_VERSION")) + } + def codeDirectoryEnvironmentVariable(): Option[String] = { validate("Project directory", "environment variable", "CODACY_CODE")(variables.get("CODACY_CODE")) } diff --git a/cli/src/main/scala/com/codacy/analysis/cli/formatter/Formatter.scala b/cli/src/main/scala/com/codacy/analysis/cli/formatter/Formatter.scala index 78451314..0677b9d2 100644 --- a/cli/src/main/scala/com/codacy/analysis/cli/formatter/Formatter.scala +++ b/cli/src/main/scala/com/codacy/analysis/cli/formatter/Formatter.scala @@ -10,7 +10,11 @@ import org.log4s.{Logger, getLogger} trait FormatterCompanion { def name: String - def apply(printStream: PrintStream, executionDirectory: File, ghCodeScanningCompat: Boolean): Formatter + + def apply(printStream: PrintStream, + executionDirectory: File, + ghCodeScanningCompat: Boolean, + version: Option[String]): Formatter } trait Formatter { @@ -40,7 +44,8 @@ object Formatter { def apply(outputConfiguration: CLIConfiguration.Output, executionDirectory: File, - printStream: Option[PrintStream] = Option.empty): Formatter = { + printStream: Option[PrintStream] = Option.empty, + version: Option[String]): Formatter = { val stream = outputConfiguration.file.map(asPrintStream).orElse(printStream).getOrElse(defaultPrintStream) @@ -50,7 +55,7 @@ object Formatter { defaultFormatter } - formatterBuilder(stream, executionDirectory, outputConfiguration.ghCodeScanningCompat) + formatterBuilder(stream, executionDirectory, outputConfiguration.ghCodeScanningCompat, version) } private def asPrintStream(file: File) = { diff --git a/cli/src/main/scala/com/codacy/analysis/cli/formatter/Json.scala b/cli/src/main/scala/com/codacy/analysis/cli/formatter/Json.scala index b59060f7..aea6299e 100644 --- a/cli/src/main/scala/com/codacy/analysis/cli/formatter/Json.scala +++ b/cli/src/main/scala/com/codacy/analysis/cli/formatter/Json.scala @@ -15,7 +15,10 @@ import scala.util.Properties object Json extends FormatterCompanion { override val name: String = "json" - override def apply(printStream: PrintStream, executionDirectory: File, ghCodeScanningCompat: Boolean): Formatter = + override def apply(printStream: PrintStream, + executionDirectory: File, + ghCodeScanningCompat: Boolean, + version: Option[String]): Formatter = new Json(printStream) } diff --git a/cli/src/main/scala/com/codacy/analysis/cli/formatter/Sarif.scala b/cli/src/main/scala/com/codacy/analysis/cli/formatter/Sarif.scala index 163f713b..a2f3c531 100644 --- a/cli/src/main/scala/com/codacy/analysis/cli/formatter/Sarif.scala +++ b/cli/src/main/scala/com/codacy/analysis/cli/formatter/Sarif.scala @@ -20,7 +20,10 @@ import scala.util.matching.Regex object Sarif extends FormatterCompanion { override val name: String = "sarif" - override def apply(printStream: PrintStream, executionDirectory: File, ghCodeScanningCompat: Boolean): Formatter = + override def apply(printStream: PrintStream, + executionDirectory: File, + ghCodeScanningCompat: Boolean, + version: Option[String]): Formatter = new Sarif(printStream, executionDirectory, ghCodeScanningCompat) } @@ -123,14 +126,15 @@ private[formatter] class Sarif(val stream: PrintStream, val executionDirectory: (for { issue <- issues.groupBy(_.patternId.value).collect { case (_, issue :: _) => issue } modelPattern <- patternsMap.get(issue.patternId.value) - } yield SarifReport.Rule( - id = issue.patternId.value, - name = modelPattern.title, - shortDescription = SarifReport.Message(modelPattern.description.getOrElse(modelPattern.title)), - help = SarifReport - .Message(text = modelPattern.description.getOrElse(modelPattern.title), markdown = modelPattern.explanation), - properties = - SarifReport.RuleProperties(category = issue.category.getOrElse(Pattern.Category.CodeStyle).toString))).toList + } yield + SarifReport.Rule( + id = issue.patternId.value, + name = modelPattern.title, + shortDescription = SarifReport.Message(modelPattern.description.getOrElse(modelPattern.title)), + help = SarifReport + .Message(text = modelPattern.description.getOrElse(modelPattern.title), markdown = modelPattern.explanation), + properties = + SarifReport.RuleProperties(category = issue.category.getOrElse(Pattern.Category.CodeStyle).toString))).toList } private def createArtifacts(issues: Seq[Issue]): List[SarifReport.Artifact] = { diff --git a/cli/src/main/scala/com/codacy/analysis/cli/formatter/Text.scala b/cli/src/main/scala/com/codacy/analysis/cli/formatter/Text.scala index 4806af13..8f6391fc 100644 --- a/cli/src/main/scala/com/codacy/analysis/cli/formatter/Text.scala +++ b/cli/src/main/scala/com/codacy/analysis/cli/formatter/Text.scala @@ -11,14 +11,18 @@ import com.codacy.plugins.api.{PatternDescription, results} object Text extends FormatterCompanion { override val name: String = "text" - override def apply(printStream: PrintStream, executionDirectory: File, ghCodeScanningCompat: Boolean): Formatter = - new Text(printStream) + override def apply(printStream: PrintStream, + executionDirectory: File, + ghCodeScanningCompat: Boolean, + version: Option[String]): Formatter = + new Text(printStream, version) } -private[formatter] class Text(val stream: PrintStream) extends Formatter { +private[formatter] class Text(val stream: PrintStream, version: Option[String]) extends Formatter { override def begin(): Unit = { stream.println("Starting analysis ...") + version.map(ver => stream.println(s"Running codacy-analysis-cli version $ver")) stream.flush() } diff --git a/project/Common.scala b/project/Common.scala index 81ac00f8..cf140b0b 100644 --- a/project/Common.scala +++ b/project/Common.scala @@ -41,7 +41,7 @@ object Common { dockerCmd := Seq(), dockerCommands := dockerCommands.value.flatMap { case cmd @ Cmd("WORKDIR", _) => - Seq(Cmd("RUN", "apk add --no-cache --update bash docker"), cmd) + Seq(Cmd("ENV", s"APP_VERSION=${version.value}"), Cmd("RUN", "apk add --no-cache --update bash docker"), cmd) case other => List(other) })