Skip to content

Commit c63d3bd

Browse files
feature: Added CHANGELOG_GITHUB_TAG_PATTERN for specifying tags to match (#74)
* fix: Add charset on slack POST `ContentType` * feature: Added `CHANGELOG_GITHUB_TAG_PATTERN` for specifying tags to match * it must be a regex where group 1 should be the version (semver or date) * chore: upgrade deps * feature: Added path exclude pattern. If all files in a commit is matched by this pattern they will not be part of the change log * ignore: Stylistic * chore: Upgrade io.kotest.multiplatform 5.6.1 -> 5.6.2
1 parent 8456048 commit c63d3bd

File tree

6 files changed

+69
-13
lines changed

6 files changed

+69
-13
lines changed

build.gradle.kts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
plugins {
2-
kotlin("multiplatform") version "1.8.10"
3-
kotlin("plugin.serialization") version "1.8.10"
4-
id("io.kotest.multiplatform") version "5.5.4"
2+
kotlin("multiplatform") version "1.8.21"
3+
kotlin("plugin.serialization") version "1.8.21"
4+
id("io.kotest.multiplatform") version "5.6.2"
55
id("org.jlleitschuh.gradle.ktlint") version "11.3.1"
66
}
77

88
group = "com.monta.gradle.changelog"
9-
version = "1.5.0"
9+
version = "1.6.0"
1010

1111
repositories {
1212
// Use Maven Central for resolving dependencies.
1313
mavenCentral()
1414
}
1515

16+
defaultTasks("commonBinaries")
17+
1618
kotlin {
1719

1820
val hostOs = System.getProperty("os.name")
@@ -44,7 +46,7 @@ kotlin {
4446
// Serialization
4547
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.0")
4648
// Atomic
47-
implementation("org.jetbrains.kotlinx:atomicfu:0.20.0")
49+
implementation("org.jetbrains.kotlinx:atomicfu:0.20.2")
4850
// Http Client
4951
val ktorVersion = "2.2.4"
5052
implementation("io.ktor:ktor-client-core:$ktorVersion")
@@ -55,7 +57,7 @@ kotlin {
5557
}
5658
val commonTest by getting {
5759
dependencies {
58-
val kotestVersion = "5.5.4"
60+
val kotestVersion = "5.6.1"
5961
implementation(kotlin("test-common"))
6062
implementation(kotlin("test-annotations-common"))
6163
implementation("io.kotest:kotest-framework-engine:$kotestVersion")

src/commonMain/kotlin/com.monta.changelog/GenerateChangeLogCommand.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ class GenerateChangeLogCommand : CliktCommand() {
5858
envvar = "CHANGELOG_VERSION_MODE"
5959
)
6060

61+
private val tagPattern: String? by option(
62+
help = "Regex pattern used for matching tag patterns (group 1 in the pattern should match the 'version')",
63+
envvar = "CHANGELOG_GITHUB_TAG_PATTERN"
64+
)
65+
66+
private val pathExcludePattern: String? by option(
67+
help = "Regex pattern used for matching file patch for which commits should not be included. I.e. if a commit only contains files that match this, it will not be in the change log",
68+
envvar = "CHANGELOG_GITHUB_PATH_EXCLUDE_PATTERN"
69+
)
70+
6171
private val output: PrintingConfig by option(
6272
help = "Name of the output used for printing the log (defaults to console)",
6373
envvar = "CHANGELOG_OUTPUT"
@@ -82,7 +92,9 @@ class GenerateChangeLogCommand : CliktCommand() {
8292
jiraAppName = jiraAppName,
8393
tagSorter = versionMode.sorter,
8494
githubRelease = githubRelease,
85-
githubToken = githubToken
95+
githubToken = githubToken,
96+
tagPattern = tagPattern,
97+
pathExcludePattern = pathExcludePattern
8698
)
8799

88100
val commitShaOptions = commitShaOptions

src/commonMain/kotlin/com.monta.changelog/git/GitCommandUtil.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ internal class GitCommandUtil {
4343
}
4444
}
4545

46+
fun getFilesInCommit(commitId: String): List<String> {
47+
return executeCommand("git diff-tree --no-commit-id --name-only $commitId -r").map { file ->
48+
file.trim()
49+
}
50+
}
51+
4652
fun getLogs(): List<LogItem> {
4753
return executeCommand(
4854
buildString {

src/commonMain/kotlin/com.monta.changelog/git/GitService.kt

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@ import kotlinx.datetime.TimeZone
88
import kotlinx.datetime.toLocalDateTime
99

1010
class GitService(
11-
private val tagSorter: TagSorter
11+
private val tagSorter: TagSorter,
12+
tagPattern: String?,
13+
pathExcludePattern: String?
1214
) {
1315

1416
private val gitCommandUtil = GitCommandUtil()
1517
private val commitMapper = CommitMapper()
18+
private val tagPattern = tagPattern?.let(::Regex)
19+
private val pathExcludePattern = pathExcludePattern?.let(::Regex)
1620

1721
fun getRepoInfo(): RepoInfo {
1822
val (repoOwner, repoName) = getGitOwnerAndRepo()
@@ -29,6 +33,16 @@ class GitService(
2933
fun getCommits(): CommitInfo {
3034
val tags = tagSorter.sort(
3135
tags = gitCommandUtil.getTags()
36+
.mapNotNull { tag ->
37+
when (tagPattern) {
38+
null -> tag
39+
else -> {
40+
// there is a tag pattern, extract group 1
41+
val match = tagPattern.matchEntire(tag)
42+
if (match != null) match.groups[1]?.value else null
43+
}
44+
}
45+
}
3246
)
3347

3448
DebugLogger.info("found tags: $tags")
@@ -64,7 +78,15 @@ class GitService(
6478
}
6579

6680
private fun List<LogItem>.mapToCommits(): List<Commit> {
67-
return this.mapNotNull { gitLogItem ->
81+
return this.filter { gitLogItem ->
82+
when (pathExcludePattern) {
83+
null -> true
84+
else -> {
85+
val filesInCommit = gitCommandUtil.getFilesInCommit(gitLogItem.commit)
86+
filesInCommit.any { !pathExcludePattern.containsMatchIn(it) }
87+
}
88+
}
89+
}.mapNotNull { gitLogItem ->
6890
commitMapper.fromGitLogItem(gitLogItem)
6991
}.toSet().toList()
7092
}

src/commonMain/kotlin/com.monta.changelog/log/ChangeLogService.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ class ChangeLogService(
1616
private val jiraAppName: String?,
1717
tagSorter: TagSorter,
1818
private val githubRelease: Boolean,
19-
githubToken: String?
19+
githubToken: String?,
20+
tagPattern: String?,
21+
pathExcludePattern: String?
2022
) {
2123

22-
private val gitService = GitService(tagSorter)
24+
private val gitService = GitService(tagSorter, tagPattern, pathExcludePattern)
2325
private val gitHubService = GitHubService(githubToken)
2426
private val repoInfo = gitService.getRepoInfo()
2527
private val linkResolvers = listOf(
@@ -44,6 +46,12 @@ class ChangeLogService(
4446
DebugLogger.info("githubRelease $githubRelease")
4547
DebugLogger.info("repoOwner ${repoInfo.repoOwner}")
4648
DebugLogger.info("repoName ${repoInfo.repoName}")
49+
if (tagPattern != null) {
50+
DebugLogger.info("tagPattern $tagPattern")
51+
}
52+
if (pathExcludePattern != null) {
53+
DebugLogger.info("pathExclude $pathExcludePattern")
54+
}
4755
}
4856

4957
suspend fun generate(

src/commonMain/kotlin/com.monta.changelog/printer/slack/SlackChangeLogPrinter.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import com.monta.changelog.model.ChangeLog
44
import com.monta.changelog.model.Commit
55
import com.monta.changelog.model.ConventionalCommitType
66
import com.monta.changelog.printer.ChangeLogPrinter
7+
import com.monta.changelog.util.DebugLogger
78
import com.monta.changelog.util.LinkResolver
89
import com.monta.changelog.util.MarkdownFormatter
910
import com.monta.changelog.util.client
1011
import com.monta.changelog.util.getBodySafe
1112
import com.monta.changelog.util.resolve
1213
import io.ktor.client.request.*
1314
import io.ktor.http.*
15+
import io.ktor.utils.io.charsets.*
1416

1517
class SlackChangeLogPrinter(
1618
private val slackToken: String,
@@ -116,10 +118,14 @@ class SlackChangeLogPrinter(
116118
private suspend fun makeRequest(slackMessageRequest: SlackMessageRequest): String? {
117119
val response = client.post("https://slack.com/api/chat.postMessage") {
118120
header("Authorization", "Bearer $slackToken")
119-
contentType(ContentType.Application.Json)
121+
contentType(ContentType.Application.Json.withParameter("charset", Charsets.UTF_8.name))
120122
setBody(slackMessageRequest)
121123
}
122124

123-
return response.getBodySafe<SlackMessageResponse>()?.ts
125+
val result = response.getBodySafe<SlackMessageResponse>()?.ts
126+
if (result == null) {
127+
DebugLogger.error("Could not post message to slack channel '${slackMessageRequest.channel}'")
128+
}
129+
return result
124130
}
125131
}

0 commit comments

Comments
 (0)