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

Commit 121e865

Browse files
committed
Merge pull request #2 from theanti9/master
Adding ability to specify custom codacy API base
2 parents 7882c1c + 8762c44 commit 121e865

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

src/main/scala/com/codacy/CodacyCoveragePlugin.scala

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,17 @@ object CodacyCoveragePlugin extends AutoPlugin {
1919
val codacyProjectToken = settingKey[Option[String]]("Your project token.")
2020
val codacyProjectTokenFile = settingKey[Option[String]]("Path for file containing your project token.")
2121
val coberturaFile = settingKey[File]("Path for project Cobertura file.")
22+
val codacyApiBaseUrl = settingKey[Option[String]]("The base URL for the Codacy API.")
2223

2324
lazy val baseSettings: Seq[Def.Setting[_]] = Seq(
2425
codacyCoverage := {
2526
codacyCoverageCommand(state.value, baseDirectory.value, coberturaFile.value,
2627
crossTarget.value / "coverage-report" / "codacy-coverage.json",
27-
codacyProjectToken.value, codacyProjectTokenFile.value)
28+
codacyProjectToken.value, codacyProjectTokenFile.value, codacyApiBaseUrl.value)
2829
},
2930
codacyProjectToken := None,
3031
codacyProjectTokenFile := None,
32+
codacyApiBaseUrl := None,
3133
coberturaFile := crossTarget.value / ("coverage-report" + File.separator + "cobertura.xml")
3234
)
3335
}
@@ -40,16 +42,17 @@ object CodacyCoveragePlugin extends AutoPlugin {
4042

4143
override val projectSettings = baseSettings
4244

45+
private val publicApiBaseUrl = "https://www.codacy.com"
46+
4347
private def codacyCoverageCommand(state: State, rootProjectDir: File, coberturaFile: File, codacyCoverageFile: File,
44-
codacyToken: Option[String], codacyTokenFile: Option[String]): Unit = {
48+
codacyToken: Option[String], codacyTokenFile: Option[String], codacyApiBaseUrl: Option[String]): Unit = {
4549
implicit val logger: Logger = state.log
4650

4751
getProjectToken(codacyToken, codacyTokenFile).fold[State] {
4852
logger.error("Project token not defined.")
4953
state.exit(ok = false)
5054
} {
5155
projectToken =>
52-
5356
Try {
5457
new GitClient(rootProjectDir).latestCommitUuid()
5558
}.toOption.fold[State] {
@@ -71,7 +74,10 @@ object CodacyCoveragePlugin extends AutoPlugin {
7174

7275
logger.info(s"Uploading coverage data...")
7376

74-
new CodacyAPIClient().postCoverageFile(projectToken, commitUuid, codacyCoverageFile).fold[State](
77+
new CodacyAPIClient().postCoverageFile(projectToken,
78+
commitUuid,
79+
codacyCoverageFile,
80+
getApiBaseUrl(codacyApiBaseUrl)).fold[State](
7581
error => {
7682
logger.error(s"Failed to upload data. Reason: $error")
7783
state.exit(ok = false)
@@ -85,6 +91,14 @@ object CodacyCoveragePlugin extends AutoPlugin {
8591
}
8692
}
8793

94+
private def getApiBaseUrl(codacyApiBaseUrl: Option[String]): String = {
95+
// Check for an environment variable to override the API URL.
96+
// If it doesn't exist, try the build options or default to the public API.
97+
sys.env.get("CODACY_API_BASE_URL")
98+
.orElse(codacyApiBaseUrl)
99+
.getOrElse(publicApiBaseUrl)
100+
}
101+
88102
private def getProjectToken(codacyProjectToken: Option[String], codacyProjectTokenFile: Option[String]) = {
89103
sys.env.get("CODACY_PROJECT_TOKEN")
90104
.orElse(codacyProjectToken)

src/main/scala/com/codacy/api/CodacyAPIClient.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import scala.util.Try
1313
class CodacyAPIClient {
1414
val client: WSClient = new NingWSClient(new AsyncHttpClient().getConfig)
1515

16-
def postCoverageFile(projectToken: String, commitUuid: String, file: File): Either[String, String] = {
17-
val url = s"https://www.codacy.com/api/coverage/$projectToken/$commitUuid"
16+
def postCoverageFile(projectToken: String, commitUuid: String, file: File, baseUrl: String): Either[String, String] = {
17+
val url = s"$baseUrl/api/coverage/$projectToken/$commitUuid"
1818

1919
val responseOpt = Try {
2020
val future = client.url(url).post(file)

0 commit comments

Comments
 (0)