Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drop repository when build fails #410

Merged
merged 2 commits into from Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions nexus/src/main/kotlin/com/vanniktech/maven/publish/nexus/Nexus.kt
Expand Up @@ -235,6 +235,20 @@ class Nexus(
println("Repository $repositoryId released")
}

fun dropStagingRepository(repositoryId: String) {
val response = service.dropRepository(
TransitionRepositoryInput(
TransitionRepositoryInputData(
stagedRepositoryIds = listOf(repositoryId)
)
)
).execute()

if (!response.isSuccessful) {
throw IOException("Cannot drop repository: ${response.errorBody()?.string()}")
}
}

companion object {
private const val PROGRESS_1 = "\u2839"
private const val PROGRESS_2 = "\u2838"
Expand Down
Expand Up @@ -32,4 +32,7 @@ internal interface NexusService {

@POST("staging/bulk/promote")
fun releaseRepository(@Body input: TransitionRepositoryInput): Call<Unit>

@POST("staging/bulk/drop")
fun dropRepository(@Body input: TransitionRepositoryInput): Call<Unit>
}
Expand Up @@ -2,16 +2,22 @@ package com.vanniktech.maven.publish.sonatype

import com.vanniktech.maven.publish.SonatypeHost
import com.vanniktech.maven.publish.nexus.Nexus
import java.io.IOException
import org.gradle.api.logging.Logger
import org.gradle.api.logging.Logging
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.api.services.BuildService
import org.gradle.api.services.BuildServiceParameters
import org.gradle.api.services.BuildServiceRegistry
import org.gradle.tooling.events.FailureResult
import org.gradle.tooling.events.FinishEvent
import org.gradle.tooling.events.OperationCompletionListener
import org.gradle.tooling.events.task.TaskFailureResult

internal abstract class SonatypeRepositoryBuildService : BuildService<SonatypeRepositoryBuildService.Params>, AutoCloseable, OperationCompletionListener {

private val logger: Logger = Logging.getLogger(SonatypeRepositoryBuildService::class.java)

internal interface Params : BuildServiceParameters {
val sonatypeHost: Property<SonatypeHost>
val repositoryUsername: Property<String>
Expand Down Expand Up @@ -42,24 +48,28 @@ internal abstract class SonatypeRepositoryBuildService : BuildService<SonatypeRe
// indicates whether we already closed a staging repository to avoid doing it more than once in a build
var repositoryClosed: Boolean = false

var buildHasFailure: Boolean = false
private var buildIsSuccess: Boolean = true

override fun onFinish(event: FinishEvent) {
if (event.result is TaskFailureResult) {
buildHasFailure = true
if (event.result is FailureResult) {
buildIsSuccess = false
}
}

override fun close() {
if (buildHasFailure) {
return
}

val stagingRepositoryId = this.stagingRepositoryId
if (stagingRepositoryId != null) {
nexus.closeStagingRepository(stagingRepositoryId)
if (parameters.automaticRelease.get()) {
nexus.releaseStagingRepository(stagingRepositoryId)
if (buildIsSuccess) {
nexus.closeStagingRepository(stagingRepositoryId)
if (parameters.automaticRelease.get()) {
nexus.releaseStagingRepository(stagingRepositoryId)
}
} else {
try {
nexus.dropStagingRepository(stagingRepositoryId)
} catch (e: IOException) {
logger.info("Failed to drop staging repository $stagingRepositoryId", e)
}
}
}
}
Expand Down