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

Commit

Permalink
Labels for PR feature
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaiR committed Nov 19, 2018
1 parent c24d637 commit b85b185
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,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_COMMAND_PULLREQUEST_LABEL
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 @@ -21,7 +22,11 @@ class DistributionVerticle : AbstractVerticle() {
println("Pull request consumed: ${msg.body()}") // TODO: remove
val pullRequest = msg.body()
when (pullRequest.action) {
PullRequestAction.OPENED, PullRequestAction.EDITED -> eventBus.send(EB_COMMAND_CHANGELOG_VALIDATE, pullRequest)
PullRequestAction.OPENED -> {
eventBus.send(EB_COMMAND_PULLREQUEST_LABEL, pullRequest)
eventBus.send(EB_COMMAND_CHANGELOG_VALIDATE, pullRequest)
}
PullRequestAction.EDITED -> eventBus.send(EB_COMMAND_CHANGELOG_VALIDATE, pullRequest)
PullRequestAction.MERGED -> eventBus.send(EB_COMMAND_CHANGELOG_UPDATE, pullRequest)
PullRequestAction.CLOSED, PullRequestAction.UNDEFINED -> {}
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/kotlin/io/github/spair/repoxbot/MainVerticle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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.command.LabelPullRequestVerticle
import io.github.spair.repoxbot.dto.PullRequest
import io.github.spair.repoxbot.dto.RemoteConfig
import io.github.spair.repoxbot.dto.UpdateCommentInfo
Expand Down Expand Up @@ -77,7 +78,8 @@ class MainVerticle : AbstractVerticle() {
initVerticle(GithubVerticle::class.java.name),
initVerticle(DistributionVerticle::class.java.name),
initVerticle(UpdateChangelogVerticle::class.java.name),
initVerticle(ValidateChangelogVerticle::class.java.name)
initVerticle(ValidateChangelogVerticle::class.java.name),
initVerticle(LabelPullRequestVerticle::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,60 @@
package io.github.spair.repoxbot.command

import io.github.spair.repoxbot.constant.AGENT_NAME
import io.github.spair.repoxbot.constant.EB_COMMAND_PULLREQUEST_LABEL
import io.github.spair.repoxbot.constant.EB_GITHUB_CONFIG_READ
import io.github.spair.repoxbot.dto.PullRequest
import io.github.spair.repoxbot.dto.RemoteConfig
import io.github.spair.repoxbot.logic.generateChangelog
import io.github.spair.repoxbot.logic.getLabelsFromChangelog
import io.github.spair.repoxbot.logic.getLabelsFromDiffText
import io.github.spair.repoxbot.util.getSharedConfig
import io.vertx.core.*
import io.vertx.core.http.HttpHeaders
import io.vertx.core.logging.LoggerFactory
import java.net.HttpURLConnection

class LabelPullRequestVerticle : AbstractVerticle() {

private val logger = LoggerFactory.getLogger(LabelPullRequestVerticle::class.java)
private val eventBus by lazy { vertx.eventBus() }
private val httpClient by lazy { vertx.createHttpClient() }

override fun start() {
eventBus.localConsumer<PullRequest>(EB_COMMAND_PULLREQUEST_LABEL) { msg ->
val pullRequest = msg.body()

val remoteConfigFuture = Future.future<RemoteConfig>()
val diffTextFuture = Future.future<String>()

eventBus.send<RemoteConfig>(EB_GITHUB_CONFIG_READ, null) { remoteConfigFuture.complete(it.result().body()) }
loadDiffText(pullRequest.diffLink).setHandler { diffTextFuture.complete(it.result()) }

val labelsToAdd = mutableSetOf<String>()

CompositeFuture.all(remoteConfigFuture, diffTextFuture).setHandler {
val remoteConfig = remoteConfigFuture.result()

generateChangelog(pullRequest)?.letIfNotEmpty { changelog ->
labelsToAdd.addAll(getLabelsFromChangelog(changelog, remoteConfig.classes))
}
labelsToAdd.addAll(getLabelsFromDiffText(diffTextFuture.result(), remoteConfig.pathsLabels))

println(labelsToAdd)
}
}
}

private fun loadDiffText(diffLink: String): Future<String> {
val future = Future.future<String>()
httpClient.getAbs(diffLink).putHeader(HttpHeaders.USER_AGENT, getSharedConfig(AGENT_NAME)).setFollowRedirects(true).handler {
if (it.statusCode() == HttpURLConnection.HTTP_OK) {
it.bodyHandler { body -> future.complete(body.toString()) }
} else {
logger.warn("Unable to get diff by link: $diffLink")
future.complete("")
}
}.end()
return future
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,16 @@ class UpdateChangelogVerticle : AbstractVerticle() {
eventBus.localConsumer<PullRequest>(EB_COMMAND_CHANGELOG_UPDATE) { msg ->
generateChangelog(msg.body())?.letIfNotEmpty { changelog ->
eventBus.send<RemoteConfig>(EB_GITHUB_CONFIG_READ, null) { readConfigRes ->
if (readConfigRes.succeeded()) {
val remoteConfig = readConfigRes.result().body()
val changelogPath = remoteConfig.path
eventBus.send<String>(EB_GITHUB_FILE_READ, changelogPath) { readFileRes ->
if (readFileRes.succeeded()) {
val updateMessage = "Automatic changelog generation for PR #${changelog.pullRequestNumber}"
val newChangelogHtml = mergeChangelogWithHtml(changelog, readFileRes.result().body())
eventBus.send(EB_GITHUB_FILE_UPDATE, UpdateFileInfo(changelogPath, updateMessage, newChangelogHtml))
} else {
logger.error("Fail to read changelog file", readFileRes.cause())
}
val remoteConfig = readConfigRes.result().body()
val changelogPath = remoteConfig.path
eventBus.send<String>(EB_GITHUB_FILE_READ, changelogPath) { readFileRes ->
if (readFileRes.succeeded()) {
val updateMessage = "Automatic changelog generation for PR #${changelog.pullRequestNumber}"
val newChangelogHtml = mergeChangelogWithHtml(changelog, readFileRes.result().body())
eventBus.send(EB_GITHUB_FILE_UPDATE, UpdateFileInfo(changelogPath, updateMessage, newChangelogHtml))
} else {
logger.error("Fail to read changelog file", readFileRes.cause())
}
} else {
logger.error("Fail to read config from github", readConfigRes.cause())
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,20 @@ class ValidateChangelogVerticle : AbstractVerticle() {

private fun validateFromConfig(changelog: Changelog) {
eventBus.send<RemoteConfig>(EB_GITHUB_CONFIG_READ, null) { readConfigRes ->
if (readConfigRes.succeeded()) {
val configChangelogClasses = readConfigRes.result().body().classes
val configChangelogClasses = readConfigRes.result().body().classes

if (configChangelogClasses.isEmpty()) {
sendOrUpdateStatus("$CHANGELOG_STATUS: $OK_STATUS", changelog.pullRequestNumber)
return@send
}
if (configChangelogClasses.isEmpty()) {
sendOrUpdateStatus("$CHANGELOG_STATUS: $OK_STATUS", changelog.pullRequestNumber)
return@send
}

findInvalidClasses(configChangelogClasses, changelog.entries).let {
if (it.isEmpty()) {
sendOrUpdateStatus("$CHANGELOG_STATUS: $OK_STATUS", changelog.pullRequestNumber)
} else {
val statusMsg = "$CHANGELOG_STATUS: $FAIL_STATUS Invalid changelog classes (${it.joinToString()})"
sendOrUpdateStatus(statusMsg, changelog.pullRequestNumber)
}
findInvalidClasses(configChangelogClasses, changelog.entries).let {
if (it.isEmpty()) {
sendOrUpdateStatus("$CHANGELOG_STATUS: $OK_STATUS", changelog.pullRequestNumber)
} else {
val statusMsg = "$CHANGELOG_STATUS: $FAIL_STATUS Invalid changelog classes (${it.joinToString()})"
sendOrUpdateStatus(statusMsg, changelog.pullRequestNumber)
}
} else {
logger.error("Fail to read config from github", readConfigRes.cause())
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ 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"
const val EB_COMMAND_PULLREQUEST_LABEL = "eb.command.pull_request.label"

// Github
const val EB_GITHUB_CONFIG_READ = "eb.github.config.read"
Expand All @@ -15,4 +16,4 @@ const val EB_GITHUB_FILE_UPDATE = "eb.github.file.update"

const val EB_GITHUB_ISSUE_COMMENT_LIST = "eb.github.issue.comment.list"
const val EB_GITHUB_ISSUE_COMMENT_CREATE = "eb.github.issue.comment.create"
const val EB_GITHUB_ISSUE_COMMENT_UPDATE = "eb/github.issue.comment.update"
const val EB_GITHUB_ISSUE_COMMENT_UPDATE = "eb.github.issue.comment.update"
3 changes: 2 additions & 1 deletion src/main/kotlin/io/github/spair/repoxbot/dto/RemoteConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ package io.github.spair.repoxbot.dto

data class RemoteConfig(
val path: String = "html/changelog.html",
val classes: Map<String, String> = emptyMap()
val classes: Map<String, String> = emptyMap(),
val pathsLabels: Map<String, String> = emptyMap()
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.github.spair.repoxbot.logic

import io.github.spair.repoxbot.dto.Changelog

fun getLabelsFromChangelog(changelog: Changelog, classes: Map<String, String>): Set<String> {
val labelsToAdd = mutableSetOf<String>()

changelog.entries.forEach { entry ->
classes[entry.className]?.let { label ->
if (label.isNotBlank()) {
labelsToAdd.add(label)
}
}
}

return labelsToAdd
}

fun getLabelsFromDiffText(diffText: String, pathsLabels: Map<String, String>): Set<String> {
val labelsToAdd = mutableSetOf<String>()

pathsLabels.forEach { regexPath, labelName ->
diffText.reader().forEachLine { diffLine ->
if (regexPath.toRegex().toPattern().matcher(diffLine).find()) {
labelsToAdd.add(labelName)
}
}
}

return labelsToAdd
}

0 comments on commit b85b185

Please sign in to comment.