mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2025-09-15 13:29:38 +00:00
first version of uploadSonatypePublicationmain.kts
This commit is contained in:
@@ -1,58 +1,120 @@
|
|||||||
#!/usr/bin/env kotlin
|
#!/usr/bin/env kotlin
|
||||||
|
|
||||||
|
@file:Repository("https://repo.maven.apache.org/maven2/")
|
||||||
|
@file:DependsOn("org.jetbrains.kotlinx:kotlinx-serialization-json:1.8.1")
|
||||||
|
|
||||||
|
import kotlinx.serialization.json.Json
|
||||||
|
import kotlinx.serialization.json.JsonArray
|
||||||
|
import kotlinx.serialization.json.JsonObject
|
||||||
|
import kotlinx.serialization.json.JsonPrimitive
|
||||||
import java.net.http.HttpRequest.BodyPublisher
|
import java.net.http.HttpRequest.BodyPublisher
|
||||||
import java.nio.charset.StandardCharsets
|
|
||||||
import java.net.http.HttpClient
|
import java.net.http.HttpClient
|
||||||
import java.net.http.HttpRequest
|
import java.net.http.HttpRequest
|
||||||
import java.net.http.HttpResponse
|
import java.net.http.HttpResponse
|
||||||
import java.net.URI
|
import java.net.URI
|
||||||
import kotlin.io.encoding.Base64
|
|
||||||
|
|
||||||
@OptIn(kotlin.io.encoding.ExperimentalEncodingApi::class)
|
class CentralSonatypeOSSRHApi(
|
||||||
fun uploadSonatypePublication (repos_state: String, publishing_type: String) {
|
private val bearer: String,
|
||||||
val bearer = let {
|
) {
|
||||||
val username = System.getenv("SONATYPE_USER")
|
data class SonatypeOSSRHRepository(
|
||||||
val password = System.getenv("SONATYPE_PASSWORD")
|
val key: String,
|
||||||
Base64.encodeToByteArray("$username:$password".encodeToByteArray())
|
val portalDeploymentId: String?,
|
||||||
}
|
val state: String,
|
||||||
val baseUrl = "https://ossrh-staging-api.central.sonatype.com/manual"
|
)
|
||||||
val client = HttpClient.newHttpClient()
|
|
||||||
val makeRequest: (String, BodyPublisher, String) -> HttpResponse<String> = { method, bodyPublisher, suffix ->
|
private val baseUrl = "https://ossrh-staging-api.central.sonatype.com/manual"
|
||||||
|
private val client = HttpClient.newHttpClient()
|
||||||
|
private fun makeRequest(
|
||||||
|
method: String,
|
||||||
|
suffix: String,
|
||||||
|
bodyPublisher: BodyPublisher = HttpRequest.BodyPublishers.ofString("")
|
||||||
|
): JsonObject? {
|
||||||
val request = HttpRequest.newBuilder()
|
val request = HttpRequest.newBuilder()
|
||||||
.uri(URI.create("$baseUrl/$suffix"))
|
.uri(URI.create("$baseUrl/$suffix"))
|
||||||
.method(method, bodyPublisher)
|
.method(method, bodyPublisher)
|
||||||
.header("Content-Type", "application/json")
|
.header("Content-Type", "application/json")
|
||||||
.header("Authorization", "Bearer $bearer")
|
.header("Authorization", "Bearer $bearer")
|
||||||
.build()
|
.build()
|
||||||
client.send(request, HttpResponse.BodyHandlers.ofString())
|
val response = client.send(request, HttpResponse.BodyHandlers.ofString())
|
||||||
|
println(response)
|
||||||
|
val responseBody = response.body()
|
||||||
|
println(responseBody)
|
||||||
|
return when {
|
||||||
|
response.statusCode() == 200 && responseBody.isNotEmpty() -> {
|
||||||
|
return Json.decodeFromString(JsonObject.serializer(), responseBody)
|
||||||
|
}
|
||||||
|
response.statusCode() == 200 -> {
|
||||||
|
return JsonObject(emptyMap())
|
||||||
|
}
|
||||||
|
else -> null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val response = makeRequest(
|
fun repositories(
|
||||||
|
profileId: String? = null,
|
||||||
|
state: String? = null,
|
||||||
|
ip: String? = null,
|
||||||
|
): List<SonatypeOSSRHRepository>? {
|
||||||
|
val repositories = makeRequest(
|
||||||
"GET",
|
"GET",
|
||||||
HttpRequest.BodyPublishers.ofString(""),
|
"search/repositories?${state ?.let { "state=$it" } ?: ""}${profileId ?.let { "&profile_id=$it" } ?: ""}${ip ?.let { "&ip=$it" } ?: ""}"
|
||||||
"/search/repositories?state=$repos_state"
|
) ?.get("repositories") as? JsonArray
|
||||||
)
|
|
||||||
val keys = mutableListOf<String>()
|
return repositories ?.mapNotNull {
|
||||||
Regex("\"key\"[\\s]*:[\\s]*\"[^\"]+\"").findAll(
|
val asObject = it as? JsonObject ?:return@mapNotNull null
|
||||||
response.body()
|
SonatypeOSSRHRepository(
|
||||||
).forEach {
|
(asObject["key"] as JsonPrimitive).content,
|
||||||
val key = Regex("[^\"]+").find(Regex("[^\"]+\"\$").find(it.value) ?.value ?: return@forEach) ?.value ?: return@forEach
|
(asObject["portal_deployment_id"] as? JsonPrimitive) ?.content,
|
||||||
keys.add(key)
|
(asObject["state"] as JsonPrimitive).content,
|
||||||
}
|
)
|
||||||
keys.forEach {
|
|
||||||
println("Start uploading $it")
|
|
||||||
val uploadResponse = makeRequest(
|
|
||||||
"POST",
|
|
||||||
HttpRequest.BodyPublishers.ofString(""),
|
|
||||||
"upload/repository/$it?publishing_type=$publishing_type"
|
|
||||||
)
|
|
||||||
if (uploadResponse.statusCode() != 200) {
|
|
||||||
println("Faced error of uploading for repo with key $it. Response: $uploadResponse")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun drop(repositoryKey: String, ): Boolean {
|
||||||
|
val dropped = makeRequest(
|
||||||
|
"DELETE",
|
||||||
|
"drop/repository/$repositoryKey"
|
||||||
|
)
|
||||||
|
|
||||||
|
return dropped != null
|
||||||
|
}
|
||||||
|
|
||||||
|
fun uploadDefault(requestedNamespace: String, publishingType: String? = null): Boolean {
|
||||||
|
val uploaded = makeRequest(
|
||||||
|
"POST",
|
||||||
|
"upload/defaultRepository/$requestedNamespace?${publishingType ?.let { "publishing_type=$publishingType" } ?: ""}"
|
||||||
|
)
|
||||||
|
|
||||||
|
return uploaded != null
|
||||||
|
}
|
||||||
|
|
||||||
|
fun upload(repositoryKey: String, publishingType: String? = null): Boolean {
|
||||||
|
val uploaded = makeRequest(
|
||||||
|
"POST",
|
||||||
|
"upload/repository/$repositoryKey?${publishingType ?.let { "publishing_type=$publishingType" } ?: ""}"
|
||||||
|
)
|
||||||
|
|
||||||
|
return uploaded != null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val repos_state = runCatching { System.getenv("repos_state").takeIf { it.isNotEmpty() } }.getOrNull() ?: "open"
|
val repos_state = runCatching { System.getenv("repos_state").takeIf { it.isNotEmpty() } }.getOrNull() ?: "open"
|
||||||
val publishing_type = runCatching { System.getenv("publishing_type").takeIf { it.isNotEmpty() } }.getOrNull() ?: "user_managed"
|
val publishing_type = runCatching { System.getenv("publishing_type").takeIf { it.isNotEmpty() } }.getOrNull() ?: "user_managed"
|
||||||
|
val api = CentralSonatypeOSSRHApi(
|
||||||
|
let {
|
||||||
|
val username = System.getenv("SONATYPE_USER")
|
||||||
|
val password = System.getenv("SONATYPE_PASSWORD")
|
||||||
|
java.util.Base64.getEncoder().encodeToString("$username:$password".encodeToByteArray())
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
uploadSonatypePublication(repos_state, publishing_type)
|
api.repositories() ?.forEach {
|
||||||
|
if (it.state == "open") {
|
||||||
|
api.upload(it.key, "user_managed")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
api.repositories() ?.forEach {
|
||||||
|
if (it.state == "closed") {
|
||||||
|
api.upload(it.key, "automatic")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user