Skip to content

Commit

Permalink
🎨 Clean code: minor refactorings/cleanings
Browse files Browse the repository at this point in the history
Use private/internal scopes for functions.
Use 'it' for single parameter lambdas.
Remove JAVA_HOME stuff that used to be required for GraalVM.
  • Loading branch information
nicokosi committed Jul 6, 2020
1 parent 62092d1 commit 5646258
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 47 deletions.
51 changes: 23 additions & 28 deletions src/main/kotlin/pullpitok/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,19 @@ import pullpitok.github.Type
import kotlin.system.exitProcess

fun main(args: Array<String>) {
loadLibSunec()
if (!checkArgs(args)) exitProcess(0)
if (invalidArguments(args)) {
println(usage)
exitProcess(0)
}
val repos = args[0].split(",")
val token = args.getOrNull(1)
repos.parallelStream()
.forEach { repo ->
displayEvents(repo, token)
}
}
private fun loadLibSunec() {
System.setProperty("java.library.path", System.getenv("JAVA_HOME"))
repos
.parallelStream()
.forEach { displayEvents(it, token) }
}

fun checkArgs(args: Array<String>): Boolean =
if (args.size !in (1..2) || args[0] in setOf("", "-h", "--help")) {
println("""Usage: pullpitoK <repositories> <token>
A command line tool to display a summary of GitHub pull requests.
<repositories>: comma-separated list of GitHub repositories
Examples:
python/peps
python/peps,haskell/rfcs
<token>: an optional GitHub personal access token""")
false
} else true
internal fun invalidArguments(args: Array<String>): Boolean =
args.size !in (1..2) || args[0] in setOf("", "-h", "--help")

private fun displayEvents(repo: String, token: String?) {
val allEvents = mutableListOf<Event>()
Expand All @@ -53,13 +39,11 @@ private fun displayEvents(repo: String, token: String?) {
""")
}

fun perAuthor(events: List<Event>): Map<String, List<Event>> = events
.filter {
it.type in Type.values().map(Type::name)
}
private fun perAuthor(events: List<Event>): Map<String, List<Event>> = events
.filter { it.type in Type.values().map(Type::name) }
.groupBy { it.actor.login }

fun counters(
internal fun counters(
eventsPerAuthor: Map<String, List<Event>>,
predicate: (Event) -> Boolean): String {
eventsPerAuthor.entries
Expand All @@ -74,3 +58,14 @@ fun counters(
}
return counters
}

val usage = """Usage: pullpitoK <repositories> <token>
A command line tool to display a summary of GitHub pull requests.
<repositories>: comma-separated list of GitHub repositories
Examples:
python/peps
python/peps,haskell/rfcs
<token>: an optional GitHub personal access token"""
2 changes: 1 addition & 1 deletion src/main/kotlin/pullpitok/github/Event.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ enum class Action {
enum class Type {
PullRequestEvent,
PullRequestReviewCommentEvent
}
}
19 changes: 7 additions & 12 deletions src/main/kotlin/pullpitok/github/EventClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,15 @@ class EventClient {

fun events(json: String): List<Event> {
return ObjectMapper().readTree(json)
.map { node ->
.map {
Event(
node.get("id").asText(),
node.get("type").asText(),
Actor(
node.get("actor").get("login").asText()),
Payload(
node?.get("payload")?.get("action")?.asText().orEmpty())
)
it.get("id").asText(),
it.get("type").asText(),
Actor(it.get("actor").get("login").asText()),
Payload(it?.get("payload")?.get("action")?.asText().orEmpty()))
}
}

private fun fail(message: String): Nothing {
throw IllegalArgumentException(message)
}
private fun fail(message: String): Nothing = throw IllegalArgumentException(message)

}
}
12 changes: 6 additions & 6 deletions src/test/kotlin/pullpitok/AppTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ class AppTest {

@Test
fun `show usage`() {
assertFalse(checkArgs(arrayOf("")))
assertFalse(checkArgs(arrayOf("-h")))
assertFalse(checkArgs(arrayOf("--help")))
assertFalse(checkArgs(arrayOf("one", "two", "three")))
assertTrue(invalidArguments(arrayOf("")))
assertTrue(invalidArguments(arrayOf("-h")))
assertTrue(invalidArguments(arrayOf("--help")))
assertTrue(invalidArguments(arrayOf("one", "two", "three")))
}

@Test
fun `do not show usage`() {
assertTrue(checkArgs(arrayOf("org/repo")))
assertTrue(checkArgs(arrayOf("org/repo", "token")))
assertFalse(invalidArguments(arrayOf("org/repo")))
assertFalse(invalidArguments(arrayOf("org/repo", "token")))
}

}

0 comments on commit 5646258

Please sign in to comment.