mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-25 23:50:15 +08:00
Update uploaders
This commit is contained in:
parent
dad62058db
commit
d87686b416
@ -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)
|
||||
}
|
||||
|
@ -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()
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user