Skip to content

Commit b48fa8a

Browse files
committed
add: GtiHubReleasePluginUpdater exception handle
1 parent c7e91ac commit b48fa8a

3 files changed

Lines changed: 74 additions & 19 deletions

File tree

.github/workflows/test.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,8 @@ jobs:
4949
env:
5050
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5151
run: ./gradlew test --tests "io.github.gnuf0rce.mirai.github.GitHubRepoTest" --scan --info
52+
53+
- name: GitHub Release Plugin Updater Test
54+
env:
55+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
56+
run: ./gradlew test --tests "io.github.gnuf0rce.mirai.github.GitHubReleasePluginUpdaterTest" --scan --info

src/main/kotlin/io/github/gnuf0rce/mirai/github/GitHubReleasePluginUpdater.kt

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
package io.github.gnuf0rce.mirai.github
1212

1313
import io.github.gnuf0rce.github.*
14+
import io.github.gnuf0rce.github.exception.*
1415
import io.ktor.client.request.*
1516
import io.ktor.client.statement.*
17+
import io.ktor.http.*
1618
import io.ktor.util.cio.*
1719
import io.ktor.utils.io.*
1820
import kotlinx.coroutines.*
@@ -50,31 +52,35 @@ public object GitHubReleasePluginUpdater {
5052
"io.github.samarium150.mirai.plugin.mirai-console-loafers-calendar" to "Samarium150/mirai-console-loafers-calendar",
5153
"io.github.samarium150.mirai.plugin.mirai-console-lolicon" to "Samarium150/mirai-console-lolicon",
5254

55+
"indi.eiriksgata.rulateday-dice" to "Eiriksgata/mirai-rulateday-dice",
56+
5357
"me.jie65535.mirai-console-jnr-plugin" to "jie65535/mirai-console-jnr-plugin",
5458

55-
"me.stageguard.obms.OsuMapSuggester" to "StageGuard/OsuMapSuggester",
59+
"me.sagiri.mirai.plugin.QShell" to "EroSagiri/QShell",
60+
61+
// "me.stageguard.obms.OsuMapSuggester" to "StageGuard/OsuMapSuggester",
5662
"me.stageguard.sctimetable" to "StageGuard/SuperCourseTimetableBot",
5763

58-
"org.laolittle.plugin.SkikoMirai" to "LaoLittle/SkikoMirai",
5964
"org.laolittle.plugin.draw.DrawMeme" to "LaoLittle/DrawMeme",
65+
"org.laolittle.plugin.SkikoMirai" to "LaoLittle/SkikoMirai",
6066

6167
"top.colter.bilibili-dynamic-mirai-plugin" to "Colter23/bilibili-dynamic-mirai-plugin",
6268
"top.colter.genshin-sign" to "Colter23/genshin-sign-mirai-plugin",
6369

6470
"top.cutestar.antiRecall" to "Pmaru-top/AntiRecall",
6571

72+
"top.jie65535.jcf" to "jie65535/mirai-console-jcf-plugin",
73+
"top.jie65535.j24" to "jie65535/mirai-console-j24-plugin",
74+
"top.jie65535.mail-notify" to "jie65535/JMailNotify",
75+
"top.jie65535.mirai.grasscutter-command" to "jie65535/JGrasscutterCommand",
76+
"top.jie65535.mirai-console-jcc-plugin" to "jie65535/mirai-console-jcc-plugin",
77+
"top.jie65535.mirai-console-jcr-plugin" to "jie65535/mirai-console-jcr-plugin",
78+
"top.jie65535.mirai-console-jms-plugin" to "jie65535/mirai-console-jms-plugin",
79+
6680
"top.limbang.mcmod" to "limbang/mirai-console-mcmod-plugin",
6781
"top.limbang.mcsm" to "limbang/mirai-console-mcsm-plugin",
6882
"top.limbang.minecraft" to "limbang/mirai-console-minecraft-plugin",
6983

70-
"top.jie65535.mirai.grasscutter-command" to "jie65535/JGrasscutterCommand",
71-
"top.jie65535.mail-notify" to "jie65535/JMailNotify",
72-
"top.jie65535.mirai-console-jms-plugin" to "jie65535/mirai-console-jms-plugin",
73-
"top.jie65535.jcf" to "jie65535/mirai-console-jcf-plugin",
74-
"top.jie65535.mirai-console-jcr-plugin" to "jie65535/mirai-console-jcr-plugin",
75-
"top.jie65535.mirai-console-jhr-plugin" to "jie65535/mirai-console-jhr-plugin",
76-
"top.jie65535.mirai-console-jcab-arg-plugin" to "jie65535/mirai-console-jcab-arg-plugin",
77-
7884
"xmmt.dituon.petpet" to "Dituon/petpet",
7985

8086
"xyz.cssxsh.mirai.plugin.novelai-helper" to "cssxsh/novelai-helper",
@@ -87,14 +93,6 @@ public object GitHubReleasePluginUpdater {
8793
dict.putAll(GitHubJson.decodeFromString(file.readText()))
8894
} else {
8995
file.writeText(GitHubJson.encodeToString(dict))
90-
val last = file.lastModified()
91-
runBlocking {
92-
ConsoleInput.requestInput(hint = "update.dict.json 已生成,你可以打开浏览一下 (输入回车结束等待)")
93-
}
94-
if (last < file.lastModified()) {
95-
dict.clear()
96-
dict.putAll(GitHubJson.decodeFromString(file.readText()))
97-
}
9896
}
9997
}
10098

