From 621112fbe667fe06924c9fdde8a9f7e1d616115c Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Wed, 17 Feb 2016 21:19:04 +0100 Subject: [PATCH] Use configured baseUrl in versioneye.properties header #8 Print the configured URI in the progress log messages. Add documentation for the plugin usage. Improve error message handling. --- README.md | 10 +++++- .../scala/com/versioneye/PropertiesUtil.scala | 5 ++- .../com/versioneye/VersionEyePlugin.scala | 31 ++++++++++++------- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index bf22730..091b628 100644 --- a/README.md +++ b/README.md @@ -16,10 +16,18 @@ Compile it: sbt compile ``` -Enable it within your `build.sbt` +Enable it within your `build.sbt` and optionally set properties: ``` enablePlugins(VersionEyePlugin) + +// VersionEyePlugin.projectSettings +propertiesPath in versioneye := "/Users/mark/playground/sbt-test/src/qa/resources/versioneye.properties" +apiKey in versioneye := "myApiKey" +baseUrl in versioneye := "https://www.versioneye.com" +apiPath in versioneye := "/api/v2" +publishCrossVersion in versioneye := true + ``` ## Create a VersionEye project diff --git a/src/main/scala/com/versioneye/PropertiesUtil.scala b/src/main/scala/com/versioneye/PropertiesUtil.scala index d8042c9..37d3b04 100644 --- a/src/main/scala/com/versioneye/PropertiesUtil.scala +++ b/src/main/scala/com/versioneye/PropertiesUtil.scala @@ -12,7 +12,7 @@ object PropertiesUtil { protected val propertiesFile: String = "versioneye.properties" - def writeProperties(response: ProjectJsonResponse, propertiesFile: File): Unit = { + def writeProperties(response: ProjectJsonResponse, propertiesFile: File, baseUrl: String): Unit = { var properties: Properties = null if (!propertiesFile.exists()) { @@ -23,13 +23,12 @@ object PropertiesUtil { properties = loadProperties(propertiesFile) } - if (response.getId != null) { properties.setProperty("project_id", response.getId) } val fos = new FileOutputStream(propertiesFile) - properties.store(fos, " Properties for https://www.VersionEye.com") + properties.store(fos, s" Properties for $baseUrl") fos.close() } diff --git a/src/main/scala/com/versioneye/VersionEyePlugin.scala b/src/main/scala/com/versioneye/VersionEyePlugin.scala index 0385d22..d13955a 100644 --- a/src/main/scala/com/versioneye/VersionEyePlugin.scala +++ b/src/main/scala/com/versioneye/VersionEyePlugin.scala @@ -150,7 +150,7 @@ object VersionEyePlugin extends sbt.AutoPlugin { val log = streams.value.log log.info(".") - log.info("Starting to upload dependencies. This can take a couple seconds ... ") + log.info("Starting to upload dependencies to " + baseUrl.value + ". This can take a couple seconds ... ") log.info(".") val scopes: List[String] = getScopes(skipScopes.value) @@ -181,7 +181,7 @@ object VersionEyePlugin extends sbt.AutoPlugin { } if (updatePropertiesAfterCreate.value) { - writeProperties(projectResponse, getPropertiesFile(propertiesPath.value, baseDirectory.value, false)) + writeProperties(projectResponse, getPropertiesFile(propertiesPath.value, baseDirectory.value, false), baseUrl.value) } prettyPrint(log, baseUrl.value, projectResponse) } @@ -194,7 +194,7 @@ object VersionEyePlugin extends sbt.AutoPlugin { val log = streams.value.log log.info(".") - log.info("Starting to upload dependencies. This can take a couple seconds ... ") + log.info("Starting to upload dependencies to " + baseUrl.value + ". This can take a couple seconds ... ") log.info(".") val scopes: List[String] = getScopes(skipScopes.value) @@ -231,7 +231,7 @@ object VersionEyePlugin extends sbt.AutoPlugin { val log = streams.value.log log.info(".") - log.info("Starting to upload dependencies for license check. This can take a couple seconds ... ") + log.info("Starting to upload dependencies to " + baseUrl.value + " for license check. This can take a couple seconds ... ") log.info(".") val scopes: List[String] = getScopes(skipScopes.value) @@ -244,7 +244,6 @@ object VersionEyePlugin extends sbt.AutoPlugin { streams.value.log.info("There are no dependencies in this project !" + organization.value + " / " + name.value) } - val apiKeyValue = getApiKey(apiKey.value, propertiesPath.value, baseDirectory.value) val projectIdValue = getVersionEyeProjectId(existingProjectId.value, propertiesPath.value, baseDirectory.value) val url = getUrl(baseUrl.value, apiPath.value, "/projects/", projectIdValue, "?api_key=" + apiKeyValue) @@ -260,11 +259,13 @@ object VersionEyePlugin extends sbt.AutoPlugin { val projectResponse = getResponse(response) if (projectResponse.getLicenses_red > 0) { - throw new IllegalStateException("Some components violate the license whitelist! " + "More details here: " + baseUrl.value + "/user/projects/" + projectResponse.getId) + throw new IllegalStateException("Some components violate the license whitelist! " + + "More details here: " + baseUrl.value + "/user/projects/" + projectResponse.getId) } if (projectResponse.getLicenses_unknown > 0 && licenseCheckBreakByUnknown.value) { - throw new IllegalStateException("Some components are without any license! " + "More details here: " + baseUrl.value + "/user/projects/" + projectResponse.getId) + throw new IllegalStateException("Some components are without any license! " + + "More details here: " + baseUrl.value + "/user/projects/" + projectResponse.getId) } prettyPrint(log, baseUrl.value, projectResponse) @@ -288,7 +289,7 @@ object VersionEyePlugin extends sbt.AutoPlugin { def handleResponseErrorIfAny(response: HttpResponse[String]): Unit = { if (!response.is2xx) { val err = getErrorMessage(response); - val errMsg: String = "Status Code: " + response.statusLine + " -> " + err + val errMsg = err.map( msg => "Status Code: " + response.statusLine + " -> " + msg).getOrElse("Status Code: " + response.statusLine); throw new scala.RuntimeException(errMsg) } } @@ -316,7 +317,6 @@ object VersionEyePlugin extends sbt.AutoPlugin { def getResponse(response: HttpResponse[String]): ProjectJsonResponse = { val body = response.body - val mapper = new ObjectMapper() with ScalaObjectMapper mapper.registerModule(DefaultScalaModule) return mapper.readValue[ProjectJsonResponse](body) @@ -326,17 +326,24 @@ object VersionEyePlugin extends sbt.AutoPlugin { * Get error message from a HTTP Response (JSON or raw body) * @param response */ - def getErrorMessage(response: HttpResponse[String]): String = { + def getErrorMessage(response: HttpResponse[String]): Option[String] = { val body = response.body + // application/json + if (response.contentType.isEmpty + || !"application/json".equals(response.contentType.get) + || response.body.isEmpty) { + return None; + } + val mapper = new ObjectMapper() with ScalaObjectMapper mapper.registerModule(DefaultScalaModule) val responseMap = mapper.readValue[Map[String, String]](body) if (responseMap.contains("error")) { - return responseMap.get("error").get + return responseMap.get("error") } - return body + return None } def getUrl(values: String*): String = {