From a599d0699bf532fc85644ea26d1eabe0e7614429 Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Wed, 16 Dec 2015 00:50:47 +0400 Subject: [PATCH] Fallback for getReleases() if no auth. --- .../kotlin/com/beust/kobalt/misc/GithubApi.kt | 15 ++++++++++++--- .../com/beust/kobalt/misc/LocalProperties.kt | 6 ++++-- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/com/beust/kobalt/misc/GithubApi.kt b/src/main/kotlin/com/beust/kobalt/misc/GithubApi.kt index 27ec797b..c04583cc 100644 --- a/src/main/kotlin/com/beust/kobalt/misc/GithubApi.kt +++ b/src/main/kotlin/com/beust/kobalt/misc/GithubApi.kt @@ -108,6 +108,10 @@ public class GithubApi @Inject constructor(val executors: KobaltExecutors, fun getReleases(@Path("owner") owner: String, @Query("access_token") accessToken: String, @Path("repo") repo: String): List + + @GET("/repos/{owner}/{repo}/releases") + fun getReleasesNoAuth(@Path("owner") owner: String, + @Path("repo") repo: String): List } val latestKobaltVersion: Future @@ -115,10 +119,15 @@ public class GithubApi @Inject constructor(val executors: KobaltExecutors, val callable = Callable { var result = "0" - val username = localProperties.get(PROPERTY_USERNAME, DOC_URL) - val accessToken = localProperties.get(PROPERTY_ACCESS_TOKEN, DOC_URL) + val username = localProperties.getNoThrows(PROPERTY_USERNAME, DOC_URL) + val accessToken = localProperties.getNoThrows(PROPERTY_ACCESS_TOKEN, DOC_URL) try { - val releases = service.getReleases(username, accessToken, "kobalt") + val releases = + if (username != null && accessToken != null) { + service.getReleases(username, accessToken, "kobalt") + } else { + service.getReleasesNoAuth("cbeust", "kobalt") + } releases.firstOrNull()?.let { try { result = listOf(it.name, it.tagName).filterNotNull().first { !it.isBlank() } diff --git a/src/main/kotlin/com/beust/kobalt/misc/LocalProperties.kt b/src/main/kotlin/com/beust/kobalt/misc/LocalProperties.kt index 06dfbb90..4fe16a5c 100644 --- a/src/main/kotlin/com/beust/kobalt/misc/LocalProperties.kt +++ b/src/main/kotlin/com/beust/kobalt/misc/LocalProperties.kt @@ -12,7 +12,7 @@ class LocalProperties { val result = Properties() val filePath = Paths.get("local.properties") filePath.let { path -> - if (Files.exists(path)) { + if (path.toFile().exists()) { Files.newInputStream(path).use { result.load(it) } @@ -22,8 +22,10 @@ class LocalProperties { result } + fun getNoThrows(name: String, docUrl: String? = null) = localProperties.getProperty(name) + fun get(name: String, docUrl: String? = null) : String { - val result = localProperties.getProperty(name) + val result = getNoThrows(name, docUrl) ?: throw KobaltException("Couldn't find $name in local.properties", docUrl = docUrl) return result as String }