Skip to content

Commit

Permalink
Merge pull request scoverage#75 from AndrewWhitaker/coveralls-endpoin…
Browse files Browse the repository at this point in the history
…t-support

Coveralls endpoint support (Issue scoverage#74)
  • Loading branch information
sksamuel committed Jan 5, 2016
2 parents a5969e6 + 913c3a6 commit 531547e
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 15 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,24 @@ Add an environment variable `COVERALLS_REPO_TOKEN`, for example:

export COVERALLS_REPO_TOKEN=my-token

## Specifying Your Coveralls Endpoint

If you're using https://coveralls.io as your endpoint, then you don't need to set this option. If you're using a hosted (enterprise) instance of coveralls, you will need to specify your endpoint in one of two ways.

### Put your endpoint directly in your `build.sbt`

```scala
import org.scoverage.coveralls.Imports.CoverallsKeys._

coverallsEndpoint := Some("http://my-instance.com")
```

### Add an environment variable

Add an environment variable `COVERALLS_ENDPOINT`, for example:

export COVERALLS_ENDPOINT=http://my-instance.com

## Custom Source File Encoding

By default `sbt-coveralls` assumes your source files are `UTF-8` encoded. To use a different encoding, add the following to your `build.sbt`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ class CoverallPayloadWriter(
serviceName: Option[String],
gitClient: GitClient,
sourcesEnc: Codec,
jsonEnc: JsonEncoding
) {
jsonEnc: JsonEncoding) {

val projectRootDirStr = projectRootDir.toString + "/"
import gitClient._
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/org/scoverage/coveralls/CoverallsClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import java.net.{ HttpURLConnection, Socket, InetAddress }
import com.fasterxml.jackson.core.JsonEncoding
import com.fasterxml.jackson.databind.ObjectMapper

class CoverallsClient(httpClient: HttpClient, sourcesEnc: Codec, jsonEnc: JsonEncoding) {
class CoverallsClient(endpoint: String, httpClient: HttpClient, sourcesEnc: Codec, jsonEnc: JsonEncoding) {

import CoverallsClient._

val mapper = newMapper
def url = s"$endpoint/api/v1/jobs"

def newMapper = {
val mapper = new ObjectMapper
Expand Down Expand Up @@ -43,7 +44,6 @@ class CoverallsClient(httpClient: HttpClient, sourcesEnc: Codec, jsonEnc: JsonEn
}

object CoverallsClient {
val url = "https://coveralls.io/api/v1/jobs"
val tokenErrorString = "Couldn't find a repository matching this job"
val errorResponseTitleTag = "title"
val defaultErrorMessage = "ERROR (no title found)"
Expand Down
15 changes: 11 additions & 4 deletions src/main/scala/org/scoverage/coveralls/CoverallsPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ object Imports {
val coberturaFile = SettingKey[File]("coberturaFile")
val coverallsEncoding = SettingKey[String]("encoding")
val coverallsSourceRoots = SettingKey[Seq[Seq[File]]]("Source roots")
val coverallsEndpoint = SettingKey[Option[String]]("coveralls-endpoint")
}
}

Expand All @@ -39,6 +40,7 @@ object CoverallsPlugin extends AutoPlugin with CommandSupport {
coverallsEncoding := "UTF-8",
coverallsToken := None,
coverallsTokenFile := None,
coverallsEndpoint := Option("https://coveralls.io"),
projectBaseDir := baseDirectory.value,
coverallsServiceName := travisJobIdent map { _ => "travis-ci" },
coverallsFile := crossTarget.value / "coveralls.json",
Expand Down Expand Up @@ -71,7 +73,9 @@ object CoverallsPlugin extends AutoPlugin with CommandSupport {
val sourcesEnc = Codec(coverallsEncoding.gimme)
val jsonEnc = JsonEncoding.UTF8

val coverallsClient = new CoverallsClient(apiHttpClient, sourcesEnc, jsonEnc)
val endpoint = userEndpoint(coverallsEndpoint.gimme).get

val coverallsClient = new CoverallsClient(endpoint, apiHttpClient, sourcesEnc, jsonEnc)

val writer = new CoverallPayloadWriter(
projectBaseDir.gimme,
Expand Down Expand Up @@ -109,21 +113,21 @@ object CoverallsPlugin extends AutoPlugin with CommandSupport {

val res = coverallsClient.postFile(coverallsFile.gimme)
if (res.error) {
log.error("Uploading to coveralls.io failed: " + res.message)
log.error(s"Uploading to $endpoint failed: " + res.message)
if (res.message.contains(CoverallsClient.tokenErrorString)) {
log.error(
"The error message '" + CoverallsClient.tokenErrorString +
"' can mean your repo token is incorrect."
)
} else {
log.error("Coveralls.io server internal error: " + res.message)
log.error(s"$endpoint server internal error: " + res.message)
}
if (coverallsFailBuildOnError.gimme)
state.fail
else
state
} else {
log.info("Uploading to coveralls.io succeeded: " + res.message)
log.info(s"Uploading to $endpoint succeeded: " + res.message)
log.info(res.url)
log.info("(results may not appear immediately)")
state
Expand All @@ -150,6 +154,9 @@ object CoverallsPlugin extends AutoPlugin with CommandSupport {
.orElse(coverallsToken)
.orElse(coverallsTokenFile.flatMap(repoTokenFromFile))

def userEndpoint(coverallsEndpoint: Option[String]) =
sys.env.get("COVERALLS_ENDPOINT")
.orElse(coverallsEndpoint)
}

case class CoberturaFile(file: File, projectBase: File) {
Expand Down
3 changes: 1 addition & 2 deletions src/main/scala/org/scoverage/coveralls/GitClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ object GitClient {
authorEmail: String,
committerName: String,
committerEmail: String,
shortMessage: String
)
shortMessage: String)
}

class GitClient(cwd: String)(implicit log: Logger) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ import scalaj.http.HttpOptions._

class CoverallsClientTest extends WordSpec with BeforeAndAfterAll with Matchers {

val defaultEndpoint = "https://coveralls.io"

"CoverallsClient" when {
"making API call" should {

"return a valid response for success" in {
val testHttpClient = new TestSuccessHttpClient()
val coverallsClient = new CoverallsClient(testHttpClient, Codec.UTF8, JsonEncoding.UTF8)
val coverallsClient = new CoverallsClient(defaultEndpoint, testHttpClient, Codec.UTF8, JsonEncoding.UTF8)

val response = coverallsClient.postFile(new File("src/test/resources/TestSourceFile.scala"))

Expand All @@ -31,7 +33,7 @@ class CoverallsClientTest extends WordSpec with BeforeAndAfterAll with Matchers

"return a valid response with Korean for success" in {
val testHttpClient = new TestSuccessHttpClient()
val coverallsClient = new CoverallsClient(testHttpClient, Codec.UTF8, JsonEncoding.UTF8)
val coverallsClient = new CoverallsClient(defaultEndpoint, testHttpClient, Codec.UTF8, JsonEncoding.UTF8)

val response = coverallsClient.postFile(new File("src/test/resources/TestSourceFileWithKorean.scala"))

Expand All @@ -46,7 +48,7 @@ class CoverallsClientTest extends WordSpec with BeforeAndAfterAll with Matchers
500,
"""{"message":"Couldn't find a repository matching this job.","error":true}"""
)
val coverallsClient = new CoverallsClient(testHttpClient, Codec.UTF8, JsonEncoding.UTF8)
val coverallsClient = new CoverallsClient(defaultEndpoint, testHttpClient, Codec.UTF8, JsonEncoding.UTF8)

val attemptAtResponse = Try {
coverallsClient.postFile(new File("src/test/resources/TestSourceFileWithKorean.scala"))
Expand All @@ -57,14 +59,22 @@ class CoverallsClientTest extends WordSpec with BeforeAndAfterAll with Matchers
assert(attemptAtResponse.get.error)

}

"use the endpoint to build the url" in {
val testHttpClient = new TestSuccessHttpClient()
val coverallsClient = new CoverallsClient("https://test.endpoint", testHttpClient, Codec.UTF8, JsonEncoding.UTF8)

assert(coverallsClient.url == "https://test.endpoint/api/v1/jobs")
}
}
}

"OpenJdkSafeSsl" when {
"connecting to " + CoverallsClient.url should {
val url = "https://coveralls.io/api/v1/jobs"
"connecting to " + url should {
"connect using ssl" in {
val openJdkSafeSsl = new OpenJdkSafeSsl
val request = Http.get(CoverallsClient.url)
val request = Http.get(url)
.option(connTimeout(60000))
.option(readTimeout(60000))
.option(sslSocketFactory(openJdkSafeSsl))
Expand Down

0 comments on commit 531547e

Please sign in to comment.