Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ repositories {

dependencies {
testImplementation(kotlin("test"))
implementation("net.dv8tion:JDA:5.3.0") // JDA Library
implementation("net.dv8tion:JDA:5.6.1") // JDA Library
implementation("org.xerial:sqlite-jdbc:3.49.1.0") // SQLite Driver
implementation("com.google.code.gson:gson:2.12.1") // Gson

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ object PullRequestCommand : BaseCommand() {
private const val BASE = "https://github.com/$USER/$REPO"

private val runIdRegex =
Regex("https://github\\.com/[\\w.]+/[\\w.]+/actions/runs/(?<RunId>\\d+)/job/(?<JobId>\\d+)")
Regex("https://github\\.com/[\\w.]+/[\\w.]+/actions/runs/(?<runId>\\d+)/job/(?<jobId>\\d+)")
private val buildVersionRegex =
Regex("Build (?<version>1\\.\\d+\\.\\d+)")

private val pullRequestPattern = "$BASE/pull/(?<pr>\\d+)".toPattern()
private val cleanPullRequestPattern = "#(?<pr>\\d+),?".toPattern()

Expand Down Expand Up @@ -150,7 +153,7 @@ object PullRequestCommand : BaseCommand() {

val lastCommit = head.sha

val job = github.getRun(lastCommit, "Build and test") ?: run {
val checkRun = github.getCheckRun(lastCommit, "Build and test") ?: run {
result(buildString {
append(title)
append(time)
Expand All @@ -162,13 +165,13 @@ object PullRequestCommand : BaseCommand() {
return
}

if (job.startedAt?.let { toTimeMark(it).passedSince() > 90.days } == true && !inBeta) {
if (checkRun.startedAt?.let { toTimeMark(it).passedSince() > 90.days } == true && !inBeta) {
result("${title}${time} \nBuild download has expired $PLEADING_FACE")
return
}

if (job.status != RunStatus.COMPLETED) {
val text = when (job.status) {
if (checkRun.status != RunStatus.COMPLETED) {
val text = when (checkRun.status) {
RunStatus.REQUESTED -> "Build has been requested $PLEADING_FACE"
RunStatus.QUEUED -> "Build is in queue $PLEADING_FACE"
RunStatus.IN_PROGRESS -> "Build is in progress $PLEADING_FACE"
Expand All @@ -188,31 +191,46 @@ object PullRequestCommand : BaseCommand() {
return
}

if (job.conclusion != Conclusion.SUCCESS && !inBeta) {
if (checkRun.conclusion != Conclusion.SUCCESS && !inBeta) {
result("$title$time\nLast development build failed $PLEADING_FACE", Color.red)
return
}

val match = job.htmlUrl?.let { runIdRegex.matchEntire(it) }
val runId = match?.groups?.get("RunId")?.value
val match = checkRun.htmlUrl?.let { runIdRegex.matchEntire(it) }
val runId = match?.groups?.get("runId")?.value!!

val uploadedVersions = mutableSetOf<String>()

for (job in github.getJobs(runId)) {
val versionMatch = buildVersionRegex.matchEntire(job.name) ?: continue

val versionGroup = versionMatch.groups["version"]?.value ?: continue

uploadedVersions.add(versionGroup)
}

val artifactLink = "$BASE/actions/runs/$runId?pr=$prNumber"
fun nightlyLink(build: String) = "https://nightly.link/$USER/$REPO/actions/runs/$runId/$build%20Build.zip"
val artifactLine = "GitHub".linkTo(artifactLink)
val nightlyLine = "Nightly (1.8.9)".linkTo(nightlyLink("Development"))
val latestNightlyLine = "Nightly (1.21.5)".linkTo(nightlyLink("Multi-version%20Development"))

val versionDownloads =
if (uploadedVersions.isNotEmpty())
uploadedVersions.joinToString(" ") { "`$it`".linkTo(nightlyLink("$it%20Development")) }
else
// Continue supporting prs built before the workflow changed
"${"`1.8.9`".linkTo(nightlyLink("Development"))} ${"`1.21.x`".linkTo(nightlyLink("Multi-version%20Development"))}"

val artifactDisplay = buildString {
append(" \n")
append("Download the latest development build of this pr!")
append("\n")
append("> From $artifactLine (requires a GitHub Account)")
append("\n")
append("> From $nightlyLine (unofficial)")
append("> From Nightly (unofficial)")
append("\n")
append("> From $latestNightlyLine (unofficial)")
append("> $versionDownloads")
append("\n")
append("> (updated ${passedSince(job.completedAt ?: "")})")
append("> (updated ${passedSince(checkRun.completedAt ?: "")})")
}

result(buildString {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,18 @@ class GitHubClient(user: String, repo: String, private val token: String) {
}
}

fun getRun(commitSha: String, checkName: String): CheckRun? {
fun getCheckRun(commitSha: String, checkName: String): CheckRun? {
val url = "$base/commits/$commitSha/check-runs?check_name=$checkName"
return readJson<CheckRunsResponse, CheckRun?>(url) { response ->
response.checkRuns.firstOrNull()
}
}

// might come handy later
fun getJob(artifactId: String): Job? {
return readJson<JobsResponse, Job?>("$base/actions/runs/$artifactId/jobs") { response ->
response.jobs.firstOrNull { job -> job.name == "Build and test" }
}
fun getJobs(runId: String): List<Job> {
return readJson<JobsResponse, List<Job>>("$base/actions/runs/$runId/jobs") { response ->
response.jobs
} ?: emptyList()
}

private inline fun <reified T : Any, R> readJson(url: String, crossinline block: (T) -> R): R? =
Expand Down