Skip to content

Commit f495726

Browse files
committed
添加监控release动态;优化实体类
1 parent ee36889 commit f495726

13 files changed

+510
-70
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.3"
10+
version = "1.4-dev-1"
1111

1212
repositories {
1313
mavenLocal()

src/main/kotlin/GithubNotice.kt

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

33

4-
import com.hcyacg.GithubTask.Companion.all
54
import com.hcyacg.command.Github
65

7-
import com.hcyacg.github.RateLimit
8-
import com.hcyacg.initial.Configurations
6+
import com.hcyacg.github.RateLimits
97
import com.hcyacg.initial.Configurations.Companion.init
108

119

@@ -34,12 +32,12 @@ object GithubNotice : KotlinPlugin(
3432

3533

3634
override fun onEnable() {
37-
CommandManager.registerCommand(Github(),true)
3835
logger.info { "github更新通知 loaded" }
36+
CommandManager.registerCommand(Github(),true)
3937
GlobalEventChannel.subscribeAlways<GroupMessageEvent> { event ->
4038

4139
if (event.message.content.indexOf("/github rate_limit")>= 0){
42-
RateLimit().getRateLimit(event)
40+
RateLimits().getRateLimit(event)
4341
}
4442

4543
}

src/main/kotlin/GithubTask.kt

+8-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import com.hcyacg.github.Branches
88

99

1010
import com.hcyacg.github.Commits
11+
import com.hcyacg.github.Releases
12+
import entity.Release
1113
import kotlinx.coroutines.*
1214
import net.mamoe.mirai.console.command.CommandSender
1315
import net.mamoe.mirai.event.events.GroupMessageEvent
@@ -28,6 +30,7 @@ class GithubTask {
2830
var admin: JSONArray = JSONArray.parseArray("[]")
2931
var project: JSONArray = JSONArray.parseArray("[]")
3032
var branches = HashMap<String,List<Branch>>()
33+
var releases = HashMap<String,Release>()
3134
var all:Int = 0
3235
var taskMillisecond:Long = 5000
3336

@@ -47,16 +50,18 @@ class GithubTask {
4750
time.schedule(object : TimerTask() {
4851
override fun run() {
4952
for (e in project) {
50-
// val j: JSONObject = JSONObject.parseObject(e.toString())
5153
runBlocking{
5254
val list: List<Branch> = branches[e.toString()]!!
5355
for (o in list){
54-
// logger.warning(o.toString())
55-
Commits().checkUpdate(
56+
Commits().checkCommitUpdate(
5657
projects = e,
5758
branch = o.name,
5859
)
60+
5961
}
62+
Releases().checkReleaseUpdate(
63+
projects = e,
64+
)
6065

6166
}
6267
}

src/main/kotlin/command/Github.kt

-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ 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
7-
import com.hcyacg.initial.Configurations
86
import com.hcyacg.initial.Configurations.Companion.overload
97

108

src/main/kotlin/entity/RateLimit.kt

+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package com.hcyacg.entity
2+
3+
import com.alibaba.fastjson.annotation.JSONField
4+
5+
data class RateLimit(
6+
7+
@JSONField(name="rate")
8+
val rate: Rate? = null,
9+
10+
@JSONField(name="resources")
11+
val resources: Resources? = null
12+
) {
13+
override fun toString(): String {
14+
return "RateLimit(rate=$rate, resources=$resources)"
15+
}
16+
}
17+
18+
data class Core(
19+
20+
@JSONField(name="resource")
21+
val resource: String? = null,
22+
23+
@JSONField(name="limit")
24+
val limit: Int? = null,
25+
26+
@JSONField(name="reset")
27+
val reset: Int? = null,
28+
29+
@JSONField(name="used")
30+
val used: Int? = null,
31+
32+
@JSONField(name="remaining")
33+
val remaining: Int? = null
34+
) {
35+
override fun toString(): String {
36+
return "Core(resource=$resource, limit=$limit, reset=$reset, used=$used, remaining=$remaining)"
37+
}
38+
}
39+
40+
data class Rate(
41+
42+
@JSONField(name="resource")
43+
val resource: String? = null,
44+
45+
@JSONField(name="limit")
46+
val limit: Int? = null,
47+
48+
@JSONField(name="reset")
49+
val reset: Int? = null,
50+
51+
@JSONField(name="used")
52+
val used: Int? = null,
53+
54+
@JSONField(name="remaining")
55+
val remaining: Int? = null
56+
) {
57+
override fun toString(): String {
58+
return "Rate(resource=$resource, limit=$limit, reset=$reset, used=$used, remaining=$remaining)"
59+
}
60+
}
61+
62+
data class IntegrationManifest(
63+
64+
@JSONField(name="resource")
65+
val resource: String? = null,
66+
67+
@JSONField(name="limit")
68+
val limit: Int? = null,
69+
70+
@JSONField(name="reset")
71+
val reset: Int? = null,
72+
73+
@JSONField(name="used")
74+
val used: Int? = null,
75+
76+
@JSONField(name="remaining")
77+
val remaining: Int? = null
78+
) {
79+
override fun toString(): String {
80+
return "IntegrationManifest(resource=$resource, limit=$limit, reset=$reset, used=$used, remaining=$remaining)"
81+
}
82+
}
83+
84+
data class Resources(
85+
86+
@JSONField(name="core")
87+
val core: Core? = null,
88+
89+
@JSONField(name="search")
90+
val search: Search? = null,
91+
92+
@JSONField(name="graphql")
93+
val graphql: Graphql? = null,
94+
95+
@JSONField(name="integration_manifest")
96+
val integrationManifest: IntegrationManifest? = null
97+
) {
98+
override fun toString(): String {
99+
return "Resources(core=$core, search=$search, graphql=$graphql, integrationManifest=$integrationManifest)"
100+
}
101+
}
102+
103+
data class Graphql(
104+
105+
@JSONField(name="resource")
106+
val resource: String? = null,
107+
108+
@JSONField(name="limit")
109+
val limit: Int? = null,
110+
111+
@JSONField(name="reset")
112+
val reset: Int? = null,
113+
114+
@JSONField(name="used")
115+
val used: Int? = null,
116+
117+
@JSONField(name="remaining")
118+
val remaining: Int? = null
119+
) {
120+
override fun toString(): String {
121+
return "Graphql(resource=$resource, limit=$limit, reset=$reset, used=$used, remaining=$remaining)"
122+
}
123+
}
124+
125+
data class Search(
126+
127+
@JSONField(name="resource")
128+
val resource: String? = null,
129+
130+
@JSONField(name="limit")
131+
val limit: Int? = null,
132+
133+
@JSONField(name="reset")
134+
val reset: Int? = null,
135+
136+
@JSONField(name="used")
137+
val used: Int? = null,
138+
139+
@JSONField(name="remaining")
140+
val remaining: Int? = null
141+
) {
142+
override fun toString(): String {
143+
return "Search(resource=$resource, limit=$limit, reset=$reset, used=$used, remaining=$remaining)"
144+
}
145+
}

src/main/kotlin/github/Branches.kt

+34-12
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import com.hcyacg.GithubTask.Companion.token
77
import com.hcyacg.entity.Branch
88
import net.mamoe.mirai.utils.MiraiLogger
99
import okhttp3.*
10+
import okhttp3.internal.closeQuietly
11+
import java.net.ConnectException
12+
import java.net.SocketTimeoutException
1013
import java.util.concurrent.TimeUnit
1114
import kotlin.collections.HashMap
1215

@@ -30,34 +33,53 @@ class Branches {
3033

3134

3235
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()
36+
val request = if (RateLimits().isResidue()){
37+
Request.Builder()
38+
.url("https://api.github.com/repos/$project/branches")
39+
.addHeader("Authorization", "token $token")
40+
.addHeader("Accept", "application/vnd.github.v3+json").build()
41+
42+
}else{
43+
Request.Builder()
44+
.url("https://api.github.com/repos/$project/branches")
45+
.addHeader("Accept", "application/vnd.github.v3+json").build()
46+
}
47+
48+
3749
response = client.build().newCall(request).execute()
3850

3951
if (response.isSuccessful) {
4052
data = response.body?.string()
4153
}
4254

43-
4455
val temp: JSONArray? = JSONArray.parseArray(data)
4556
val tempList = mutableListOf<Branch>()
46-
for (t in temp!!){
4757

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))
58+
if (temp != null) {
59+
for (t in temp){
60+
61+
val name = JSONObject.parseObject(t.toString())!!.getString("name")
62+
val protected = JSONObject.parseObject(t.toString()).getBoolean("protected")
63+
val sha = JSONObject.parseObject(JSONObject.parseObject(t.toString()).getString("commit")).getString("sha")
64+
val url = JSONObject.parseObject(JSONObject.parseObject(t.toString()).getString("commit")).getString("url")
65+
tempList.add(Branch(name,sha,url,protected))
66+
}
5367
}
5468

5569
list[project.toString()] = tempList
5670
}
5771
return list
72+
}catch (e: SocketTimeoutException){
73+
logger.warning("请求超时")
74+
return getBranchesByRepo(projects)
75+
}catch (e: ConnectException){
76+
logger.warning("无法连接到api.github.com")
77+
return getBranchesByRepo(projects)
5878
} catch (e: Exception) {
5979
e.printStackTrace()
60-
return list
80+
return getBranchesByRepo(projects)
81+
}finally {
82+
response?.closeQuietly()
6183
}
6284
}
6385

src/main/kotlin/github/Commits.kt

+22-13
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import okhttp3.OkHttpClient
2626
import okhttp3.Request
2727
import okhttp3.Response
2828
import okhttp3.internal.closeQuietly
29+
import java.net.ConnectException
30+
import java.net.SocketTimeoutException
2931
import java.text.SimpleDateFormat
3032
import java.util.*
3133
import java.util.concurrent.TimeUnit
@@ -38,7 +40,7 @@ class Commits {
3840
/**
3941
* 检查github推送更新
4042
*/
41-
suspend fun checkUpdate(
43+
suspend fun checkCommitUpdate(
4244
projects: Any?,
4345
branch: Any?,
4446
) {
@@ -52,10 +54,17 @@ class Commits {
5254
// logger.warning("${projects.toString()} => ${branch.toString()}")
5355
val bots = Bot.instances
5456
try {
55-
val request: Request = Request.Builder()
56-
.url("https://api.github.com/repos/${projects.toString()}/commits/${branch.toString()}")
57-
.addHeader("Authorization", "token $token")
58-
.addHeader("Accept", "application/vnd.github.v3+json").build()
57+
val request = if (RateLimits().isResidue()){
58+
Request.Builder()
59+
.url("https://api.github.com/repos/${projects.toString()}/commits/${branch.toString()}")
60+
.addHeader("Authorization", "token $token")
61+
.addHeader("Accept", "application/vnd.github.v3+json").build()
62+
}else{
63+
Request.Builder()
64+
.url("https://api.github.com/repos/${projects.toString()}/commits/${branch.toString()}")
65+
.addHeader("Accept", "application/vnd.github.v3+json").build()
66+
}
67+
5968
response = client.build().newCall(request).execute()
6069

6170
if (response.isSuccessful) {
@@ -93,7 +102,6 @@ class Commits {
93102
val committers: Any? = jsonObject["committer"]
94103
avatar = JSONObject.parseObject(committers.toString())["avatar_url"]
95104

96-
97105
for (e in groups) {
98106
for (bot in bots){
99107
bot.getGroup(e.toString().toLong())?.sendMessage(
@@ -105,11 +113,7 @@ class Commits {
105113
name = name.toString()
106114
)
107115
)
108-
109-
110116
}
111-
//BUG注 需判断该机器人群组是否存在该群
112-
// event.bot.getGroup(e.toString().toLong())?.sendMessage("${name}推送了代码\n${message}\n${time}\n${html}")
113117
}
114118

115119
for (u in users) {
@@ -124,13 +128,18 @@ class Commits {
124128
)
125129
)
126130
}
127-
128131
}
129-
130132
}
131-
response.closeQuietly()
133+
}catch (e: SocketTimeoutException){
134+
logger.warning("请求超时")
135+
return
136+
}catch (e: ConnectException){
137+
logger.warning("无法连接到api.github.com")
138+
return
132139
} catch (e: Exception) {
133140
e.printStackTrace()
141+
}finally {
142+
response?.closeQuietly()
134143
}
135144
}
136145

0 commit comments

Comments
 (0)