Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Ktlint code linter via jlleitschuh/ktlint-gradle Gradle plugin #35

Closed
wants to merge 2 commits into from
Closed
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
61 changes: 33 additions & 28 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/pullpitoK

# Created by https://www.gitignore.io/api/gradle,kotlin,eclipse,intellij,visualstudiocode
# Edit at https://www.gitignore.io/?templates=gradle,kotlin,eclipse,intellij,visualstudiocode
# Created by https://www.toptal.com/developers/gitignore/api/gradle,kotlin,eclipse,intellij+all,visualstudiocode
# Edit at https://www.toptal.com/developers/gitignore?templates=gradle,kotlin,eclipse,intellij+all,visualstudiocode

### Eclipse ###

.metadata
bin/
tmp/
Expand Down Expand Up @@ -54,26 +54,23 @@ local.properties

# Annotation Processing
.apt_generated/
.apt_generated_test/

# Scala IDE specific (Scala & Java development for Eclipse)
.cache-main
.scala_dependencies
.worksheet

### Eclipse Patch ###
# Eclipse Core
.project

# JDT-specific (Eclipse Java Development Tools)
.classpath

# Annotation Processing
.apt_generated
# Uncomment this line if you wish to ignore the project description file.
# Typically, this file would be tracked if it contains build/dependency configurations:
#.project

### Eclipse Patch ###
# Spring Boot Tooling
.sts4-cache/

### Intellij ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
### Intellij+all ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff
Expand Down Expand Up @@ -103,9 +100,14 @@ local.properties
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
.idea/modules.xml
.idea/*.iml
.idea/modules
# .idea/artifacts
# .idea/compiler.xml
# .idea/jarRepositories.xml
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr

# CMake
cmake-build-*/
Expand Down Expand Up @@ -140,13 +142,18 @@ fabric.properties
# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser

### Intellij Patch ###
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
### Intellij+all Patch ###
# Ignores the whole .idea folder and all .iml files
# See https://github.com/joeblau/gitignore.io/issues/186 and https://github.com/joeblau/gitignore.io/issues/360

# *.iml
# modules.xml
# .idea/misc.xml
# *.ipr
.idea/

# Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-249601023

*.iml
modules.xml
.idea/misc.xml
*.ipr

# Sonarlint plugin
.idea/sonarlint
Expand Down Expand Up @@ -182,14 +189,15 @@ hs_err_pid*
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
*.code-workspace

### VisualStudioCode Patch ###
# Ignore all local history of files
.history

### Gradle ###
.gradle
/build/
build/

# Ignore Gradle GUI config
gradle-app.setting
Expand All @@ -206,7 +214,4 @@ gradle-app.setting
### Gradle Patch ###
**/build/

# End of https://www.gitignore.io/api/gradle,kotlin,eclipse,intellij,visualstudiocode
/.idea/sonarIssues.xml
/pullpitoK
/libsunec.*
# End of https://www.toptal.com/developers/gitignore/api/gradle,kotlin,eclipse,intellij+all,visualstudiocode
5 changes: 0 additions & 5 deletions .idea/codeStyles/codeStyleConfig.xml

This file was deleted.

16 changes: 0 additions & 16 deletions .idea/compiler.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/encodings.xml

This file was deleted.

25 changes: 0 additions & 25 deletions .idea/jarRepositories.xml

This file was deleted.

10 changes: 0 additions & 10 deletions .idea/misc.xml

This file was deleted.

11 changes: 0 additions & 11 deletions .idea/vcs.xml

This file was deleted.

1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ plugins {
id("com.github.ben-manes.versions") version "0.28.0"
id("com.github.johnrengelman.shadow") version "5.2.0"
id("com.adarshr.test-logger") version "2.0.0"
id("org.jlleitschuh.gradle.ktlint") version "9.2.1"
application
}

Expand Down
41 changes: 25 additions & 16 deletions src/main/kotlin/pullpitok/App.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package pullpitok

import kotlin.system.exitProcess
import pullpitok.github.Action
import pullpitok.github.Event
import pullpitok.github.EventClient
import pullpitok.github.Type
import kotlin.system.exitProcess

