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 1 commit
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,14 +2,15 @@ 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.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 {
internal interface Params : BuildServiceParameters {
Expand Down Expand Up @@ -42,24 +43,26 @@ 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 (_: IOException) {}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we log it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the build fails people often jump to the last message that looks like an error which in this case would be misleading because this didn't fail the build. Maybe as an info level log? Then you could find out about it when running with --info but by default Gradle doesn't print it

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good!

}
}
}
Expand Down