28 lines
1.0 KiB
Kotlin
28 lines
1.0 KiB
Kotlin
package io.gitlab.jfronny.scripts
|
|
|
|
enum class CommitType {
|
|
FIX, FEAT, BREAKING;
|
|
|
|
companion object {
|
|
private val pattern: Regex = Regex("([a-zA-Z ]+)(?:\\([a-zA-Z0-9 -]+\\))?(!)?: .+", RegexOption.DOT_MATCHES_ALL)
|
|
|
|
fun from(commitMessage: String, warn: (String) -> Unit): CommitType {
|
|
val match = pattern.matchEntire(commitMessage)
|
|
if (match != null) {
|
|
val m = match.groupValues
|
|
if (m[2] == "!") return BREAKING
|
|
return when (m[1].lowercase()) {
|
|
"fix", "build", "chore", "ci", "docs", "style", "refactor", "perf", "test" -> FIX
|
|
"feat" -> FEAT
|
|
"breaking change", "breaking" -> BREAKING
|
|
else -> {
|
|
warn("Unrecognized commit type: ${m[1]}, guessing FEAT")
|
|
FEAT
|
|
}
|
|
}
|
|
}
|
|
warn("Could not parse commit, guessing type is FEAT: $commitMessage")
|
|
return FEAT
|
|
}
|
|
}
|
|
} |