Skip to content

Commit

Permalink
[#4] Override staging repo id to close/release
Browse files Browse the repository at this point in the history
Using a command line switch.
  • Loading branch information
szpak committed Jan 4, 2020
1 parent edb31ee commit 7388b13
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ class NexusPublishPluginTests {
companion object {
private const val STAGING_PROFILE_ID = "someProfileId"
private const val STAGED_REPOSITORY_ID = "orgexample-42"
private const val OVERRIDDEN_STAGED_REPOSITORY_ID = "orgexample-42o"
}

private enum class StagingRepoTransitionOperation(val urlSufix: String, val type: String) {
CLOSE("close", "CLOSED"), RELEASE("promote", "RELEASED")
}

private val gson = Gson()
Expand Down Expand Up @@ -636,24 +641,10 @@ class NexusPublishPluginTests {
@Test
fun `should close staging repository`(@Wiremock server: WireMockServer) {
writeDefaultSingleProjectConfiguration()
buildGradle.append("""
nexusPublishing {
repositories {
sonatype {
nexusUrl = uri('${server.baseUrl()}')
stagingProfileId = '$STAGING_PROFILE_ID'
}
}
}
""")
writeMockedSonatypeNexusPublishingConfiguration(server)

stubCreateStagingRepoRequest(server, "/staging/profiles/$STAGING_PROFILE_ID/start", STAGED_REPOSITORY_ID)
server.stubFor(post(urlEqualTo("/staging/bulk/close"))
.withRequestBody(matchingJsonPath("\$.data[?(@.stagedRepositoryIds[0] == '$STAGED_REPOSITORY_ID')]"))
.withRequestBody(matchingJsonPath("\$.data[?(@.autoDropAfterRelease == true)]"))
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBody("{}")))
server.stubFor(get(urlEqualTo("/staging/repository/$STAGED_REPOSITORY_ID"))
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBody("{\"transitioning\":false,\"type\":\"CLOSED\"}")))
stubCloseStagingRepoRequestWithSubsequentQueryAboutItsState(server, STAGED_REPOSITORY_ID)

val result = run("initializeSonatypeStagingRepository", "closeSonatypeStagingRepository")

Expand All @@ -662,27 +653,40 @@ class NexusPublishPluginTests {
assertCloseOfStagingRepo(server)
}

private fun stubCloseStagingRepoRequestWithSubsequentQueryAboutItsState(
server: WireMockServer,
stagingRepositoryId: String = STAGED_REPOSITORY_ID
) {
stubTransitToDesiredStateStagingRepoRequestWithSubsequentQueryAboutItsState(server, stagingRepositoryId, StagingRepoTransitionOperation.CLOSE)
}

private fun stubReleaseStagingRepoRequestWithSubsequentQueryAboutItsState(
server: WireMockServer,
stagingRepositoryId: String = STAGED_REPOSITORY_ID
) {
stubTransitToDesiredStateStagingRepoRequestWithSubsequentQueryAboutItsState(server, stagingRepositoryId, StagingRepoTransitionOperation.RELEASE)
}

private fun stubTransitToDesiredStateStagingRepoRequestWithSubsequentQueryAboutItsState(
server: WireMockServer,
stagingRepositoryId: String,
operation: StagingRepoTransitionOperation
) {
server.stubFor(post(urlEqualTo("/staging/bulk/${operation.urlSufix}"))
.withRequestBody(matchingJsonPath("\$.data[?(@.stagedRepositoryIds[0] == '$stagingRepositoryId')]"))
.withRequestBody(matchingJsonPath("\$.data[?(@.autoDropAfterRelease == true)]"))
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBody("{}")))
server.stubFor(get(urlEqualTo("/staging/repository/$stagingRepositoryId"))
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBody("{\"transitioning\":false,\"type\":\"${operation.type}\"}")))
}

@Test
fun `should close and release staging repository`(@Wiremock server: WireMockServer) {
writeDefaultSingleProjectConfiguration()
buildGradle.append("""
nexusPublishing {
repositories {
sonatype {
nexusUrl = uri('${server.baseUrl()}')
stagingProfileId = '$STAGING_PROFILE_ID'
}
}
}
""")
writeMockedSonatypeNexusPublishingConfiguration(server)

stubCreateStagingRepoRequest(server, "/staging/profiles/$STAGING_PROFILE_ID/start", STAGED_REPOSITORY_ID)
server.stubFor(post(urlEqualTo("/staging/bulk/promote"))
.withRequestBody(matchingJsonPath("\$.data[?(@.stagedRepositoryIds[0] == '$STAGED_REPOSITORY_ID')]"))
.withRequestBody(matchingJsonPath("\$.data[?(@.autoDropAfterRelease == true)]"))
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBody("{}")))
server.stubFor(get(urlEqualTo("/staging/repository/$STAGED_REPOSITORY_ID"))
.willReturn(aResponse().withHeader("Content-Type", "application/json").withBody("{\"transitioning\":false,\"type\":\"RELEASED\"}")))
stubReleaseStagingRepoRequestWithSubsequentQueryAboutItsState(server, STAGED_REPOSITORY_ID)

val result = run("tasks", "initializeSonatypeStagingRepository", "releaseSonatypeStagingRepository")

Expand All @@ -691,13 +695,43 @@ class NexusPublishPluginTests {
assertReleaseOfStagingRepo(server)
}

