From 2187a25d01806ca43b15be73e6fecf188b93d6ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20Z=C3=B8rn?= Date: Fri, 3 May 2024 15:02:40 +0200 Subject: [PATCH 1/2] fix(action): Change the way some fields are populated in the slack message and fixed the way "created" events was matched --- .../com/monta/slack/notifier/SlackClient.kt | 4 +-- .../notifier/command/PublishSlackCommand.kt | 5 +-- .../monta/slack/notifier/model/GithubEvent.kt | 33 ++++++++++++++++--- .../model/serializers/BaseGithubContext.kt | 1 + .../model/serializers/GithubCreatedContext.kt | 14 ++++---- .../monta/slack/notifier/util/FileUtils.kt | 20 +++++++---- 6 files changed, 55 insertions(+), 22 deletions(-) diff --git a/src/commonMain/kotlin/com/monta/slack/notifier/SlackClient.kt b/src/commonMain/kotlin/com/monta/slack/notifier/SlackClient.kt index 5bbc05f..5540afb 100644 --- a/src/commonMain/kotlin/com/monta/slack/notifier/SlackClient.kt +++ b/src/commonMain/kotlin/com/monta/slack/notifier/SlackClient.kt @@ -103,11 +103,11 @@ class SlackClient( ), SlackBlock.Text( type = "mrkdwn", - text = " \n*Message:*\n<${githubEvent.getCommitUrl()}|${githubEvent.getCommitMessage()}>" + text = " \n*Message:*\n<${githubEvent.getChangeUrl()}|${githubEvent.getChangeMessage()}>" ), SlackBlock.Text( type = "mrkdwn", - text = " \n*SHA:*\n<${githubEvent.getCommitUrl()}|${githubEvent.commitSHA}>" + text = " \n*Change:*\n<${githubEvent.getChangeUrl()}|${githubEvent.getChangeIdentifier()}>" ) ) ), diff --git a/src/commonMain/kotlin/com/monta/slack/notifier/command/PublishSlackCommand.kt b/src/commonMain/kotlin/com/monta/slack/notifier/command/PublishSlackCommand.kt index b38601f..075d77c 100644 --- a/src/commonMain/kotlin/com/monta/slack/notifier/command/PublishSlackCommand.kt +++ b/src/commonMain/kotlin/com/monta/slack/notifier/command/PublishSlackCommand.kt @@ -105,8 +105,9 @@ class PublishSlackCommand : CliktCommand() { runId = githubRunId, displayName = baseGithubContext.displayName, commitSHA = baseGithubContext.sha, - commitMessage = baseGithubContext.message, - workflow = githubWorkflow + message = baseGithubContext.message, + workflow = githubWorkflow, + prUrl = baseGithubContext.prUrl ) } diff --git a/src/commonMain/kotlin/com/monta/slack/notifier/model/GithubEvent.kt b/src/commonMain/kotlin/com/monta/slack/notifier/model/GithubEvent.kt index 404a506..d5c6780 100644 --- a/src/commonMain/kotlin/com/monta/slack/notifier/model/GithubEvent.kt +++ b/src/commonMain/kotlin/com/monta/slack/notifier/model/GithubEvent.kt @@ -6,19 +6,42 @@ class GithubEvent( var runId: String, val displayName: String?, val commitSHA: String?, - val commitMessage: String?, + val message: String?, val workflow: String?, + val prUrl: String?, ) { fun getRunUrl(): String { return "https://github.com/$repository/actions/runs/$runId" } - fun getCommitUrl(): String { - return "https://github.com/$repository/commit/$commitSHA" + fun getChangeIdentifier(): String? { + if (commitSHA != null) { + return commitSHA + } else if (prUrl != null) { + return getPRidentifier(prUrl) + } + return null } - fun getCommitMessage(): String? { - return commitMessage + fun getChangeUrl(): String { + if (commitSHA != null) { + return "https://github.com/$repository/commit/$commitSHA" + } else if (prUrl != null) { + return prUrl + } + return "https://github.com/$repository/" + } + fun getChangeMessage(): String? { + return message ?.replace("\n", " ") ?.replace("\r", " ") + ?.replace("<", "") + ?.replace(">", "") ?.take(120) } + fun getPRidentifier(url: String): String? { + // Will extract the "pull/51" part of + // "https://github.com/monta-app/data-smart-charge/pull/51", + val regex = Regex("""pull/\d+""") + val matchResult = regex.find(url) + return matchResult?.value + } } diff --git a/src/commonMain/kotlin/com/monta/slack/notifier/model/serializers/BaseGithubContext.kt b/src/commonMain/kotlin/com/monta/slack/notifier/model/serializers/BaseGithubContext.kt index bf5d137..6e18620 100644 --- a/src/commonMain/kotlin/com/monta/slack/notifier/model/serializers/BaseGithubContext.kt +++ b/src/commonMain/kotlin/com/monta/slack/notifier/model/serializers/BaseGithubContext.kt @@ -4,4 +4,5 @@ class BaseGithubContext( val displayName: String?, val sha: String?, val message: String?, + val prUrl: String?, ) diff --git a/src/commonMain/kotlin/com/monta/slack/notifier/model/serializers/GithubCreatedContext.kt b/src/commonMain/kotlin/com/monta/slack/notifier/model/serializers/GithubCreatedContext.kt index 3be60f5..d1ddcf3 100644 --- a/src/commonMain/kotlin/com/monta/slack/notifier/model/serializers/GithubCreatedContext.kt +++ b/src/commonMain/kotlin/com/monta/slack/notifier/model/serializers/GithubCreatedContext.kt @@ -5,17 +5,17 @@ import kotlinx.serialization.Serializable @Serializable class GithubCreatedContext( - @SerialName("after") val sha: String, - @SerialName("pull_request") val pullRequest: PullRequest, + @SerialName("sender") val sender: Sender, + @SerialName("issue") val issue: Issue, ) { @Serializable - data class PullRequest( - @SerialName("title") val title: String, - @SerialName("user") val user: PullRequestUser, + data class Sender( + @SerialName("login") val login: String, ) @Serializable - data class PullRequestUser( - @SerialName("login") val login: String, + data class Issue( + @SerialName("title") val title: String, + @SerialName("html_url") val url: String, ) } diff --git a/src/commonMain/kotlin/com/monta/slack/notifier/util/FileUtils.kt b/src/commonMain/kotlin/com/monta/slack/notifier/util/FileUtils.kt index 621c94e..5cd45bc 100644 --- a/src/commonMain/kotlin/com/monta/slack/notifier/util/FileUtils.kt +++ b/src/commonMain/kotlin/com/monta/slack/notifier/util/FileUtils.kt @@ -48,10 +48,12 @@ private fun populateOnJsonPush(eventJson: String): BaseGithubContext? { @Suppress("SwallowedException") return try { val event = JsonUtil.instance.decodeFromString(eventJson) + println("Matched with a Push event") return BaseGithubContext( displayName = event.pusher.displayName, sha = event.headCommit.id, - message = event.headCommit.message + message = event.headCommit.message, + prUrl = null ) } catch (e: SerializationException) { null @@ -62,10 +64,12 @@ private fun populateOnJsonOpened(eventJson: String): BaseGithubContext? { @Suppress("SwallowedException") return try { val event = JsonUtil.instance.decodeFromString(eventJson) + println("Matched with a Opened event") return BaseGithubContext( displayName = event.pullRequest.user.login, sha = event.pullRequest.head.sha, - message = event.pullRequest.title + message = event.pullRequest.title, + prUrl = null ) } catch (e: SerializationException) { null @@ -76,10 +80,12 @@ private fun populateOnJsonCreated(eventJson: String): BaseGithubContext? { @Suppress("SwallowedException") return try { val event = JsonUtil.instance.decodeFromString(eventJson) + println("Matched with a Created event") return BaseGithubContext( - displayName = event.pullRequest.user.login, - sha = event.sha, - message = event.pullRequest.title + displayName = event.sender.login, + sha = null, + message = event.issue.title, + prUrl = event.issue.url ) } catch (e: SerializationException) { null @@ -87,9 +93,11 @@ private fun populateOnJsonCreated(eventJson: String): BaseGithubContext? { } private fun handleFailure(): BaseGithubContext { + println("DIDNT MATCH an event :((((") return BaseGithubContext( displayName = null, sha = null, - message = null + message = null, + prUrl = null ) } From 8667a443bcc7d10fc7a049914fe1964d6ddb1ec6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20Z=C3=B8rn?= Date: Fri, 3 May 2024 15:04:08 +0200 Subject: [PATCH 2/2] Remove println --- .../kotlin/com/monta/slack/notifier/util/FileUtils.kt | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/commonMain/kotlin/com/monta/slack/notifier/util/FileUtils.kt b/src/commonMain/kotlin/com/monta/slack/notifier/util/FileUtils.kt index 5cd45bc..b689900 100644 --- a/src/commonMain/kotlin/com/monta/slack/notifier/util/FileUtils.kt +++ b/src/commonMain/kotlin/com/monta/slack/notifier/util/FileUtils.kt @@ -48,7 +48,6 @@ private fun populateOnJsonPush(eventJson: String): BaseGithubContext? { @Suppress("SwallowedException") return try { val event = JsonUtil.instance.decodeFromString(eventJson) - println("Matched with a Push event") return BaseGithubContext( displayName = event.pusher.displayName, sha = event.headCommit.id, @@ -64,7 +63,6 @@ private fun populateOnJsonOpened(eventJson: String): BaseGithubContext? { @Suppress("SwallowedException") return try { val event = JsonUtil.instance.decodeFromString(eventJson) - println("Matched with a Opened event") return BaseGithubContext( displayName = event.pullRequest.user.login, sha = event.pullRequest.head.sha, @@ -80,7 +78,6 @@ private fun populateOnJsonCreated(eventJson: String): BaseGithubContext? { @Suppress("SwallowedException") return try { val event = JsonUtil.instance.decodeFromString(eventJson) - println("Matched with a Created event") return BaseGithubContext( displayName = event.sender.login, sha = null, @@ -93,7 +90,6 @@ private fun populateOnJsonCreated(eventJson: String): BaseGithubContext? { } private fun handleFailure(): BaseGithubContext { - println("DIDNT MATCH an event :((((") return BaseGithubContext( displayName = null, sha = null,