Skip to content

Commit

Permalink
fix: Add UTF-8 charset on slack POST call to remove the warning from …
Browse files Browse the repository at this point in the history
…our build logs. (#18)

* fix: Add UTF-8 charset on slack POST call to remove the warning from our build logs.

Also upgraded all dependencies

* chore: Very basic readme file

* ignore: Ktlint + autoformat
  • Loading branch information
morten-andersen authored May 26, 2023
1 parent 28eb051 commit acbc004
Show file tree
Hide file tree
Showing 20 changed files with 138 additions and 80 deletions.
2 changes: 1 addition & 1 deletion .github/detekt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -783,4 +783,4 @@ style:
active: true
excludes: [ '**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/jsTest/**', '**/iosTest/**' ]
excludeImports:
- 'java.util.*'
- 'java.util.*'
5 changes: 3 additions & 2 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ jobs:
key: ${{ runner.os }}-${{ hashFiles('**/build.gradle.kts') }}
- name: Install dependencies
run: |
sudo apt-get install libcurl4-openssl-dev
sudo apt update
sudo apt install libcurl4-openssl-dev
- name: Build with Gradle
run: ./gradlew commonBinaries
- name: Move and apply correct permissions to binary
Expand Down Expand Up @@ -66,4 +67,4 @@ jobs:
file: ${{steps.download.outputs.download-path}}
asset_name: slack-notifier-cli
tag: ${{ github.ref }}
overwrite: true
overwrite: true
37 changes: 37 additions & 0 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Pull Request Workflow

on: [ pull_request ]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up JDK
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'adopt'
cache: 'gradle'
- uses: actions/cache@v3
with:
path: "/home/runner/.konan"
key: ${{ runner.os }}-${{ hashFiles('**/build.gradle.kts') }}
- name: Install dependencies
run: |
sudo apt update
sudo apt install libcurl4-openssl-dev
- name: Build with Gradle
run: ./gradlew commonBinaries
review-dog:
name: ReviewDog
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
# Detekt
- name: Run Detekt
uses: alaegin/[email protected]
with:
github_token: ${{ secrets.github_token }}
detekt_config: ./.github/detekt.yml
14 changes: 14 additions & 0 deletions .github/workflows/upgrade_gradle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: Update Gradle Wrapper

on:
schedule:
- cron: "0 7 * * *"

jobs:
update-gradle-wrapper:
runs-on: ubuntu-latest
steps:
- name: Checkout Project
uses: actions/checkout@v3
- name: Update Gradle Wrapper
uses: gradle-update/update-gradle-wrapper-action@v1
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
### About
### About

A CLI project for sending slack messages usable in GitHub workflows.

### Usage

Typically you do not use this directly but via the [slack-notifier-cli-action](https://github.com/monta-app/slack-notifier-cli-action)

Most project don't use this directly, but indirectly through the [github-workflows](https://github.com/monta-app/github-workflows)
21 changes: 12 additions & 9 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
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.2"
}

group = "com.monta.slack.notifier"
version = "1.0.9"
version = "1.1.0"

repositories {
mavenCentral()
}

defaultTasks("commonBinaries")

kotlin {

val hostOs = System.getProperty("os.name")
Expand Down Expand Up @@ -40,11 +43,11 @@ kotlin {
// Date Time Support
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.4.0")
// Serialization
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.0")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.1")
// Atomic
implementation("org.jetbrains.kotlinx:atomicfu:0.20.0")
implementation("org.jetbrains.kotlinx:atomicfu:0.20.2")
// Http Client
val ktorVersion = "2.2.4"
val ktorVersion = "2.3.0"
implementation("io.ktor:ktor-client-core:$ktorVersion")
implementation("io.ktor:ktor-client-curl:$ktorVersion")
implementation("io.ktor:ktor-client-content-negotiation:$ktorVersion")
Expand All @@ -53,7 +56,7 @@ kotlin {
}
val commonTest by getting {
dependencies {
val kotestVersion = "5.5.4"
val kotestVersion = "5.6.2"
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
implementation("io.kotest:kotest-framework-engine:$kotestVersion")
Expand All @@ -67,4 +70,4 @@ kotlin.targets.withType<org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarge
binaries.all {
freeCompilerArgs = freeCompilerArgs + "-Xdisable-phases=EscapeAnalysis"
}
}
}
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
rootProject.name = "slack-notifier-cli"
rootProject.name = "slack-notifier-cli"
2 changes: 1 addition & 1 deletion src/commonMain/kotlin/com/monta/slack/notifier/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ import com.monta.slack.notifier.command.PublishSlackCommand

fun main(args: Array<String>) {
PublishSlackCommand().main(args)
}
}
34 changes: 16 additions & 18 deletions src/commonMain/kotlin/com/monta/slack/notifier/SlackClient.kt
Original file line number Diff line number Diff line change
@@ -1,34 +1,36 @@
package com.monta.slack.notifier

import com.monta.slack.notifier.model.*
import com.monta.slack.notifier.model.GithubPushContext
import com.monta.slack.notifier.model.JobStatus
import com.monta.slack.notifier.model.JobType
import com.monta.slack.notifier.model.SlackMessage
import com.monta.slack.notifier.util.JsonUtil
import com.monta.slack.notifier.util.client
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.utils.io.charsets.*
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.decodeFromString

class SlackClient(
private val serviceName: String?,
private val serviceEmoji: String?,
private val slackToken: String,
private val slackChannelId: String,
private val slackChannelId: String
) {

suspend fun create(
githubPushContext: GithubPushContext,
jobType: JobType,
jobStatus: JobStatus,
jobStatus: JobStatus
): String {

val response = makeSlackRequest(
url = "https://slack.com/api/chat.postMessage",
message = generateMessage(
githubPushContext = githubPushContext,
jobType = jobType,
jobStatus = jobStatus,
jobStatus = jobStatus
)
)

Expand All @@ -39,9 +41,8 @@ class SlackClient(
messageId: String,
githubPushContext: GithubPushContext,
jobType: JobType,
jobStatus: JobStatus,
jobStatus: JobStatus
): String {

val previousMessage = getSlackMessageById(messageId)

val response = makeSlackRequest(
Expand All @@ -51,7 +52,7 @@ class SlackClient(
jobType = jobType,
jobStatus = jobStatus,
messageId = messageId,
previousAttachments = previousMessage?.messages?.firstOrNull()?.attachments,
previousAttachments = previousMessage?.messages?.firstOrNull()?.attachments
)
)

Expand All @@ -63,9 +64,8 @@ class SlackClient(
jobType: JobType,
jobStatus: JobStatus,
messageId: String? = null,
previousAttachments: List<SlackMessage.Attachment>? = null,
previousAttachments: List<SlackMessage.Attachment>? = null
): SlackMessage {

val attachments = mutableMapOf<JobType, SlackMessage.Attachment>()

previousAttachments?.forEach { previousAttachment ->
Expand Down Expand Up @@ -96,9 +96,8 @@ class SlackClient(
}

private suspend fun getSlackMessageById(
messageId: String,
messageId: String
): MessageResponse? {

val response = client.get {
header("Authorization", "Bearer $slackToken")
url {
Expand All @@ -122,10 +121,9 @@ class SlackClient(
}

private suspend fun makeSlackRequest(url: String, message: SlackMessage): Response? {

val response = client.post(url) {
header("Authorization", "Bearer $slackToken")
contentType(ContentType.Application.Json)
contentType(ContentType.Application.Json.withParameter("charset", Charsets.UTF_8.name))
setBody(message)
}

Expand All @@ -147,14 +145,14 @@ class SlackClient(
@SerialName("channel")
val channel: String, // C024BE91L
@SerialName("ts")
val ts: String, // 1401383885.000061
val ts: String // 1401383885.000061
)

@Serializable
private data class MessageResponse(
@SerialName("ok")
val ok: Boolean, // true
@SerialName("messages")
val messages: List<SlackMessage>,
val messages: List<SlackMessage>
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class PublishSlackCommand : CliktCommand() {
envvar = "PUBLISH_SLACK_GITHUB_CONTEXT"
).required()


private val serviceName: String? by option(
help = "Emoji for the app!",
envvar = "PUBLISH_SLACK_SERVICE_NAME"
Expand Down Expand Up @@ -62,7 +61,7 @@ class PublishSlackCommand : CliktCommand() {
githubContext = githubContext,
jobType = JobType.fromString(jobType),
jobStatus = JobStatus.fromString(jobStatus),
slackMessageId = slackMessageId,
slackMessageId = slackMessageId
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.monta.slack.notifier.model


import com.monta.slack.notifier.util.buildTitle
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
Expand All @@ -14,7 +13,7 @@ data class GithubPushContext(
@SerialName("repository")
val repository: String? = null, // monta-app/service-integrations
@SerialName("run_id")
val runId: String? = null, // 4399287439
val runId: String? = null, // 4399287439
@SerialName("actor")
val actor: String? = null, // BrianEstrada
@SerialName("triggering_actor")
Expand Down Expand Up @@ -58,7 +57,7 @@ data class GithubPushContext(
@SerialName("tree_id")
val treeId: String? = null, // f3667a7332372de2ccb6a1cc5c310e780915a28e
@SerialName("url")
val url: String? = null// https://github.com/monta-app/service-integrations/commit/c545a1613f18937a88a13935c4d644e8f81b71d6
val url: String? = null // https://github.com/monta-app/service-integrations/commit/c545a1613f18937a88a13935c4d644e8f81b71d6
)

@Serializable
Expand All @@ -68,7 +67,7 @@ data class GithubPushContext(
@SerialName("name")
val name: String? = null, // Brian Estrada
@SerialName("username")
val username: String? = null// BrianEstrada
val username: String? = null // BrianEstrada
) {
val displayName: String? = when {
email == null && name == null -> null
Expand All @@ -94,9 +93,8 @@ data class GithubPushContext(
serviceEmoji: String?,
slackChannelId: String,
messageId: String?,
attachments: List<SlackMessage.Attachment>?,
attachments: List<SlackMessage.Attachment>?
): SlackMessage {

val commit = event?.headCommit

val title = buildTitle(repository, workflow, serviceName, serviceEmoji)
Expand All @@ -121,23 +119,23 @@ data class GithubPushContext(
fields = listOf(
SlackBlock.Text(
type = "mrkdwn",
text = " \n*Branch:*\n${refName}",
text = " \n*Branch:*\n$refName"
),
SlackBlock.Text(
type = "mrkdwn",
text = " \n*Run:*\n<${getRunUrl()}|${runId}>",
text = " \n*Run:*\n<${getRunUrl()}|$runId>"
),
SlackBlock.Text(
type = "mrkdwn",
text = " \n*Comitter:*\n${commit?.committer?.displayName}",
text = " \n*Comitter:*\n${commit?.committer?.displayName}"
),
SlackBlock.Text(
type = "mrkdwn",
text = " \n*Message:*\n<${commit?.url}|${getSanitizedMessage()}>",
text = " \n*Message:*\n<${commit?.url}|${getSanitizedMessage()}>"
),
SlackBlock.Text(
type = "mrkdwn",
text = " \n*SHA:*\n<${commit?.url}|${commit?.id}>",
text = " \n*SHA:*\n<${commit?.url}|${commit?.id}>"
)
)
),
Expand All @@ -148,4 +146,4 @@ data class GithubPushContext(
attachments = attachments
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.monta.slack.notifier.model

enum class JobStatus(
val message: String,
val color: String,
val color: String
) {
Progress(
message = "In Progress :construction:",
Expand Down Expand Up @@ -32,4 +32,4 @@ enum class JobStatus(
} ?: Unknown
}
}
}
}
Loading

0 comments on commit acbc004

Please sign in to comment.