From 45bb1b70e9d45462738e83911b3dd28b6d1ee6a4 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 start of execution and always pull image changes --- bin/codacy-analysis-cli.sh | 1 + build.sbt | 11 +++++++++++ .../analysis/cli/command/AnalyseCommand.scala | 14 ++++++++------ .../codacy/analysis/cli/command/CLIApp.scala | 13 ++++--------- .../analysis/cli/formatter/Formatter.scala | 1 + .../codacy/analysis/cli/formatter/Sarif.scala | 17 +++++++++-------- .../analysis/cli/AnalyseExecutorSpec.scala | 3 ++- project/Common.scala | 2 +- 8 files changed, 37 insertions(+), 25 deletions(-) diff --git a/bin/codacy-analysis-cli.sh b/bin/codacy-analysis-cli.sh index b4f82617..8cd09143 100755 --- a/bin/codacy-analysis-cli.sh +++ b/bin/codacy-analysis-cli.sh @@ -31,6 +31,7 @@ run() { output_volume="--volume ${OUTPUT_DIRECTORY}:${OUTPUT_DIRECTORY}"; fi local CODACY_ANALYSIS_CLI_VERSION="${CODACY_ANALYSIS_CLI_VERSION:-stable}" + docker pull codacy/codacy-analysis-cli:${CODACY_ANALYSIS_CLI_VERSION} docker run \ --rm \ --env CODACY_CODE="$CODACY_CODE" \ 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..5b777417 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 @@ -1,7 +1,6 @@ package com.codacy.analysis.cli.command import java.nio.file.Path - import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http.scaladsl.model.{HttpRequest, HttpResponse} @@ -17,7 +16,7 @@ import com.codacy.analysis.cli.analysis.AnalyseExecutor.{ import com.codacy.analysis.cli.analysis.{AnalyseExecutor, ExitStatus, ToolSelector} import com.codacy.analysis.cli.clients.Credentials import com.codacy.analysis.cli.configuration.{CLIConfiguration, Environment} -import com.codacy.analysis.cli.formatter.Formatter +import com.codacy.analysis.cli.formatter.{Formatter, Text} import com.codacy.analysis.clientapi.tools.ToolsClient import com.codacy.analysis.core.analysis.Analyser import com.codacy.analysis.core.clients.CodacyClient @@ -28,6 +27,7 @@ import com.codacy.analysis.core.model._ import com.codacy.analysis.core.upload.ResultsUploader import com.codacy.analysis.core.utils.Logger import com.codacy.analysis.core.utils.SeqOps._ +import com.codacy.cli.Versions import com.codacy.toolRepository.remote.ToolRepositoryRemote import com.codacy.toolRepository.remote.storage.{PatternSpecDataStorage, ToolSpecDataStorage} import org.log4s.getLogger @@ -94,6 +94,10 @@ class AnalyseCommand(analyze: Analyze, Logger.setLevel(analyze.options.verboseValue) + if (analyze.options.verboseValue || configuration.analysis.output.format == Text.name) { + Console.println(s"Running codacy-analysis-cli version ${Versions.cliVersion}") + } + private val logger: org.log4s.Logger = getLogger def run(): ExitStatus.ExitCode = { @@ -117,8 +121,7 @@ class AnalyseCommand(analyze: Analyze, .fold( { _ => Right(()) - }, - { repository => + }, { repository => for { _ <- validateNoUncommitedChanges(repository, configuration.upload.upload) _ <- validateGitCommitUuid(repository, analyze.commitUuid, analyze.skipCommitUuidValidationValue) @@ -130,8 +133,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/command/CLIApp.scala b/cli/src/main/scala/com/codacy/analysis/cli/command/CLIApp.scala index 54531495..7181a5ff 100644 --- a/cli/src/main/scala/com/codacy/analysis/cli/command/CLIApp.scala +++ b/cli/src/main/scala/com/codacy/analysis/cli/command/CLIApp.scala @@ -7,8 +7,9 @@ import com.codacy.analysis.cli.analysis.ExitStatus import com.codacy.analysis.cli.command.ArgumentParsers._ import com.codacy.analysis.cli.formatter.Formatter import com.codacy.analysis.core.analysis.Analyser -import com.codacy.analysis.core.clients.{ProjectName, UserName, OrganizationProvider} +import com.codacy.analysis.core.clients.{OrganizationProvider, ProjectName, UserName} import com.codacy.analysis.core.git.Commit +import com.codacy.cli.Versions import scala.concurrent.duration.Duration import scala.util.matching.Regex @@ -77,14 +78,8 @@ object ArgumentParsers { } } -object Version { - - val version: String = - Option(getClass.getPackage.getImplementationVersion).getOrElse("0.1.0-SNAPSHOT") -} - @AppName("Codacy Analysis Cli") -@AppVersion(Version.version) +@AppVersion(Versions.cliVersion) @ProgName("codacy-analysis-cli") final case class DefaultCommand( @ExtraName("v") @ValueDescription("Prints the version of the program") @@ -95,7 +90,7 @@ final case class DefaultCommand( def run(): Unit = { if (versionValue) { - Console.println(s"codacy-analysis-cli is on version ${Version.version}") + Console.println(s"codacy-analysis-cli is on version ${Versions.cliVersion}") } } } 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..964e97b3 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,6 +10,7 @@ import org.log4s.{Logger, getLogger} trait FormatterCompanion { def name: String + def apply(printStream: PrintStream, executionDirectory: File, ghCodeScanningCompat: Boolean): Formatter } 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..b7484c8b 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 @@ -123,14 +123,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/test/scala/com/codacy/analysis/cli/AnalyseExecutorSpec.scala b/cli/src/test/scala/com/codacy/analysis/cli/AnalyseExecutorSpec.scala index a606bc04..1f2d5e9a 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) val analyser: Analyser[Try] = Analyser(analyserName) val fileCollector: FileCollector[Try] = FileCollector.defaultCollector() 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