From 6f07b2ffdab04ba63864cf94dc4297e81955bd0f Mon Sep 17 00:00:00 2001 From: Nisheeth Barthwal Date: Wed, 23 Dec 2020 16:05:29 +0100 Subject: [PATCH 1/5] use kotlin version 1.4 --- build.gradle.kts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/build.gradle.kts b/build.gradle.kts index c496f73..861cde0 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -12,7 +12,10 @@ buildscript { } tasks.withType { - kotlinOptions.jvmTarget = JavaVersion.VERSION_1_8.toString() + kotlinOptions { + jvmTarget = JavaVersion.VERSION_1_8.toString() + languageVersion = "1.4" + } } java { From c9245e2b07f4b49b25b77910a05bbc8df21b7a77 Mon Sep 17 00:00:00 2001 From: Nisheeth Barthwal Date: Wed, 23 Dec 2020 16:06:04 +0100 Subject: [PATCH 2/5] add OptionsParser --- src/main/kotlin/OptionsParser.kt | 19 +++++++ src/test/kotlin/OptionsParserTest.kt | 75 ++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 src/main/kotlin/OptionsParser.kt create mode 100644 src/test/kotlin/OptionsParserTest.kt diff --git a/src/main/kotlin/OptionsParser.kt b/src/main/kotlin/OptionsParser.kt new file mode 100644 index 0000000..2971f57 --- /dev/null +++ b/src/main/kotlin/OptionsParser.kt @@ -0,0 +1,19 @@ +package org.gradle.plugin.coveralls.jacoco + +data class Options( + val repoToken: String, + val parallel: Boolean?, + val flagName: String? +) + +class OptionsParser(val envGetter: EnvGetter) { + fun parse(): Options { + val repoToken = envGetter("COVERALLS_REPO_TOKEN") ?: envGetter("GITHUB_TOKEN") + check(repoToken != null && repoToken.isNotBlank()) { "COVERALLS_REPO_TOKEN not set" } + + val parallel = envGetter("COVERALLS_PARALLEL")?.toBoolean() + val flagName = envGetter("COVERALLS_FLAG_NAME") + + return Options(repoToken, parallel, flagName) + } +} diff --git a/src/test/kotlin/OptionsParserTest.kt b/src/test/kotlin/OptionsParserTest.kt new file mode 100644 index 0000000..e0addc6 --- /dev/null +++ b/src/test/kotlin/OptionsParserTest.kt @@ -0,0 +1,75 @@ +package org.gradle.plugin.coveralls.jacoco + +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows + +internal class OptionsParserTest { + @Test + fun `OptionsParser throws exception when repo token not set`() { + val envGetter = createEnvGetter(mapOf()) + + assertThrows { OptionsParser(envGetter).parse() }.also { + assertEquals("COVERALLS_REPO_TOKEN not set", it.message) + } + } + + @Test + fun `OptionsParser ignores missing parameter`() { + val envGetter = createEnvGetter( + mapOf( + "COVERALLS_REPO_TOKEN" to "foo", + ) + ) + + val actual = OptionsParser(envGetter).parse() + val expected = Options( + repoToken = "foo", + parallel = null, + flagName = null, + ) + assertEquals(expected, actual) + } + + @Test + fun `OptionsParser parses falsy parallel parameter`() { + val envGetter = createEnvGetter( + mapOf( + "COVERALLS_REPO_TOKEN" to "foo", + "COVERALLS_PARALLEL" to "false", + ) + ) + + val actual = OptionsParser(envGetter).parse() + val expected = Options( + repoToken = "foo", + parallel = false, + flagName = null, + ) + assertEquals(expected, actual) + } + + + @Test + fun `OptionsParser parses coveralls parameter`() { + val envGetter = createEnvGetter( + mapOf( + "COVERALLS_REPO_TOKEN" to "foo", + "COVERALLS_PARALLEL" to "true", + "COVERALLS_FLAG_NAME" to "flag_name", + ) + ) + + val actual = OptionsParser(envGetter).parse() + val expected = Options( + repoToken = "foo", + parallel = true, + flagName = "flag_name", + ) + assertEquals(expected, actual) + } + + private fun createEnvGetter(entries: Map): EnvGetter { + return { k: String -> entries[k] } + } +} From 94968989ef3c39e2875db1713355ce561b385569 Mon Sep 17 00:00:00 2001 From: Nisheeth Barthwal Date: Wed, 23 Dec 2020 16:06:24 +0100 Subject: [PATCH 3/5] support paralle and flag_name options --- src/main/kotlin/CoverallsReporter.kt | 64 +++++++++++++----------- src/test/kotlin/CoverallsReporterTest.kt | 11 ---- src/test/kotlin/DataClassTest.kt | 28 ++++++----- 3 files changed, 51 insertions(+), 52 deletions(-) diff --git a/src/main/kotlin/CoverallsReporter.kt b/src/main/kotlin/CoverallsReporter.kt index 3998d09..d5947f2 100644 --- a/src/main/kotlin/CoverallsReporter.kt +++ b/src/main/kotlin/CoverallsReporter.kt @@ -22,7 +22,9 @@ data class Request( val service_branch: String?, val service_build_url: String?, val git: GitInfo?, - val source_files: List + val parallel: Boolean?, + val flag_name: String?, + val source_files: List, ) private fun Request.json() = Gson().toJson(this) @@ -49,22 +51,22 @@ class CoverallsReporter(val envGetter: EnvGetter) { logger.info("retrieving ci service info") val serviceInfo = ServiceInfoParser(envGetter).parse() - - val repoToken = envGetter("COVERALLS_REPO_TOKEN") ?: envGetter("GITHUB_TOKEN") - check(repoToken != null && repoToken.isNotBlank()) { "COVERALLS_REPO_TOKEN not set" } + val options = OptionsParser(envGetter).parse() val req = Request( - repo_token = repoToken, - repo_name = serviceInfo.repoName, - service_name = serviceInfo.name, - service_number = serviceInfo.number, - service_job_id = serviceInfo.jobId, - service_job_number = serviceInfo.jobNumber, - service_pull_request = serviceInfo.pr, - service_branch = serviceInfo.branch, - service_build_url = serviceInfo.buildUrl, - git = gitInfo, - source_files = sourceFiles + repo_token = options.repoToken, + repo_name = serviceInfo.repoName, + service_name = serviceInfo.name, + service_number = serviceInfo.number, + service_job_id = serviceInfo.jobId, + service_job_number = serviceInfo.jobNumber, + service_pull_request = serviceInfo.pr, + service_branch = serviceInfo.branch, + service_build_url = serviceInfo.buildUrl, + git = gitInfo, + parallel = options.parallel, + flag_name = options.flagName, + source_files = sourceFiles, ) send(pluginExtension.apiEndpoint, req) @@ -74,28 +76,32 @@ class CoverallsReporter(val envGetter: EnvGetter) { val httpClient = HttpClients.createDefault() val httpPost = HttpPost(endpoint).apply { config = RequestConfig - .custom() - .setConnectTimeout(defaultHttpTimeoutMs) - .setSocketTimeout(defaultHttpTimeoutMs) - .setConnectionRequestTimeout(defaultHttpTimeoutMs) - .build() + .custom() + .setConnectTimeout(defaultHttpTimeoutMs) + .setSocketTimeout(defaultHttpTimeoutMs) + .setConnectionRequestTimeout(defaultHttpTimeoutMs) + .build() entity = MultipartEntityBuilder - .create() - .addBinaryBody( - "json_file", - req.json().toByteArray(Charsets.UTF_8), - ContentType.APPLICATION_JSON, - "json_file" - ) - .build() + .create() + .addBinaryBody( + "json_file", + req.json().toByteArray(Charsets.UTF_8), + ContentType.APPLICATION_JSON, + "json_file" + ) + .build() } logger.info("sending payload to coveralls") logger.debug(req.json()) val res = httpClient.execute(httpPost) if (res.statusLine.statusCode != 200) { - throw Exception("coveralls returned HTTP ${res.statusLine.statusCode}: ${EntityUtils.toString(res.entity).trim()}") + throw Exception( + "coveralls returned HTTP ${res.statusLine.statusCode}: ${ + EntityUtils.toString(res.entity).trim() + }" + ) } logger.info("OK") } diff --git a/src/test/kotlin/CoverallsReporterTest.kt b/src/test/kotlin/CoverallsReporterTest.kt index 2829c44..4a318fc 100644 --- a/src/test/kotlin/CoverallsReporterTest.kt +++ b/src/test/kotlin/CoverallsReporterTest.kt @@ -51,17 +51,6 @@ internal class CoverallsReporterTest { } } - @Test - fun `CoverallsReporter throws exception when repo token not set`() { - val envGetter = createEnvGetter(mapOf()) - val project = mockProject() - - val reporter = CoverallsReporter(envGetter) - assertThrows { reporter.report(project) }.also { - assertEquals("COVERALLS_REPO_TOKEN not set", it.message) - } - } - @Test fun `CoverallsReporter throws exception on coveralls server error`() = runBlocking { val mockServer = HttpServer.create(InetSocketAddress("localhost", 0), 0).apply { diff --git a/src/test/kotlin/DataClassTest.kt b/src/test/kotlin/DataClassTest.kt index bc93556..c41205e 100644 --- a/src/test/kotlin/DataClassTest.kt +++ b/src/test/kotlin/DataClassTest.kt @@ -37,7 +37,7 @@ internal class DataClassTest { @Test fun `data class ServiceInfo`() { - val svcInfo = ServiceInfo("name", "repo_name","number", "jobId", "jobNumber", "pr", "branch", "buildNumber") + val svcInfo = ServiceInfo("name", "repo_name", "number", "jobId", "jobNumber", "pr", "branch", "buildNumber") assertEquals("name", svcInfo.name) assertEquals("repo_name", svcInfo.repoName) assertEquals("number", svcInfo.number) @@ -69,17 +69,19 @@ internal class DataClassTest { val gitInfo = mockk() val sourceFiles = emptyList() val req = Request( - "repo_token", - "service_name", - "repo_name", - "service_number", - "service_job_id", - "service_job_number", - "service_pull_request", - "service_branch", - "service_build_url", - gitInfo, - sourceFiles + "repo_token", + "service_name", + "repo_name", + "service_number", + "service_job_id", + "service_job_number", + "service_pull_request", + "service_branch", + "service_build_url", + gitInfo, + true, + "flag_name", + sourceFiles ) assertEquals("repo_token", req.repo_token) assertEquals("service_name", req.service_name) @@ -90,6 +92,8 @@ internal class DataClassTest { assertEquals("service_pull_request", req.service_pull_request) assertEquals("service_branch", req.service_branch) assertEquals("service_build_url", req.service_build_url) + assertEquals(true, req.parallel) + assertEquals("flag_name", req.flag_name) assertEquals(gitInfo, req.git) assertEquals(sourceFiles, req.source_files) } From d9b2e1f7224552c6f322831cd3fb7863a0096dae Mon Sep 17 00:00:00 2001 From: Nisheeth Barthwal Date: Wed, 23 Dec 2020 16:08:54 +0100 Subject: [PATCH 4/5] update readme --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 14a2cf7..3d9a4ca 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,10 @@ $ ./gradlew test jacocoTestReport coverallsJacoco Set the value of `COVERALLS_REPO_TOKEN` from the project page on coveralls.io +Additionally, the following coveralls parameters may be specified via environment variables: +* `COVERALLS_PARALLEL` (`true`/`false`) +* `COVERALLS_FLAG_NAME` + ## Options ```kotlin // build.gradle.kts From 2a74d8ccc58032d3e27879aa0b7ca070b57235c7 Mon Sep 17 00:00:00 2001 From: Nisheeth Barthwal Date: Wed, 23 Dec 2020 16:09:13 +0100 Subject: [PATCH 5/5] bump version to 1.2.5 --- build.gradle.kts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle.kts b/build.gradle.kts index 861cde0..3e999dd 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,7 +1,7 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile group = "com.github.nbaztec" -version = "1.2.4" +version = "1.2.5" buildscript { repositories {