Skip to content

Commit 307805b

Browse files
committed
获取github的rate_limit;修改setting中的project配置项;添加下一次循环延迟的时间间隔配置;自动获取分支
1 parent 7a0f47d commit 307805b

File tree

13 files changed

+217
-129
lines changed

13 files changed

+217
-129
lines changed

build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ plugins {
77
}
88

99
group = "com.hcyacg"
10-
version = "1.2"
10+
version = "1.3"
1111

1212
repositories {
1313
mavenLocal()

gradlew

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ case "`uname`" in
7272
Darwin* )
7373
darwin=true
7474
;;
75-
MINGW* )
75+
MSYS* | MINGW* )
7676
msys=true
7777
;;
7878
NONSTOP* )

src/main/kotlin/GithubNotice.kt

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package com.hcyacg
22

33

4+
import com.hcyacg.GithubTask.Companion.all
45
import com.hcyacg.command.Github
6+
7+
import com.hcyacg.github.RateLimit
8+
import com.hcyacg.initial.Configurations
59
import com.hcyacg.initial.Configurations.Companion.init
610

711

@@ -10,6 +14,9 @@ import net.mamoe.mirai.console.command.CommandManager
1014
import net.mamoe.mirai.console.extension.PluginComponentStorage
1115
import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription
1216
import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin
17+
import net.mamoe.mirai.event.GlobalEventChannel
18+
import net.mamoe.mirai.event.events.GroupMessageEvent
19+
import net.mamoe.mirai.message.data.content
1320
import net.mamoe.mirai.utils.info
1421

1522

