diff options
author | Li Haoyi <haoyi.sg@gmail.com> | 2019-05-19 15:15:24 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-19 15:15:24 +0800 |
commit | 68505aad4a22686594965bc910e60c8d2b6a95d4 (patch) | |
tree | 4c2099e21593a840c7119901becc0cbb8682956f /scalalib/src/publish | |
parent | 7d706641f6f9caa0509903f691d16d78402b4623 (diff) | |
download | mill-68505aad4a22686594965bc910e60c8d2b6a95d4.tar.gz mill-68505aad4a22686594965bc910e60c8d2b6a95d4.tar.bz2 mill-68505aad4a22686594965bc910e60c8d2b6a95d4.zip |
Bump ammonite to 1.6.7 (#610)
* bump ammonite to 1.6.7
* upgrade all the things
* add scalaj-http shims for bootstrapping
* wip
* tweak-error-message
* tweak coursier
* .
Diffstat (limited to 'scalalib/src/publish')
-rw-r--r-- | scalalib/src/publish/SonatypeHttpApi.scala | 116 | ||||
-rw-r--r-- | scalalib/src/publish/SonatypePublisher.scala | 5 | ||||
-rw-r--r-- | scalalib/src/publish/settings.scala | 6 |
3 files changed, 65 insertions, 62 deletions
diff --git a/scalalib/src/publish/SonatypeHttpApi.scala b/scalalib/src/publish/SonatypeHttpApi.scala index 12defa93..217d556e 100644 --- a/scalalib/src/publish/SonatypeHttpApi.scala +++ b/scalalib/src/publish/SonatypeHttpApi.scala @@ -5,18 +5,10 @@ import java.util.Base64 import scala.concurrent.duration._ -import scalaj.http.{BaseHttp, HttpOptions, HttpRequest, HttpResponse} - -object PatientHttp - extends BaseHttp( - options = Seq( - HttpOptions.connTimeout(5.seconds.toMillis.toInt), - HttpOptions.readTimeout(1.minute.toMillis.toInt), - HttpOptions.followRedirects(false) - ) - ) + class SonatypeHttpApi(uri: String, credentials: String) { + val http = requests.Session(connectTimeout = 5000, readTimeout = 1000, maxRedirects = 0) private val base64Creds = base64(credentials) @@ -29,12 +21,19 @@ class SonatypeHttpApi(uri: String, credentials: String) { // https://oss.sonatype.org/nexus-staging-plugin/default/docs/path__staging_profiles.html def getStagingProfileUri(groupId: String): String = { val response = withRetry( - PatientHttp(s"$uri/staging/profiles").headers(commonHeaders)) - .throwError + http.get( + s"$uri/staging/profiles", + headers = commonHeaders + ) + ) + + if (!response.is2xx) { + throw new Exception(s"$uri/staging/profiles returned ${response.statusCode}") + } val resourceUri = ujson - .read(response.body)("data") + .read(response.data.text)("data") .arr .find(profile => groupId.split('.').startsWith(profile("name").str.split('.'))) @@ -47,79 +46,84 @@ class SonatypeHttpApi(uri: String, credentials: String) { } def getStagingRepoState(stagingRepoId: String): String = { - val response = PatientHttp(s"${uri}/staging/repository/${stagingRepoId}") - .option(HttpOptions.readTimeout(60000)) - .headers(commonHeaders) - .asString - .throwError - - ujson.read(response.body)("type").str.toString + val response = http.get( + s"${uri}/staging/repository/${stagingRepoId}", + readTimeout = 60000, + headers = commonHeaders + ) + ujson.read(response.data.text)("type").str.toString } // https://oss.sonatype.org/nexus-staging-plugin/default/docs/path__staging_profiles_-profileIdKey-_start.html def createStagingRepo(profileUri: String, groupId: String): String = { - val response = withRetry(PatientHttp(s"${profileUri}/start") - .headers(commonHeaders) - .postData( - s"""{"data": {"description": "fresh staging profile for ${groupId}"}}""")) - .throwError + val response = http.post( + s"${profileUri}/start", + headers = commonHeaders, + data = s"""{"data": {"description": "fresh staging profile for ${groupId}"}}""" + ) - ujson.read(response.body)("data")("stagedRepositoryId").str.toString + if (!response.is2xx) { + throw new Exception(s"$uri/staging/profiles returned ${response.statusCode}") + } + + ujson.read(response.data.text)("data")("stagedRepositoryId").str.toString } // https://oss.sonatype.org/nexus-staging-plugin/default/docs/path__staging_profiles_-profileIdKey-_finish.html def closeStagingRepo(profileUri: String, repositoryId: String): Boolean = { val response = withRetry( - PatientHttp(s"${profileUri}/finish") - .headers(commonHeaders) - .postData( - s"""{"data": {"stagedRepositoryId": "${repositoryId}", "description": "closing staging repository"}}""" - )) + http.post( + s"${profileUri}/finish", + headers = commonHeaders, + data = s"""{"data": {"stagedRepositoryId": "${repositoryId}", "description": "closing staging repository"}}""" + ) + ) - response.code == 201 + response.statusCode == 201 } // https://oss.sonatype.org/nexus-staging-plugin/default/docs/path__staging_profiles_-profileIdKey-_promote.html def promoteStagingRepo(profileUri: String, repositoryId: String): Boolean = { val response = withRetry( - PatientHttp(s"${profileUri}/promote") - .headers(commonHeaders) - .postData( - s"""{"data": {"stagedRepositoryId": "${repositoryId}", "description": "promote staging repository"}}""" - )) + http.post( + s"${profileUri}/promote", + headers = commonHeaders, + data = s"""{"data": {"stagedRepositoryId": "${repositoryId}", "description": "promote staging repository"}}""" + ) + ) - response.code == 201 + response.statusCode == 201 } // https://oss.sonatype.org/nexus-staging-plugin/default/docs/path__staging_profiles_-profileIdKey-_drop.html def dropStagingRepo(profileUri: String, repositoryId: String): Boolean = { val response = withRetry( - PatientHttp(s"${profileUri}/drop") - .headers(commonHeaders) - .postData( - s"""{"data": {"stagedRepositoryId": "${repositoryId}", "description": "drop staging repository"}}""" - )) - - response.code == 201 + http.post( + s"${profileUri}/drop", + headers = commonHeaders, + data = s"""{"data": {"stagedRepositoryId": "${repositoryId}", "description": "drop staging repository"}}""" + ) + ) + response.statusCode == 201 } private val uploadTimeout = 5.minutes.toMillis.toInt - def upload(uri: String, data: Array[Byte]): HttpResponse[String] = { - PatientHttp(uri) - .option(HttpOptions.readTimeout(uploadTimeout)) - .method("PUT") - .headers( + def upload(uri: String, data: Array[Byte]): requests.Response = { + http.put( + uri, + readTimeout = uploadTimeout, + headers = Seq( "Content-Type" -> "application/binary", "Authorization" -> s"Basic ${base64Creds}" - ) - .put(data) - .asString + ), + data = data + ) } - private def withRetry(request: HttpRequest, - retries: Int = 10): HttpResponse[String] = { - val resp = request.asString + private def withRetry(request: => requests.Response, + retries: Int = 10): requests.Response = { + val resp = request if (resp.is5xx && retries > 0) { Thread.sleep(500) withRetry(request, retries - 1) diff --git a/scalalib/src/publish/SonatypePublisher.scala b/scalalib/src/publish/SonatypePublisher.scala index 6dcadd5b..5ca8f5c1 100644 --- a/scalalib/src/publish/SonatypePublisher.scala +++ b/scalalib/src/publish/SonatypePublisher.scala @@ -5,7 +5,6 @@ import java.security.MessageDigest import mill.api.Logger import os.Shellable -import scalaj.http.HttpResponse class SonatypePublisher(uri: String, snapshotUri: String, @@ -105,13 +104,13 @@ class SonatypePublisher(uri: String, } } - private def reportPublishResults(publishResults: Seq[HttpResponse[String]], + private def reportPublishResults(publishResults: Seq[requests.Response], artifacts: Seq[Artifact]) = { if (publishResults.forall(_.is2xx)) { log.info(s"Published ${artifacts.map(_.id).mkString(", ")} to Sonatype") } else { val errors = publishResults.filterNot(_.is2xx).map { response => - s"Code: ${response.code}, message: ${response.body}" + s"Code: ${response.statusCode}, message: ${response.data.text}" } throw new RuntimeException( s"Failed to publish ${artifacts.map(_.id).mkString(", ")} to Sonatype. Errors: \n${errors.mkString("\n")}" diff --git a/scalalib/src/publish/settings.scala b/scalalib/src/publish/settings.scala index bca81cf0..d2801752 100644 --- a/scalalib/src/publish/settings.scala +++ b/scalalib/src/publish/settings.scala @@ -23,13 +23,13 @@ object Artifact { ) Dependency( Artifact( - dep.dep.module.organization, + dep.dep.module.organization.value, name, dep.dep.version ), Scope.Compile, - if (dep.dep.configuration == "") None else Some(dep.dep.configuration), - dep.dep.exclusions.toList + if (dep.dep.configuration == "") None else Some(dep.dep.configuration.value), + dep.dep.exclusions.toList.map{case (a, b) => (a.value, b.value)} ) } } |