Skip to content

Commit

Permalink
添加监控release动态;优化实体类
Browse files Browse the repository at this point in the history
  • Loading branch information
Nekoer committed Aug 4, 2021
1 parent ee36889 commit f495726
Show file tree
Hide file tree
Showing 13 changed files with 510 additions and 70 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group = "com.hcyacg"
version = "1.3"
version = "1.4-dev-1"

repositories {
mavenLocal()
Expand Down
8 changes: 3 additions & 5 deletions src/main/kotlin/GithubNotice.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.hcyacg


import com.hcyacg.GithubTask.Companion.all
import com.hcyacg.command.Github

import com.hcyacg.github.RateLimit
import com.hcyacg.initial.Configurations
import com.hcyacg.github.RateLimits
import com.hcyacg.initial.Configurations.Companion.init


Expand Down Expand Up @@ -34,12 +32,12 @@ object GithubNotice : KotlinPlugin(


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

if (event.message.content.indexOf("/github rate_limit")>= 0){
RateLimit().getRateLimit(event)
RateLimits().getRateLimit(event)
}

}
Expand Down
11 changes: 8 additions & 3 deletions src/main/kotlin/GithubTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import com.hcyacg.github.Branches


import com.hcyacg.github.Commits
import com.hcyacg.github.Releases
import entity.Release
import kotlinx.coroutines.*
import net.mamoe.mirai.console.command.CommandSender
import net.mamoe.mirai.event.events.GroupMessageEvent
Expand All @@ -28,6 +30,7 @@ class GithubTask {
var admin: JSONArray = JSONArray.parseArray("[]")
var project: JSONArray = JSONArray.parseArray("[]")
var branches = HashMap<String,List<Branch>>()
var releases = HashMap<String,Release>()
var all:Int = 0
var taskMillisecond:Long = 5000

Expand All @@ -47,16 +50,18 @@ class GithubTask {
time.schedule(object : TimerTask() {
override fun run() {
for (e in project) {
// val j: JSONObject = JSONObject.parseObject(e.toString())
runBlocking{
val list: List<Branch> = branches[e.toString()]!!
for (o in list){
// logger.warning(o.toString())
Commits().checkUpdate(
Commits().checkCommitUpdate(
projects = e,
branch = o.name,
)

}
Releases().checkReleaseUpdate(
projects = e,
)

}
}
Expand Down
2 changes: 0 additions & 2 deletions src/main/kotlin/command/Github.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package com.hcyacg.command
import com.hcyacg.GithubNotice
import com.hcyacg.GithubTask
import com.hcyacg.GithubTask.Companion.switch
import com.hcyacg.github.RateLimit
import com.hcyacg.initial.Configurations
import com.hcyacg.initial.Configurations.Companion.overload


Expand Down
145 changes: 145 additions & 0 deletions src/main/kotlin/entity/RateLimit.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package com.hcyacg.entity

import com.alibaba.fastjson.annotation.JSONField

data class RateLimit(

@JSONField(name="rate")
val rate: Rate? = null,

@JSONField(name="resources")
val resources: Resources? = null
) {
override fun toString(): String {
return "RateLimit(rate=$rate, resources=$resources)"
}
}

data class Core(

@JSONField(name="resource")
val resource: String? = null,

@JSONField(name="limit")
val limit: Int? = null,

@JSONField(name="reset")
val reset: Int? = null,

@JSONField(name="used")
val used: Int? = null,

@JSONField(name="remaining")
val remaining: Int? = null
) {
override fun toString(): String {
return "Core(resource=$resource, limit=$limit, reset=$reset, used=$used, remaining=$remaining)"
}
}

data class Rate(

@JSONField(name="resource")
val resource: String? = null,

@JSONField(name="limit")
val limit: Int? = null,

@JSONField(name="reset")
val reset: Int? = null,

@JSONField(name="used")
val used: Int? = null,

@JSONField(name="remaining")
val remaining: Int? = null
) {
override fun toString(): String {
return "Rate(resource=$resource, limit=$limit, reset=$reset, used=$used, remaining=$remaining)"
}
}

data class IntegrationManifest(

@JSONField(name="resource")
val resource: String? = null,

@JSONField(name="limit")
val limit: Int? = null,

@JSONField(name="reset")
val reset: Int? = null,

@JSONField(name="used")
val used: Int? = null,

@JSONField(name="remaining")
val remaining: Int? = null
) {
override fun toString(): String {
return "IntegrationManifest(resource=$resource, limit=$limit, reset=$reset, used=$used, remaining=$remaining)"
}
}

data class Resources(

@JSONField(name="core")
val core: Core? = null,

@JSONField(name="search")
val search: Search? = null,

@JSONField(name="graphql")
val graphql: Graphql? = null,

@JSONField(name="integration_manifest")
val integrationManifest: IntegrationManifest? = null
) {
override fun toString(): String {
return "Resources(core=$core, search=$search, graphql=$graphql, integrationManifest=$integrationManifest)"
}
}

data class Graphql(

@JSONField(name="resource")
val resource: String? = null,

@JSONField(name="limit")
val limit: Int? = null,

@JSONField(name="reset")
val reset: Int? = null,

@JSONField(name="used")
val used: Int? = null,

@JSONField(name="remaining")
val remaining: Int? = null
) {
override fun toString(): String {
return "Graphql(resource=$resource, limit=$limit, reset=$reset, used=$used, remaining=$remaining)"
}
}

data class Search(

@JSONField(name="resource")
val resource: String? = null,

@JSONField(name="limit")
val limit: Int? = null,

@JSONField(name="reset")
val reset: Int? = null,

@JSONField(name="used")
val used: Int? = null,

@JSONField(name="remaining")
val remaining: Int? = null
) {
override fun toString(): String {
return "Search(resource=$resource, limit=$limit, reset=$reset, used=$used, remaining=$remaining)"
}
}
46 changes: 34 additions & 12 deletions src/main/kotlin/github/Branches.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import com.hcyacg.GithubTask.Companion.token
import com.hcyacg.entity.Branch
import net.mamoe.mirai.utils.MiraiLogger
import okhttp3.*
import okhttp3.internal.closeQuietly
import java.net.ConnectException
import java.net.SocketTimeoutException
import java.util.concurrent.TimeUnit
import kotlin.collections.HashMap

Expand All @@ -30,34 +33,53 @@ class Branches {


for (project in projects){
val request: Request = Request.Builder()
.url("https://api.github.com/repos/$project/branches")
.addHeader("Authorization", "token $token")
.addHeader("Accept", "application/vnd.github.v3+json").build()
val request = if (RateLimits().isResidue()){
Request.Builder()
.url("https://api.github.com/repos/$project/branches")
.addHeader("Authorization", "token $token")
.addHeader("Accept", "application/vnd.github.v3+json").build()

}else{
Request.Builder()
.url("https://api.github.com/repos/$project/branches")
.addHeader("Accept", "application/vnd.github.v3+json").build()
}


response = client.build().newCall(request).execute()

if (response.isSuccessful) {
data = response.body?.string()
}


val temp: JSONArray? = JSONArray.parseArray(data)
val tempList = mutableListOf<Branch>()
for (t in temp!!){

val name = JSONObject.parseObject(t.toString())!!.getString("name")
val protected = JSONObject.parseObject(t.toString()).getBoolean("protected")
val sha = JSONObject.parseObject(JSONObject.parseObject(t.toString()).getString("commit")).getString("sha")
val url = JSONObject.parseObject(JSONObject.parseObject(t.toString()).getString("commit")).getString("url")
tempList.add(Branch(name,sha,url,protected))
if (temp != null) {
for (t in temp){

val name = JSONObject.parseObject(t.toString())!!.getString("name")
val protected = JSONObject.parseObject(t.toString()).getBoolean("protected")
val sha = JSONObject.parseObject(JSONObject.parseObject(t.toString()).getString("commit")).getString("sha")
val url = JSONObject.parseObject(JSONObject.parseObject(t.toString()).getString("commit")).getString("url")
tempList.add(Branch(name,sha,url,protected))
}
}

list[project.toString()] = tempList
}
return list
}catch (e: SocketTimeoutException){
logger.warning("请求超时")
return getBranchesByRepo(projects)
}catch (e: ConnectException){
logger.warning("无法连接到api.github.com")
return getBranchesByRepo(projects)
} catch (e: Exception) {
e.printStackTrace()
return list
return getBranchesByRepo(projects)
}finally {
response?.closeQuietly()
}
}

Expand Down
35 changes: 22 additions & 13 deletions src/main/kotlin/github/Commits.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import okhttp3.internal.closeQuietly
import java.net.ConnectException
import java.net.SocketTimeoutException
import java.text.SimpleDateFormat
import java.util.*
import java.util.concurrent.TimeUnit
Expand All @@ -38,7 +40,7 @@ class Commits {
/**
* 检查github推送更新
*/
suspend fun checkUpdate(
suspend fun checkCommitUpdate(
projects: Any?,
branch: Any?,
) {
Expand All @@ -52,10 +54,17 @@ class Commits {
// logger.warning("${projects.toString()} => ${branch.toString()}")
val bots = Bot.instances
try {
val request: Request = Request.Builder()
.url("https://api.github.com/repos/${projects.toString()}/commits/${branch.toString()}")
.addHeader("Authorization", "token $token")
.addHeader("Accept", "application/vnd.github.v3+json").build()
val request = if (RateLimits().isResidue()){
Request.Builder()
.url("https://api.github.com/repos/${projects.toString()}/commits/${branch.toString()}")
.addHeader("Authorization", "token $token")
.addHeader("Accept", "application/vnd.github.v3+json").build()
}else{
Request.Builder()
.url("https://api.github.com/repos/${projects.toString()}/commits/${branch.toString()}")
.addHeader("Accept", "application/vnd.github.v3+json").build()
}

response = client.build().newCall(request).execute()

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


for (e in groups) {
for (bot in bots){
bot.getGroup(e.toString().toLong())?.sendMessage(
Expand All @@ -105,11 +113,7 @@ class Commits {
name = name.toString()
)
)


}
//BUG注 需判断该机器人群组是否存在该群
// event.bot.getGroup(e.toString().toLong())?.sendMessage("${name}推送了代码\n${message}\n${time}\n${html}")
}

for (u in users) {
Expand All @@ -124,13 +128,18 @@ class Commits {
)
)
}

}

}
response.closeQuietly()
}catch (e: SocketTimeoutException){
logger.warning("请求超时")
return
}catch (e: ConnectException){
logger.warning("无法连接到api.github.com")
return
} catch (e: Exception) {
e.printStackTrace()
}finally {
response?.closeQuietly()
}
}

Expand Down
Loading

0 comments on commit f495726

Please sign in to comment.