Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(action): Change the way some fields are populated in the slack... #65

Merged
merged 2 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/commonMain/kotlin/com/monta/slack/notifier/SlackClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()}>"
)
)
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ class BaseGithubContext(
val displayName: String?,
val sha: String?,
val message: String?,
val prUrl: String?,
)
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
}
16 changes: 10 additions & 6 deletions src/commonMain/kotlin/com/monta/slack/notifier/util/FileUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ private fun populateOnJsonPush(eventJson: String): BaseGithubContext? {
return BaseGithubContext(
displayName = event.pusher.displayName,
sha = event.headCommit.id,
message = event.headCommit.message
message = event.headCommit.message,
prUrl = null
)
} catch (e: SerializationException) {
null
Expand All @@ -65,7 +66,8 @@ private fun populateOnJsonOpened(eventJson: String): BaseGithubContext? {
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
Expand All @@ -77,9 +79,10 @@ private fun populateOnJsonCreated(eventJson: String): BaseGithubContext? {
return try {
val event = JsonUtil.instance.decodeFromString<GithubCreatedContext>(eventJson)
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
Expand All @@ -90,6 +93,7 @@ private fun handleFailure(): BaseGithubContext {
return BaseGithubContext(
displayName = null,
sha = null,
message = null
message = null,
prUrl = null
)
}
Loading