diff --git a/.github/workflows/yappu-world-server-lambda-cd.yaml b/.github/workflows/yappu-world-server-lambda-cd.yaml index d26b3fb..8847258 100644 --- a/.github/workflows/yappu-world-server-lambda-cd.yaml +++ b/.github/workflows/yappu-world-server-lambda-cd.yaml @@ -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 diff --git a/.github/workflows/yappu-world-server-lambda-ci.yaml b/.github/workflows/yappu-world-server-lambda-ci.yaml index c95c8a9..2c84a19 100644 --- a/.github/workflows/yappu-world-server-lambda-ci.yaml +++ b/.github/workflows/yappu-world-server-lambda-ci.yaml @@ -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 diff --git a/src/main/kotlin/discord/DiscordClient.kt b/src/main/kotlin/discord/DiscordClient.kt index 8f5fc3e..bc0f400 100644 --- a/src/main/kotlin/discord/DiscordClient.kt +++ b/src/main/kotlin/discord/DiscordClient.kt @@ -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"]) { diff --git a/src/main/kotlin/handler/SentryDiscordWebhookHandler.kt b/src/main/kotlin/handler/SentryDiscordWebhookHandler.kt index 19f53e4..2aaca82 100644 --- a/src/main/kotlin/handler/SentryDiscordWebhookHandler.kt +++ b/src/main/kotlin/handler/SentryDiscordWebhookHandler.kt @@ -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( @@ -16,24 +17,13 @@ class SentryDiscordWebhookHandler( override fun handleRequest(input: Any, context: Context): APIGatewayProxyResponseEvent { val event = parseEvent(input) - val fields = mutableListOf().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) @@ -49,4 +39,23 @@ class SentryDiscordWebhookHandler( } return data["event"] as Map<*, *> } + + private fun makeDiscordMessageBy(event: Map<*, *>): DiscordMessage { + val fields = mutableListOf().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 + ) + ) + } }