1-
21import com.android.build.gradle.BaseExtension
32import org.apache.http.client.methods.HttpGet
43import org.apache.http.client.methods.HttpPost
@@ -131,20 +130,21 @@ tasks.register("closeAndReleaseMultipleRepositories") {
131130 description = " Release all Sonatype staging repositories"
132131
133132 doLast {
134- val repos = fetchRepositoryIds ()
133+ val repos = fetchOpenRepositoryIds ()
135134 if (repos.isEmpty()) {
136135 println (" No open repositories found" )
137136 return @doLast
138137 }
139138 closeRepositories(repos)
140- waitForAllRepositoriesToClose (repos)
139+ waitForAllRepositoriesToDesireState (repos, " closed " )
141140 releaseRepositories(repos)
141+ waitForAllRepositoriesToDesireState(repos, " released" )
142+ dropRepositories(repos)
142143 waitForArtifactsToBeAvailable()
143- // todo task for pushing a tag
144144 }
145145}
146146
147- fun fetchRepositoryIds (): List <String > {
147+ fun fetchOpenRepositoryIds (): List <String > {
148148 val client = HttpClients .createDefault()
149149 val httpGet = HttpGet (" $nexusUrl /profile_repositories" ).apply {
150150 setHeader(" Authorization" , " Basic " + Base64 .getEncoder().encodeToString(" $nexusUsername :$nexusPassword " .toByteArray()))
@@ -189,9 +189,36 @@ fun closeRepositories(repoIds: List<String>) {
189189 }
190190 """ .trimIndent()
191191 executePostRequest(closeUrl, json)
192+ println (" Closed repositories: ${repoIds.joinToString(" , " )} " )
192193}
193194
194- fun waitForAllRepositoriesToClose (repoIds : List <String >) {
195+ fun releaseRepositories (repoIds : List <String >) {
196+ val releaseUrl = " $nexusUrl /bulk/promote"
197+ val json = """
198+ {
199+ "data": {
200+ "stagedRepositoryIds": ${repoIds.joinToString(prefix = " [\" " , separator = " \" ,\" " , postfix = " \" ]" )}
201+ }
202+ }
203+ """ .trimIndent()
204+ println (" Released repositories: ${repoIds.joinToString(" , " )} " )
205+ executePostRequest(releaseUrl, json)
206+ }
207+
208+ fun dropRepositories (repoIds : List <String >) {
209+ val dropUrl = " $nexusUrl /bulk/drop"
210+ val json = """
211+ {
212+ "data": {
213+ "stagedRepositoryIds": ${repoIds.joinToString(prefix = " [\" " , separator = " \" ,\" " , postfix = " \" ]" )}
214+ }
215+ }
216+ """ .trimIndent()
217+ executePostRequest(dropUrl, json)
218+ println (" Dropped repositories: ${repoIds.joinToString(" , " )} " )
219+ }
220+
221+ fun waitForAllRepositoriesToDesireState (repoIds : List <String >, desireState : String ) {
195222 val client = HttpClients .createDefault()
196223 val statusUrl = " $nexusUrl /repository/"
197224 val closedRepos = mutableSetOf<String >()
@@ -207,11 +234,11 @@ fun waitForAllRepositoriesToClose(repoIds: List<String>) {
207234 val responseBody = EntityUtils .toString(response.entity)
208235
209236 val state = parseRepositoryState(responseBody, repoId)
210- if (state == " closed " ) {
237+ if (state == desireState ) {
211238 println (" Repository $repoId is now in state: $state " )
212239 closedRepos.add(repoId)
213240 } else {
214- println (" Waiting for repository $repoId to be closed , current state: $state " )
241+ println (" Waiting for repository $repoId to be $desireState , current state: $state " )
215242 }
216243 }
217244 }
@@ -221,37 +248,50 @@ fun waitForAllRepositoriesToClose(repoIds: List<String>) {
221248 }
222249}
223250
224- fun releaseRepositories (repoIds : List <String >) {
225- val releaseUrl = " $nexusUrl /bulk/promote"
226- val json = """
227- {
228- "data": {
229- "stagedRepositoryIds": ${repoIds.joinToString(prefix = " [\" " , separator = " \" ,\" " , postfix = " \" ]" )}
230- }
231- }
232- """ .trimIndent()
233- executePostRequest(releaseUrl, json)
234- }
235-
236251fun waitForArtifactsToBeAvailable () {
252+ val repoIds: List <String > = repoIdWithVersion.map { it.first }
237253 val client = HttpClients .createDefault()
238- var artifactsAvailable = false
239-
240- while (! artifactsAvailable) {
241- artifactsAvailable = repoIdWithVersion.all { (repoId, version) ->
242- val artifactUrl = " https://repo1.maven.org/maven2/com/walletconnect/$repoId /$version /"
243- val httpGet = HttpGet (artifactUrl)
244- val response = client.execute(httpGet)
245- response.statusLine.statusCode == 200
254+ val artifactUrls = repoIdWithVersion.map { (repoId, version) ->
255+ println (" Checking: https://repo1.maven.org/maven2/com/walletconnect/$repoId /$version /" )
256+ " https://repo1.maven.org/maven2/com/walletconnect/$repoId /$version /"
257+ }
258+ val maxRetries = 20
259+ var attempt = 0
260+ val availableRepos = mutableSetOf<String >()
261+
262+ while (availableRepos.size < repoIds.size && attempt < maxRetries) {
263+ artifactUrls.forEachIndexed { index, artifactUrl ->
264+ if (! availableRepos.contains(repoIds[index])) {
265+ val httpGet = HttpGet (artifactUrl)
266+ try {
267+ val response = client.execute(httpGet)
268+ val statusCode = response.statusLine.statusCode
269+ EntityUtils .consume(response.entity) // Ensure the response is fully consumed
270+ if (statusCode == 200 || statusCode == 201 ) {
271+ println (" Artifact for repository ${repoIds[index]} is now available." )
272+ availableRepos.add(repoIds[index])
273+ } else {
274+ println (" Artifact for repository ${repoIds[index]} not yet available. Status code: $statusCode " )
275+ }
276+ } catch (e: Exception ) {
277+ println (" Error checking artifact for repository ${repoIds[index]} : ${e.message} " )
278+ } finally {
279+ httpGet.releaseConnection() // Ensure the connection is released
280+ }
281+ }
246282 }
247-
248- if (! artifactsAvailable) {
249- println (" Artifacts not yet available. Waiting..." )
250- Thread .sleep(30000 ) // Wait for 30 seconds before retrying
251- } else {
252- println (" All artifacts are now available." )
283+ if (availableRepos.size < repoIds.size) {
284+ println (" Waiting for artifacts to be available... Attempt: ${attempt + 1 } " )
285+ attempt++
286+ Thread .sleep(10000 ) // Wait for 10 seconds before retrying
253287 }
254288 }
289+
290+ if (availableRepos.size < repoIds.size) {
291+ throw RuntimeException (" Artifacts were not available after ${maxRetries * 10 } seconds." )
292+ } else {
293+ println (" All artifacts are now available." )
294+ }
255295}
256296
257297fun executePostRequest (url : String , json : String ) {
@@ -287,6 +327,20 @@ fun parseRepositoryState(xmlResponse: String, repositoryId: String): String? {
287327 return null
288328}
289329
330+ tasks.register<Exec >(" createTag" ) {
331+ val tagName = " BOM_$BOM_VERSION "
332+ commandLine(" git" , " tag" , tagName)
333+ }
334+
335+ tasks.register<Exec >(" pushTagToMain" ) {
336+ val tagName = " BOM_$BOM_VERSION "
337+ val repoUrl = " https://github.com/WalletConnect/WalletConnectKotlinV2.git"
338+ val token = System .getenv(" GITHUB_TOKEN" ) ? : throw GradleException (" GITHUB_TOKEN environment variable is not set" )
339+ dependsOn(" createTag" )
340+ val authenticatedRepoUrl = repoUrl.replace(" https://" , " https://$token :@" )
341+ commandLine(" git" , " push" , authenticatedRepoUrl, tagName, " refs/heads/main" )
342+ }
343+
290344private val repoIdWithVersion = listOf (
291345 Pair (ANDROID_BOM , BOM_VERSION ),
292346 Pair (FOUNDATION , FOUNDATION_VERSION ),
0 commit comments