mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-26 08:27:12 -07:00
Use authenticated queries to get the releases.
This commit is contained in:
parent
e933faf0c5
commit
d0349d44ce
1 changed files with 26 additions and 25 deletions
|
@ -4,27 +4,17 @@ import com.beust.kobalt.KobaltException
|
||||||
import com.beust.kobalt.internal.DocUrl
|
import com.beust.kobalt.internal.DocUrl
|
||||||
import com.beust.kobalt.maven.Http
|
import com.beust.kobalt.maven.Http
|
||||||
import com.google.gson.Gson
|
import com.google.gson.Gson
|
||||||
import com.google.gson.JsonArray
|
|
||||||
import com.google.gson.JsonObject
|
|
||||||
import com.google.gson.JsonParser
|
|
||||||
import com.google.gson.annotations.SerializedName
|
import com.google.gson.annotations.SerializedName
|
||||||
import com.squareup.okhttp.Headers
|
import com.squareup.okhttp.Headers
|
||||||
import com.squareup.okhttp.OkHttpClient
|
import com.squareup.okhttp.OkHttpClient
|
||||||
import retrofit.RestAdapter
|
import retrofit.RestAdapter
|
||||||
import retrofit.RetrofitError
|
import retrofit.RetrofitError
|
||||||
import retrofit.client.OkClient
|
import retrofit.client.OkClient
|
||||||
import retrofit.http.Body
|
import retrofit.http.*
|
||||||
import retrofit.http.POST
|
|
||||||
import retrofit.http.Path
|
|
||||||
import retrofit.http.Query
|
|
||||||
import retrofit.mime.TypedByteArray
|
import retrofit.mime.TypedByteArray
|
||||||
import retrofit.mime.TypedFile
|
import retrofit.mime.TypedFile
|
||||||
import rx.Observable
|
import rx.Observable
|
||||||
import java.io.BufferedReader
|
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.IOException
|
|
||||||
import java.io.InputStreamReader
|
|
||||||
import java.net.URL
|
|
||||||
import java.util.concurrent.Callable
|
import java.util.concurrent.Callable
|
||||||
import java.util.concurrent.Future
|
import java.util.concurrent.Future
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
@ -43,6 +33,8 @@ public class GithubApi @Inject constructor(val executors: KobaltExecutors,
|
||||||
class RetrofitErrorResponse(val code: String?, val field: String?)
|
class RetrofitErrorResponse(val code: String?, val field: String?)
|
||||||
class RetrofitErrorsResponse(val message: String?, val errors: List<RetrofitErrorResponse>)
|
class RetrofitErrorsResponse(val message: String?, val errors: List<RetrofitErrorResponse>)
|
||||||
|
|
||||||
|
private val docUrl = DocUrl.PUBLISH_PLUGIN_URL
|
||||||
|
|
||||||
private fun parseRetrofitError(e: Throwable) : RetrofitErrorsResponse {
|
private fun parseRetrofitError(e: Throwable) : RetrofitErrorsResponse {
|
||||||
val re = e as RetrofitError
|
val re = e as RetrofitError
|
||||||
val json = String((re.response.body as TypedByteArray).bytes)
|
val json = String((re.response.body as TypedByteArray).bytes)
|
||||||
|
@ -52,7 +44,6 @@ public class GithubApi @Inject constructor(val executors: KobaltExecutors,
|
||||||
fun uploadRelease(packageName: String, tagName: String, zipFile: File) {
|
fun uploadRelease(packageName: String, tagName: String, zipFile: File) {
|
||||||
log(1, "Uploading release ${zipFile.name}")
|
log(1, "Uploading release ${zipFile.name}")
|
||||||
|
|
||||||
val docUrl = DocUrl.PUBLISH_PLUGIN_URL
|
|
||||||
val username = localProperties.get(PROPERTY_USERNAME, docUrl)
|
val username = localProperties.get(PROPERTY_USERNAME, docUrl)
|
||||||
val accessToken = localProperties.get(PROPERTY_ACCESS_TOKEN, docUrl)
|
val accessToken = localProperties.get(PROPERTY_ACCESS_TOKEN, docUrl)
|
||||||
try {
|
try {
|
||||||
|
@ -103,6 +94,8 @@ public class GithubApi @Inject constructor(val executors: KobaltExecutors,
|
||||||
var name: String? = tagName)
|
var name: String? = tagName)
|
||||||
class CreateReleaseResponse(var id: String? = null, @SerializedName("upload_url") var uploadUrl: String?)
|
class CreateReleaseResponse(var id: String? = null, @SerializedName("upload_url") var uploadUrl: String?)
|
||||||
class UploadAssetResponse(var id: String? = null, val name: String? = null)
|
class UploadAssetResponse(var id: String? = null, val name: String? = null)
|
||||||
|
class ReleasesResponse(@SerializedName("tag_name") var tagName: String? = null,
|
||||||
|
var name: String? = tagName)
|
||||||
|
|
||||||
interface Api {
|
interface Api {
|
||||||
@POST("/repos/{owner}/{repo}/releases")
|
@POST("/repos/{owner}/{repo}/releases")
|
||||||
|
@ -110,31 +103,39 @@ public class GithubApi @Inject constructor(val executors: KobaltExecutors,
|
||||||
@Query("access_token") accessToken: String,
|
@Query("access_token") accessToken: String,
|
||||||
@Path("repo") repo: String,
|
@Path("repo") repo: String,
|
||||||
@Body createRelease: CreateRelease): Observable<CreateReleaseResponse>
|
@Body createRelease: CreateRelease): Observable<CreateReleaseResponse>
|
||||||
|
|
||||||
|
@GET("/repos/{owner}/{repo}/releases")
|
||||||
|
fun getReleases(@Path("owner") owner: String,
|
||||||
|
@Query("access_token") accessToken: String,
|
||||||
|
@Path("repo") repo: String): List<ReleasesResponse>
|
||||||
}
|
}
|
||||||
|
|
||||||
val latestKobaltVersion: Future<String>
|
val latestKobaltVersion: Future<String>
|
||||||
get() {
|
get() {
|
||||||
val callable = Callable<String> {
|
val callable = Callable<String> {
|
||||||
var result = "0"
|
var result = "0"
|
||||||
|
|
||||||
|
val username = localProperties.get(PROPERTY_USERNAME, docUrl)
|
||||||
|
val accessToken = localProperties.get(PROPERTY_ACCESS_TOKEN, docUrl)
|
||||||
try {
|
try {
|
||||||
val ins = URL(RELEASES_URL).openConnection().inputStream
|
val releases = service.getReleases(username, accessToken, "kobalt")
|
||||||
@Suppress("UNCHECKED_CAST")
|
if (releases.size > 0) {
|
||||||
val reader = BufferedReader(InputStreamReader(ins))
|
var versionName = releases[0].name
|
||||||
val jo = JsonParser().parse(reader) as JsonArray
|
if (versionName == null || versionName.isBlank()) {
|
||||||
if (jo.size() > 0) {
|
versionName = releases[0].tagName
|
||||||
var versionName = (jo.get(0) as JsonObject).get("name").asString
|
if (versionName != null && !versionName.isBlank()) {
|
||||||
if (Strings.isEmpty(versionName)) {
|
result = versionName
|
||||||
versionName = (jo.get(0) as JsonObject).get("tag_name").asString
|
}
|
||||||
}
|
|
||||||
if (versionName != null) {
|
|
||||||
result = versionName
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch(ex: IOException) {
|
} catch(e: RetrofitError) {
|
||||||
warn("Couldn't load the release URL: $RELEASES_URL")
|
val error = parseRetrofitError(e)
|
||||||
|
throw KobaltException("Couldn't upload release, ${error.message}: "
|
||||||
|
+ error.errors[0].code + " field: " + error.errors[0].field)
|
||||||
}
|
}
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
return executors.miscExecutor.submit(callable)
|
return executors.miscExecutor.submit(callable)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue