Update uploaders

This commit is contained in:
Him188 2020-08-27 22:18:26 +08:00
parent dad62058db
commit d87686b416
2 changed files with 24 additions and 12 deletions

View File

@ -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 <R> retryCatching(n: Int, block: () -> R): Result<R> {
internal inline fun <R> retryCatching(n: Int, onFailure: () -> Unit = {}, block: () -> R): Result<R> {
contract {
callsInPlace(block, InvocationKind.AT_LEAST_ONCE)
}
@ -164,11 +164,11 @@ internal inline fun <R> retryCatching(n: Int, block: () -> R): Result<R> {
} catch (e: Throwable) {
}
exception = e
onFailure()
}
}
return Result.failure(exception!!)
}
inline fun <E> buildList(builderAction: MutableList<E>.() -> Unit): List<E> {
return ArrayList<E>().apply(builderAction)
}

View File

@ -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<String>("$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<String>("$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()
}