Skip to content

Commit

Permalink
Merge pull request #2 from nbaztec/make-plugin-generic
Browse files Browse the repository at this point in the history
remove kotlin dependency, rename plugin, improve test coverage
  • Loading branch information
Nisheeth Barthwal authored Aug 3, 2020
2 parents 1b80b77 + e301857 commit 01d196e
Show file tree
Hide file tree
Showing 17 changed files with 549 additions and 144 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/release-workflow.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: release
on:
push:
tags:
- 'v*'
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: create github release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.ref }}
release_name: Release ${{ github.ref }}
draft: false
prerelease: false
- name: publish to gradle
env:
GRADLE_PUBLISH_KEY: ${{ secrets.GRADLE_PUBLISH_KEY }}
GRADLE_PUBLISH_SECRET: ${{ secrets.GRADLE_PUBLISH_SECRET }}
run: ./gradlew setupPublishSecrets publishPlugins
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: CI
name: check

on:
push:
Expand All @@ -7,12 +7,11 @@ on:
branches: [ master ]

jobs:
build:
name: test
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: test-and-coverage
- name: test and publish coverage
env:
COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }}
run: ./gradlew jacocoTestReport coverallsJacoco --debug
run: ./gradlew jacocoTestReport coverallsJacoco
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
# Coveralls Jacoco Kotlin Gradle Plugin
[![Coverage Status](https://coveralls.io/repos/github/nbaztec/coveralls-jacoco-kotlin-gradle-plugin/badge.svg?branch=master)](https://coveralls.io/github/nbaztec/coveralls-jacoco-kotlin-gradle-plugin?branch=master)
# Coveralls Jacoco Gradle Plugin
[![Coverage Status](https://coveralls.io/repos/github/nbaztec/coveralls-jacoco-gradle-plugin/badge.svg?branch=master)](https://coveralls.io/github/nbaztec/coveralls-jacoco-gradle-plugin?branch=master)

A jacoco test coverage reporter gradle plugin for Kotlin and [coveralls.io](https://coveralls.io).
A jacoco test coverage reporter gradle plugin for [coveralls.io](https://coveralls.io).

The plugin supports non-root packages in line with the recommended [Kotlin directory structure](https://kotlinlang.org/docs/reference/coding-conventions.html#directory-structure)
which was missing in many other plugins for the Kotlin ecosystem.

## Usage

[Gradle Plugin page](https://plugins.gradle.org/plugin/com.github.nbaztec.coveralls-jacoco-kotlin)
[Gradle Plugin page](https://plugins.gradle.org/plugin/com.github.nbaztec.coveralls-jacoco)

Apply the plugin with the ID: `com.github.nbaztec.coveralls-jacoco-kotlin`
Apply the plugin with the ID: `com.github.nbaztec.coveralls-jacoco`
This will add a gradle task `coverallsJacoco` that can be used to publish coverage report via `./gradlew test coverallsJacoco`

## Options
```kotlin
// build.gradle.kts

coverallsJacoco {
reportPath = 'build/reports/jacoco/test/jacocoTestReport.xml'
rootPackage = 'com.github.nbaztec.foo' // optional, leave out if project has java directory structure
additionalSourceSets = [ sourceSets.foo, sourceSets.bar ] // optional, sourceSet.main is always included
apiEndpoint = "https://coveralls.io/api/v1/jobs" // optional
reportPath = "" // default: "build/reports/jacoco/test/jacocoTestReport.xml"
rootPackage = "com.github.nbaztec.foo" // optional, leave out if project has a normal java styled directory structure
reportSourceSets = [ sourceSets.foo, sourceSets.bar ] // optional, default: main
apiEndpoint = "" // optional, default: https://coveralls.io/api/v1/jobs
}
```
52 changes: 36 additions & 16 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

group = "com.github.nbaztec"
version = "1.0.3"
version = "1.0.4"

tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = JavaVersion.VERSION_1_8.toString()
}

repositories {
mavenCentral()
jcenter()
}

dependencies {
implementation("org.jetbrains.kotlinx", "kotlinx-coroutines-core", "1.3.8")
implementation("org.dom4j", "dom4j", "2.1.0")
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.61")
implementation("org.eclipse.jgit:org.eclipse.jgit:5.8.1.202007141445-r")
implementation("org.apache.httpcomponents:httpmime:4.5.2")
implementation("com.google.code.gson:gson:2.8.5")
testImplementation("junit:junit:4.13")
testImplementation( "org.junit.jupiter:junit-jupiter-api:5.6.2")
testImplementation("io.mockk:mockk:1.10.0")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.6.2")
implementation("org.jetbrains.kotlin", "kotlin-gradle-plugin", "1.3.72")
implementation("org.eclipse.jgit", "org.eclipse.jgit", "5.8.1.202007141445-r")
implementation("org.apache.httpcomponents", "httpmime", "4.5.12")
implementation("com.google.code.gson", "gson", "2.8.5")
testImplementation("junit", "junit", "4.13")
testImplementation("org.junit.jupiter", "junit-jupiter-api", "5.6.2")
testRuntimeOnly("org.junit.jupiter", "junit-jupiter-engine", "5.6.2")
testImplementation("io.mockk", "mockk", "1.10.0")
}

tasks {
Expand All @@ -29,6 +36,19 @@ tasks {
html.isEnabled = true
}
}

create("setupPublishSecrets") {
doLast {
val key = System.getenv("GRADLE_PUBLISH_KEY")
val secret = System.getenv("GRADLE_PUBLISH_SECRET")

check(key != null) { "GRADLE_PUBLISH_KEY is required" }
check(secret != null) { "GRADLE_PUBLISH_SECRET is required" }

System.setProperty("gradle.publish.key", key)
System.setProperty("gradle.publish.secret", secret)
}
}
}

plugins {
Expand All @@ -47,7 +67,7 @@ coverallsJacoco {
publishing {
publications {
create<MavenPublication>("maven") {
artifactId = "coveralls-jacoco-kotlin"
artifactId = "coveralls-jacoco"

from(components["java"])
}
Expand All @@ -57,21 +77,21 @@ publishing {

gradlePlugin {
plugins {
create("coverallsJacocoKotlinPlugin") {
id = "com.github.nbaztec.coveralls-jacoco-kotlin"
create("coverallsJacocoPlugin") {
id = "com.github.nbaztec.coveralls-jacoco"
implementationClass = "org.gradle.plugin.coveralls.jacoco.CoverallsJacocoPlugin"
}
}
}

pluginBundle {
(plugins) {
"coverallsJacocoKotlinPlugin" {
website = "http://github.com/nbaztec/coveralls-jacoco-kotlin-gradle-plugin/"
vcsUrl = "https://github.com/nbaztec/coveralls-jacoco-kotlin-gradle-plugin.git"
"coverallsJacocoPlugin" {
website = "http://github.com/nbaztec/coveralls-jacoco-gradle-plugin/"
vcsUrl = "https://github.com/nbaztec/coveralls-jacoco-gradle-plugin.git"
description = "Send jacoco coverage data to coveralls.io"
tags = listOf("coverage", "coveralls")
displayName = "Coveralls Jacoco Kotlin Plugin"
displayName = "Coveralls Jacoco Plugin"
}
}
}
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
rootProject.name = 'coveralls-jacoco-kotlin-gradle-plugin'
rootProject.name = 'coveralls-jacoco-gradle-plugin'

4 changes: 2 additions & 2 deletions src/main/kotlin/CoverallsJacocoPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ open class CoverallsJacocoPluginExtension {
var rootPackage: String? = null
var reportPath = "build/reports/jacoco/test/jacocoTestReport.xml"
var apiEndpoint = "https://coveralls.io/api/v1/jobs"
var additionalSourceSets = emptyList<SourceSet>()
var reportSourceSets: Iterable<SourceSet> = emptySet()
}

class CoverallsJacocoPlugin : Plugin<Project> {
Expand All @@ -17,7 +17,7 @@ class CoverallsJacocoPlugin : Plugin<Project> {

project.task("coverallsJacoco") {
it.doLast {
val envGetter = {v: String -> System.getenv(v)?.ifBlank { null } }
val envGetter = { v: String -> System.getenv(v)?.ifBlank { null } }
CoverallsReporter(envGetter).report(project)
}
}
Expand Down
18 changes: 13 additions & 5 deletions src/main/kotlin/CoverallsReporter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.gradle.plugin.coveralls.jacoco


import com.google.gson.Gson
import org.apache.http.client.config.RequestConfig
import org.apache.http.client.methods.HttpPost
import org.apache.http.entity.ContentType
import org.apache.http.entity.mime.MultipartEntityBuilder
Expand All @@ -27,6 +28,7 @@ typealias EnvGetter = (String) -> String?

class CoverallsReporter(val envGetter: EnvGetter) {
private val logger: Logger by lazy { LogManager.getLogger(CoverallsReporter::class.java) }
private val defaultHttpTimeoutMs = 10 * 1000

fun report(project: Project) {
logger.info("retrieving git info")
Expand All @@ -43,8 +45,8 @@ class CoverallsReporter(val envGetter: EnvGetter) {
logger.info("retrieving ci service info")
val serviceInfo = ServiceInfoParser(envGetter).parse()

val repoToken = System.getenv("COVERALLS_REPO_TOKEN")
check(repoToken.isNotBlank()) { "COVERALLS_REPO_TOKEN not set" }
val repoToken = envGetter("COVERALLS_REPO_TOKEN")
check(repoToken != null && repoToken.isNotBlank()) { "COVERALLS_REPO_TOKEN not set" }

val req = Request(
repoToken,
Expand All @@ -54,14 +56,21 @@ class CoverallsReporter(val envGetter: EnvGetter) {
gitInfo,
sourceFiles
)
val pluginExtension = project.extensions.getByName("coverallsJacoco") as CoverallsJacocoPluginExtension
val pluginExtension = project.extensions.getByType(CoverallsJacocoPluginExtension::class.java)

send(pluginExtension.apiEndpoint, req)
}

private fun send(endpoint: String, req: Request) {
val httpClient = HttpClients.createDefault()
val httpPost = HttpPost(endpoint).apply {
config = RequestConfig
.custom()
.setConnectTimeout(defaultHttpTimeoutMs)
.setSocketTimeout(defaultHttpTimeoutMs)
.setConnectionRequestTimeout(defaultHttpTimeoutMs)
.build()

entity = MultipartEntityBuilder
.create()
.addBinaryBody(
Expand All @@ -75,10 +84,9 @@ class CoverallsReporter(val envGetter: EnvGetter) {

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)}")
throw Exception("coveralls returned HTTP ${res.statusLine.statusCode}: ${EntityUtils.toString(res.entity).trim()}")
}
logger.info("OK")
}
Expand Down
58 changes: 28 additions & 30 deletions src/main/kotlin/GitInfoParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,39 +26,37 @@ data class GitInfo(
val remotes: Collection<Remote>
)

class GitInfoParser {
companion object {
private val logger: Logger by lazy { LogManager.getLogger(GitInfoParser::class.java) }

fun parse(directory: File): GitInfo? {
try {
val repo = RepositoryBuilder().findGitDir(directory).build()
val head = repo.let {
val rev = repo.resolve("HEAD")
val commit = RevWalk(repo).parseCommit(rev)

Head(
rev.name,
commit.authorIdent.name,
commit.authorIdent.emailAddress,
commit.committerIdent.name,
commit.committerIdent.emailAddress,
commit.fullMessage
)
}
object GitInfoParser {
private val logger: Logger by lazy { LogManager.getLogger(GitInfoParser::class.java) }

fun parse(directory: File): GitInfo? {
try {
val repo = RepositoryBuilder().findGitDir(directory).build()
val head = repo.let {
val rev = repo.resolve("HEAD")
val commit = RevWalk(repo).parseCommit(rev)

Head(
rev.name,
commit.authorIdent.name,
commit.authorIdent.emailAddress,
commit.committerIdent.name,
commit.committerIdent.emailAddress,
commit.fullMessage
)
}

val remotes = repo.let {
val config = repo.config
config.getSubsections("remote").map {
Remote(it, config.getString("remote", it, "url"))
}
val remotes = repo.let {
val config = repo.config
config.getSubsections("remote").map {
Remote(it, config.getString("remote", it, "url"))
}

return GitInfo(head, repo.branch, remotes)
} catch (e: Exception) {
logger.info("unable to read git info: ${e.message}")
return null
}

return GitInfo(head, repo.branch, remotes)
} catch (e: Exception) {
logger.info("unable to read git info: ${e.message}")
return null
}
}
}
Loading

0 comments on commit 01d196e

Please sign in to comment.