From 2c2cd8b16bdeb222a5d158b870441f6d1c5ab099 Mon Sep 17 00:00:00 2001 From: evanchooly Date: Thu, 31 Mar 2016 23:26:30 -0400 Subject: [PATCH] trying to retrofit BintrayApi and add package creation support --- .idea/libraries/KotlinJavaRuntime.xml | 12 --- .idea/libraries/kobalt__Test_.xml | 4 +- .idea/misc.xml | 2 +- kobalt.iml | 2 - kobalt/Build.kt.iml | 6 +- kobalt/src/Build.kt | 3 +- .../kobalt-plugin-api/kobalt-plugin-api.iml | 5 +- modules/wrapper/kobalt-wrapper.iml | 11 +-- .../beust/kobalt/plugin/publish/BintrayApi.kt | 84 +++++++++++++++++-- .../resources/templates/idea/Build.kt.iml | 12 --- 10 files changed, 87 insertions(+), 54 deletions(-) delete mode 100644 .idea/libraries/KotlinJavaRuntime.xml delete mode 100644 src/main/resources/templates/idea/Build.kt.iml diff --git a/.idea/libraries/KotlinJavaRuntime.xml b/.idea/libraries/KotlinJavaRuntime.xml deleted file mode 100644 index 207fc74d..00000000 --- a/.idea/libraries/KotlinJavaRuntime.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/libraries/kobalt__Test_.xml b/.idea/libraries/kobalt__Test_.xml index faf74539..a93b84bc 100644 --- a/.idea/libraries/kobalt__Test_.xml +++ b/.idea/libraries/kobalt__Test_.xml @@ -1,9 +1,9 @@ - - + + diff --git a/.idea/misc.xml b/.idea/misc.xml index 4079311b..df7e8eb0 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -3,7 +3,7 @@ - + \ No newline at end of file diff --git a/kobalt.iml b/kobalt.iml index 9510060e..8fbec6cf 100644 --- a/kobalt.iml +++ b/kobalt.iml @@ -3,7 +3,6 @@ - @@ -11,7 +10,6 @@ - diff --git a/kobalt/Build.kt.iml b/kobalt/Build.kt.iml index d3683da5..62cf5e77 100644 --- a/kobalt/Build.kt.iml +++ b/kobalt/Build.kt.iml @@ -2,12 +2,8 @@ - - - - + - \ No newline at end of file diff --git a/kobalt/src/Build.kt b/kobalt/src/Build.kt index 2388fe60..b790fe9f 100644 --- a/kobalt/src/Build.kt +++ b/kobalt/src/Build.kt @@ -132,8 +132,9 @@ val kobaltApp = project(kobaltPluginApi, wrapper) { "com.google.code.findbugs:jsr305:3.0.1", "com.google.code.gson:gson:${Versions.gson}", "com.squareup.okhttp3:okhttp:${Versions.okhttp}", + "com.squareup.retrofit2:retrofit:${Versions.retrofit}", + "com.squareup.retrofit2:converter-gson:${Versions.retrofit}", "org.codehaus.plexus:plexus-utils:3.0.22", - "biz.aQute.bnd:bndlib:2.4.0" ) diff --git a/modules/kobalt-plugin-api/kobalt-plugin-api.iml b/modules/kobalt-plugin-api/kobalt-plugin-api.iml index 64fabb37..21255f15 100644 --- a/modules/kobalt-plugin-api/kobalt-plugin-api.iml +++ b/modules/kobalt-plugin-api/kobalt-plugin-api.iml @@ -1,13 +1,12 @@ + - - - + diff --git a/modules/wrapper/kobalt-wrapper.iml b/modules/wrapper/kobalt-wrapper.iml index d31f73ee..8cefc473 100644 --- a/modules/wrapper/kobalt-wrapper.iml +++ b/modules/wrapper/kobalt-wrapper.iml @@ -1,21 +1,12 @@ + - - - - - - - - - - diff --git a/src/main/kotlin/com/beust/kobalt/plugin/publish/BintrayApi.kt b/src/main/kotlin/com/beust/kobalt/plugin/publish/BintrayApi.kt index cae618ed..7c1c42d1 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/publish/BintrayApi.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/publish/BintrayApi.kt @@ -13,8 +13,20 @@ import com.beust.kobalt.misc.warn import com.google.common.net.MediaType import com.google.gson.JsonObject import com.google.gson.JsonParser +import com.google.gson.annotations.SerializedName import com.google.inject.assistedinject.Assisted +import okhttp3.Credentials +import okhttp3.Interceptor +import okhttp3.OkHttpClient import okhttp3.Response +import retrofit2.Call +import retrofit2.Retrofit +import retrofit2.converter.gson.GsonConverterFactory +import retrofit2.http.Body +import retrofit2.http.GET +import retrofit2.http.PATCH +import retrofit2.http.POST +import retrofit2.http.Path import java.io.File import javax.annotation.Nullable import javax.inject.Inject @@ -60,21 +72,81 @@ class BintrayApi @Inject constructor ( @Nullable @Assisted("org") org: String?) : BintrayApi } - fun packageExists(packageName: String) : Boolean { + class ReleaseResponse(var id: String? = null, @SerializedName("upload_url") var uploadUrl: String?) + + interface Api { + @GET("/packages/{owner}/maven/{package}") + fun getPackage(@Path("owner") owner: String, + @Path("package") name: String): Call + + @POST("/packages/{owner}/maven/{package}") + fun createPackage(@Path("owner") owner: String, + @Path("package") name: String, + @Body content: String): Call + +/* + @GET("/repos/{owner}/{repo}/releases") + fun getReleases(@Path("owner") owner: String, + @Path("repo") repo: String, + @Query("access_token") accessToken: String): Call> + + @GET("/repos/{owner}/{repo}/releases") + fun getReleasesNoAuth(@Path("owner") owner: String, + @Path("repo") repo: String): Call> +*/ + } + + private val service: Api + + init { + val builder = OkHttpClient.Builder() + builder.interceptors().add(Interceptor { chain -> + var original = chain.request(); + + var requestBuilder = original.newBuilder() + .header("Authorization", Credentials.basic(username, password)) + .header("Accept", "application/json") + .method(original.method(), original.body()); + + chain.proceed(requestBuilder.build()); + }) + val okHttpClient = builder.build() + + service = Retrofit.Builder() + .client(okHttpClient) + .baseUrl(UnauthenticatedBintrayApi.BINTRAY_URL_API) + .addConverterFactory(GsonConverterFactory.create()) + .build() + .create(Api::class.java) + } + + fun packageExists(project: Project) : Boolean { val url = arrayListOf(UnauthenticatedBintrayApi.BINTRAY_URL_API, "packages", org ?: username!!, - "maven", packageName) + "maven", project.name) .joinToString("/") val jcResponse = parseResponse(http.get(username, password, url)) + val execute = service.getPackage(org ?: username!!, project.name).execute() - if (jcResponse.errorMessage != null) { - throw KobaltException("Error from Bintray: ${jcResponse.errorMessage}") + if (execute.errorBody()?.string()?.contains("was not found") ?: false) { + warn("Package does not exist on bintray. Creating now.") + val content = mapOf( + "desc" to project.description, + "vcs_url" to (project.scm?.url ?: ""), + "licences" to """[ "Apache-2.0" ]""", + "website_url" to (project.url ?: "") + ).toString() + val result = service.createPackage(org ?: username!!, project.name, content).execute() + if (result.errorBody() != null) { + error(" Errors while creating package:\n" + result.errorBody().string()) + return false + } } - return jcResponse.jo!!.get("name").asString == packageName + return jcResponse.jo!!.get("name").asString == project.name } fun uploadMaven(project: Project, files: List, config: BintrayConfig?) : TaskResult { - if (! packageExists(project.name)) { + if (! packageExists(project)) { throw KobaltException("Couldn't find a package called ${project.name} on bintray, please create one first" + " as explained at https://bintray.com/docs/usermanual/uploads/uploads_creatinganewpackage.html") } diff --git a/src/main/resources/templates/idea/Build.kt.iml b/src/main/resources/templates/idea/Build.kt.iml deleted file mode 100644 index dfa4c6b9..00000000 --- a/src/main/resources/templates/idea/Build.kt.iml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - - - - - - -