From 7f415f9e20da4d4c7e10802f591fe1754a74afca 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 --- build.sbt | 11 ++++++++++ .../analysis/cli/command/AnalyseCommand.scala | 11 +++++----- .../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 ++++++--- .../analysis/cli/AnalyseExecutorSpec.scala | 3 ++- .../analysis/cli/formatter/SarifSpec.scala | 2 +- project/Common.scala | 2 +- 9 files changed, 53 insertions(+), 24 deletions(-) diff --git a/build.sbt b/build.sbt index 6b5c5072..de9eb750 100644 --- a/build.sbt +++ b/build.sbt @@ -70,6 +70,17 @@ lazy val codacyAnalysisCli = project name := "codacy-analysis-cli", coverageExcludedPackages := ";com\\.codacy\\..*CLIError.*", Common.dockerSettings, + Compile / sourceGenerators += Def.task { + val file = (Compile / sourceManaged).value / "com" / "codacy" / "cli" / "Versions.scala" + IO.write( + file, + s"""package com.codacy.cli + |object Versions { + | val cliVersion: String = "${version.value}" + |} + |""".stripMargin) + Seq(file) + }.taskValue, Common.genericSettings, Universal / javaOptions ++= Seq("-XX:MinRAMPercentage=60.0", "-XX:MaxRAMPercentage=90.0"), publish := (Docker / publish).value, diff --git a/cli/src/main/scala/com/codacy/analysis/cli/command/AnalyseCommand.scala b/cli/src/main/scala/com/codacy/analysis/cli/command/AnalyseCommand.scala index 9310abac..0476e43f 100644 --- a/cli/src/main/scala/com/codacy/analysis/cli/command/AnalyseCommand.scala +++ b/cli/src/main/scala/com/codacy/analysis/cli/command/AnalyseCommand.scala @@ -55,7 +55,10 @@ object AnalyseCommand { val configuration: CLIConfiguration = CLIConfiguration(codacyClientOpt, environment, analyze, new CodacyConfigurationFileLoader) val formatter: Formatter = - Formatter(configuration.analysis.output, environment.baseProjectDirectory(analyze.directory)) + Formatter( + configuration.analysis.output, + environment.baseProjectDirectory(analyze.directory), + version = com.codacy.cli.Versions.cliVersion) val fileCollector: FileCollector[Try] = FileCollector.defaultCollector() val toolSelector = new ToolSelector(toolRepository(apiUrl)) @@ -117,8 +120,7 @@ class AnalyseCommand(analyze: Analyze, .fold( { _ => Right(()) - }, - { repository => + }, { repository => for { _ <- validateNoUncommitedChanges(repository, configuration.upload.upload) _ <- validateGitCommitUuid(repository, analyze.commitUuid, analyze.skipCommitUuidValidationValue) @@ -130,8 +132,7 @@ class AnalyseCommand(analyze: Analyze, repository.uncommitedFiles.fold( { _ => Right(()) - }, - { uncommitedFiles => + }, { uncommitedFiles => if (uncommitedFiles.nonEmpty) { val error: CLIError = CLIError.UncommitedChanges(uncommitedFiles) if (upload) { 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..17bdaaf7 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: 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: 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..30fa30e1 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: 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..00b6bc73 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: 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..c7bf214d 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: String): Formatter = + new Text(printStream, version) } -private[formatter] class Text(val stream: PrintStream) extends Formatter { +private[formatter] class Text(val stream: PrintStream, version: String) extends Formatter { override def begin(): Unit = { stream.println("Starting analysis ...") + stream.println(s"Running codacy-analysis-cli version $version") stream.flush() } diff --git a/cli/src/test/scala/com/codacy/analysis/cli/AnalyseExecutorSpec.scala b/cli/src/test/scala/com/codacy/analysis/cli/AnalyseExecutorSpec.scala index a606bc04..0193e8a6 100644 --- a/cli/src/test/scala/com/codacy/analysis/cli/AnalyseExecutorSpec.scala +++ b/cli/src/test/scala/com/codacy/analysis/cli/AnalyseExecutorSpec.scala @@ -162,7 +162,8 @@ class AnalyseExecutorSpec extends Specification with NoLanguageFeatures with Moc } private def runAnalyseExecutor(analyserName: String, configuration: CLIConfiguration.Analysis) = { - val formatter: Formatter = Formatter(configuration.output, configuration.projectDirectory) + val formatter: Formatter = + Formatter(configuration.output, configuration.projectDirectory, version = Some("test-version")) val analyser: Analyser[Try] = Analyser(analyserName) val fileCollector: FileCollector[Try] = FileCollector.defaultCollector() diff --git a/cli/src/test/scala/com/codacy/analysis/cli/formatter/SarifSpec.scala b/cli/src/test/scala/com/codacy/analysis/cli/formatter/SarifSpec.scala index d2c2a5aa..fbb6ba46 100644 --- a/cli/src/test/scala/com/codacy/analysis/cli/formatter/SarifSpec.scala +++ b/cli/src/test/scala/com/codacy/analysis/cli/formatter/SarifSpec.scala @@ -115,7 +115,7 @@ class SarifSpec extends Specification with NoLanguageFeatures { // Act val bos = new ByteArrayOutputStream() val printStream = new PrintStream(bos) - val formatter = Sarif(printStream, sourceDirectory, reduceIssueSeverity) + val formatter = Sarif(printStream, sourceDirectory, reduceIssueSeverity, Some("test-version")) formatter.begin() formatter.addAll( toolSpecification = Option(toolSpecification), diff --git a/project/Common.scala b/project/Common.scala index 81ac00f8..96cc6e1d 100644 --- a/project/Common.scala +++ b/project/Common.scala @@ -2,7 +2,7 @@ import com.typesafe.sbt.packager.Keys._ import com.typesafe.sbt.packager.docker.DockerPlugin.autoImport.Docker import com.typesafe.sbt.packager.docker.{Cmd, CmdLike, DockerAlias} import sbt.Keys._ -import sbt.{Def, _} +import sbt.{Compile, Def, IO, _} import scala.util.Properties