Skip to content

Commit 87b0deb

Browse files
authored
fix: Skip only when none of auth mechanism are available (#490)
1 parent 85fb80b commit 87b0deb

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

src/main/scala/com/codacy/CodacyCoverageReporter.scala

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package com.codacy
22

33
import com.codacy.configuration.parser.{CommandConfiguration, ConfigurationParsingApp}
44
import com.codacy.di.Components
5-
import com.codacy.model.configuration.{FinalConfig, ReportConfig}
5+
import com.codacy.model.configuration.{Configuration, FinalConfig, ReportConfig}
66
import com.codacy.rules.ConfigurationRules
77
import wvlet.airframe.log
88
import wvlet.log.{LogSupport, Logger}
@@ -11,13 +11,16 @@ object CodacyCoverageReporter extends ConfigurationParsingApp with LogSupport {
1111
log.initNoColor
1212

1313
def run(commandConfig: CommandConfiguration): Int = {
14-
val noAvailableTokens = commandConfig.baseConfig.projectToken.isEmpty && commandConfig.baseConfig.apiToken.isEmpty
14+
val configRules = new ConfigurationRules(commandConfig, sys.env)
15+
16+
val noAvailableTokens =
17+
configRules.getProjectToken(commandConfig.baseConfig).isEmpty &&
18+
configRules.getApiToken(commandConfig.baseConfig).isEmpty
1519
if (commandConfig.baseConfig.skipValue && noAvailableTokens) {
1620
logger.info("Skip reporting coverage")
1721
0
1822
} else {
19-
val result: Either[String, String] = sendReport(commandConfig, sys.env)
20-
result match {
23+
sendReport(configRules.validatedConfig) match {
2124
case Right(message) =>
2225
logger.info(message)
2326
0
@@ -28,10 +31,8 @@ object CodacyCoverageReporter extends ConfigurationParsingApp with LogSupport {
2831
}
2932
}
3033

31-
private def sendReport(commandConfig: CommandConfiguration, envVars: Map[String, String]) = {
32-
val configRules = new ConfigurationRules(commandConfig, envVars)
33-
34-
configRules.validatedConfig.flatMap { validatedConfig =>
34+
private def sendReport(validatedConfig: Either[String, Configuration]) = {
35+
validatedConfig.flatMap { validatedConfig =>
3536
val components = new Components(validatedConfig)
3637

3738
if (validatedConfig.baseConfig.debug) {

src/main/scala/com/codacy/rules/ConfigurationRules.scala

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,18 @@ class ConfigurationRules(cmdConfig: CommandConfiguration, envVars: Map[String, S
8383
}
8484
}
8585

86+
def getProjectToken(baseCommandConfig: BaseCommandConfig): Option[String] =
87+
getValueOrEnvironmentVar(baseCommandConfig.projectToken, "CODACY_PROJECT_TOKEN")
88+
89+
def getApiToken(baseCommandConfig: BaseCommandConfig): Option[String] =
90+
getValueOrEnvironmentVar(baseCommandConfig.apiToken, "CODACY_API_TOKEN")
91+
8692
private def validateAuthConfig(baseCommandConfig: BaseCommandConfig): Either[String, AuthenticationConfig] = {
8793
val errorMessage =
8894
"Either a project or account API token must be provided or available in an environment variable"
8995

90-
val projectToken = getValueOrEnvironmentVar(baseCommandConfig.projectToken, "CODACY_PROJECT_TOKEN")
91-
val apiToken = getValueOrEnvironmentVar(baseCommandConfig.apiToken, "CODACY_API_TOKEN")
96+
val projectToken = getProjectToken(baseCommandConfig)
97+
val apiToken = getApiToken(baseCommandConfig)
9298

9399
if (projectToken.isDefined)
94100
validateProjectTokenAuth(projectToken)

0 commit comments

Comments
 (0)