@@ -113,7 +111,16 @@ public object GitHubReleasePluginUpdater {
113111
var target = PluginManager.pluginsFolder.resolve(plugin.description.id)
114112

115113
plugin.launch {
116-
val latest = github.repo(id).releases.latest()
114+
val latest = try {
115+
github.repo(id).releases.latest()
116+
} catch (exception: GitHubApiException) {
117+
if (exception.cause.response.status == HttpStatusCode.NotFound) {
118+
plugin.logger.warning("项目未设置 Release Latest!")
119+
} else {
120+
plugin.logger.warning("GitHub API 异常", exception)
121+
}
122+
return@launch
123+
}
117124
val jar = latest.assets.find { it.name.endsWith(".mirai2.jar") }
118125
?: latest.assets.find { it.name.endsWith(".mirai.jar") }
119126
?: latest.assets.find { it.name.endsWith(".jar") }
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package io.github.gnuf0rce.mirai.github
2+
3+
import io.github.gnuf0rce.github.*
4+
import io.github.gnuf0rce.github.exception.*
5+
import io.ktor.client.request.*
6+
import io.ktor.client.statement.*
7+
import io.ktor.util.cio.*
8+
import io.ktor.utils.io.*
9+
import kotlinx.coroutines.*
10+
import kotlinx.serialization.*
11+
import org.junit.jupiter.api.*
12+
import java.io.File
13+
import java.util.TreeMap
14+
15+
internal class GitHubReleasePluginUpdaterTest : GitHubClientTest() {
16+
17+
@Test
18+
fun dict(): Unit = runBlocking {
19+
for ((id, repo) in TreeMap(GitHubReleasePluginUpdater.dict)) {
20+
val folder = File("run/update/$id")
21+
folder.mkdirs()
22+
val latest = try {
23+
github.repo(repo).releases.latest()
24+
} catch (exception: GitHubApiException) {
25+
continue
26+
}
27+
folder.resolve("latest.json").writeText(GitHubJson.encodeToString(latest))
28+
val jar = latest.assets.find { it.name.endsWith(".mirai2.jar") }
29+
?: latest.assets.find { it.name.endsWith(".mirai.jar") }
30+
?: latest.assets.find { it.name.endsWith(".jar") }
31+
?: throw NoSuchElementException(latest.htmlUrl)
32+
val target = folder.resolve(jar.name)
33+
34+
if (target.exists()) continue
35+
36+
github.useHttpClient { http ->
37+
http.get(jar.browserDownloadUrl)
38+
.bodyAsChannel()
39+
.copyAndClose(target.writeChannel())
40+
}
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)