-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add utility file for managing the regex
- Loading branch information
1 parent
c6927b9
commit 62e9500
Showing
2 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
src/main/kotlin/it/nicolasfarabegoli/gradle/CommitRegex.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package it.nicolasfarabegoli.gradle | ||
|
||
private val types = listOf("build", "chore", "ci", "docs", "feat", "fix", "perf", "refactor", "revert", "style", "test") | ||
|
||
fun buildRegex(listOfScopes: List<String> = emptyList()): Regex { | ||
return if (listOfScopes.isEmpty()) { | ||
"^(${types.joinToString("|")})(\\([a-z \\-]+\\))?!?: .+\$".toRegex() | ||
} else { | ||
"^(${types.joinToString("|")})\\((${listOfScopes.joinToString("|")})\\)?!?: .+\$".toRegex() | ||
} | ||
} | ||
|
||
val scriptContent = """ | ||
#!/usr/bin/env bash | ||
# Create a regex for a conventional commit. | ||
conventional_commit_regex="{0}" | ||
# Get the commit message (the parameter we're given is just the path to the | ||
# temporary file which holds the message). | ||
commit_message=${'$'}(cat "${'$'}1") | ||
# Check the message, if we match, all good baby. | ||
if [[ "${'$'}commit_message" =~ ${'$'}conventional_commit_regex ]]; then | ||
echo -e "\e[32mCommit message meets Conventional Commit standards...\e[0m" | ||
exit 0 | ||
fi | ||
# Uh-oh, this is not a conventional commit, show an example and link to the spec. | ||
echo -e "\e[31mThe commit message does not meet the Conventional Commit standard\e[0m" | ||
echo "An example of a valid message is: " | ||
echo " feat(login): add the 'remember me' button" | ||
echo "More details at: https://www.conventionalcommits.org/en/v1.0.0/#summary" | ||
exit 1 | ||
""".trimIndent() |
20 changes: 20 additions & 0 deletions
20
src/test/kotlin/it/nicolasfarabegoli/gradle/CommitRegexKtTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package it.nicolasfarabegoli.gradle | ||
|
||
import io.kotest.core.spec.style.WordSpec | ||
import io.kotest.matchers.shouldBe | ||
|
||
class CommitRegexKtTest : WordSpec({ | ||
"A commit message" should { | ||
"admit any scope if no scopes are given" { | ||
buildRegex().matches("feat(scope): foo bar") shouldBe true | ||
buildRegex().matches("foo bar") shouldBe false | ||
buildRegex().matches("feat: foo bar") shouldBe true | ||
} | ||
"not admit scopes not allowed" { | ||
val scopes = listOf("scope1", "scope2", "scope3") | ||
buildRegex(scopes).matches("feat(scope1): foo bar") shouldBe true | ||
buildRegex(scopes).matches("feat: foo bar") shouldBe false | ||
buildRegex(scopes).matches("feat(scope4): foo bar") shouldBe false | ||
} | ||
} | ||
}) |