From 2f4108b3c24b7e17d29334eff16d4c67eb607581 Mon Sep 17 00:00:00 2001 From: Roberto Perez Alcolea Date: Tue, 26 Mar 2019 10:46:14 -0700 Subject: [PATCH] added checks for status code on bintray calls to fail build when something goes wrong --- build.gradle.kts | 2 +- .../nebula/plugin/bintray/BintrayClient.kt | 25 ++++++++++++------- .../nebula/plugin/bintray/BintrayService.kt | 9 ++++--- .../bintray/NebulaBintrayVersionTask.kt | 9 ++----- ...trayPublishingPluginIntegrationSpec.groovy | 19 +++++++++++--- 5 files changed, 40 insertions(+), 24 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 7e701a6..3834fa3 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -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 { diff --git a/src/main/kotlin/nebula/plugin/bintray/BintrayClient.kt b/src/main/kotlin/nebula/plugin/bintray/BintrayClient.kt index 7ea3c33..36759c9 100644 --- a/src/main/kotlin/nebula/plugin/bintray/BintrayClient.kt +++ b/src/main/kotlin/nebula/plugin/bintray/BintrayClient.kt @@ -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 @@ -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 { - 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 { - 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()}") + } } } diff --git a/src/main/kotlin/nebula/plugin/bintray/BintrayService.kt b/src/main/kotlin/nebula/plugin/bintray/BintrayService.kt index 75c89b0..ce00c65 100644 --- a/src/main/kotlin/nebula/plugin/bintray/BintrayService.kt +++ b/src/main/kotlin/nebula/plugin/bintray/BintrayService.kt @@ -1,5 +1,6 @@ package nebula.plugin.bintray +import okhttp3.ResponseBody import retrofit2.Call import retrofit2.http.* @@ -12,7 +13,7 @@ interface BintrayService { @Path("pkg") pkg: String, @Path("version") version: String, @Body body: PublishRequest - ): Call + ): Call @Headers("Content-Type: application/json") @GET("/packages/{subject}/{repo}/{pkg}") @@ -20,7 +21,7 @@ interface BintrayService { @Path("subject") subject: String, @Path("repo") repo: String, @Path("pkg") pkg: String - ): Call + ): Call @Headers("Content-Type: application/json") @POST("/packages/{subject}/{repo}") @@ -28,7 +29,7 @@ interface BintrayService { @Path("subject") subject: String, @Path("repo") repo: String, @Body body: PackageRequest - ): Call + ): Call @Headers("Content-Type: application/json") @PUT("/packages/{subject}/{repo}") @@ -36,5 +37,5 @@ interface BintrayService { @Path("subject") subject: String, @Path("repo") repo: String, @Body body: PackageRequest - ): Call + ): Call } \ No newline at end of file diff --git a/src/main/kotlin/nebula/plugin/bintray/NebulaBintrayVersionTask.kt b/src/main/kotlin/nebula/plugin/bintray/NebulaBintrayVersionTask.kt index b5fbfbe..81cb02c 100644 --- a/src/main/kotlin/nebula/plugin/bintray/NebulaBintrayVersionTask.kt +++ b/src/main/kotlin/nebula/plugin/bintray/NebulaBintrayVersionTask.kt @@ -1,6 +1,5 @@ package nebula.plugin.bintray -import org.gradle.api.GradleException import org.gradle.api.tasks.TaskAction open class NebulaBintrayVersionTask : NebulaBintrayAbstractTask() { @@ -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") } } \ No newline at end of file diff --git a/src/test/groovy/nebula/plugin/bintray/NebulaBintrayPublishingPluginIntegrationSpec.groovy b/src/test/groovy/nebula/plugin/bintray/NebulaBintrayPublishingPluginIntegrationSpec.groovy index a7191b0..4aec228 100644 --- a/src/test/groovy/nebula/plugin/bintray/NebulaBintrayPublishingPluginIntegrationSpec.groovy +++ b/src/test/groovy/nebula/plugin/bintray/NebulaBintrayPublishingPluginIntegrationSpec.groovy @@ -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 << """ @@ -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' @@ -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 } @@ -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') } }