Skip to content

Commit

Permalink
Reduce code duplication (NexusClient and determineStagingProfileId) (#…
Browse files Browse the repository at this point in the history
…252)

* Deduplicate NexusClient creation

* Deduplicate determineStagingProfileId

* Remove unused import

* It's not a class, but if Spotless says so.
  • Loading branch information
TWiStErRob authored Jul 19, 2023
1 parent 8f9f098 commit 0c2f8df
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -52,25 +51,13 @@ 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))
logger.lifecycle("Staging repository for {} at {}, stagingProfileId '{}', descriptionRegex '{}' is '{}'", repository.name, serverUrl, stagingProfileId, descriptionRegex, descriptor.stagingRepositoryId)
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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0c2f8df

Please sign in to comment.