Skip to content
This repository was archived by the owner on Feb 19, 2020. It is now read-only.

Commit 259943d

Browse files
committed
initial commit
0 parents  commit 259943d

File tree

16 files changed

+471
-0
lines changed

16 files changed

+471
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.*
2+
!.gitignore
3+
!.java-version
4+
!.travis.yml
5+
project/project
6+
target

.java-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
oracle64-1.8.0.25

.travis.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
language: scala
2+
scala:
3+
- 2.10.4
4+
before_script:
5+
- cd module-code/
6+
script:
7+
- sbt ++$TRAVIS_SCALA_VERSION compile
8+
- sbt ++$TRAVIS_SCALA_VERSION test
9+
jdk:
10+
- oraclejdk8

build.sbt

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import Dependencies._
2+
3+
name := "sbt-codacy-coverage"
4+
5+
version := "1.0.0"
6+
7+
scalaVersion := "2.10.4"
8+
9+
scalacOptions := Seq("-deprecation", "-feature", "-unchecked", "-Ywarn-adapted-args", "-Xlint", "-Xfatal-warnings")
10+
11+
sbtPlugin := true
12+
13+
resolvers ++= Seq(
14+
DefaultMavenRepository,
15+
"Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/",
16+
"Sonatype snapshots" at "http://oss.sonatype.org/content/repositories/snapshots/",
17+
Classpaths.typesafeReleases,
18+
Classpaths.sbtPluginReleases
19+
)
20+
21+
libraryDependencies ++= Seq(
22+
playJson,
23+
playWs,
24+
jGit,
25+
scalaTest
26+
)
27+
28+
organization := "com.codacy"
29+
30+
organizationName := "Codacy"
31+
32+
organizationHomepage := Some(new URL("https://www.codacy.com"))
33+
34+
publishMavenStyle := true
35+
36+
publishArtifact in Test := false
37+
38+
pomIncludeRepository := { _ => false}
39+
40+
publishTo := {
41+
val nexus = "https://oss.sonatype.org/"
42+
if (version.value.trim.endsWith("SNAPSHOT"))
43+
Some("snapshots" at nexus + "content/repositories/snapshots")
44+
else
45+
Some("releases" at nexus + "service/local/staging/deploy/maven2")
46+
}
47+
48+
startYear := Some(2014)
49+
50+
description := "Codacy Coverage Plugin for Scala"
51+
52+
licenses := Seq("The Apache Software License, Version 2.0" -> url("http://www.apache.org/licenses/LICENSE-2.0.txt"))
53+
54+
homepage := Some(url("http://www.github.com/codacy/sbt-codacy-coverage/"))
55+
56+
pomExtra :=
57+
<scm>
58+
<url>https://github.com/codacy/sbt-codacy-coverage</url>
59+
<connection>scm:git:git@github.com:codacy/sbt-codacy-coverage.git</connection>
60+
<developerConnection>scm:git:https://github.com/codacy/sbt-codacy-coverage.git</developerConnection>
61+
</scm>
62+
<developers>
63+
<developer>
64+
<id>mrfyda</id>
65+
<name>Rafael</name>
66+
<email>rafael [at] codacy.com</email>
67+
<url>https://github.com/mrfyda</url>
68+
</developer>
69+
</developers>

project/Dependencies.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import sbt._
2+
3+
object Dependencies {
4+
5+
lazy val playJson = "com.typesafe.play" %% "play-json" % "2.3.6"
6+
lazy val playWs = "com.typesafe.play" %% "play-ws" % "2.3.6"
7+
lazy val jGit = "org.eclipse.jgit" % "org.eclipse.jgit" % "3.4.1.201406201815-r"
8+
lazy val scalaTest = "org.scalatest" %% "scalatest" % "2.2.1" % "test"
9+
10+
}

project/build.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sbt.version=0.13.6

project/plugins.sbt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import sbt._
2+
3+
ivyLoggingLevel := UpdateLogging.Full
4+
5+
logLevel := sbt.Level.Error
6+
7+
resolvers ++= Seq(
8+
DefaultMavenRepository,
9+
"Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/",
10+
"Sonatype snapshots" at "http://oss.sonatype.org/content/repositories/snapshots/",
11+
Classpaths.typesafeReleases,
12+
Classpaths.sbtPluginReleases
13+
)
14+
15+
addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.6.0")
16+
17+
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.4.0")
18+
19+
addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "0.2.1")
20+
21+
addSbtPlugin("com.typesafe.sbt" % "sbt-pgp" % "0.8.3")
22+
23+
addSbtPlugin("com.timushev.sbt" % "sbt-updates" % "0.1.6")

