Skip to content

Commit

Permalink
Make staging repo description configurable
Browse files Browse the repository at this point in the history
The description can now be customized on the extension or individual
tasks and now defaults to the GAV of the root project.

Resolves #31.
  • Loading branch information
marcphilipp committed May 31, 2020
1 parent b7cd0de commit abc2fd3
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ class NexusPublishPluginTests {
assertSuccess(result, ":initializeMyNexusStagingRepository")
assertNotConsidered(result, ":initializeSomeOtherNexusStagingRepository")
server.verify(postRequestedFor(urlEqualTo("/staging/profiles/$STAGING_PROFILE_ID/start"))
.withRequestBody(matchingJsonPath("\$.data[?(@.description == 'Created by io.github.gradle-nexus.publish-plugin Gradle plugin')]")))
.withRequestBody(matchingJsonPath("\$.data[?(@.description == 'org.example:sample:0.0.1')]")))
assertUploadedToStagingRepo("/org/example/sample/0.0.1/sample-0.0.1.pom")
assertUploadedToStagingRepo("/org/example/sample/0.0.1/sample-0.0.1.jar")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package io.github.gradlenexus.publishplugin

import org.gradle.api.DefaultTask
import org.gradle.api.model.ObjectFactory
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.Nested
import org.gradle.kotlin.dsl.property
Expand All @@ -44,6 +45,11 @@ constructor(objects: ObjectFactory, extension: NexusPublishExtension, repository
set(repository)
}

@Input
val description = objects.property<String>().apply {
set(extension.description)
}

