From 40ba983937286d17190edf756c6befd2f8884c6b Mon Sep 17 00:00:00 2001 From: Adrien Ferrand Date: Sun, 22 Nov 2020 15:04:02 +0100 Subject: [PATCH 1/2] Fix Harbor chart POST requests --- .../publishing/dsl/HarborHelmPublishingRepository.kt | 12 ++++++++++++ .../publishers/AbstractHttpHelmChartPublisher.kt | 10 ++++------ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/main/kotlin/org/unbrokendome/gradle/plugins/helm/publishing/dsl/HarborHelmPublishingRepository.kt b/src/main/kotlin/org/unbrokendome/gradle/plugins/helm/publishing/dsl/HarborHelmPublishingRepository.kt index f6de14d7..81ba5576 100644 --- a/src/main/kotlin/org/unbrokendome/gradle/plugins/helm/publishing/dsl/HarborHelmPublishingRepository.kt +++ b/src/main/kotlin/org/unbrokendome/gradle/plugins/helm/publishing/dsl/HarborHelmPublishingRepository.kt @@ -1,5 +1,9 @@ package org.unbrokendome.gradle.plugins.helm.publishing.dsl +import okhttp3.MediaType.Companion.toMediaType +import okhttp3.MultipartBody +import okhttp3.RequestBody +import okhttp3.RequestBody.Companion.asRequestBody import org.gradle.api.model.ObjectFactory import org.gradle.api.provider.Property import org.unbrokendome.gradle.plugins.helm.dsl.credentials.SerializableCredentials @@ -8,6 +12,7 @@ import org.unbrokendome.gradle.plugins.helm.publishing.publishers.AbstractHttpHe import org.unbrokendome.gradle.plugins.helm.publishing.publishers.HelmChartPublisher import org.unbrokendome.gradle.plugins.helm.publishing.publishers.PublisherParams import org.unbrokendome.gradle.plugins.helm.util.property +import java.io.File import java.net.URI import javax.inject.Inject @@ -63,6 +68,13 @@ private open class DefaultHarborHelmPublishingRepository override fun uploadPath(chartName: String, chartVersion: String): String = "/api/chartrepo/$projectName/charts" + + override fun requestBody(chartFile: File): RequestBody = + MultipartBody.Builder().run { + setType(MultipartBody.FORM) + addFormDataPart("chart", chartFile.name, chartFile.asRequestBody("application/x-gzip".toMediaType())) + build() + } } } diff --git a/src/main/kotlin/org/unbrokendome/gradle/plugins/helm/publishing/publishers/AbstractHttpHelmChartPublisher.kt b/src/main/kotlin/org/unbrokendome/gradle/plugins/helm/publishing/publishers/AbstractHttpHelmChartPublisher.kt index 38dfd6fe..5488a211 100644 --- a/src/main/kotlin/org/unbrokendome/gradle/plugins/helm/publishing/publishers/AbstractHttpHelmChartPublisher.kt +++ b/src/main/kotlin/org/unbrokendome/gradle/plugins/helm/publishing/publishers/AbstractHttpHelmChartPublisher.kt @@ -1,12 +1,8 @@ package org.unbrokendome.gradle.plugins.helm.publishing.publishers -import okhttp3.Credentials +import okhttp3.* import okhttp3.HttpUrl.Companion.toHttpUrl -import okhttp3.Interceptor -import okhttp3.MediaType import okhttp3.MediaType.Companion.toMediaType -import okhttp3.OkHttpClient -import okhttp3.Request import okhttp3.RequestBody.Companion.asRequestBody import okhttp3.tls.HandshakeCertificates import okhttp3.tls.HeldCertificate @@ -42,6 +38,8 @@ internal abstract class AbstractHttpHelmChartPublisher( ): Map = emptyMap() + protected open fun requestBody(chartFile: File): RequestBody = chartFile.asRequestBody(MEDIA_TYPE_GZIP) + private companion object { val MEDIA_TYPE_GZIP: MediaType = "application/x-gzip".toMediaType() @@ -73,7 +71,7 @@ internal abstract class AbstractHttpHelmChartPublisher( val request = Request.Builder().run { url(uploadUrl.toHttpUrl()) - method(uploadMethod, chartFile.asRequestBody(MEDIA_TYPE_GZIP)) + method(uploadMethod, requestBody(chartFile)) build() } From 681ad3d20417922b281af22402a69527a4398fff Mon Sep 17 00:00:00 2001 From: Adrien Ferrand Date: Sun, 22 Nov 2020 15:12:56 +0100 Subject: [PATCH 2/2] Reuse companion object --- .../helm/publishing/dsl/HarborHelmPublishingRepository.kt | 3 +-- .../publishing/publishers/AbstractHttpHelmChartPublisher.kt | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/org/unbrokendome/gradle/plugins/helm/publishing/dsl/HarborHelmPublishingRepository.kt b/src/main/kotlin/org/unbrokendome/gradle/plugins/helm/publishing/dsl/HarborHelmPublishingRepository.kt index 81ba5576..18f0455d 100644 --- a/src/main/kotlin/org/unbrokendome/gradle/plugins/helm/publishing/dsl/HarborHelmPublishingRepository.kt +++ b/src/main/kotlin/org/unbrokendome/gradle/plugins/helm/publishing/dsl/HarborHelmPublishingRepository.kt @@ -1,6 +1,5 @@ package org.unbrokendome.gradle.plugins.helm.publishing.dsl -import okhttp3.MediaType.Companion.toMediaType import okhttp3.MultipartBody import okhttp3.RequestBody import okhttp3.RequestBody.Companion.asRequestBody @@ -72,7 +71,7 @@ private open class DefaultHarborHelmPublishingRepository override fun requestBody(chartFile: File): RequestBody = MultipartBody.Builder().run { setType(MultipartBody.FORM) - addFormDataPart("chart", chartFile.name, chartFile.asRequestBody("application/x-gzip".toMediaType())) + addFormDataPart("chart", chartFile.name, chartFile.asRequestBody(MEDIA_TYPE_GZIP)) build() } } diff --git a/src/main/kotlin/org/unbrokendome/gradle/plugins/helm/publishing/publishers/AbstractHttpHelmChartPublisher.kt b/src/main/kotlin/org/unbrokendome/gradle/plugins/helm/publishing/publishers/AbstractHttpHelmChartPublisher.kt index 5488a211..7ba1a92e 100644 --- a/src/main/kotlin/org/unbrokendome/gradle/plugins/helm/publishing/publishers/AbstractHttpHelmChartPublisher.kt +++ b/src/main/kotlin/org/unbrokendome/gradle/plugins/helm/publishing/publishers/AbstractHttpHelmChartPublisher.kt @@ -41,7 +41,7 @@ internal abstract class AbstractHttpHelmChartPublisher( protected open fun requestBody(chartFile: File): RequestBody = chartFile.asRequestBody(MEDIA_TYPE_GZIP) - private companion object { + protected companion object { val MEDIA_TYPE_GZIP: MediaType = "application/x-gzip".toMediaType() }