src/main/resources/logback.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<configuration>
2+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
3+
<encoder>
4+
<pattern>%d{HH:mm:ss.SSS} TKD [%thread] %-5level %logger{36} - %msg%n
5+
</pattern>
6+
</encoder>
7+
</appender>
8+
9+
<root level="debug">
10+
<appender-ref ref="STDOUT"/>
11+
</root>
12+
</configuration>
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.codacy
2+
3+
import java.io.File
4+
5+
import com.codacy.api.CodacyAPIClient
6+
import com.codacy.io.FileUtils
7+
import com.codacy.parsers.CoberturaParser
8+
import com.codacy.vcs.GitClient
9+
import play.api.libs.json.Json
10+
import sbt.Keys._
11+
import sbt._
12+
13+
import scala.util.Try
14+
15+
object CodacyCoveragePlugin extends AutoPlugin {
16+
17+
object autoImport {
18+
val codacyCoverage = taskKey[Unit]("Upload coverage reports to Codacy.")
19+
val codacyProjectToken = settingKey[Option[String]]("Your project token.")
20+
val codacyProjectTokenFile = settingKey[Option[String]]("Path for file containing your project token.")
21+
val coberturaFile = settingKey[File]("Path for project Cobertura file.")
22+
23+
lazy val baseSettings: Seq[Def.Setting[_]] = Seq(
24+
codacyCoverage := {
25+
codacyCoverageCommand(state.value, baseDirectory.value, coberturaFile.value,
26+
crossTarget.value / "coverage-report" / "codacy-coverage.json",
27+
codacyProjectToken.value, codacyProjectTokenFile.value)
28+
},
29+
codacyProjectToken := None,
30+
codacyProjectTokenFile := None,
31+
coberturaFile := crossTarget.value / ("coverage-report" + File.separator + "cobertura.xml")
32+
)
33+
}
34+
35+
import com.codacy.CodacyCoveragePlugin.autoImport._
36+
37+
//override def requires = ScoverageSbtPlugin
38+
39+
override def trigger = allRequirements
40+
41+
override val projectSettings = baseSettings
42+
43+
private def codacyCoverageCommand(state: State, rootProjectDir: File, coberturaFile: File, codacyCoverageFile: File,
44+
codacyToken: Option[String], codacyTokenFile: Option[String]): Unit = {
45+
implicit val logger: Logger = state.log
46+
47+
getProjectToken(codacyToken, codacyTokenFile).fold[State] {
48+
logger.error("Project token not defined.")
49+
state.exit(ok = false)
50+
} {
51+
projectToken =>
52+
53+
Try {
54+
new GitClient(rootProjectDir).latestCommitUuid()
55+
}.toOption.fold[State] {
56+
logger.error("Could not get current commit.")
57+
state.exit(ok = false)
58+
} {
59+
commitUuid =>
60+
61+
logger.info(s"Preparing coverage data for commit ${commitUuid.take(7)}...")
62+
63+
FileUtils.get(coberturaFile).fold[State] {
64+
state.exit(ok = false)
65+
} {
66+
coberturaFile =>
67+
68+
val reader = new CoberturaParser(coberturaFile, rootProjectDir)
69+
val report = reader.generateReport()
70+
FileUtils.write(codacyCoverageFile, Json.toJson(report).toString())
71+
72+
logger.info(s"Uploading coverage data...")
73+
74+
new CodacyAPIClient().postCoverageFile(projectToken, commitUuid, codacyCoverageFile).fold[State](
75+
error => {
76+
logger.error(s"Failed to upload data. Reason: $error")
77+
state.exit(ok = false)
78+
},
79+
response => {
80+
logger.success(s"Coverage data uploaded. $response")
81+
state
82+
})
83+
}
84+
}
85+
}
86+
}
87+
88+
private def getProjectToken(codacyProjectToken: Option[String], codacyProjectTokenFile: Option[String]) = {
89+
sys.env.get("CODACY_PROJECT_TOKEN")
90+
.orElse(codacyProjectToken)
91+
.orElse(codacyProjectTokenFile.flatMap(FileUtils.read))
92+
}
93+
94+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.codacy.api
2+
3+
import java.io.File
4+
5+
import com.ning.http.client.AsyncHttpClient
6+
import play.api.libs.ws.WSClient
7+
import play.api.libs.ws.ning.NingWSClient
8+
9+
import scala.concurrent.Await
10+
import scala.concurrent.duration._
11+
import scala.util.Try
12+
13+
class CodacyAPIClient {
14+
val client: WSClient = new NingWSClient(new AsyncHttpClient().getConfig)
15+
16+
def postCoverageFile(projectToken: String, commitUuid: String, file: File): Either[String, String] = {
17+
val url = s"https://www.codacy.com/api/coverage/$projectToken/$commitUuid"
18+
19+
val responseOpt = Try {
20+
val future = client.url(url).post(file)
21+
Await.result(future, Duration(10, SECONDS))
22+
}.toOption
23+
24+
responseOpt.map {
25+
response =>
26+
Try {
27+
Right((response.json \ "success").as[String])
28+
}.toOption.getOrElse {
29+
Left(response.body)
30+
}
31+
}.getOrElse {
32+
Left("Could not connect to server.")
33+
}
34+
}
35+
36+
}

0 commit comments

Comments
 (0)