@@ -29,9 +36,13 @@ object GithubNotice : KotlinPlugin(
2936
override fun onEnable() {
3037
CommandManager.registerCommand(Github(),true)
3138
logger.info { "github更新通知 loaded" }
32-
// GlobalEventChannel.subscribeAlways<GroupMessageEvent> { event ->
33-
// GithubTask.event = event
34-
// }
39+
GlobalEventChannel.subscribeAlways<GroupMessageEvent> { event ->
40+
41+
if (event.message.content.indexOf("/github rate_limit")>= 0){
42+
RateLimit().getRateLimit(event)
43+
}
44+
45+
}
3546
}
3647

3748

@@ -43,6 +54,7 @@ object GithubNotice : KotlinPlugin(
4354
*/
4455
override fun PluginComponentStorage.onLoad() {
4556
init()
57+
4658
}
4759

4860

src/main/kotlin/GithubTask.kt

+16-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package com.hcyacg
33

44
import com.alibaba.fastjson.JSONArray
55
import com.alibaba.fastjson.JSONObject
6+
import com.hcyacg.entity.Branch
7+
import com.hcyacg.github.Branches
68

79

810
import com.hcyacg.github.Commits
@@ -25,7 +27,10 @@ class GithubTask {
2527
var users: JSONArray = JSONArray.parseArray("[]")
2628
var admin: JSONArray = JSONArray.parseArray("[]")
2729
var project: JSONArray = JSONArray.parseArray("[]")
28-
// var event : GroupMessageEvent? = null
30+
var branches = HashMap<String,List<Branch>>()
31+
var all:Int = 0
32+
var taskMillisecond:Long = 5000
33+
2934
}
3035
/**
3136
* 开启推送通知,循环仓库
@@ -41,22 +46,26 @@ class GithubTask {
4146
time.purge()
4247
time.schedule(object : TimerTask() {
4348
override fun run() {
44-
for ((index, e) in project.withIndex()) {
45-
val j: JSONObject = JSONObject.parseObject(e.toString())
49+
for (e in project) {
50+
// val j: JSONObject = JSONObject.parseObject(e.toString())
4651
runBlocking{
52+
val list: List<Branch> = branches[e.toString()]!!
53+
for (o in list){
54+
// logger.warning(o.toString())
4755
Commits().checkUpdate(
48-
projects = j["name"],
49-
branch = j["branch"],
50-
index = index
56+
projects = e,
57+
branch = o.name,
5158
)
59+
}
60+
5261
}
5362
}
5463
if (!switch) {
5564
this.cancel()
5665
logger.info("Github推送通知已关闭")
5766
}
5867
}
59-
}, Date(), 5000)
68+
}, Date(), taskMillisecond)
6069
}catch (e:Exception){
6170
e.printStackTrace()
6271
}

src/main/kotlin/command/Github.kt

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.hcyacg.command
33
import com.hcyacg.GithubNotice
44
import com.hcyacg.GithubTask
55
import com.hcyacg.GithubTask.Companion.switch
6+
import com.hcyacg.github.RateLimit
67
import com.hcyacg.initial.Configurations
78
import com.hcyacg.initial.Configurations.Companion.overload
89

@@ -18,20 +19,25 @@ class Github : CompositeCommand(
1819
var logger: MiraiLogger = MiraiLogger.create("Bot")
1920

2021
@SubCommand("start","启动")
22+
@Description("开启监控")
2123
suspend fun CommandSender.start() {
2224
switch = true
2325
GithubTask().openTask()
2426
}
2527

2628
@SubCommand("stop","关闭")
29+
@Description("停止监控")
2730
fun CommandSender.stop() {
2831
switch = false
2932
logger.info("请稍等片刻……")
3033
}
3134

3235
@SubCommand("reload","重载")
36+
@Description("重载配置")
3337
fun CommandSender.reload() {
3438
overload()
3539
}
3640

41+
42+
3743
}

src/main/kotlin/config/Config.kt

-22
This file was deleted.

src/main/kotlin/entity/Branch.kt

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.hcyacg.entity
2+
3+
import java.io.Serializable
4+
5+
data class Branch(
6+
var name: String,
7+
var sha: String,
8+
var url: String,
9+
var protected: Boolean
10+
): Serializable {
11+
override fun toString(): String {
12+
return "Branch(name='$name', sha='$sha', url='$url', protected=$protected)"
13+
}
14+
}
15+

src/main/kotlin/github/Branches.kt

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.hcyacg.github
2+
3+
import com.alibaba.fastjson.JSONArray
4+
import com.alibaba.fastjson.JSONObject
5+
import com.hcyacg.GithubTask
6+
import com.hcyacg.GithubTask.Companion.token
7+
import com.hcyacg.entity.Branch
8+
import net.mamoe.mirai.utils.MiraiLogger
9+
import okhttp3.*
10+
import java.util.concurrent.TimeUnit
11+
import kotlin.collections.HashMap
12+
13+
/**
14+
* 获取配置中project所有项目各自的分支
15+
*/
16+
class Branches {
17+
private val headers =
18+
Headers.Builder().add("Accept", "application/vnd.github.v3+json").add("Authorization", "token ${GithubTask.token}")
19+
private val requestBody: RequestBody? = null
20+
val logger:MiraiLogger = MiraiLogger.create("Bot")
21+
private val client = OkHttpClient().newBuilder().connectTimeout(60000, TimeUnit.MILLISECONDS)
22+
.readTimeout(60000, TimeUnit.MILLISECONDS)
23+
24+
fun getBranchesByRepo(projects : JSONArray) : HashMap<String,List<Branch>>{
25+
26+
var data: String? = null
27+
val list = HashMap<String,List<Branch>>()
28+
var response: Response? = null
29+
try {
30+
31+
32+
for (project in projects){
33+
val request: Request = Request.Builder()
34+
.url("https://api.github.com/repos/$project/branches")
35+
.addHeader("Authorization", "token $token")
36+
.addHeader("Accept", "application/vnd.github.v3+json").build()
37+
response = client.build().newCall(request).execute()
38+
39+
if (response.isSuccessful) {
40+
data = response.body?.string()
41+
}
42+
43+
44+
val temp: JSONArray? = JSONArray.parseArray(data)
45+
val tempList = mutableListOf<Branch>()
46+
for (t in temp!!){
47+
48+
val name = JSONObject.parseObject(t.toString())!!.getString("name")
49+
val protected = JSONObject.parseObject(t.toString()).getBoolean("protected")
50+
val sha = JSONObject.parseObject(JSONObject.parseObject(t.toString()).getString("commit")).getString("sha")
51+
val url = JSONObject.parseObject(JSONObject.parseObject(t.toString()).getString("commit")).getString("url")
52+
tempList.add(Branch(name,sha,url,protected))
53+
}
54+
55+
list[project.toString()] = tempList
56+
}
57+
return list
58+
} catch (e: Exception) {
59+
e.printStackTrace()
60+
return list
61+
}
62+
}
63+
64+
}

src/main/kotlin/github/Commits.kt

+14-12
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package com.hcyacg.github
33
import com.alibaba.fastjson.JSONObject
44
import com.hcyacg.GithubNotice
55
import com.hcyacg.GithubTask
6+
import com.hcyacg.GithubTask.Companion.all
7+
import com.hcyacg.GithubTask.Companion.branches
68
import com.hcyacg.GithubTask.Companion.groups
79
import com.hcyacg.GithubTask.Companion.logger
810
import com.hcyacg.GithubTask.Companion.num
@@ -39,19 +41,16 @@ class Commits {
3941
suspend fun checkUpdate(
4042
projects: Any?,
4143
branch: Any?,
42-
index: Int
4344
) {
4445
var name: Any? = null
4546
var time: Any? = null
4647
var html: Any? = null
4748
var avatar: Any? = null
4849
var message: Any? = null
4950
var stA: String? = null
50-
var sha1: Any? = null
5151
var response: Response? = null
5252
// logger.warning("${projects.toString()} => ${branch.toString()}")
5353
val bots = Bot.instances
54-
5554
try {
5655
val request: Request = Request.Builder()
5756
.url("https://api.github.com/repos/${projects.toString()}/commits/${branch.toString()}")
@@ -66,11 +65,17 @@ class Commits {
6665
val jsonObject: JSONObject? = JSONObject.parseObject(stA)
6766

6867
if (null != jsonObject) {
69-
val sha1: Any? = jsonObject["sha"]
70-
if (sha[projects].contentEquals(sha1.toString())) {
68+
if (sha["${projects}/$branch"].contentEquals(jsonObject["sha"].toString())) {
69+
return
70+
}
71+
if (null == sha["${projects}/$branch"]){
72+
sha["${projects}/$branch"] = jsonObject["sha"].toString()
7173
return
7274
}
73-
sha[projects.toString()] = sha1.toString()
75+
// logger.warning("${sha["${projects}/$branch"]} => $sha1")
76+
77+
sha["${projects}/$branch"] = jsonObject["sha"].toString()
78+
7479

7580
val commit: Any? = jsonObject["commit"]
7681
val committer: Any? = JSONObject.parseObject(commit.toString())["committer"]
@@ -88,10 +93,9 @@ class Commits {
8893
val committers: Any? = jsonObject["committer"]
8994
avatar = JSONObject.parseObject(committers.toString())["avatar_url"]
9095

91-
if (num >= project.size) {
9296

9397
for (e in groups) {
94-
for ((index,bot) in bots.withIndex()){
98+
for (bot in bots){
9599
bot.getGroup(e.toString().toLong())?.sendMessage(
96100
process(
97101
message = message.toString(),
@@ -109,7 +113,7 @@ class Commits {
109113
}
110114

111115
for (u in users) {
112-
for ((index,bot) in bots.withIndex()){
116+
for (bot in bots){
113117
bot.getStranger(u.toString().toLong())?.sendMessage(
114118
process(
115119
message = message.toString(),
@@ -122,9 +126,7 @@ class Commits {
122126
}
123127

124128
}
125-
} else {
126-
num += 1
127-
}
129+
128130
}
129131
response.closeQuietly()
130132
} catch (e: Exception) {

0 commit comments

Comments
 (0)