Skip to content

Commit

Permalink
fix: Skip only when none of auth mechanism are available (#490)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrfyda authored Mar 22, 2024
1 parent 85fb80b commit 87b0deb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
17 changes: 9 additions & 8 deletions src/main/scala/com/codacy/CodacyCoverageReporter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.codacy

import com.codacy.configuration.parser.{CommandConfiguration, ConfigurationParsingApp}
import com.codacy.di.Components
import com.codacy.model.configuration.{FinalConfig, ReportConfig}
import com.codacy.model.configuration.{Configuration, FinalConfig, ReportConfig}
import com.codacy.rules.ConfigurationRules
import wvlet.airframe.log
import wvlet.log.{LogSupport, Logger}
Expand All @@ -11,13 +11,16 @@ object CodacyCoverageReporter extends ConfigurationParsingApp with LogSupport {
log.initNoColor

def run(commandConfig: CommandConfiguration): Int = {
val noAvailableTokens = commandConfig.baseConfig.projectToken.isEmpty && commandConfig.baseConfig.apiToken.isEmpty
val configRules = new ConfigurationRules(commandConfig, sys.env)

val noAvailableTokens =
configRules.getProjectToken(commandConfig.baseConfig).isEmpty &&
configRules.getApiToken(commandConfig.baseConfig).isEmpty
if (commandConfig.baseConfig.skipValue && noAvailableTokens) {
logger.info("Skip reporting coverage")
0
} else {
val result: Either[String, String] = sendReport(commandConfig, sys.env)
result match {
sendReport(configRules.validatedConfig) match {
case Right(message) =>
logger.info(message)
0
Expand All @@ -28,10 +31,8 @@ object CodacyCoverageReporter extends ConfigurationParsingApp with LogSupport {
}
}

private def sendReport(commandConfig: CommandConfiguration, envVars: Map[String, String]) = {
val configRules = new ConfigurationRules(commandConfig, envVars)

configRules.validatedConfig.flatMap { validatedConfig =>
private def sendReport(validatedConfig: Either[String, Configuration]) = {
validatedConfig.flatMap { validatedConfig =>
val components = new Components(validatedConfig)

if (validatedConfig.baseConfig.debug) {
Expand Down
10 changes: 8 additions & 2 deletions src/main/scala/com/codacy/rules/ConfigurationRules.scala
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,18 @@ class ConfigurationRules(cmdConfig: CommandConfiguration, envVars: Map[String, S
}
}

def getProjectToken(baseCommandConfig: BaseCommandConfig): Option[String] =
getValueOrEnvironmentVar(baseCommandConfig.projectToken, "CODACY_PROJECT_TOKEN")

def getApiToken(baseCommandConfig: BaseCommandConfig): Option[String] =
getValueOrEnvironmentVar(baseCommandConfig.apiToken, "CODACY_API_TOKEN")

private def validateAuthConfig(baseCommandConfig: BaseCommandConfig): Either[String, AuthenticationConfig] = {
val errorMessage =
"Either a project or account API token must be provided or available in an environment variable"

val projectToken = getValueOrEnvironmentVar(baseCommandConfig.projectToken, "CODACY_PROJECT_TOKEN")
val apiToken = getValueOrEnvironmentVar(baseCommandConfig.apiToken, "CODACY_API_TOKEN")
val projectToken = getProjectToken(baseCommandConfig)
val apiToken = getApiToken(baseCommandConfig)

if (projectToken.isDefined)
validateProjectTokenAuth(projectToken)
Expand Down

0 comments on commit 87b0deb

Please sign in to comment.