diff --git a/gradle/templates/publish.gradle b/gradle/templates/publish.gradle index c2524fc8ae5..dae1cf2bf5e 100644 --- a/gradle/templates/publish.gradle +++ b/gradle/templates/publish.gradle @@ -1,52 +1,4 @@ -import java.nio.charset.StandardCharsets -import java.net.http.HttpClient -import java.net.http.HttpRequest -import java.net.http.HttpResponse - apply plugin: 'maven-publish' -// This script work based on https://ossrh-staging-api.central.sonatype.com/swagger-ui/#/default/manual_upload_repository -// and getting available open repos and just uploading them -if ((project.hasProperty('SONATYPE_USER') || System.getenv('SONATYPE_USER') != null) && (project.hasProperty('SONATYPE_PASSWORD') || System.getenv('SONATYPE_PASSWORD') != null)) { - def taskName = "uploadSonatypePublication" - if (rootProject.tasks.names.contains(taskName) == false) { - System.setProperty("jdk.httpclient.allowRestrictedHeaders", "Connection,Keep-Alive") - rootProject.tasks.register(taskName) { - doLast { - def username = project.hasProperty('SONATYPE_USER') ? project.property('SONATYPE_USER') : System.getenv('SONATYPE_USER') - def password = project.hasProperty('SONATYPE_PASSWORD') ? project.property('SONATYPE_PASSWORD') : System.getenv('SONATYPE_PASSWORD') - def bearer = Base64.getEncoder().encodeToString("$username:$password".getBytes(StandardCharsets.UTF_8)) - - def client = HttpClient.newHttpClient() - def request = HttpRequest.newBuilder() - .uri(URI.create("https://ossrh-staging-api.central.sonatype.com/manual/search/repositories?state=open")) - .GET() - .header("Content-Type", "application/json") - .header("Authorization", "Bearer $bearer") - .build() - - def response = client.send(request, HttpResponse.BodyHandlers.ofString()) - def keys = new ArrayList() - response.body().findAll("\"key\"[\\s]*:[\\s]*\"[^\"]+\"").forEach { - def key = it.find("[^\"]+\"\$").find("[^\"]+") - keys.add(key) - } - keys.forEach { - println("Start uploading $it") - def uploadRequest = HttpRequest.newBuilder() - .uri(URI.create("https://ossrh-staging-api.central.sonatype.com/manual/upload/repository/$it?publishing_type=user_managed")) - .POST(HttpRequest.BodyPublishers.ofString("")) - .header("Content-Type", "application/json") - .header("Authorization", "Bearer $bearer") - .build() - def uploadResponse = client.send(uploadRequest, HttpResponse.BodyHandlers.ofString()) - if (uploadResponse.statusCode() != 200) { - throw IllegalStateException("Faced error of uploading for repo with key $it. Response: $uploadResponse") - } - } - } - } - } -} task javadocsJar(type: Jar) { archiveClassifier = 'javadoc' diff --git a/gradle/templates/publish_jvm.gradle b/gradle/templates/publish_jvm.gradle index 2d01685237c..f0128744122 100644 --- a/gradle/templates/publish_jvm.gradle +++ b/gradle/templates/publish_jvm.gradle @@ -1,52 +1,4 @@ -import java.nio.charset.StandardCharsets -import java.net.http.HttpClient -import java.net.http.HttpRequest -import java.net.http.HttpResponse - apply plugin: 'maven-publish' -// This script work based on https://ossrh-staging-api.central.sonatype.com/swagger-ui/#/default/manual_upload_repository -// and getting available open repos and just uploading them -if ((project.hasProperty('SONATYPE_USER') || System.getenv('SONATYPE_USER') != null) && (project.hasProperty('SONATYPE_PASSWORD') || System.getenv('SONATYPE_PASSWORD') != null)) { - def taskName = "uploadSonatypePublication" - if (rootProject.tasks.names.contains(taskName) == false) { - System.setProperty("jdk.httpclient.allowRestrictedHeaders", "Connection,Keep-Alive") - rootProject.tasks.register(taskName) { - doLast { - def username = project.hasProperty('SONATYPE_USER') ? project.property('SONATYPE_USER') : System.getenv('SONATYPE_USER') - def password = project.hasProperty('SONATYPE_PASSWORD') ? project.property('SONATYPE_PASSWORD') : System.getenv('SONATYPE_PASSWORD') - def bearer = Base64.getEncoder().encodeToString("$username:$password".getBytes(StandardCharsets.UTF_8)) - - def client = HttpClient.newHttpClient() - def request = HttpRequest.newBuilder() - .uri(URI.create("https://ossrh-staging-api.central.sonatype.com/manual/search/repositories?state=open")) - .GET() - .header("Content-Type", "application/json") - .header("Authorization", "Bearer $bearer") - .build() - - def response = client.send(request, HttpResponse.BodyHandlers.ofString()) - def keys = new ArrayList() - response.body().findAll("\"key\"[\\s]*:[\\s]*\"[^\"]+\"").forEach { - def key = it.find("[^\"]+\"\$").find("[^\"]+") - keys.add(key) - } - keys.forEach { - println("Start uploading $it") - def uploadRequest = HttpRequest.newBuilder() - .uri(URI.create("https://ossrh-staging-api.central.sonatype.com/manual/upload/repository/$it?publishing_type=user_managed")) - .POST(HttpRequest.BodyPublishers.ofString("")) - .header("Content-Type", "application/json") - .header("Authorization", "Bearer $bearer") - .build() - def uploadResponse = client.send(uploadRequest, HttpResponse.BodyHandlers.ofString()) - if (uploadResponse.statusCode() != 200) { - throw IllegalStateException("Faced error of uploading for repo with key $it. Response: $uploadResponse") - } - } - } - } - } -} task javadocJar(type: Jar) { diff --git a/publish_all_script b/publish_all_script index fbc9d92971e..eb1af192754 100755 --- a/publish_all_script +++ b/publish_all_script @@ -19,6 +19,6 @@ for project in "${projects[@]}"; do assert_success ./gradlew "$project:publishAllPublicationsToSonatypeRepository" --no-parallel --quiet echo "Complete publishing of $project" echo "Start uploading of $project" - assert_success ./gradlew uploadSonatypePublication --quiet + assert_success ./uploadSonatypePublication.main.kts --quiet echo "Complete uploading of $project" done diff --git a/uploadSonatypePublication.main.kts b/uploadSonatypePublication.main.kts new file mode 100755 index 00000000000..f6517cfa3ab --- /dev/null +++ b/uploadSonatypePublication.main.kts @@ -0,0 +1,58 @@ +#!/usr/bin/env kotlin + +import java.net.http.HttpRequest.BodyPublisher +import java.nio.charset.StandardCharsets +import java.net.http.HttpClient +import java.net.http.HttpRequest +import java.net.http.HttpResponse +import java.net.URI +import kotlin.io.encoding.Base64 + +@OptIn(kotlin.io.encoding.ExperimentalEncodingApi::class) +fun uploadSonatypePublication (repos_state: String, publishing_type: String) { + val bearer = let { + val username = System.getenv("SONATYPE_USER") + val password = System.getenv("SONATYPE_PASSWORD") + Base64.encodeToByteArray("$username:$password".encodeToByteArray()) + } + val baseUrl = "https://ossrh-staging-api.central.sonatype.com/manual" + val client = HttpClient.newHttpClient() + val makeRequest: (String, BodyPublisher, String) -> HttpResponse = { method, bodyPublisher, suffix -> + val request = HttpRequest.newBuilder() + .uri(URI.create("$baseUrl/$suffix")) + .method(method, bodyPublisher) + .header("Content-Type", "application/json") + .header("Authorization", "Bearer $bearer") + .build() + client.send(request, HttpResponse.BodyHandlers.ofString()) + } + + val response = makeRequest( + "GET", + HttpRequest.BodyPublishers.ofString(""), + "/search/repositories?state=$repos_state" + ) + val keys = mutableListOf() + Regex("\"key\"[\\s]*:[\\s]*\"[^\"]+\"").findAll( + response.body() + ).forEach { + val key = Regex("[^\"]+").find(Regex("[^\"]+\"\$").find(it.value) ?.value ?: return@forEach) ?.value ?: return@forEach + keys.add(key) + } + 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") + } + } +} + +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" + +uploadSonatypePublication(repos_state, publishing_type)