Skip to content

Commit

Permalink
feature: Added CHANGELOG_GITHUB_TAG_PATTERN for specifying tags to ma…
Browse files Browse the repository at this point in the history
…tch (#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
  • Loading branch information
morten-andersen authored May 23, 2023
1 parent 8456048 commit c63d3bd
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 13 deletions.
14 changes: 8 additions & 6 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
plugins {
kotlin("multiplatform") version "1.8.10"
kotlin("plugin.serialization") version "1.8.10"
id("io.kotest.multiplatform") version "5.5.4"
kotlin("multiplatform") version "1.8.21"
kotlin("plugin.serialization") version "1.8.21"
id("io.kotest.multiplatform") version "5.6.2"
id("org.jlleitschuh.gradle.ktlint") version "11.3.1"
}

group = "com.monta.gradle.changelog"
version = "1.5.0"
version = "1.6.0"

repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
}

defaultTasks("commonBinaries")

kotlin {

val hostOs = System.getProperty("os.name")
Expand Down Expand Up @@ -44,7 +46,7 @@ kotlin {
// Serialization
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.0")
// Atomic
implementation("org.jetbrains.kotlinx:atomicfu:0.20.0")
implementation("org.jetbrains.kotlinx:atomicfu:0.20.2")
// Http Client
val ktorVersion = "2.2.4"
implementation("io.ktor:ktor-client-core:$ktorVersion")
Expand All @@ -55,7 +57,7 @@ kotlin {
}
val commonTest by getting {
dependencies {
val kotestVersion = "5.5.4"
val kotestVersion = "5.6.1"
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
implementation("io.kotest:kotest-framework-engine:$kotestVersion")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ class GenerateChangeLogCommand : CliktCommand() {
envvar = "CHANGELOG_VERSION_MODE"
)

private val tagPattern: String? by option(
help = "Regex pattern used for matching tag patterns (group 1 in the pattern should match the 'version')",
envvar = "CHANGELOG_GITHUB_TAG_PATTERN"
)

private val pathExcludePattern: String? by option(
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",
envvar = "CHANGELOG_GITHUB_PATH_EXCLUDE_PATTERN"
)

private val output: PrintingConfig by option(
help = "Name of the output used for printing the log (defaults to console)",
envvar = "CHANGELOG_OUTPUT"
Expand All @@ -82,7 +92,9 @@ class GenerateChangeLogCommand : CliktCommand() {
jiraAppName = jiraAppName,
tagSorter = versionMode.sorter,
githubRelease = githubRelease,
githubToken = githubToken
githubToken = githubToken,
tagPattern = tagPattern,
pathExcludePattern = pathExcludePattern
)

val commitShaOptions = commitShaOptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ internal class GitCommandUtil {
}
}

fun getFilesInCommit(commitId: String): List<String> {
return executeCommand("git diff-tree --no-commit-id --name-only $commitId -r").map { file ->
file.trim()
}
}

fun getLogs(): List<LogItem> {
return executeCommand(
buildString {
Expand Down
26 changes: 24 additions & 2 deletions src/commonMain/kotlin/com.monta.changelog/git/GitService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ import kotlinx.datetime.TimeZone
import kotlinx.datetime.toLocalDateTime

class GitService(
private val tagSorter: TagSorter
private val tagSorter: TagSorter,
tagPattern: String?,
pathExcludePattern: String?
) {

private val gitCommandUtil = GitCommandUtil()
private val commitMapper = CommitMapper()
private val tagPattern = tagPattern?.let(::Regex)
private val pathExcludePattern = pathExcludePattern?.let(::Regex)

fun getRepoInfo(): RepoInfo {
val (repoOwner, repoName) = getGitOwnerAndRepo()
Expand All @@ -29,6 +33,16 @@ class GitService(
fun getCommits(): CommitInfo {
val tags = tagSorter.sort(
tags = gitCommandUtil.getTags()
.mapNotNull { tag ->
when (tagPattern) {
null -> tag
else -> {
// there is a tag pattern, extract group 1
val match = tagPattern.matchEntire(tag)
if (match != null) match.groups[1]?.value else null
}
}
}
)

DebugLogger.info("found tags: $tags")
Expand Down Expand Up @@ -64,7 +78,15 @@ class GitService(
}

private fun List<LogItem>.mapToCommits(): List<Commit> {
return this.mapNotNull { gitLogItem ->
return this.filter { gitLogItem ->
when (pathExcludePattern) {
null -> true
else -> {
val filesInCommit = gitCommandUtil.getFilesInCommit(gitLogItem.commit)
filesInCommit.any { !pathExcludePattern.containsMatchIn(it) }
}
}
}.mapNotNull { gitLogItem ->
commitMapper.fromGitLogItem(gitLogItem)
}.toSet().toList()
}
Expand Down
12 changes: 10 additions & 2 deletions src/commonMain/kotlin/com.monta.changelog/log/ChangeLogService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ class ChangeLogService(
private val jiraAppName: String?,
tagSorter: TagSorter,
private val githubRelease: Boolean,
githubToken: String?
githubToken: String?,
tagPattern: String?,
pathExcludePattern: String?
) {

private val gitService = GitService(tagSorter)
private val gitService = GitService(tagSorter, tagPattern, pathExcludePattern)
private val gitHubService = GitHubService(githubToken)
private val repoInfo = gitService.getRepoInfo()
private val linkResolvers = listOf(
Expand All @@ -44,6 +46,12 @@ class ChangeLogService(
DebugLogger.info("githubRelease $githubRelease")
DebugLogger.info("repoOwner ${repoInfo.repoOwner}")
DebugLogger.info("repoName ${repoInfo.repoName}")
if (tagPattern != null) {
DebugLogger.info("tagPattern $tagPattern")
}
if (pathExcludePattern != null) {
DebugLogger.info("pathExclude $pathExcludePattern")
}
}

suspend fun generate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import com.monta.changelog.model.ChangeLog
import com.monta.changelog.model.Commit
import com.monta.changelog.model.ConventionalCommitType
import com.monta.changelog.printer.ChangeLogPrinter
import com.monta.changelog.util.DebugLogger
import com.monta.changelog.util.LinkResolver
import com.monta.changelog.util.MarkdownFormatter
import com.monta.changelog.util.client
import com.monta.changelog.util.getBodySafe
import com.monta.changelog.util.resolve
import io.ktor.client.request.*
import io.ktor.http.*
import io.ktor.utils.io.charsets.*

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

return response.getBodySafe<SlackMessageResponse>()?.ts
val result = response.getBodySafe<SlackMessageResponse>()?.ts
if (result == null) {
DebugLogger.error("Could not post message to slack channel '${slackMessageRequest.channel}'")
}
return result
}
}

0 comments on commit c63d3bd

Please sign in to comment.