Skip to content

Commit

Permalink
Merge pull request #400 from codacy/Add_retry_process_when_sending_co…
Browse files Browse the repository at this point in the history
…verage_fails_CY-6106

Add retry process when sending coverage CY-6106
  • Loading branch information
stefanvacareanu7 authored Jul 12, 2022
2 parents 48ff66a + e3743af commit 0091caf
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 19 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ Usage: codacy-coverage-reporter report
--project-name | -p <your project name>
--codacy-api-base-url <the base URL for the Codacy API>
--commit-uuid <your commitUUID>
--http-timeout <Sets a specified read timeout value, in milliseconds, to be used when interacting with Codacy API>
--http-timeout <Sets a specified read timeout value, in milliseconds, to be used when interacting with Codacy API. By default, the value is 10 seconds>
--skip | -s <skip if token isn't defined>
--sleep-time <Sets a specified time, in milliseconds, to be used when waiting between retries. By default, the value is 10 seconds>
--num-retries <Sets a number of retries in case of failure. By default, the value is 3 times>
--language | -l <language associated with your coverage report>
--coverage-reports | -r <your project coverage file name>
--partial <if the report is partial>
Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ lazy val coverageParser = project
.in(file("coverage-parser"))
.settings(
libraryDependencies ++= Seq(
"com.codacy" %% "codacy-api-scala" % "7.0.1",
"com.codacy" %% "codacy-api-scala" % "7.0.2",
"com.codacy" %% "codacy-plugins-api" % "5.2.0",
"org.scala-lang.modules" %% "scala-xml" % "1.2.0",
scalatest % Test
Expand Down
20 changes: 14 additions & 6 deletions src/it/scala/com/codacy/CodacyCoverageReporterSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ class CodacyCoverageReporterSpec extends WordSpec with Matchers with EitherValue
organizationProvider: Option[OrganizationProvider.Value],
username: Option[String],
projectName: Option[String],
commitUuid: Option[String]
commitUuid: Option[String],
sleepTime: Int,
numRetries: Int
) = {
val baseConfig =
BaseCommandConfig(
Expand All @@ -33,7 +35,9 @@ class CodacyCoverageReporterSpec extends WordSpec with Matchers with EitherValue
username,
projectName,
apiBaseUrl,
commitUuid
commitUuid,
sleepTime,
numRetries
)

val commandConfig = Report(
Expand All @@ -56,7 +60,7 @@ class CodacyCoverageReporterSpec extends WordSpec with Matchers with EitherValue
"run" should {
"be successful" when {
"using a project token to send coverage" in {
val result = runCoverageReport(projectToken, None, None, None, None, commitUuid)
val result = runCoverageReport(projectToken, None, None, None, None, commitUuid, 10000, 3)

result shouldBe 'right
}
Expand All @@ -71,7 +75,9 @@ class CodacyCoverageReporterSpec extends WordSpec with Matchers with EitherValue
Option(OrganizationProvider.gh),
username,
projectName,
commitUuid
commitUuid,
10000,
3
)

result shouldBe 'right
Expand All @@ -80,7 +86,7 @@ class CodacyCoverageReporterSpec extends WordSpec with Matchers with EitherValue

"fail" when {
"project token is invalid" in {
val result = runCoverageReport(Some("invalid token"), None, None, None, None, commitUuid)
val result = runCoverageReport(Some("invalid token"), None, None, None, None, commitUuid, 10000, 3)

result shouldBe 'left
}
Expand All @@ -92,7 +98,9 @@ class CodacyCoverageReporterSpec extends WordSpec with Matchers with EitherValue
Option(OrganizationProvider.gh),
username,
projectName,
commitUuid
commitUuid,
10000,
3
)

result shouldBe 'left
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,15 @@ case class BaseCommandConfig(
@ValueDescription("your commitUUID")
commitUUID: Option[String],
@ValueDescription(
"Sets a specified read timeout value, in milliseconds, to be used when interacting with Codacy API"
"Sets a specified read timeout value, in milliseconds, to be used when interacting with Codacy API. By default, the value is 10 seconds"
)
httpTimeout: Int = 10000,
@ValueDescription(
"Sets a specified time, in milliseconds, to be used when waiting between retries. By default, the value is 10 seconds"
)
sleepTime: Int = 10000,
@ValueDescription("Sets a number of retries in case of failure. By default, the value is 3 times")
numRetries: Int = 3,
@Name("s") @ValueDescription("skip if token isn't defined")
skip: Int @@ Counter = Tag.of(0),
@Hidden
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ case class BaseConfig(
codacyApiBaseUrl: String,
commitUUID: Option[CommitUUID],
debug: Boolean,
timeout: RequestTimeout
timeout: RequestTimeout,
sleepTime: Int,
numRetries: Int
)

sealed trait CommitUUID extends Any {
Expand Down
4 changes: 3 additions & 1 deletion src/main/scala/com/codacy/rules/ConfigurationRules.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ class ConfigurationRules(cmdConfig: CommandConfiguration, envVars: Map[String, S
baseConfig.codacyApiBaseUrl.getOrElse(getApiBaseUrl),
commitUUID,
baseConfig.debugValue,
timeout = RequestTimeout(connTimeoutMs = 1000, readTimeoutMs = baseConfig.httpTimeout)
timeout = RequestTimeout(connTimeoutMs = 1000, readTimeoutMs = baseConfig.httpTimeout),
baseConfig.sleepTime,
baseConfig.numRetries
)
validatedConfig <- validateBaseConfigUrl(baseConf)
} yield {
Expand Down
25 changes: 21 additions & 4 deletions src/main/scala/com/codacy/rules/ReportRules.scala
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,15 @@ class ReportRules(coverageServices: => CoverageServices) extends LogSupport {
) = {
val coverageResponse = config.baseConfig.authentication match {
case _: ProjectTokenAuthenticationConfig =>
coverageServices.sendReport(commitUUID, language, report, config.partial, Some(config.baseConfig.timeout))
coverageServices.sendReport(
commitUUID,
language,
report,
config.partial,
Some(config.baseConfig.timeout),
Some(config.baseConfig.sleepTime),
Some(config.baseConfig.numRetries)
)

case ApiTokenAuthenticationConfig(_, organizationProvider, username, projectName) =>
coverageServices.sendReportWithProjectName(
Expand All @@ -159,7 +167,9 @@ class ReportRules(coverageServices: => CoverageServices) extends LogSupport {
language,
report,
config.partial,
Some(config.baseConfig.timeout)
Some(config.baseConfig.timeout),
Some(config.baseConfig.sleepTime),
Some(config.baseConfig.numRetries)
)
}
coverageResponse match {
Expand Down Expand Up @@ -283,15 +293,22 @@ class ReportRules(coverageServices: => CoverageServices) extends LogSupport {
withCommitUUID(config.baseConfig) { commitUUID =>
val coverageResponse = config.baseConfig.authentication match {
case _: ProjectTokenAuthenticationConfig =>
coverageServices.sendFinalNotification(commitUUID, Some(config.baseConfig.timeout))
coverageServices.sendFinalNotification(
commitUUID,
Some(config.baseConfig.timeout),
Some(config.baseConfig.sleepTime),
Some(config.baseConfig.numRetries)
)

case ApiTokenAuthenticationConfig(_, organizationProvider, username, projectName) =>
coverageServices.sendFinalWithProjectName(
organizationProvider,
username,
projectName,
commitUUID,
Some(config.baseConfig.timeout)
Some(config.baseConfig.timeout),
Some(config.baseConfig.sleepTime),
Some(config.baseConfig.numRetries)
)
}
coverageResponse match {
Expand Down
14 changes: 10 additions & 4 deletions src/test/scala/com/codacy/rules/ReportRulesSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ReportRulesSpec extends WordSpec with Matchers with PrivateMethodTester wi
val coverageFiles = List(new File("coverage.xml"))
val apiBaseUrl = "https://api.codacy.com"

val baseConf = BaseCommandConfig(Some(projToken), None, None, None, None, Some(apiBaseUrl), None)
val baseConf = BaseCommandConfig(Some(projToken), None, None, None, None, Some(apiBaseUrl), None, 10000, 3)

val conf =
Report(baseConf, Some("Scala"), coverageReports = Some(coverageFiles), prefix = None, forceCoverageParser = None)
Expand All @@ -37,7 +37,9 @@ class ReportRulesSpec extends WordSpec with Matchers with PrivateMethodTester wi
apiBaseUrl,
None,
debug = false,
timeout = RequestTimeout(1000, 10000)
timeout = RequestTimeout(1000, 10000),
sleepTime = 10000,
numRetries = 3
)

def assertCodacyCoverage(
Expand Down Expand Up @@ -88,7 +90,9 @@ class ReportRulesSpec extends WordSpec with Matchers with PrivateMethodTester wi
any[String],
any[CoverageReport],
anyBoolean,
Some(RequestTimeout(1000, 10000))
Some(RequestTimeout(1000, 10000)),
Some(10000),
Some(3)
) returns FailedResponse("Failed to send report")

assertCodacyCoverage(coverageServices, List("src/test/resources/dotcover-example.xml"), success = false)
Expand All @@ -103,7 +107,9 @@ class ReportRulesSpec extends WordSpec with Matchers with PrivateMethodTester wi
any[String],
any[CoverageReport],
anyBoolean,
Some(RequestTimeout(1000, 10000))
Some(RequestTimeout(1000, 10000)),
Some(10000),
Some(3)
) returns SuccessfulResponse(RequestSuccess("Success"))

assertCodacyCoverage(coverageServices, List("src/test/resources/dotcover-example.xml"), success = true)
Expand Down

0 comments on commit 0091caf

Please sign in to comment.