fun main(args: Array<String>) {
if (invalidArguments(args)) {
Expand All @@ -13,13 +13,14 @@ fun main(args: Array<String>) {
}
val repos = args[0].split(",")
val token = args.getOrNull(1)
repos
.parallelStream()
.forEach { displayEvents(it, token) }
repos.parallelStream()
.forEach {
displayEvents(it, token)
}
}

internal fun invalidArguments(args: Array<String>): Boolean =
args.size !in (1..2) || args[0] in setOf("", "-h", "--help")
args.size !in (1..2) || args[0] in setOf("", "-h", "--help")

private fun displayEvents(repo: String, token: String?) {
val allEvents = mutableListOf<Event>()
Expand All @@ -29,30 +30,38 @@ private fun displayEvents(repo: String, token: String?) {
else break
}
val eventsPerAuthor = perAuthor(allEvents)
val opened: (Event) -> Boolean = { it.type == Type.PullRequestEvent.name && it.payload.action == Action.opened.name }
val commented: (Event) -> Boolean = { it.type == Type.PullRequestReviewCommentEvent.name && it.payload.action == Action.created.name }
val closed: (Event) -> Boolean = { it.type == Type.PullRequestEvent.name && it.payload.action == Action.closed.name }
println("""pull requests for "$repo" ->
val opened: (Event) -> Boolean =
{ it.type == Type.PullRequestEvent.name && it.payload.action == Action.opened.name }
val commented: (Event) -> Boolean =
{ it.type == Type.PullRequestReviewCommentEvent.name && it.payload.action == Action.created.name }
val closed: (Event) -> Boolean =
{ it.type == Type.PullRequestEvent.name && it.payload.action == Action.closed.name }
println(
"""pull requests for "$repo" ->
opened per author ${counters(eventsPerAuthor, opened)}
commented per author ${counters(eventsPerAuthor, commented)}
closed per author ${counters(eventsPerAuthor, closed)}
""")
"""
)
}

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

internal fun counters(
eventsPerAuthor: Map<String, List<Event>>,
predicate: (Event) -> Boolean): String {
eventsPerAuthor: Map<String, List<Event>>,
predicate: (Event) -> Boolean
): String {
eventsPerAuthor.entries
var counters = ""
for (events in eventsPerAuthor.entries) {
val author = events.key
val count = events.value
.filter(predicate)
.count()
.filter(predicate)
.count()
if (count > 0)
counters += "\n\t\t$author: $count"
}
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/pullpitok/github/Event.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ enum class Action {
closed,
opened
}

enum class Type {
PullRequestEvent,
PullRequestReviewCommentEvent
Expand Down
9 changes: 4 additions & 5 deletions src/main/kotlin/pullpitok/github/EventClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ class EventClient {
fun githubEvents(repo: String, token: String?, page: Int): List<Event> {
val url = "https://api.github.com/repos/$repo/events?page=$page"
val request = HttpRequest.newBuilder()
.timeout(Duration.ofSeconds(30))
.header("Content-Type", "application/json")
.uri(URI.create(url))
.timeout(Duration.ofSeconds(30))
.header("Content-Type", "application/json")
.uri(URI.create(url))
if (token != null) {
request.header("Authorization", "token $token")
}
}
val response = client.send(request.build(), BodyHandlers.ofString())
if (response.statusCode() != 200) {
fail("Status code is ${response.statusCode()} for $url")
Expand All @@ -39,5 +39,4 @@ class EventClient {
}

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

}
15 changes: 7 additions & 8 deletions src/test/kotlin/pullpitok/AppTest.kt
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
package pullpitok

import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue
import org.junit.Test
import pullpitok.github.Actor
import pullpitok.github.Event
import pullpitok.github.Payload
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue

class AppTest {

@Test
fun `no counters`() {
assertEquals(
"",
counters(emptyMap()) { true })
"",
counters(emptyMap()) { true })
}

@Test
fun `one counter`() {
val event = Event(id = "1", type = "PullRequestEvent", actor = Actor("alice"), payload = Payload("opened"))
assertEquals(
"\n\t\tauthor: 1",
counters(mapOf("author" to listOf(event))) { true })
"\n\t\tauthor: 1",
counters(mapOf("author" to listOf(event))) { true })
}

@Test
Expand All @@ -38,5 +38,4 @@ class AppTest {
assertFalse(invalidArguments(arrayOf("org/repo")))
assertFalse(invalidArguments(arrayOf("org/repo", "token")))
}

}
Loading