From d334e1f9c11a3f8ef8199339d8ac77094a40c785 Mon Sep 17 00:00:00 2001 From: richard-dennehy-ovo <104198393+richard-dennehy-ovo@users.noreply.github.com> Date: Sat, 4 Feb 2023 17:38:20 +0000 Subject: [PATCH] add support for parallel builds (#240) --- README.md | 19 +++++++++++++ .../coveralls/CoverallPayloadWriter.scala | 5 ++++ .../scoverage/coveralls/CoverallsPlugin.scala | 5 +++- .../coveralls/CoverallPayloadWriterTest.scala | 28 +++++++++++++++---- 4 files changed, 51 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index ea44cfb..7a91a26 100644 --- a/README.md +++ b/README.md @@ -210,6 +210,25 @@ import org.scoverage.coveralls.TravisPro coverallsService := Some(TravisPro) ``` +## Uploading coverage from parallel CI builds + +[Coveralls supports merging reports from multiple CI builds.](https://docs.coveralls.io/parallel-build-webhook) +Each report must be flagged as coming from a parallel job, then a webhook must be called after all jobs have completed to merge the reports together. + +To mark uploaded reports as parallel, either: + +### Put the flag directly in `build.sbt` +```scala +import org.scoverage.coveralls.Imports.CoverallsKeys._ + +coverallsParallel := true +``` + +### Set an environment variable +```shell +export COVERALLS_PARALLEL=true +``` + # License `sbt-coveralls` is open source software released under the [Apache 2 diff --git a/src/main/scala/org/scoverage/coveralls/CoverallPayloadWriter.scala b/src/main/scala/org/scoverage/coveralls/CoverallPayloadWriter.scala index cf68dfd..e43be3f 100644 --- a/src/main/scala/org/scoverage/coveralls/CoverallPayloadWriter.scala +++ b/src/main/scala/org/scoverage/coveralls/CoverallPayloadWriter.scala @@ -12,6 +12,7 @@ class CoverallPayloadWriter( coverallsFile: File, repoToken: Option[String], service: Option[CIService], + parallel: Boolean, gitClient: GitClient )(implicit log: Logger) { import gitClient._ @@ -38,6 +39,10 @@ class CoverallPayloadWriter( // to provide an unique additional label in jobs with multiple submissions writeOpt("flag_name", sys.env.get("COVERALLS_FLAG_NAME")) + if (parallel) { + gen.writeBooleanField("parallel", true) + } + addGitInfo() gen.writeFieldName("source_files") diff --git a/src/main/scala/org/scoverage/coveralls/CoverallsPlugin.scala b/src/main/scala/org/scoverage/coveralls/CoverallsPlugin.scala index 7486353..f139280 100644 --- a/src/main/scala/org/scoverage/coveralls/CoverallsPlugin.scala +++ b/src/main/scala/org/scoverage/coveralls/CoverallsPlugin.scala @@ -28,6 +28,7 @@ object Imports { val coverallsEndpoint = SettingKey[Option[String]]("coverallsEndpoint") val coverallsGitRepoLocation = SettingKey[Option[String]]("coveralls-git-repo") + val coverallsParallel = SettingKey[Boolean]("coverallsParallel") } } @@ -56,7 +57,8 @@ object CoverallsPlugin extends AutoPlugin { }, coverallsFile := crossTarget.value / "coveralls.json", coberturaFile := crossTarget.value / "coverage-report" / "cobertura.xml", - coverallsGitRepoLocation := Some(".") + coverallsGitRepoLocation := Some("."), + coverallsParallel := sys.env.get("COVERALLS_PARALLEL").contains("true") ) val aggregateFilter = ScopeFilter( @@ -98,6 +100,7 @@ object CoverallsPlugin extends AutoPlugin { coverallsFile.value, repoToken, coverallsService.value, + coverallsParallel.value, new GitClient(repoRootDirectory) ) diff --git a/src/test/scala/org/scoverage/coveralls/CoverallPayloadWriterTest.scala b/src/test/scala/org/scoverage/coveralls/CoverallPayloadWriterTest.scala index c5830cd..85b80e6 100644 --- a/src/test/scala/org/scoverage/coveralls/CoverallPayloadWriterTest.scala +++ b/src/test/scala/org/scoverage/coveralls/CoverallPayloadWriterTest.scala @@ -32,13 +32,15 @@ class CoverallPayloadWriterTest def coverallsWriter( writer: Writer, tokenIn: Option[String], - service: Option[CIService] + service: Option[CIService], + parallel: Boolean, ): (CoverallPayloadWriter, Writer) = { val payloadWriter = new CoverallPayloadWriter( new File(".").getAbsoluteFile, new File("."), tokenIn, service, + parallel, testGitClient ) { override def generator(file: File) = { @@ -66,7 +68,8 @@ class CoverallPayloadWriterTest val (payloadWriter, writer) = coverallsWriter( new StringWriter(), Some("testRepoToken"), - Some(testService) + Some(testService), + false ) payloadWriter.start @@ -81,7 +84,7 @@ class CoverallPayloadWriterTest "generate a correct starting payload without a CI service" in { val (payloadWriter, writer) = - coverallsWriter(new StringWriter(), Some("testRepoToken"), None) + coverallsWriter(new StringWriter(), Some("testRepoToken"), None, false) payloadWriter.start payloadWriter.flush() @@ -109,7 +112,8 @@ class CoverallPayloadWriterTest val (payloadWriter, writer) = coverallsWriter( new StringWriter(), Some("testRepoToken"), - Some(TravisCI) + Some(TravisCI), + false ) payloadWriter.addSourceFile( SourceFileReport( @@ -128,7 +132,8 @@ class CoverallPayloadWriterTest val (payloadWriter, writer) = coverallsWriter( new StringWriter(), Some("testRepoToken"), - Some(TravisCI) + Some(TravisCI), + false ) payloadWriter.start @@ -136,6 +141,19 @@ class CoverallPayloadWriterTest writer.toString should endWith("]}") } + + "include parallel correctly" in { + val (payloadWriter, writer) = coverallsWriter(new StringWriter(), Some("testRepoToken"), None, true) + + payloadWriter.start + payloadWriter.flush() + + writer.toString should equal( + """{"repo_token":"testRepoToken","parallel":true,""" + + expectedGit + + ""","source_files":[""" + ) + } } } }