diff --git a/build.gradle.kts b/build.gradle.kts index 2877e59..33e6a7b 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -7,7 +7,7 @@ plugins { } group = "com.hcyacg" -version = "1.4-dev-2" +version = "1.4-dev-3" repositories { mavenLocal() diff --git a/src/main/kotlin/GithubTask.kt b/src/main/kotlin/GithubTask.kt index 8d7b9c8..703d700 100644 --- a/src/main/kotlin/GithubTask.kt +++ b/src/main/kotlin/GithubTask.kt @@ -8,7 +8,10 @@ import com.hcyacg.github.Branches import com.hcyacg.github.Commits +import com.hcyacg.github.Issues import com.hcyacg.github.Releases +import entity.Issue +import entity.IssueItem import entity.Release import kotlinx.coroutines.* import net.mamoe.mirai.console.command.CommandSender @@ -31,6 +34,7 @@ class GithubTask { var project: JSONArray = JSONArray.parseArray("[]") var branches = HashMap>() var releases = HashMap() + var issueItem = HashMap() var all:Int = 0 var taskMillisecond:Long = 5000 @@ -63,6 +67,10 @@ class GithubTask { projects = e, ) + Issues().checkIssuesUpdate( + projects = e + ) + } } if (!switch) { diff --git a/src/main/kotlin/entity/Issue.kt b/src/main/kotlin/entity/Issue.kt new file mode 100644 index 0000000..f664b27 --- /dev/null +++ b/src/main/kotlin/entity/Issue.kt @@ -0,0 +1,323 @@ +package entity + +import com.alibaba.fastjson.annotation.JSONField +import java.math.BigInteger + +data class Issue( + + @JSONField(name="Issue") + val issue: List? = null +) { + override fun toString(): String { + return "Issue(issue=$issue)" + } +} + +data class PullRequest( + + @JSONField(name="patch_url") + val patchUrl: String? = null, + + @JSONField(name="html_url") + val htmlUrl: String? = null, + + @JSONField(name="diff_url") + val diffUrl: String? = null, + + @JSONField(name="url") + val url: String? = null +) { + override fun toString(): String { + return "PullRequest(patchUrl=$patchUrl, htmlUrl=$htmlUrl, diffUrl=$diffUrl, url=$url)" + } +} + +data class IssueItem( + + @JSONField(name="assignees") + val assignees: List? = null, + + @JSONField(name="created_at") + val createdAt: String? = null, + + @JSONField(name="title") + val title: String? = null, + + @JSONField(name="body") + val body: String? = null, + + @JSONField(name="labels_url") + val labelsUrl: String? = null, + + @JSONField(name="author_association") + val authorAssociation: String? = null, + + @JSONField(name="number") + val number: Int? = null, + + @JSONField(name="updated_at") + val updatedAt: String? = null, + + @JSONField(name="performed_via_github_app") + val performedViaGithubApp: Any? = null, + + @JSONField(name="comments_url") + val commentsUrl: String? = null, + + @JSONField(name="active_lock_reason") + val activeLockReason: Any? = null, + + @JSONField(name="repository_url") + val repositoryUrl: String? = null, + + @JSONField(name="id") + val id: Long? = null, + + @JSONField(name="state") + val state: String? = null, + + @JSONField(name="locked") + val locked: Boolean? = null, + + @JSONField(name="comments") + val comments: Int? = null, + + @JSONField(name="closed_at") + val closedAt: Any? = null, + + @JSONField(name="url") + val url: String? = null, + + @JSONField(name="labels") + val labels: List? = null, + + @JSONField(name="milestone") + val milestone: Milestone? = null, + + @JSONField(name="events_url") + val eventsUrl: String? = null, + + @JSONField(name="html_url") + val htmlUrl: String? = null, + + @JSONField(name="assignee") + val assignee: Any? = null, + + @JSONField(name="user") + val user: User? = null, + + @JSONField(name="node_id") + val nodeId: String? = null, + + @JSONField(name="pull_request") + val pullRequest: PullRequest? = null +) { + override fun toString(): String { + return "IssueItem(assignees=$assignees, createdAt=$createdAt, title=$title, body=$body, labelsUrl=$labelsUrl, authorAssociation=$authorAssociation, number=$number, updatedAt=$updatedAt, performedViaGithubApp=$performedViaGithubApp, commentsUrl=$commentsUrl, activeLockReason=$activeLockReason, repositoryUrl=$repositoryUrl, id=$id, state=$state, locked=$locked, comments=$comments, closedAt=$closedAt, url=$url, labels=$labels, milestone=$milestone, eventsUrl=$eventsUrl, htmlUrl=$htmlUrl, assignee=$assignee, user=$user, nodeId=$nodeId, pullRequest=$pullRequest)" + } +} + +data class Milestone( + + @JSONField(name="creator") + val creator: Creator? = null, + + @JSONField(name="closed_at") + val closedAt: Any? = null, + + @JSONField(name="description") + val description: String? = null, + + @JSONField(name="created_at") + val createdAt: String? = null, + + @JSONField(name="title") + val title: String? = null, + + @JSONField(name="closed_issues") + val closedIssues: Int? = null, + + @JSONField(name="url") + val url: String? = null, + + @JSONField(name="due_on") + val dueOn: Any? = null, + + @JSONField(name="labels_url") + val labelsUrl: String? = null, + + @JSONField(name="number") + val number: Int? = null, + + @JSONField(name="updated_at") + val updatedAt: String? = null, + + @JSONField(name="html_url") + val htmlUrl: String? = null, + + @JSONField(name="id") + val id: Long? = null, + + @JSONField(name="state") + val state: String? = null, + + @JSONField(name="open_issues") + val openIssues: Int? = null, + + @JSONField(name="node_id") + val nodeId: String? = null +) { + override fun toString(): String { + return "Milestone(creator=$creator, closedAt=$closedAt, description=$description, createdAt=$createdAt, title=$title, closedIssues=$closedIssues, url=$url, dueOn=$dueOn, labelsUrl=$labelsUrl, number=$number, updatedAt=$updatedAt, htmlUrl=$htmlUrl, id=$id, state=$state, openIssues=$openIssues, nodeId=$nodeId)" + } +} + +data class User( + + @JSONField(name="gists_url") + val gistsUrl: String? = null, + + @JSONField(name="repos_url") + val reposUrl: String? = null, + + @JSONField(name="following_url") + val followingUrl: String? = null, + + @JSONField(name="starred_url") + val starredUrl: String? = null, + + @JSONField(name="login") + val login: String? = null, + + @JSONField(name="followers_url") + val followersUrl: String? = null, + + @JSONField(name="type") + val type: String? = null, + + @JSONField(name="url") + val url: String? = null, + + @JSONField(name="subscriptions_url") + val subscriptionsUrl: String? = null, + + @JSONField(name="received_events_url") + val receivedEventsUrl: String? = null, + + @JSONField(name="avatar_url") + val avatarUrl: String? = null, + + @JSONField(name="events_url") + val eventsUrl: String? = null, + + @JSONField(name="html_url") + val htmlUrl: String? = null, + + @JSONField(name="site_admin") + val siteAdmin: Boolean? = null, + + @JSONField(name="id") + val id: Long? = null, + + @JSONField(name="gravatar_id") + val gravatarId: String? = null, + + @JSONField(name="node_id") + val nodeId: String? = null, + + @JSONField(name="organizations_url") + val organizationsUrl: String? = null +) { + override fun toString(): String { + return "User(gistsUrl=$gistsUrl, reposUrl=$reposUrl, followingUrl=$followingUrl, starredUrl=$starredUrl, login=$login, followersUrl=$followersUrl, type=$type, url=$url, subscriptionsUrl=$subscriptionsUrl, receivedEventsUrl=$receivedEventsUrl, avatarUrl=$avatarUrl, eventsUrl=$eventsUrl, htmlUrl=$htmlUrl, siteAdmin=$siteAdmin, id=$id, gravatarId=$gravatarId, nodeId=$nodeId, organizationsUrl=$organizationsUrl)" + } +} + +data class LabelsItem( + + @JSONField(name="default") + val jsonMemberDefault: Boolean? = null, + + @JSONField(name="color") + val color: String? = null, + + @JSONField(name="name") + val name: String? = null, + + @JSONField(name="description") + val description: String? = null, + + @JSONField(name="id") + val id: Long? = null, + + @JSONField(name="url") + val url: String? = null, + + @JSONField(name="node_id") + val nodeId: String? = null +) { + override fun toString(): String { + return "LabelsItem(jsonMemberDefault=$jsonMemberDefault, color=$color, name=$name, description=$description, id=$id, url=$url, nodeId=$nodeId)" + } +} + +data class Creator( + + @JSONField(name="gists_url") + val gistsUrl: String? = null, + + @JSONField(name="repos_url") + val reposUrl: String? = null, + + @JSONField(name="following_url") + val followingUrl: String? = null, + + @JSONField(name="starred_url") + val starredUrl: String? = null, + + @JSONField(name="login") + val login: String? = null, + + @JSONField(name="followers_url") + val followersUrl: String? = null, + + @JSONField(name="type") + val type: String? = null, + + @JSONField(name="url") + val url: String? = null, + + @JSONField(name="subscriptions_url") + val subscriptionsUrl: String? = null, + + @JSONField(name="received_events_url") + val receivedEventsUrl: String? = null, + + @JSONField(name="avatar_url") + val avatarUrl: String? = null, + + @JSONField(name="events_url") + val eventsUrl: String? = null, + + @JSONField(name="html_url") + val htmlUrl: String? = null, + + @JSONField(name="site_admin") + val siteAdmin: Boolean? = null, + + @JSONField(name="id") + val id: Long? = null, + + @JSONField(name="gravatar_id") + val gravatarId: String? = null, + + @JSONField(name="node_id") + val nodeId: String? = null, + + @JSONField(name="organizations_url") + val organizationsUrl: String? = null +) { + override fun toString(): String { + return "Creator(gistsUrl=$gistsUrl, reposUrl=$reposUrl, followingUrl=$followingUrl, starredUrl=$starredUrl, login=$login, followersUrl=$followersUrl, type=$type, url=$url, subscriptionsUrl=$subscriptionsUrl, receivedEventsUrl=$receivedEventsUrl, avatarUrl=$avatarUrl, eventsUrl=$eventsUrl, htmlUrl=$htmlUrl, siteAdmin=$siteAdmin, id=$id, gravatarId=$gravatarId, nodeId=$nodeId, organizationsUrl=$organizationsUrl)" + } +} diff --git a/src/main/kotlin/github/Issues.kt b/src/main/kotlin/github/Issues.kt new file mode 100644 index 0000000..f9aba94 --- /dev/null +++ b/src/main/kotlin/github/Issues.kt @@ -0,0 +1,110 @@ +package com.hcyacg.github + +import com.alibaba.fastjson.JSONArray +import com.alibaba.fastjson.JSONObject +import com.hcyacg.GithubTask +import com.hcyacg.utils.CardUtil +import com.hcyacg.utils.HttpUtil +import entity.Issue +import entity.IssueItem +import entity.Release +import net.mamoe.mirai.Bot +import net.mamoe.mirai.utils.MiraiLogger +import okhttp3.Headers +import okhttp3.RequestBody +import java.net.ConnectException +import java.net.SocketTimeoutException +import java.text.SimpleDateFormat +import java.util.* + +class Issues { + val logger: MiraiLogger = MiraiLogger.create("Bot") + private val headers = Headers.Builder().add("Accept","application/vnd.github.v3+json").add("Authorization","token ${GithubTask.token}") + private val requestBody: RequestBody? = null + + suspend fun checkIssuesUpdate( + projects: Any? + ){ + val bots = Bot.instances + var time: String? = null + var myDate :Date? =null + try{ + if (!RateLimits().isResidue()){ + headers.removeAll("Authorization") + } + val data = HttpUtil.request( + method = HttpUtil.Companion.Method.GET, + uri = "https://api.github.com/repos/$projects/issues?state=open", + body = requestBody, + headers = headers.build(), + logger = logger + ) + + if (null == data || JSONArray.parseArray(data).size < 1){ + return + } + + val issueItem = JSONObject.parseObject(JSONArray.parseArray(data)[0].toString(), IssueItem::class.java) + + if (null == GithubTask.issueItem[projects.toString()]?.nodeId){ + GithubTask.issueItem[projects.toString()] = issueItem + return + } + + if (GithubTask.issueItem[projects.toString()]!!.nodeId == issueItem.nodeId){ + return + } + GithubTask.issueItem[projects.toString()] = issueItem + + if (null != issueItem.createdAt){ + val dateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.US) + myDate = dateFormat.parse(issueItem.createdAt.toString().replace("Z", "+0000")) + val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss") + time = sdf.format(Date(myDate.time.toString().toLong())) + + } + + if (myDate != null) { + if (Date().time-(5 *60 * 1000) > myDate.time){ + return + } + } + + for (e in GithubTask.groups) { + for (bot in bots){ + bot.getGroup(e.toString().toLong())?.sendMessage( + CardUtil().process( + message = issueItem.body.toString(), + html = issueItem.htmlUrl.toString(), + avatar = issueItem.user!!.avatarUrl.toString(), + time = time.toString(), + name = issueItem.user.login.toString()+ "提交了新问题" + ) + ) + } + } + + for (u in GithubTask.users) { + for (bot in bots){ + bot.getStranger(u.toString().toLong())?.sendMessage( + CardUtil().process( + message = issueItem.body.toString(), + html = issueItem.htmlUrl.toString(), + avatar = issueItem.user!!.avatarUrl.toString(), + time = time.toString(), + name = issueItem.user.login.toString()+ "提交了新问题" + ) + ) + } + } + }catch (e: SocketTimeoutException){ + logger.warning("请求超时") + return + }catch (e: ConnectException){ + GithubTask.logger.warning("无法连接到api.github.com") + return + } catch (e: Exception) { + e.printStackTrace() + } + } +} \ No newline at end of file