init {
this.onlyIf { extension.useStaging.getOrElse(false) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ constructor(objects: ObjectFactory, extension: NexusPublishExtension, repository
val client = NexusClient(repository.get().nexusUrl.get(), repository.get().username.orNull, repository.get().password.orNull, clientTimeout.orNull, connectTimeout.orNull)
val repositoryTransitioner = StagingRepositoryTransitioner(client, BasicActionRetrier.retryUntilRepoTransitionIsCompletedRetrier(repository.get().retrying.get()))
logger.info("Closing staging repository with id '{}'", stagingRepositoryId.get())
repositoryTransitioner.effectivelyClose(stagingRepositoryId.get())
repositoryTransitioner.effectivelyClose(stagingRepositoryId.get(), description.get())
logger.info("Repository with id '{}' effectively closed", stagingRepositoryId.get())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ open class InitializeNexusStagingRepository @Inject constructor(
val client = NexusClient(serverUrl, repository.username.orNull, repository.password.orNull, clientTimeout.orNull, connectTimeout.orNull)
val stagingProfileId = determineStagingProfileId(repository, client)
logger.info("Creating staging repository for {} at {}, stagingProfileId '{}'", repository.name, serverUrl, stagingProfileId)
registry.get()[repository.name] = client.createStagingRepository(stagingProfileId)
registry.get()[repository.name] = client.createStagingRepository(stagingProfileId, description.get())
}

private fun determineStagingProfileId(repository: NexusRepository, client: NexusClient): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ open class NexusPublishExtension(project: Project) {
set(project.provider { project.group.toString() })
}

val description = project.objects.property<String>().apply {
set(project.provider { project.run { "$group:$name:$version" } })
}

val clientTimeout = project.objects.property<Duration>().apply {
set(Duration.ofMinutes(1))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ constructor(objects: ObjectFactory, extension: NexusPublishExtension, repository
val client = NexusClient(repository.get().nexusUrl.get(), repository.get().username.orNull, repository.get().password.orNull, clientTimeout.orNull, connectTimeout.orNull)
val repositoryTransitioner = StagingRepositoryTransitioner(client, BasicActionRetrier.retryUntilRepoTransitionIsCompletedRetrier(repository.get().retrying.get()))
logger.info("Releasing staging repository with id '{}'", stagingRepositoryId.get())
repositoryTransitioner.effectivelyRelease(stagingRepositoryId.get())
repositoryTransitioner.effectivelyRelease(stagingRepositoryId.get(), description.get())
logger.info("Repository with id '{}' effectively released", stagingRepositoryId.get())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,24 +86,24 @@ open class NexusClient(private val baseUrl: URI, username: String?, password: St
?.id
}

fun createStagingRepository(stagingProfileId: String): StagingRepositoryDescriptor {
val response = api.startStagingRepo(stagingProfileId, Dto(Description("Created by io.github.gradle-nexus.publish-plugin Gradle plugin"))).execute()
fun createStagingRepository(stagingProfileId: String, description: String): StagingRepositoryDescriptor {
val response = api.startStagingRepo(stagingProfileId, Dto(Description(description))).execute()
if (!response.isSuccessful) {
throw failure("create staging repository", response)
}
val stagingRepositoryId = response.body()?.data?.stagedRepositoryId ?: throw RuntimeException("No response body")
return StagingRepositoryDescriptor(baseUrl, stagingRepositoryId)
}

open fun closeStagingRepository(stagingRepositoryId: String) {
val response = api.closeStagingRepo(Dto(StagingRepositoryToTransit(listOf(stagingRepositoryId), "Closed by io.github.gradle-nexus.publish-plugin Gradle plugin"))).execute()
open fun closeStagingRepository(stagingRepositoryId: String, description: String) {
val response = api.closeStagingRepo(Dto(StagingRepositoryToTransit(listOf(stagingRepositoryId), description))).execute()
if (!response.isSuccessful) {
throw failure("close staging repository", response)
}
}

open fun releaseStagingRepository(stagingRepositoryId: String) {
val response = api.releaseStagingRepo(Dto(StagingRepositoryToTransit(listOf(stagingRepositoryId), "Release by io.github.gradle-nexus.publish-plugin Gradle plugin"))).execute()
open fun releaseStagingRepository(stagingRepositoryId: String, description: String) {
val response = api.releaseStagingRepo(Dto(StagingRepositoryToTransit(listOf(stagingRepositoryId), description))).execute()
if (!response.isSuccessful) {
throw failure("release staging repository", response)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ class StagingRepositoryTransitioner(val nexusClient: NexusClient, val retrier: A
private val log: Logger = LoggerFactory.getLogger(StagingRepositoryTransitioner::class.java.simpleName)
}

fun effectivelyClose(repoId: String) {
effectivelyChangeState(repoId, StagingRepository.State.CLOSED, nexusClient::closeStagingRepository)
fun effectivelyClose(repoId: String, description: String) {
effectivelyChangeState(repoId, StagingRepository.State.CLOSED) { nexusClient.closeStagingRepository(it, description) }
}

//TODO: Add support for autoDrop=false
fun effectivelyRelease(repoId: String) {
effectivelyChangeState(repoId, StagingRepository.State.NOT_FOUND, nexusClient::releaseStagingRepository)
fun effectivelyRelease(repoId: String, description: String) {
effectivelyChangeState(repoId, StagingRepository.State.NOT_FOUND) { nexusClient.releaseStagingRepository(it, description) }
}

private fun effectivelyChangeState(repoId: String, desiredState: StagingRepository.State, transitionClientRequest: (String) -> Unit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ internal class StagingRepositoryTransitionerTest {

companion object {
private const val TEST_STAGING_REPO_ID = "orgexample-42"
private const val DESCRIPTION = "some DESCRIPTION"
}

@Mock
Expand All @@ -53,10 +54,10 @@ internal class StagingRepositoryTransitionerTest {
.willReturn(StagingRepository(TEST_STAGING_REPO_ID, StagingRepository.State.CLOSED, false))
given(retrier.execute(anyOrNull())).willAnswer(executeFunctionPassedAsFirstArgument())

transitioner.effectivelyClose(TEST_STAGING_REPO_ID)
transitioner.effectivelyClose(TEST_STAGING_REPO_ID, DESCRIPTION)

val inOrder = inOrder(nexusClient, retrier)
inOrder.verify(nexusClient).closeStagingRepository(TEST_STAGING_REPO_ID)
inOrder.verify(nexusClient).closeStagingRepository(TEST_STAGING_REPO_ID, DESCRIPTION)
inOrder.verify(nexusClient).getStagingRepositoryStateById(TEST_STAGING_REPO_ID)
}

Expand All @@ -66,10 +67,10 @@ internal class StagingRepositoryTransitionerTest {
.willReturn(StagingRepository(TEST_STAGING_REPO_ID, StagingRepository.State.NOT_FOUND, false))
given(retrier.execute(anyOrNull())).willAnswer(executeFunctionPassedAsFirstArgument())

transitioner.effectivelyRelease(TEST_STAGING_REPO_ID)
transitioner.effectivelyRelease(TEST_STAGING_REPO_ID, DESCRIPTION)

val inOrder = inOrder(nexusClient, retrier)
inOrder.verify(nexusClient).releaseStagingRepository(TEST_STAGING_REPO_ID)
inOrder.verify(nexusClient).releaseStagingRepository(TEST_STAGING_REPO_ID, DESCRIPTION)
inOrder.verify(nexusClient).getStagingRepositoryStateById(TEST_STAGING_REPO_ID)
}

Expand All @@ -80,7 +81,7 @@ internal class StagingRepositoryTransitionerTest {
given(retrier.execute(anyOrNull())).willAnswer(executeFunctionPassedAsFirstArgument())

assertThatExceptionOfType(RepositoryTransitionException::class.java)
.isThrownBy { transitioner.effectivelyClose(TEST_STAGING_REPO_ID) }
.isThrownBy { transitioner.effectivelyClose(TEST_STAGING_REPO_ID, DESCRIPTION) }
.withMessageContainingAll(TEST_STAGING_REPO_ID, "transitioning=true")
}

Expand All @@ -91,7 +92,7 @@ internal class StagingRepositoryTransitionerTest {
given(retrier.execute(anyOrNull())).willAnswer(executeFunctionPassedAsFirstArgument())

assertThatExceptionOfType(RepositoryTransitionException::class.java)
.isThrownBy { transitioner.effectivelyClose(TEST_STAGING_REPO_ID) }
.isThrownBy { transitioner.effectivelyClose(TEST_STAGING_REPO_ID, DESCRIPTION) }
.withMessageContainingAll(TEST_STAGING_REPO_ID, StagingRepository.State.OPEN.toString(), StagingRepository.State.CLOSED.toString())
}

Expand Down

0 comments on commit abc2fd3

Please sign in to comment.