Skip to content
This repository has been archived by the owner on Jan 8, 2023. It is now read-only.

Commit

Permalink
ValidateChangelogVerticle and list issue comments option
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaiR committed Nov 13, 2018
1 parent 7b2212d commit b9102e6
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.spair.repoxbot

import io.github.spair.repoxbot.constant.EB_COMMAND_CHANGELOG_UPDATE
import io.github.spair.repoxbot.constant.EB_COMMAND_CHANGELOG_VALIDATE
import io.github.spair.repoxbot.constant.EB_EVENT_PULLREQUEST
import io.github.spair.repoxbot.dto.PullRequest
import io.github.spair.repoxbot.dto.PullRequestAction
Expand All @@ -18,8 +19,10 @@ class DistributionVerticle : AbstractVerticle() {

private fun consumePullRequest() = Handler<Message<PullRequest>> { msg ->
println("Pull request consumed: ${msg.body()}") // TODO: remove
when (msg.body().action) {
PullRequestAction.MERGED -> eventBus.send(EB_COMMAND_CHANGELOG_UPDATE, msg.body())
val pullRequest = msg.body()
when (pullRequest.action) {
PullRequestAction.OPENED -> eventBus.send(EB_COMMAND_CHANGELOG_VALIDATE, pullRequest)
PullRequestAction.MERGED -> eventBus.send(EB_COMMAND_CHANGELOG_UPDATE, pullRequest)
PullRequestAction.CLOSED, PullRequestAction.UNDEFINED -> {}
}
}
Expand Down
49 changes: 47 additions & 2 deletions src/main/kotlin/io/github/spair/repoxbot/GitHubVerticle.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
@file:Suppress("NOTHING_TO_INLINE")

package io.github.spair.repoxbot

import io.github.spair.repoxbot.constant.* // ktlint-disable
import io.github.spair.repoxbot.dto.IssueComment
import io.github.spair.repoxbot.dto.RemoteConfig
import io.github.spair.repoxbot.dto.UpdateFileInfo
import io.github.spair.repoxbot.dto.codec.IssueCommentListCodec
import io.github.spair.repoxbot.dto.codec.StringJsonToRemoteConfigCodec
import io.github.spair.repoxbot.util.getSharedConfig
import io.vertx.core.AbstractVerticle
Expand All @@ -26,8 +30,11 @@ class GithubVerticle : AbstractVerticle() {

override fun start() {
eventBus.localConsumer<JsonObject>(EB_GITHUB_CONFIG_READ, readRemoteConfig())

eventBus.localConsumer<String>(EB_GITHUB_FILE_READ, readGithubFile())
eventBus.localConsumer<UpdateFileInfo>(EB_GITHUB_FILE_UPDATE, updateGithubFile())

eventBus.localConsumer<Int>(EB_GITHUB_ISSUE_LIST, listIssueComments())
}

private fun readRemoteConfig() = Handler<Message<JsonObject>> { msg ->
Expand Down Expand Up @@ -74,6 +81,15 @@ class GithubVerticle : AbstractVerticle() {
}
}

private fun listIssueComments() = Handler<Message<Int>> { msg ->
val issueComments = mutableListOf<IssueComment>()
recursiveLinkProcess(issueComments(msg.body())) {
issueComments.add(IssueComment(it.getInteger(ID), it.getJsonObject(USER).getString(LOGIN), it.getString(BODY)))
}.setHandler {
msg.reply(issueComments.toList(), DeliveryOptions().setCodecName(IssueCommentListCodec.NAME))
}
}

private fun getFileSha(path: String): Future<String> {
var dirPath: String
var fileName: String
Expand Down Expand Up @@ -103,6 +119,32 @@ class GithubVerticle : AbstractVerticle() {
}
}

private fun recursiveLinkProcess(link: String, action: (JsonObject) -> Unit): Future<Unit> {
val nextLinkReg = "<([\\w\\d/?=:.]*)>;[ ]rel=\"next\"".toRegex()
val future = Future.future<Unit>()

fun process(linkToProcess: String) {
httpClient.getAbs(linkToProcess).authHeader().handler { resp ->
resp.bodyHandler { body ->
body.toJsonArray().forEach { node -> if (node is JsonObject) action(node) }
val headers = resp.headers()
if (headers.contains("link")) {
val nextLink = nextLinkReg.toPattern().matcher(headers["link"])
if (nextLink.find()) {
process(nextLink.group(1))
} else {
future.complete()
}
} else {
future.complete()
}
}
}.end()
}
process(link)
return future
}

private fun HttpClientRequest.authHeader(): HttpClientRequest = apply {
putHeader(HttpHeaders.AUTHORIZATION, "token ${getSharedConfig(GITHUB_TOKEN)}")
putHeader(HttpHeaders.USER_AGENT, getSharedConfig(AGENT_NAME))
Expand All @@ -118,7 +160,10 @@ private fun JsonObject.readContents(): String = getString(CONTENT).decodeBase64(
private fun String.decodeBase64(): String = Base64.getMimeDecoder().decode(this).toString(Charsets.UTF_8)
private fun String.encodeBase64(): String = Base64.getEncoder().encodeToString(toByteArray())

@Suppress("NOTHING_TO_INLINE")
inline fun GithubVerticle.contents(relPath: String): String {
private inline fun GithubVerticle.contents(relPath: String): String {
return "$GITHUB_API_URL/repos/${getSharedConfig(GITHUB_ORG)}/${getSharedConfig(GITHUB_REPO)}/contents/$relPath"
}

private inline fun GithubVerticle.issueComments(issueNum: Int): String {
return "$GITHUB_API_URL/repos/${getSharedConfig(GITHUB_ORG)}/${getSharedConfig(GITHUB_REPO)}/issues/$issueNum/comments"
}
5 changes: 4 additions & 1 deletion src/main/kotlin/io/github/spair/repoxbot/MainVerticle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.github.spair.repoxbot
import io.github.spair.repoxbot.constant.* // ktlint-disable
import io.github.spair.repoxbot.dto.codec.* // ktlint-disable
import io.github.spair.repoxbot.command.UpdateChangelogVerticle
import io.github.spair.repoxbot.command.ValidateChangelogVerticle
import io.github.spair.repoxbot.dto.PullRequest
import io.github.spair.repoxbot.dto.RemoteConfig
import io.github.spair.repoxbot.dto.UpdateFileInfo
Expand Down Expand Up @@ -62,6 +63,7 @@ class MainVerticle : AbstractVerticle() {

registerCodec(JsonToPullRequestCodec())
registerCodec(StringJsonToRemoteConfigCodec())
registerCodec(IssueCommentListCodec())

logger.info("Event bus codecs registered")
}
Expand All @@ -72,7 +74,8 @@ class MainVerticle : AbstractVerticle() {
initVerticle(EntryPointVerticle::class.java.name),
initVerticle(GithubVerticle::class.java.name),
initVerticle(DistributionVerticle::class.java.name),
initVerticle(UpdateChangelogVerticle::class.java.name)
initVerticle(UpdateChangelogVerticle::class.java.name),
initVerticle(ValidateChangelogVerticle::class.java.name)
)).setHandler(reporter(future) {
logger.info("All verticles deployed")
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.github.spair.repoxbot.command

import io.github.spair.repoxbot.constant.EB_COMMAND_CHANGELOG_VALIDATE
import io.github.spair.repoxbot.constant.EB_GITHUB_ISSUE_LIST
import io.github.spair.repoxbot.dto.IssueComment
import io.github.spair.repoxbot.dto.PullRequest
import io.vertx.core.AbstractVerticle

class ValidateChangelogVerticle : AbstractVerticle() {

private val eventBus by lazy { vertx.eventBus() }

override fun start() {
eventBus.localConsumer<PullRequest>(EB_COMMAND_CHANGELOG_VALIDATE) { msg ->
val pullRequest = msg.body()
eventBus.send<List<IssueComment>>(EB_GITHUB_ISSUE_LIST, pullRequest.number) { issueComments ->
println(issueComments.result().body())
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package io.github.spair.repoxbot.constant

// Event
const val EB_EVENT_PULLREQUEST = "eb.event.pull_request"

// Command
const val EB_COMMAND_CHANGELOG_UPDATE = "eb.command.changelog.update"
const val EB_COMMAND_CHANGELOG_VALIDATE = "eb.command.changelog.validate"

// Github
const val EB_GITHUB_CONFIG_READ = "eb.github.config.read"

const val EB_GITHUB_FILE_READ = "eb.github.file.read"
const val EB_GITHUB_FILE_UPDATE = "eb.github.file.update"

const val EB_GITHUB_ISSUE_LIST = "eb.github.issue.list"
3 changes: 3 additions & 0 deletions src/main/kotlin/io/github/spair/repoxbot/dto/IssueComment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package io.github.spair.repoxbot.dto

data class IssueComment(val id: Int, val user: String, val body: String)
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ data class PullRequest(
)

enum class PullRequestAction {
CLOSED, MERGED, UNDEFINED
OPENED, CLOSED, MERGED, UNDEFINED
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.github.spair.repoxbot.dto.codec

import io.github.spair.repoxbot.dto.IssueComment

class IssueCommentListCodec : LocalMessageCodec<List<IssueComment>, List<IssueComment>>() {

companion object {
const val NAME = "localIssueCommentListCodec"
}

override fun transform(issueCommentList: List<IssueComment>): List<IssueComment> = issueCommentList

override fun name(): String = NAME
}

0 comments on commit b9102e6

Please sign in to comment.