11package nmcp.internal.task
22
33import gratatouille.tasks.GInputFile
4- import gratatouille.tasks.GInputFiles
54import gratatouille.tasks.GLogger
65import gratatouille.tasks.GTask
76import java.net.SocketTimeoutException
8- import java.util.zip.ZipEntry
9- import java.util.zip.ZipOutputStream
107import kotlin.time.Duration
118import kotlin.time.Duration.Companion.minutes
129import kotlin.time.Duration.Companion.seconds
@@ -15,15 +12,12 @@ import kotlinx.serialization.json.Json
1512import kotlinx.serialization.json.JsonObject
1613import kotlinx.serialization.json.JsonPrimitive
1714import nmcp.transport.nmcpClient
18- import okhttp3.MediaType
1915import okhttp3.MediaType.Companion.toMediaType
2016import okhttp3.MultipartBody
2117import okhttp3.Request
22- import okhttp3.RequestBody
2318import okhttp3.RequestBody.Companion.asRequestBody
2419import okhttp3.RequestBody.Companion.toRequestBody
2520import okio.Buffer
26- import okio.BufferedSink
2721import okio.ByteString
2822import okio.use
2923
@@ -58,8 +52,10 @@ internal fun nmcpPublishWithPublisherApi(
5852 )
5953 .build()
6054
55+ @Suppress(" NAME_SHADOWING" )
6156 val publishingType = publishingType ? : " AUTOMATIC"
6257
58+ @Suppress(" NAME_SHADOWING" )
6359 val baseUrl = baseUrl ? : " https://central.sonatype.com/"
6460 val url = baseUrl + " api/v1/publisher/upload?publishingType=$publishingType "
6561
@@ -73,10 +69,10 @@ internal fun nmcpPublishWithPublisherApi(
7369 nmcpClient.newCall(it).execute()
7470 }.use {
7571 if (! it.isSuccessful) {
76- error(" Cannot deploy to maven central (status='${it.code} '): ${it.body? .string()} " )
72+ error(" Cannot deploy to maven central (status='${it.code} '): ${it.body.string()} " )
7773 }
7874
79- it.body!! .string()
75+ it.body.string()
8076 }
8177
8278 logger.lifecycle(" Nmcp: deployment bundle '$deploymentId ' uploaded." )
@@ -180,10 +176,10 @@ private fun verifyStatus(
180176 }
181177 }.use {
182178 if (! it.isSuccessful) {
183- error(" Cannot verify deployment $deploymentId status (HTTP status='${it.code} '): ${it.body? .string()} " )
179+ error(" Cannot verify deployment $deploymentId status (HTTP status='${it.code} '): ${it.body.string()} " )
184180 }
185181
186- val str = it.body!! .string()
182+ val str = it.body.string()
187183 val element = Json .parseToJsonElement(str)
188184 check(element is JsonObject ) {
189185 " Nmcp: unexpected status response for deployment $deploymentId : $str "
@@ -207,32 +203,3 @@ private fun verifyStatus(
207203 }
208204 }
209205}
210-
211- internal class ZipBody (val files : GInputFiles , private val logger : GLogger ) : RequestBody() {
212- override fun contentType (): MediaType {
213- return " application/octet-stream" .toMediaType()
214- }
215-
216- override fun writeTo (sink : BufferedSink ) {
217- val stream = ZipOutputStream (sink.outputStream())
218- files.forEach {
219- if (it.file.isDirectory) {
220- return @forEach
221- }
222- // Exclude maven-metadata files or the bundle is not recognized
223- // See https://slack-chats.kotlinlang.org/t/16407246/anyone-tried-the-https-central-sonatype-org-publish-publish-#c8738fe5-8051-4f64-809f-ca67a645216e
224- if (it.file.name.startsWith(" maven-metadata" )) {
225- return @forEach
226- }
227- logger.info(" Nmcp: zip ${it.normalizedPath} " )
228- stream.putNextEntry(ZipEntry (it.normalizedPath))
229- it.file.inputStream().use {
230- it.copyTo(stream)
231- }
232- stream.closeEntry()
233- }
234- stream.finish()
235- stream.flush()
236- sink.flush()
237- }
238- }
0 commit comments