Skip to content

Commit

Permalink
feature: print version on the start of Text output
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrocodacy committed Aug 19, 2021
1 parent 7966f09 commit 7f415f9
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 24 deletions.
11 changes: 11 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ lazy val codacyAnalysisCli = project
name := "codacy-analysis-cli",
coverageExcludedPackages := "<empty>;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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -117,8 +120,7 @@ class AnalyseCommand(analyze: Analyze,
.fold(
{ _ =>
Right(())
},
{ repository =>
}, { repository =>
for {
_ <- validateNoUncommitedChanges(repository, configuration.upload.upload)
_ <- validateGitCommitUuid(repository, analyze.commitUuid, analyze.skipCommitUuidValidationValue)
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)

Expand All @@ -50,7 +55,7 @@ object Formatter {
defaultFormatter
}

formatterBuilder(stream, executionDirectory, outputConfiguration.ghCodeScanningCompat)
formatterBuilder(stream, executionDirectory, outputConfiguration.ghCodeScanningCompat, version)
}

private def asPrintStream(file: File) = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
22 changes: 13 additions & 9 deletions cli/src/main/scala/com/codacy/analysis/cli/formatter/Sarif.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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] = {
Expand Down
10 changes: 7 additions & 3 deletions cli/src/main/scala/com/codacy/analysis/cli/formatter/Text.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion project/Common.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 7f415f9

Please sign in to comment.