Skip to content

Commit

Permalink
drop repository when build fails (#410)
Browse files Browse the repository at this point in the history
* drop repository when build fails

* add log
  • Loading branch information
gabrielittner committed Sep 9, 2022
1 parent 19a8631 commit 0db5be2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
14 changes: 14 additions & 0 deletions nexus/src/main/kotlin/com/vanniktech/maven/publish/nexus/Nexus.kt
Expand Up @@ -253,6 +253,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 @@ -35,4 +35,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

0 comments on commit 0db5be2

Please sign in to comment.