src/main/kotlin/ltd/guimc/lgzbot/utils/GithubUtils.kt
/*
* THIS FILE IS PART OF lgz-bot PROJECT
*
* You must disclose the source code of your modified work and the source code you took from this project. This means you are not allowed to use code from this project (even partially) in a closed-source (or even obfuscated) application.
* Your modified application must also be licensed under the AGPLv3.
*
* Copyright (c) 2022 - now Guimc Team.
*/
package ltd.guimc.lgzbot.utils
import ltd.guimc.lgzbot.files.GithubSubConfig
import ltd.guimc.lgzbot.github.CommitInfo
import ltd.guimc.lgzbot.github.OwnerInfo
import ltd.guimc.lgzbot.github.RepoInfo
import ltd.guimc.lgzbot.github.UserInfo
import org.json.JSONArray
import org.json.JSONObject
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
object GithubUtils {
val gitLinkRegex = Regex(pattern = "(git|https|git@)(:\\/\\/|)github.com(:|\\/).*\\/.*(.git|\\/| |)")
val githubHead = Regex(pattern = "(git|https|git@)(:\\/\\/|)github.com(:|\\/)")
fun findGitLink(text: String): String? {
return gitLinkRegex.find(text)?.value
}
private fun apiObject(url: String): JSONObject = HttpUtils.getJsonObject("https://api.github.com$url", if (GithubSubConfig.key != "" ) GithubSubConfig.key else null)
private fun apiArray(url: String): JSONArray = HttpUtils.getJsonArray("https://api.github.com$url", if (GithubSubConfig.key != "" ) GithubSubConfig.key else null)
fun convert(url: String): String {
val s = url
var dropLength = 0
if (s.endsWith("/")) dropLength++
if (s.endsWith(".git")) dropLength += 4
if (s.endsWith(" ")) dropLength++
return s.dropLast(dropLength).replace(githubHead, "")
}
fun getGithubRepo(repo: String): RepoInfo {
val infoJson = apiObject("/repos/$repo")
return RepoInfo(
repo,
OwnerInfo(infoJson.getJSONObject("owner").getString("login")),
infoJson.getString("description"),
LocalDateTime.parse(infoJson.getString("created_at"), DateTimeFormatter.ISO_DATE_TIME),
LocalDateTime.parse(infoJson.getString("pushed_at"), DateTimeFormatter.ISO_DATE_TIME),
getLastCommit(repo),
infoJson.getString("language"),
infoJson.getString("default_branch")
)
}
fun getLastCommit(repo: String): CommitInfo {
val infoJson = apiArray("/repos/$repo/commits").getJSONObject(0)
val commit = infoJson.getJSONObject("commit")
val author = commit.getJSONObject("committer")
val commitID = infoJson.getString("sha").dropLast(8)
// Commit Info
val verification = try {
commit.getJSONObject("verification").getBoolean("verified")
} catch (_: Exception) {
false
}
val commitMessage = commit.getString("message")
val commitTime = LocalDateTime.parse(commit.getJSONObject("committer").getString("date"), DateTimeFormatter.ISO_DATE_TIME)
return CommitInfo(
commitID,
commitMessage,
UserInfo(
author.getString("name"),
author.getString("email")
),
commitTime,
verification
)
}
}