Skip to content

feat: environment 인자에 맞추어 웹훅 채널 변경 #3

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

Merged
merged 1 commit into from
Feb 15, 2025
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
1 change: 1 addition & 0 deletions .github/workflows/yappu-world-server-lambda-cd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ jobs:
run: |
mkdir -p src/main/resources
echo "DISCORD_SERVER_ALERT_WEBHOOK=${{ secrets.DISCORD_SERVER_ALERT_WEBHOOK }}" > src/main/resources/.env
echo "DISCORD_SERVER_TEST_WEBHOOK=${{ secrets.DISCORD_SERVER_TEST_WEBHOOK }}" >> src/main/resources/.env
cat src/main/resources/.env

- name: Build ShadowJar
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/yappu-world-server-lambda-ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ jobs:
- name: Create .env file
run: |
mkdir -p src/main/resources
echo "DISCORD_SERVER_ALERT_WEBHOOK=${{ secrets.DISCORD_SERVER_TEST_WEBHOOK }}" > src/main/resources/.env
echo "DISCORD_SERVER_ALERT_WEBHOOK=${{ secrets.DISCORD_SERVER_ALERT_WEBHOOK }}" > src/main/resources/.env
echo "DISCORD_SERVER_TEST_WEBHOOK=${{ secrets.DISCORD_SERVER_TEST_WEBHOOK }}" >> src/main/resources/.env
cat src/main/resources/.env

- name: Compile and run test
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/discord/DiscordClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import io.ktor.client.request.*
import io.ktor.http.*

class DiscordClient {
suspend fun send(message: DiscordMessage): DiscordWebhookResponse {
suspend fun sendAlertServer(message: DiscordMessage, serverWebhook: String): DiscordWebhookResponse {
val dotenv = Dotenv.load()
val response = HttpClient(CIO).use { client ->
client.post(dotenv["DISCORD_SERVER_ALERT_WEBHOOK"]) {
Expand Down
41 changes: 25 additions & 16 deletions src/main/kotlin/handler/SentryDiscordWebhookHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.amazonaws.services.lambda.runtime.Context
import com.amazonaws.services.lambda.runtime.RequestHandler
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import io.github.cdimascio.dotenv.Dotenv
import kotlinx.coroutines.runBlocking

class SentryDiscordWebhookHandler(
Expand All @@ -16,24 +17,13 @@ class SentryDiscordWebhookHandler(

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

val message = DiscordMessage.of(
DiscordEmbed.error(
event["title"] as String,
event["message"] as String,
event["web_url"] as String,
fields
)
)

return runBlocking { discordClient.send(message) }
return runBlocking { discordClient.sendAlertServer(message, discordWebhook) }
.let {
APIGatewayProxyResponseEvent()
.withStatusCode(200)
Expand All @@ -49,4 +39,23 @@ class SentryDiscordWebhookHandler(
}
return data["event"] as Map<*, *>
}

private fun makeDiscordMessageBy(event: Map<*, *>): DiscordMessage {
val fields = mutableListOf<DiscordEmbedField>().apply {
listOf("environment", "level", "type", "location", "issue_id").forEach { element ->
if (event[element] != null) {
add(DiscordEmbedField(element, event[element] as String))
}
}
}

return DiscordMessage.of(
DiscordEmbed.error(
event["title"] as String,
event["message"] as String,
event["web_url"] as String,
fields
)
)
}
}