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 18, 2021
1 parent 7966f09 commit 8f0550a
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
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: Option[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: Option[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: Option[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: Option[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: 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()
}

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

Expand Down

0 comments on commit 8f0550a

Please sign in to comment.