-
Notifications
You must be signed in to change notification settings - Fork 7
Add nmcpPublishCentralPortalDeployment task
#251
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
Draft
marcphilipp
wants to merge
1
commit into
GradleUp:main
Choose a base branch
from
marcphilipp:marc/publish-deployment-task
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
nmcp-tasks/src/main/kotlin/nmcp/internal/task/nmcpPublishDeployment.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package nmcp.internal.task | ||
|
|
||
| import gratatouille.tasks.GLogger | ||
| import gratatouille.tasks.GTask | ||
| import kotlin.time.Duration.Companion.seconds | ||
| import nmcp.transport.Success | ||
| import nmcp.transport.executeWithRetries | ||
| import nmcp.transport.nmcpClient | ||
| import okhttp3.Request | ||
| import okhttp3.RequestBody | ||
|
|
||
| @GTask(pure = false) | ||
| internal fun nmcpPublishDeployment( | ||
| logger: GLogger, | ||
| username: String?, | ||
| password: String?, | ||
| deploymentId: String?, | ||
| baseUrl: String?, | ||
| publishingTimeoutSeconds: Long? | ||
| ) { | ||
| check(!deploymentId.isNullOrBlank()) { | ||
| "Nmcp: deploymentId is missing" | ||
| } | ||
|
|
||
| val token = toBearerToken(username, password) | ||
|
|
||
| @Suppress("NAME_SHADOWING") | ||
| val baseUrl = baseUrl ?: "https://central.sonatype.com/" | ||
| val url = baseUrl + "api/v1/publisher/deployment/$deploymentId" | ||
|
|
||
| logger.lifecycle("Publishing previously uploaded deployment bundle '$deploymentId'") | ||
| val request = Request.Builder() | ||
| .post(RequestBody.EMPTY) | ||
| .addHeader("Authorization", "Bearer $token") | ||
| .url(url) | ||
| .build() | ||
| val result = executeWithRetries(logger, nmcpClient, request) | ||
|
|
||
| if (result !is Success) { | ||
| error("Cannot publish deployment '$deploymentId' to maven central: ($result)}") | ||
| } | ||
|
|
||
| logger.lifecycle("Nmcp: deployment bundle '$deploymentId' moved to 'publishing' status.") | ||
|
|
||
| val timeout = publishingTimeoutSeconds?.seconds ?: 0.seconds | ||
| if (timeout.isPositive()) { | ||
| logger.lifecycle("Nmcp: waiting for publication...") | ||
| waitForStatus(setOf(PUBLISHED), timeout, logger, deploymentId, baseUrl, token) | ||
| logger.lifecycle("Nmcp: deployment is published.") | ||
| } else { | ||
| logger.lifecycle("Nmcp: deployment is publishing... Check the central portal UI to verify its status.") | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
nmcp-tasks/src/main/kotlin/nmcp/internal/task/portal.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| package nmcp.internal.task | ||
|
|
||
| import gratatouille.tasks.GLogger | ||
| import kotlin.time.Duration | ||
| import kotlin.time.Duration.Companion.seconds | ||
| import kotlin.time.TimeSource.Monotonic.markNow | ||
| import kotlinx.serialization.json.Json | ||
| import kotlinx.serialization.json.JsonObject | ||
| import kotlinx.serialization.json.JsonPrimitive | ||
| import nmcp.transport.Success | ||
| import nmcp.transport.executeWithRetries | ||
| import nmcp.transport.nmcpClient | ||
| import okhttp3.Request | ||
| import okhttp3.RequestBody.Companion.toRequestBody | ||
| import okio.Buffer | ||
| import okio.ByteString | ||
| import okio.use | ||
|
|
||
| internal fun waitForStatus( | ||
| target: Set<Status>, | ||
| timeout: Duration, | ||
| logger: GLogger, | ||
| deploymentId: String, | ||
| baseUrl: String, | ||
| token: String, | ||
| ) { | ||
| val pollingInterval = 5.seconds | ||
| val mark = markNow() | ||
| while (true) { | ||
| check(mark.elapsedNow() < timeout) { | ||
| "Nmcp: timeout while checking deployment '$deploymentId'. You might need to check the deployment status on the Central Portal UI (see $baseUrl), or you could increase the timeout." | ||
| } | ||
|
|
||
| val status = verifyStatus( | ||
| logger = logger, | ||
| deploymentId = deploymentId, | ||
| baseUrl = baseUrl, | ||
| token = token, | ||
| ) | ||
| if (status is FAILED) { | ||
| error("Nmcp: deployment has failed:\n${status.error}") | ||
| } else if (status in target) { | ||
| return | ||
| } else { | ||
| logger.lifecycle("Nmcp: deployment status is '$status', will try again in ${pollingInterval.inWholeSeconds}s (${(timeout - mark.elapsedNow()).inWholeSeconds.seconds} left)...") | ||
| // Wait for the next attempt to reduce the load on the Central Portal API | ||
| Thread.sleep(pollingInterval.inWholeMilliseconds) | ||
| continue | ||
| } | ||
| } | ||
| } | ||
|
|
||
| internal sealed interface Status | ||
|
|
||
| // A deployment is uploaded and waiting for processing by the validation service | ||
| internal data object PENDING : Status | ||
|
|
||
| // A deployment is being processed by the validation service | ||
| internal data object VALIDATING : Status | ||
|
|
||
| // A deployment has passed validation and is waiting on a user to manually publish via the Central Portal UI | ||
| internal data object VALIDATED : Status | ||
|
|
||
| // A deployment has been either automatically or manually published and is being uploaded to Maven Central | ||
| internal data object PUBLISHING : Status | ||
|
|
||
| // A deployment has successfully been uploaded to Maven Central | ||
| internal data object PUBLISHED : Status | ||
|
|
||
| // A deployment has encountered an error | ||
| internal class FAILED(val error: String) : Status | ||
|
|
||
| internal fun verifyStatus( | ||
| logger: GLogger, | ||
| deploymentId: String, | ||
| baseUrl: String, | ||
| token: String, | ||
| ): Status { | ||
| val request = Request.Builder() | ||
| .post(ByteString.EMPTY.toRequestBody()) | ||
| .addHeader("Authorization", "Bearer $token") | ||
| .url(baseUrl + "api/v1/publisher/status?id=$deploymentId") | ||
| .build() | ||
| val result = executeWithRetries(logger, nmcpClient, request) | ||
| if (result !is Success) { | ||
| error("Cannot verify deployment $deploymentId status ($result)") | ||
| } | ||
|
|
||
| val str = result.body.use { it.readUtf8() } | ||
| val element = Json.parseToJsonElement(str) | ||
| check(element is JsonObject) { | ||
| "Nmcp: unexpected status response for deployment $deploymentId: $str" | ||
| } | ||
|
|
||
| val state = element["deploymentState"] | ||
| check(state is JsonPrimitive && state.isString) { | ||
| "Nmcp: unexpected deploymentState for deployment $deploymentId: $state" | ||
| } | ||
|
|
||
| return when (state.content) { | ||
| "PENDING" -> PENDING | ||
| "VALIDATING" -> VALIDATING | ||
| "VALIDATED" -> VALIDATED | ||
| "PUBLISHING" -> PUBLISHING | ||
| "PUBLISHED" -> PUBLISHED | ||
| "FAILED" -> { | ||
| FAILED(element["errors"].toString()) | ||
| } | ||
| else -> error("Nmcp: unexpected deploymentState for deployment $deploymentId: $state") | ||
| } | ||
|
|
||
| } | ||
|
|
||
| internal fun toBearerToken(username: String?, password: String?): String { | ||
| check(!username.isNullOrBlank()) { | ||
| "Nmcp: username is missing" | ||
| } | ||
| check(!password.isNullOrBlank()) { | ||
| "Nmcp: password is missing" | ||
| } | ||
|
|
||
| val token = "$username:$password".let { | ||
| Buffer().writeUtf8(it).readByteString().base64() | ||
| } | ||
| return token | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!