Skip to content

Commit 1c945b9

Browse files
authored
feat: environment 인자에 맞추어 웹훅 채널 변경 (#3)
1 parent 6953fa2 commit 1c945b9

File tree

4 files changed

+29
-18
lines changed

4 files changed

+29
-18
lines changed

.github/workflows/yappu-world-server-lambda-cd.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ jobs:
2727
run: |
2828
mkdir -p src/main/resources
2929
echo "DISCORD_SERVER_ALERT_WEBHOOK=${{ secrets.DISCORD_SERVER_ALERT_WEBHOOK }}" > src/main/resources/.env
30+
echo "DISCORD_SERVER_TEST_WEBHOOK=${{ secrets.DISCORD_SERVER_TEST_WEBHOOK }}" >> src/main/resources/.env
3031
cat src/main/resources/.env
3132
3233
- name: Build ShadowJar

.github/workflows/yappu-world-server-lambda-ci.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ jobs:
2626
- name: Create .env file
2727
run: |
2828
mkdir -p src/main/resources
29-
echo "DISCORD_SERVER_ALERT_WEBHOOK=${{ secrets.DISCORD_SERVER_TEST_WEBHOOK }}" > src/main/resources/.env
29+
echo "DISCORD_SERVER_ALERT_WEBHOOK=${{ secrets.DISCORD_SERVER_ALERT_WEBHOOK }}" > src/main/resources/.env
30+
echo "DISCORD_SERVER_TEST_WEBHOOK=${{ secrets.DISCORD_SERVER_TEST_WEBHOOK }}" >> src/main/resources/.env
3031
cat src/main/resources/.env
3132
3233
- name: Compile and run test

src/main/kotlin/discord/DiscordClient.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import io.ktor.client.request.*
99
import io.ktor.http.*
1010

1111
class DiscordClient {
12-
suspend fun send(message: DiscordMessage): DiscordWebhookResponse {
12+
suspend fun sendAlertServer(message: DiscordMessage, serverWebhook: String): DiscordWebhookResponse {
1313
val dotenv = Dotenv.load()
1414
val response = HttpClient(CIO).use { client ->
1515
client.post(dotenv["DISCORD_SERVER_ALERT_WEBHOOK"]) {

src/main/kotlin/handler/SentryDiscordWebhookHandler.kt

+25-16
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import com.amazonaws.services.lambda.runtime.Context
88
import com.amazonaws.services.lambda.runtime.RequestHandler
99
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent
1010
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
11+
import io.github.cdimascio.dotenv.Dotenv
1112
import kotlinx.coroutines.runBlocking
1213

1314
class SentryDiscordWebhookHandler(
@@ -16,24 +17,13 @@ class SentryDiscordWebhookHandler(
1617

1718
override fun handleRequest(input: Any, context: Context): APIGatewayProxyResponseEvent {
1819
val event = parseEvent(input)
19-
val fields = mutableListOf<DiscordEmbedField>().apply {
20-
listOf("environment", "level", "type", "location", "issue_id").forEach { element ->
21-
if (event[element] != null) {
22-
add(DiscordEmbedField(element, event[element] as String))
23-
}
24-
}
20+
val message = makeDiscordMessageBy(event)
21+
val discordWebhook = when (event["environment"]) {
22+
"prod" -> Dotenv.load()["DISCORD_SERVER_ALERT_WEBHOOK"]
23+
else -> Dotenv.load()["DISCORD_SERVER_TEST_WEBHOOK"]
2524
}
2625

27-
val message = DiscordMessage.of(
28-
DiscordEmbed.error(
29-
event["title"] as String,
30-
event["message"] as String,
31-
event["web_url"] as String,
32-
fields
33-
)
34-
)
35-
36-
return runBlocking { discordClient.send(message) }
26+
return runBlocking { discordClient.sendAlertServer(message, discordWebhook) }
3727
.let {
3828
APIGatewayProxyResponseEvent()
3929
.withStatusCode(200)
@@ -49,4 +39,23 @@ class SentryDiscordWebhookHandler(
4939
}
5040
return data["event"] as Map<*, *>
5141
}
42+
43+
private fun makeDiscordMessageBy(event: Map<*, *>): DiscordMessage {
44+
val fields = mutableListOf<DiscordEmbedField>().apply {
45+
listOf("environment", "level", "type", "location", "issue_id").forEach { element ->
46+
if (event[element] != null) {
47+
add(DiscordEmbedField(element, event[element] as String))
48+
}
49+
}
50+
}
51+
52+
return DiscordMessage.of(
53+
DiscordEmbed.error(
54+
event["title"] as String,
55+
event["message"] as String,
56+
event["web_url"] as String,
57+
fields
58+
)
59+
)
60+
}
5261
}

0 commit comments

Comments
 (0)