//TODO: Move to separate subclass with command line tests for @Option
//TODO: Consider switching to parameterized tests for close and release
@Test
fun `should allow to take staging repo id to close from command line without its initialization`(@Wiremock server: WireMockServer) {
writeDefaultSingleProjectConfiguration()
writeMockedSonatypeNexusPublishingConfiguration(server)
//and
stubCloseStagingRepoRequestWithSubsequentQueryAboutItsState(server, OVERRIDDEN_STAGED_REPOSITORY_ID)

val result = run("closeSonatypeStagingRepository", "--stagingRepositoryId=$OVERRIDDEN_STAGED_REPOSITORY_ID")

assertSuccess(result, ":closeSonatypeStagingRepository")
assertCloseOfStagingRepo(server, OVERRIDDEN_STAGED_REPOSITORY_ID)
}

@Test
fun `should allow to take staging repo id to release from command line without its initialization`(@Wiremock server: WireMockServer) {
writeDefaultSingleProjectConfiguration()
writeMockedSonatypeNexusPublishingConfiguration(server)
//and
stubReleaseStagingRepoRequestWithSubsequentQueryAboutItsState(server, OVERRIDDEN_STAGED_REPOSITORY_ID)

val result = run("releaseSonatypeStagingRepository", "--stagingRepositoryId=$OVERRIDDEN_STAGED_REPOSITORY_ID")

assertSuccess(result, ":releaseSonatypeStagingRepository")
assertReleaseOfStagingRepo(server, OVERRIDDEN_STAGED_REPOSITORY_ID)
}

@Test
@Disabled("TODO")
fun `should allow to override stagingRepositoryId to close from command line`() {}
@Disabled("Should override or fail with meaningful error?")
fun `command line option should override initialized staging repository to close`() {
}

@Test
@Disabled("TODO")
fun `should allow to override stagingRepositoryId to release from command line`() {}
@Disabled("Should override or fail with meaningful error?")
fun `command line option should override initialized staging repository to release`() {
}

// TODO: To be used also in other tests
private fun writeDefaultSingleProjectConfiguration() {
Expand Down Expand Up @@ -729,6 +763,19 @@ class NexusPublishPluginTests {
""")
}

private fun writeMockedSonatypeNexusPublishingConfiguration(server: WireMockServer) {
buildGradle.append("""
nexusPublishing {
repositories {
sonatype {
nexusUrl = uri('${server.baseUrl()}')
stagingProfileId = '$STAGING_PROFILE_ID'
}
}
}
""")
}

private fun run(vararg arguments: String): BuildResult {
return gradleRunner(*arguments).build()
}
Expand Down Expand Up @@ -786,16 +833,16 @@ class NexusPublishPluginTests {
server.verify(putRequestedFor(urlMatching(testUrl)))
}

private fun assertCloseOfStagingRepo(server: WireMockServer) {
assertGivenTransitionOperationOfStagingRepo(server, "close")
private fun assertCloseOfStagingRepo(server: WireMockServer, stagingRepositoryId: String = STAGED_REPOSITORY_ID) {
assertGivenTransitionOperationOfStagingRepo(server, "close", stagingRepositoryId)
}

private fun assertReleaseOfStagingRepo(server: WireMockServer) {
assertGivenTransitionOperationOfStagingRepo(server, "promote")
private fun assertReleaseOfStagingRepo(server: WireMockServer, stagingRepositoryId: String = STAGED_REPOSITORY_ID) {
assertGivenTransitionOperationOfStagingRepo(server, "promote", stagingRepositoryId)
}

private fun assertGivenTransitionOperationOfStagingRepo(server: WireMockServer, transitionOperation: String) {
private fun assertGivenTransitionOperationOfStagingRepo(server: WireMockServer, transitionOperation: String, stagingRepositoryId: String) {
server.verify(postRequestedFor(urlMatching("/staging/bulk/$transitionOperation"))
.withRequestBody(matchingJsonPath("\$.data[?(@.stagedRepositoryIds[0] == '$STAGED_REPOSITORY_ID')]")))
.withRequestBody(matchingJsonPath("\$.data[?(@.stagedRepositoryIds[0] == '$stagingRepositoryId')]")))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Nested
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.options.Option
import org.gradle.kotlin.dsl.property
import javax.inject.Inject

Expand All @@ -32,7 +33,10 @@ constructor(objects: ObjectFactory, extension: NexusPublishExtension, repository
@get:Nested
val stagingRepository: Property<NexusStagingRepository> = objects.property()

//TODO: Bring back an ability to define stagingRepositoryId from a command line (and propagate that value back to NexusStagingRepository for Release*)
@Option(option = "stagingRepositoryId", description = "stagingRepositoryId to close")
fun setStagingRepositoryId(stagingRepositoryId: String) {
stagingRepository.set(NexusStagingRepository(stagingRepositoryId))
}

init {
// TODO: Replace with convention() once only Gradle 5.1+ is supported
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Nested
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.options.Option
import org.gradle.kotlin.dsl.property
import javax.inject.Inject

Expand All @@ -33,7 +34,10 @@ constructor(objects: ObjectFactory, extension: NexusPublishExtension, repository
@get:Nested
val stagingRepository: Property<NexusStagingRepository> = objects.property()

//TODO: Bring back an ability to define stagingRepositoryId from a command line
@Option(option = "stagingRepositoryId", description = "stagingRepositoryId to release")
fun setStagingRepositoryId(stagingRepositoryId: String) {
stagingRepository.set(NexusStagingRepository(stagingRepositoryId))
}

init {
// TODO: Replace with convention() once only Gradle 5.1+ is supported
Expand Down

0 comments on commit 7388b13

Please sign in to comment.