diff --git a/buildSrc/src/main/kotlin/upload/CuiCloud.kt b/buildSrc/src/main/kotlin/upload/CuiCloud.kt index 27403ba38..63db223fe 100644 --- a/buildSrc/src/main/kotlin/upload/CuiCloud.kt +++ b/buildSrc/src/main/kotlin/upload/CuiCloud.kt @@ -149,7 +149,7 @@ object CuiCloud { @OptIn(ExperimentalContracts::class) @Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE", "RESULT_CLASS_IN_RETURN_TYPE") @kotlin.internal.InlineOnly -internal inline fun retryCatching(n: Int, block: () -> R): Result { +internal inline fun retryCatching(n: Int, onFailure: () -> Unit = {}, block: () -> R): Result { contract { callsInPlace(block, InvocationKind.AT_LEAST_ONCE) } @@ -164,11 +164,11 @@ internal inline fun retryCatching(n: Int, block: () -> R): Result { } catch (e: Throwable) { } exception = e + onFailure() } } return Result.failure(exception!!) } - inline fun buildList(builderAction: MutableList.() -> Unit): List { return ArrayList().apply(builderAction) } diff --git a/buildSrc/src/main/kotlin/upload/GitHub.kt b/buildSrc/src/main/kotlin/upload/GitHub.kt index 706ded4b8..e25750764 100644 --- a/buildSrc/src/main/kotlin/upload/GitHub.kt +++ b/buildSrc/src/main/kotlin/upload/GitHub.kt @@ -18,6 +18,7 @@ import io.ktor.client.engine.cio.* import io.ktor.client.features.* import io.ktor.client.request.* import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.delay import kotlinx.coroutines.runBlocking import kotlinx.coroutines.withContext import org.gradle.api.Project @@ -82,23 +83,34 @@ object GitHub { ) } - fun upload(file: File, url: String, project: Project) = runBlocking { + fun upload(file: File, project: Project, repo: String, targetFilePath: String) = runBlocking { val token = getGithubToken(project) println("token.length=${token.length}") - Http.put("$url?access_token=$token") { - val sha = getGithubSha("mirai-repo", "shadow/${project.name}/${file.name}", "master", project) - println("sha=$sha") - val content = String(Base64.getEncoder().encode(file.readBytes())) - body = """ + val url = "https://api.github.com/repos/project-mirai/$repo/contents/$targetFilePath" + retryCatching(100, onFailure = { delay(30_000) }) { // 403 forbidden? + Http.put("$url?access_token=$token") { + val sha = retryCatching(3, onFailure = { delay(30_000) }) { + getGithubSha( + repo, + targetFilePath, + "master", + project + ) + }.getOrNull() + println("sha=$sha") + val content = String(Base64.getEncoder().encode(file.readBytes())) + body = """ { - "message": "automatically upload on release", + "message": "Automatically upload on release ${project.name}:${project.version}", "content": "$content" ${if (sha == null) "" else """, "sha": "$sha" """} } """.trimIndent() - }.let { - println("Upload response: $it") - } + }.let { + println("Upload response: $it") + } + delay(1000) + }.getOrThrow() }