Skip to content
This repository has been archived by the owner on Feb 7, 2021. It is now read-only.

Commit

Permalink
added checks for status code on bintray calls to fail build when some…
Browse files Browse the repository at this point in the history
…thing goes wrong
  • Loading branch information
rpalcolea committed Mar 26, 2019
1 parent 09b1f71 commit 2f4108b
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 24 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ dependencies {
implementation("org.jfrog.buildinfo:build-info-extractor-gradle:latest.release") {
exclude(module = "groovy-all")
}

testImplementation("com.github.tomakehurst:wiremock:2.22.0")
}

pluginBundle {
Expand Down
25 changes: 16 additions & 9 deletions src/main/kotlin/nebula/plugin/bintray/BintrayClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import okhttp3.Credentials
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Call
import retrofit2.Response
import org.apache.http.HttpStatus
import org.gradle.api.GradleException
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory

Expand All @@ -23,16 +23,23 @@ class BintrayClient private constructor(val bintrayService: BintrayService) {
fun build() = BintrayClient(bintray(apiUrl!!, user!!, apiKey!!))
}

fun createOrUpdatePackage(subject: String, repo: String, pkg: String, packageRequest: PackageRequest) : Call<Void> {
if(bintrayService.getPackage(subject, repo, pkg).execute().isSuccessful) {
return bintrayService.updatePackage(subject, repo, packageRequest)
} else {
return bintrayService.createPackage(subject, repo, packageRequest)
fun createOrUpdatePackage(subject: String, repo: String, pkg: String, packageRequest: PackageRequest) {
val getPackageResult = bintrayService.getPackage(subject, repo, pkg).execute()
if(!getPackageResult.isSuccessful && getPackageResult.code() != HttpStatus.SC_NOT_FOUND) {
throw GradleException("Could not obtain information for package $repo/$subject/$pkg - ${getPackageResult.errorBody()?.string()}")
}

val createOrUpdatePackageResult = if(getPackageResult.isSuccessful) bintrayService.updatePackage(subject, repo, packageRequest).execute() else bintrayService.createPackage(subject, repo, packageRequest).execute()
if(!createOrUpdatePackageResult.isSuccessful) {
throw GradleException("Could not create or update information for package $repo/$subject/$pkg - ${getPackageResult.errorBody()?.string()}")
}
}

fun publishVersion(subject: String, repo: String, pkg: String, version: String, publishRequest: PublishRequest) : Response<Void> {
return bintrayService.publishVersion(subject, repo, pkg, version, publishRequest).execute()
fun publishVersion(subject: String, repo: String, pkg: String, version: String, publishRequest: PublishRequest) {
val publishVersionResult = bintrayService.publishVersion(subject, repo, pkg, version, publishRequest).execute()
if(!publishVersionResult.isSuccessful) {
throw GradleException("Could not publish $version version for package $repo/$subject/$pkg - ${publishVersionResult.errorBody()?.string()}")
}
}
}

Expand Down
9 changes: 5 additions & 4 deletions src/main/kotlin/nebula/plugin/bintray/BintrayService.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package nebula.plugin.bintray

import okhttp3.ResponseBody
import retrofit2.Call
import retrofit2.http.*

Expand All @@ -12,29 +13,29 @@ interface BintrayService {
@Path("pkg") pkg: String,
@Path("version") version: String,
@Body body: PublishRequest
): Call<Void>
): Call<ResponseBody>

@Headers("Content-Type: application/json")
@GET("/packages/{subject}/{repo}/{pkg}")
fun getPackage(
@Path("subject") subject: String,
@Path("repo") repo: String,
@Path("pkg") pkg: String
): Call<Void>
): Call<ResponseBody>

@Headers("Content-Type: application/json")
@POST("/packages/{subject}/{repo}")
fun createPackage(
@Path("subject") subject: String,
@Path("repo") repo: String,
@Body body: PackageRequest
): Call<Void>
): Call<ResponseBody>

@Headers("Content-Type: application/json")
@PUT("/packages/{subject}/{repo}")
fun updatePackage(
@Path("subject") subject: String,
@Path("repo") repo: String,
@Body body: PackageRequest
): Call<Void>
): Call<ResponseBody>
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package nebula.plugin.bintray

import org.gradle.api.GradleException
import org.gradle.api.tasks.TaskAction

open class NebulaBintrayVersionTask : NebulaBintrayAbstractTask() {
Expand All @@ -19,11 +18,7 @@ open class NebulaBintrayVersionTask : NebulaBintrayAbstractTask() {
.build()


val result = bintrayClient.publishVersion(resolvedSubject, resolvedRepoName, resolvedPkgName, resolvedVersion, PublishRequest())
if (result.isSuccessful) {
logger.info("$resolvedPkgName version $resolvedVersion has been published")
} else {
throw GradleException("Received ${result.code()} attempting to publish $resolvedPkgName version $resolvedVersion")
}
bintrayClient.publishVersion(resolvedSubject, resolvedRepoName, resolvedPkgName, resolvedVersion, PublishRequest())
logger.info("$resolvedPkgName version $resolvedVersion has been published")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,16 @@
*/
package nebula.plugin.bintray

import com.github.tomakehurst.wiremock.core.WireMockConfiguration
import com.github.tomakehurst.wiremock.junit.WireMockRule
import org.junit.Rule
import static com.github.tomakehurst.wiremock.client.WireMock.*

class NebulaBintrayPublishingPluginIntegrationSpec extends LauncherSpec {

@Rule
WireMockRule wireMockRule = new WireMockRule(WireMockConfiguration.wireMockConfig().dynamicPort().dynamicPort())

def 'apply plugin'() {
given:
buildFile << """
Expand Down Expand Up @@ -45,8 +52,14 @@ class NebulaBintrayPublishingPluginIntegrationSpec extends LauncherSpec {
result.standardOutput.contains('Task :publishPackageToBintray')
}

def 'publishes library to bintray'() {
def 'build fails if bad response from bintray'() {
given:
stubFor(get(urlEqualTo("/packages/nebula/gradle-plugins/my-plugin"))
.willReturn(aResponse()
.withStatus(500)
.withHeader("Content-Type", "application/json")))


buildFile << """
apply plugin: 'nebula.nebula-bintray'
apply plugin: 'java'
Expand All @@ -58,7 +71,7 @@ class NebulaBintrayPublishingPluginIntegrationSpec extends LauncherSpec {
bintray {
user = 'nebula-plugins'
apiKey = 'mykey'
apiUrl = 'https://api.bintray.com'
apiUrl = 'http://localhost:${wireMockRule.port()}'
pkgName = 'my-plugin'
autoPublish = true
}
Expand All @@ -71,6 +84,6 @@ class NebulaBintrayPublishingPluginIntegrationSpec extends LauncherSpec {
def result = runTasks('publishPackageToBintray')

then:
result.standardOutput.contains('Task :publishPackageToBintray')
result.standardError.contains('Could not obtain information for package gradle-plugins/nebula/my-plugin')
}
}

0 comments on commit 2f4108b

Please sign in to comment.