From 0c2f8df8b40acf2251023ab164c6fa8469fdc59d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B3bert=20Papp?= Date: Wed, 19 Jul 2023 21:13:35 +0100 Subject: [PATCH] Reduce code duplication (NexusClient and determineStagingProfileId) (#252) * Deduplicate NexusClient creation * Deduplicate determineStagingProfileId * Remove unused import * It's not a class, but if Spotless says so. --- .../AbstractNexusStagingRepositoryTask.kt | 12 +++++++ ...actTransitionNexusStagingRepositoryTask.kt | 10 +----- .../publishplugin/FindStagingRepository.kt | 19 ++-------- .../InitializeNexusStagingRepository.kt | 18 ++-------- .../publishplugin/RetrieveStagingProfile.kt | 12 +------ .../internal/DetermineStagingProfileId.kt | 36 +++++++++++++++++++ .../publishplugin/internal/NexusClient.kt | 1 - 7 files changed, 56 insertions(+), 52 deletions(-) create mode 100644 src/main/kotlin/io/github/gradlenexus/publishplugin/internal/DetermineStagingProfileId.kt diff --git a/src/main/kotlin/io/github/gradlenexus/publishplugin/AbstractNexusStagingRepositoryTask.kt b/src/main/kotlin/io/github/gradlenexus/publishplugin/AbstractNexusStagingRepositoryTask.kt index 769fdac8..3bb9caeb 100644 --- a/src/main/kotlin/io/github/gradlenexus/publishplugin/AbstractNexusStagingRepositoryTask.kt +++ b/src/main/kotlin/io/github/gradlenexus/publishplugin/AbstractNexusStagingRepositoryTask.kt @@ -16,6 +16,7 @@ package io.github.gradlenexus.publishplugin +import io.github.gradlenexus.publishplugin.internal.NexusClient import org.gradle.api.DefaultTask import org.gradle.api.provider.Property import org.gradle.api.tasks.Input @@ -44,4 +45,15 @@ abstract class AbstractNexusStagingRepositoryTask : DefaultTask() { init { this.onlyIf { useStaging.getOrElse(false) } } + + protected fun createNexusClient(): NexusClient { + val repository = repository.get() + return NexusClient( + repository.nexusUrl.get(), + repository.username.orNull, + repository.password.orNull, + clientTimeout.orNull, + connectTimeout.orNull + ) + } } diff --git a/src/main/kotlin/io/github/gradlenexus/publishplugin/AbstractTransitionNexusStagingRepositoryTask.kt b/src/main/kotlin/io/github/gradlenexus/publishplugin/AbstractTransitionNexusStagingRepositoryTask.kt index 9813aba4..73b40252 100644 --- a/src/main/kotlin/io/github/gradlenexus/publishplugin/AbstractTransitionNexusStagingRepositoryTask.kt +++ b/src/main/kotlin/io/github/gradlenexus/publishplugin/AbstractTransitionNexusStagingRepositoryTask.kt @@ -17,7 +17,6 @@ package io.github.gradlenexus.publishplugin import io.github.gradlenexus.publishplugin.internal.BasicActionRetrier -import io.github.gradlenexus.publishplugin.internal.NexusClient import io.github.gradlenexus.publishplugin.internal.StagingRepository import io.github.gradlenexus.publishplugin.internal.StagingRepositoryTransitioner import org.gradle.api.Action @@ -40,17 +39,10 @@ abstract class AbstractTransitionNexusStagingRepositoryTask : AbstractNexusStagi @TaskAction fun transitionStagingRepo() { - val client = NexusClient( - repository.get().nexusUrl.get(), - repository.get().username.orNull, - repository.get().password.orNull, - clientTimeout.orNull, - connectTimeout.orNull - ) val retrier = transitionCheckOptions.get().run { BasicActionRetrier(maxRetries.get(), delayBetween.get(), StagingRepository::transitioning) } - transitionStagingRepo(StagingRepositoryTransitioner(client, retrier)) + transitionStagingRepo(StagingRepositoryTransitioner(createNexusClient(), retrier)) } protected abstract fun transitionStagingRepo(repositoryTransitioner: StagingRepositoryTransitioner) diff --git a/src/main/kotlin/io/github/gradlenexus/publishplugin/FindStagingRepository.kt b/src/main/kotlin/io/github/gradlenexus/publishplugin/FindStagingRepository.kt index 2c6a6bbf..a9a1fdc1 100644 --- a/src/main/kotlin/io/github/gradlenexus/publishplugin/FindStagingRepository.kt +++ b/src/main/kotlin/io/github/gradlenexus/publishplugin/FindStagingRepository.kt @@ -17,8 +17,7 @@ package io.github.gradlenexus.publishplugin import io.github.gradlenexus.publishplugin.internal.InvalidatingStagingRepositoryDescriptorRegistry -import io.github.gradlenexus.publishplugin.internal.NexusClient -import org.gradle.api.GradleException +import io.github.gradlenexus.publishplugin.internal.determineStagingProfileId import org.gradle.api.Incubating import org.gradle.api.provider.Property import org.gradle.api.tasks.Input @@ -52,8 +51,8 @@ abstract class FindStagingRepository : AbstractNexusStagingRepositoryTask() { fun findStagingRepository() { val repository = repository.get() val serverUrl = repository.nexusUrl.get() - val client = NexusClient(serverUrl, repository.username.orNull, repository.password.orNull, clientTimeout.orNull, connectTimeout.orNull) - val stagingProfileId = determineStagingProfileId(repository, client) + val client = createNexusClient() + val stagingProfileId = determineStagingProfileId(client, logger, repository, packageGroup.get()) logger.info("Fetching staging repositories for {} at {}, stagingProfileId '{}'", repository.name, serverUrl, stagingProfileId) val descriptionRegex = descriptionRegex.get() val descriptor = client.findStagingRepository(stagingProfileId, Regex(descriptionRegex)) @@ -61,16 +60,4 @@ abstract class FindStagingRepository : AbstractNexusStagingRepositoryTask() { stagingRepositoryId.set(descriptor.stagingRepositoryId) registry.get()[repository.name] = descriptor } - - // TODO: Duplication with InitializeNexusStagingRepository - private fun determineStagingProfileId(repository: NexusRepository, client: NexusClient): String { - var stagingProfileId = repository.stagingProfileId.orNull - if (stagingProfileId == null) { - val packageGroup = packageGroup.get() - logger.info("No stagingProfileId set, querying for packageGroup '{}'", packageGroup) - stagingProfileId = client.findStagingProfileId(packageGroup) - ?: throw GradleException("Failed to find staging profile for package group: $packageGroup") - } - return stagingProfileId - } } diff --git a/src/main/kotlin/io/github/gradlenexus/publishplugin/InitializeNexusStagingRepository.kt b/src/main/kotlin/io/github/gradlenexus/publishplugin/InitializeNexusStagingRepository.kt index 0357e79c..e7288281 100644 --- a/src/main/kotlin/io/github/gradlenexus/publishplugin/InitializeNexusStagingRepository.kt +++ b/src/main/kotlin/io/github/gradlenexus/publishplugin/InitializeNexusStagingRepository.kt @@ -17,9 +17,8 @@ package io.github.gradlenexus.publishplugin import io.github.gradlenexus.publishplugin.internal.InvalidatingStagingRepositoryDescriptorRegistry -import io.github.gradlenexus.publishplugin.internal.NexusClient +import io.github.gradlenexus.publishplugin.internal.determineStagingProfileId import okhttp3.HttpUrl -import org.gradle.api.GradleException import org.gradle.api.provider.Property import org.gradle.api.tasks.Input import org.gradle.api.tasks.Internal @@ -39,23 +38,12 @@ abstract class InitializeNexusStagingRepository : AbstractNexusStagingRepository fun createStagingRepo() { val repository = repository.get() val serverUrl = repository.nexusUrl.get() - val client = NexusClient(serverUrl, repository.username.orNull, repository.password.orNull, clientTimeout.orNull, connectTimeout.orNull) - val stagingProfileId = determineStagingProfileId(repository, client) + val client = createNexusClient() + val stagingProfileId = determineStagingProfileId(client, logger, repository, packageGroup.get()) logger.info("Creating staging repository for {} at {}, stagingProfileId '{}'", repository.name, serverUrl, stagingProfileId) val descriptor = client.createStagingRepository(stagingProfileId, repositoryDescription.get()) val consumerUrl = HttpUrl.get(serverUrl)!!.newBuilder().addEncodedPathSegments("repositories/${descriptor.stagingRepositoryId}/content/").build() logger.lifecycle("Created staging repository '{}' at {}", descriptor.stagingRepositoryId, consumerUrl) registry.get()[repository.name] = descriptor } - - private fun determineStagingProfileId(repository: NexusRepository, client: NexusClient): String { - var stagingProfileId = repository.stagingProfileId.orNull - if (stagingProfileId == null) { - val packageGroup = packageGroup.get() - logger.info("No stagingProfileId set, querying for packageGroup '{}'", packageGroup) - stagingProfileId = client.findStagingProfileId(packageGroup) - ?: throw GradleException("Failed to find staging profile for package group: $packageGroup") - } - return stagingProfileId - } } diff --git a/src/main/kotlin/io/github/gradlenexus/publishplugin/RetrieveStagingProfile.kt b/src/main/kotlin/io/github/gradlenexus/publishplugin/RetrieveStagingProfile.kt index d44bc8a0..c5bf85ec 100644 --- a/src/main/kotlin/io/github/gradlenexus/publishplugin/RetrieveStagingProfile.kt +++ b/src/main/kotlin/io/github/gradlenexus/publishplugin/RetrieveStagingProfile.kt @@ -16,7 +16,6 @@ package io.github.gradlenexus.publishplugin -import io.github.gradlenexus.publishplugin.internal.NexusClient import org.gradle.api.GradleException import org.gradle.api.Incubating import org.gradle.api.provider.Property @@ -34,16 +33,7 @@ abstract class RetrieveStagingProfile : AbstractNexusStagingRepositoryTask() { @TaskAction fun retrieveStagingProfile() { - val repository = repository.get() - - val client = NexusClient( - repository.nexusUrl.get(), - repository.username.orNull, - repository.password.orNull, - clientTimeout.orNull, - connectTimeout.orNull - ) - + val client = createNexusClient() val packageGroup = packageGroup.get() val stagingProfileId = client.findStagingProfileId(packageGroup) ?: throw GradleException("Failed to find staging profile for package group: $packageGroup") diff --git a/src/main/kotlin/io/github/gradlenexus/publishplugin/internal/DetermineStagingProfileId.kt b/src/main/kotlin/io/github/gradlenexus/publishplugin/internal/DetermineStagingProfileId.kt new file mode 100644 index 00000000..20c48395 --- /dev/null +++ b/src/main/kotlin/io/github/gradlenexus/publishplugin/internal/DetermineStagingProfileId.kt @@ -0,0 +1,36 @@ +/* + * Copyright 2021 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.github.gradlenexus.publishplugin.internal + +import io.github.gradlenexus.publishplugin.NexusRepository +import org.gradle.api.GradleException +import org.gradle.api.logging.Logger + +internal fun determineStagingProfileId( + client: NexusClient, + logger: Logger, + repository: NexusRepository, + packageGroup: String +): String { + var stagingProfileId = repository.stagingProfileId.orNull + if (stagingProfileId == null) { + logger.info("No stagingProfileId set, querying for packageGroup '{}'", packageGroup) + stagingProfileId = client.findStagingProfileId(packageGroup) + ?: throw GradleException("Failed to find staging profile for package group: $packageGroup") + } + return stagingProfileId +} diff --git a/src/main/kotlin/io/github/gradlenexus/publishplugin/internal/NexusClient.kt b/src/main/kotlin/io/github/gradlenexus/publishplugin/internal/NexusClient.kt index 817462ac..72112f53 100644 --- a/src/main/kotlin/io/github/gradlenexus/publishplugin/internal/NexusClient.kt +++ b/src/main/kotlin/io/github/gradlenexus/publishplugin/internal/NexusClient.kt @@ -30,7 +30,6 @@ import retrofit2.http.Path import java.io.IOException import java.net.URI import java.time.Duration -import java.util.NoSuchElementException open class NexusClient(private val baseUrl: URI, username: String?, password: String?, timeout: Duration?, connectTimeout: Duration?) { private val api: NexusApi