Skip to content

Commit

Permalink
Introduce message pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
goodwinnk committed Nov 22, 2017
1 parent 36c171c commit 2673be1
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 24 deletions.
44 changes: 30 additions & 14 deletions core/src/main/kotlin/extract/core/engine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,41 @@ fun assignLabels(commitInfo: CommitInfo, extracts: Extracts): List<ExtractLabel>

fun assignLabel(commitInfo: CommitInfo, extract: Extract): ExtractLabel? {
val values = MutablePredefinedVariables(commitInfo)
val titlePattern = extract.titlePattern
if (titlePattern != null) {
val titleCompiledPattern = Pattern.compile(titlePattern)
val matcher = titleCompiledPattern.matcher(commitInfo.title)
if (matcher.find()) {
return extract.createExtractLabel(matcher, values)

run {
val titlePattern = extract.titlePattern
if (titlePattern != null) {
val titleCompiledPattern = Pattern.compile(titlePattern)
val matcher = titleCompiledPattern.matcher(commitInfo.title)
if (matcher.find()) {
return extract.createExtractLabel(matcher, values)
}
}
}

if (extract.files.isNotEmpty()) {
val isActionMatches = if (extract.hasVariable(PredefinedVariables.MATCHES)) {
values.matches = commitInfo.fileActions.count { fileAction -> pathMatch(fileAction.path, extract.files) }
values.matches != 0
} else {
commitInfo.fileActions.any { fileAction -> pathMatch(fileAction.path, extract.files) }
run {
val messagePattern = extract.messagePattern
if (messagePattern != null) {
val messageCompiledPattern = Pattern.compile(messagePattern, Pattern.DOTALL)
val matcher = messageCompiledPattern.matcher(commitInfo.message)
if (matcher.find()) {
return extract.createExtractLabel(matcher, values)
}
}
}

if (isActionMatches) {
return extract.createExtractLabel(null, values)
run {
if (extract.files.isNotEmpty()) {
val isActionMatches = if (extract.hasVariable(PredefinedVariables.MATCHES)) {
values.matches = commitInfo.fileActions.count { fileAction -> pathMatch(fileAction.path, extract.files) }
values.matches != 0
} else {
commitInfo.fileActions.any { fileAction -> pathMatch(fileAction.path, extract.files) }
}

if (isActionMatches) {
return extract.createExtractLabel(null, values)
}
}
}

Expand Down
1 change: 1 addition & 0 deletions core/src/main/kotlin/extract/core/extracts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ data class Extract(
val name: String,

val titlePattern: String? = null,
val messagePattern: String? = null,
val files: List<String> = listOf(),

val icon: String? = null,
Expand Down
13 changes: 9 additions & 4 deletions core/src/main/kotlin/extract/core/parser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ private class ExtractInternal {

@JsonProperty("title-pattern")
var titlePattern: String? = null
@JsonProperty("message-pattern")
var messagePattern: String? = null
var files: List<String> = listOf()

var text: String? = null
Expand All @@ -27,16 +29,19 @@ private class ExtractInternal {
var style: String? = null
var badge: String? = null

override fun toString(): String {
return "$name, $titlePattern, $files, $icon, $text, $hint, $url, $style, $badge"
}
override fun toString() =
"$name, $titlePattern, $messagePattern, $files, $icon, $text, $hint, $url, $style, $badge"

fun toExtract(): Extract {
if (name == null) {
throw IllegalStateException("Extract doesn't have name: $this")
}

return Extract(name!!, titlePattern, files,
return Extract(
name!!,
titlePattern = titlePattern,
messagePattern = messagePattern,
files = files,
icon = icon,
text = text,
hint = hint ?: text,
Expand Down
46 changes: 45 additions & 1 deletion core/src/test/kotlin/extract/core/EngineKtTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal class EngineKtTest {
fun assignLabelWithPatterns() {
val label = assignLabel(
testCommit(title = "Open created actual class in editor #KT-20135 Fixed"),
Extract("YouTrack", "^.*(KT-\\d+).*$", listOf(),
Extract("YouTrack", "^.*(KT-\\d+).*$", null, listOf(),
"path", "\${1}", "\${0}", "https://youtrack.jetbrains.com/issue/\${1}")
)

Expand All @@ -22,7 +22,51 @@ internal class EngineKtTest {
badges = listOf()),
label
)
}

@Test
fun assignLabelWithMessagePattern() {
val title = "Open created actual class in editor"
val label = assignLabel(
testCommit(hash = "123", title = title, message = "$title\n\n #KT-20135 Fixed"),
Extract("YouTrack", null, "^.*(KT-\\d+).*$", listOf(),
"path", "\${1}", "\${1}", "https://youtrack.jetbrains.com/issue/\${1}")
)

Assert.assertEquals(
ExtractLabel(
"YouTrack", "KT-20135",
hint = "KT-20135",
icon = "path",
url = "https://youtrack.jetbrains.com/issue/KT-20135",
style = null,
badges = listOf()),
label
)
}

@Test
fun assignLabelByBothPatterns() {
val title = "Open created actual class in editor"
val extract =
Extract("Some",
titlePattern = "^.*(first).*$",
messagePattern = "^.*(second).*$",
text = "\${1}")

Assert.assertEquals(
ExtractLabel(name = "Some", text = "first", icon = null, hint = null, url = null, style = null, badges = listOf()),
assignLabel(
testCommit(hash = "123", title = "bla bla bla first", message = "bla bla bla first\n\nfoo foo foo first second"),
extract)
)

Assert.assertEquals(
ExtractLabel(name = "Some", text = "second", icon = null, hint = null, url = null, style = null, badges = listOf()),
assignLabel(
testCommit(hash = "345", title = "bla bla bla", message = "bla bla bla\n\nfoo foo foo first second"),
extract)
)
}

@Test
Expand Down
17 changes: 12 additions & 5 deletions core/src/test/kotlin/extract/core/ParserKtTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,33 @@ class ParserKtTest {
@Test
fun example() {
val extracts = parseFile("src/test/resources/test.yaml").extracts
assertEquals(4, extracts.size)
assertEquals(5, extracts.size)

assertEquals(
Extract("YouTrack", "^.*(KT-\\d+).*$", listOf(),
Extract("YouTrack", "^.*(KT-\\d+).*$", null, listOf(),
icon = "path", text = "\${1}", hint = "\${1}", url = "https://youtrack.jetbrains.com/issue/\${1}"),
extracts[0])

assertEquals(
Extract("IDE", null, listOf("idea/**")),
Extract("IDE", null, null, listOf("idea/**")),
extracts[1]
)

assertEquals(
Extract("Minor", null, listOf(), style = "e1"),
Extract("Minor", null, null, listOf(), style = "e1"),
extracts[2]
)

assertEquals(
Extract("WithBadge", null, listOf(), badge = "\${matched}"),
Extract("WithBadge", null, null, listOf(), badge = "\${matched}"),
extracts[3]
)

assertEquals(
Extract("WithMessagePattern",
titlePattern = null, messagePattern = "^.*(KT-\\d+).*$",
files = listOf(), badge = null),
extracts[4]
)
}
}
3 changes: 3 additions & 0 deletions core/src/test/resources/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ extracts:

- name: WithBadge
badge: "${matched}"

- name: WithMessagePattern
message-pattern: "^.*(KT-\\d+).*$"

0 comments on commit 2673be1

Please sign in to comment.