From 9e92441487c07fa9ceee287c628f5ccc698c4a1d Mon Sep 17 00:00:00 2001 From: pirosiki197 Date: Mon, 1 Jul 2024 18:45:23 +0900 Subject: [PATCH 1/3] feat: send error logs --- src/index.ts | 53 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 41 insertions(+), 12 deletions(-) diff --git a/src/index.ts b/src/index.ts index 7c4e165..f46f23b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,7 +12,15 @@ function doPost(e: GoogleAppsScript.Events.DoPost) { const req: AlertRequest = JSON.parse(e.postData.contents) const alertMessages = req.alerts.map(alertToMessage) const text = alertMessages.join('\n\n') - sendMessage(text) + const alertType = e.parameter.alertType + switch (alertType) { + case 'services': + sendServiceStatusMessage(text) + break + case 'logs': + sendErrorLogMessage(text) + break + } } function getStatusEmoji(status: string) { @@ -37,17 +45,8 @@ ${link} `.trim() } -function sendMessage(message: string) { - const signature = Utilities.computeHmacSignature( - Utilities.MacAlgorithm.HMAC_SHA_1, - message, - WEBHOOK_SECRET, - Utilities.Charset.UTF_8 - ) - const sign = signature.reduce((str, ch) => { - const chr = (ch < 0 ? ch + 256 : ch).toString(16) - return str + (chr.length === 1 ? '0' : '') + chr - }, '') +function sendServiceStatusMessage(message: string) { + const sign = computeSignature(message) UrlFetchApp.fetch(`https://q.trap.jp/api/v3/webhooks/${WEBHOOK_ID}`, { method: 'post', contentType: 'text/plain; charset=utf-8', @@ -58,3 +57,33 @@ function sendMessage(message: string) { payload: message }) } + +// #team/SysAd/logs/error +const ERROR_LOG_CHANNEL_ID = 'cec4f852-817f-4fab-91d7-a668712b9ab6' + +function sendErrorLogMessage(message: string) { + const sign = computeSignature(message) + UrlFetchApp.fetch(`https://q.trap.jp/api/v3/webhooks/${WEBHOOK_ID}`, { + method: 'post', + contentType: 'text/plain; charset=utf-8', + headers: { + 'Content-Type': 'text/plain; charset=utf-8', + 'X-TRAQ-Signature': sign, + 'X-TRAQ-Channel-Id': ERROR_LOG_CHANNEL_ID + }, + payload: message + }) +} + +function computeSignature(message: string) { + const signature = Utilities.computeHmacSignature( + Utilities.MacAlgorithm.HMAC_SHA_1, + message, + WEBHOOK_SECRET, + Utilities.Charset.UTF_8 + ) + return signature.reduce((str, ch) => { + const chr = (ch < 0 ? ch + 256 : ch).toString(16) + return str + (chr.length === 1 ? '0' : '') + chr + }, '') +} From 3aabea6d24800ed3d5d6a11f8e5e4d1c88a1127a Mon Sep 17 00:00:00 2001 From: pirosiki197 Date: Mon, 1 Jul 2024 18:51:47 +0900 Subject: [PATCH 2/3] fix: use channelID parameter --- src/index.ts | 31 ++++--------------------------- 1 file changed, 4 insertions(+), 27 deletions(-) diff --git a/src/index.ts b/src/index.ts index f46f23b..16e83db 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,15 +12,8 @@ function doPost(e: GoogleAppsScript.Events.DoPost) { const req: AlertRequest = JSON.parse(e.postData.contents) const alertMessages = req.alerts.map(alertToMessage) const text = alertMessages.join('\n\n') - const alertType = e.parameter.alertType - switch (alertType) { - case 'services': - sendServiceStatusMessage(text) - break - case 'logs': - sendErrorLogMessage(text) - break - } + const channelID = e.parameter.channelID + sendMessage(text, channelID) } function getStatusEmoji(status: string) { @@ -45,23 +38,7 @@ ${link} `.trim() } -function sendServiceStatusMessage(message: string) { - const sign = computeSignature(message) - UrlFetchApp.fetch(`https://q.trap.jp/api/v3/webhooks/${WEBHOOK_ID}`, { - method: 'post', - contentType: 'text/plain; charset=utf-8', - headers: { - 'Content-Type': 'text/plain; charset=utf-8', - 'X-TRAQ-Signature': sign - }, - payload: message - }) -} - -// #team/SysAd/logs/error -const ERROR_LOG_CHANNEL_ID = 'cec4f852-817f-4fab-91d7-a668712b9ab6' - -function sendErrorLogMessage(message: string) { +function sendMessage(message: string, channelID: string) { const sign = computeSignature(message) UrlFetchApp.fetch(`https://q.trap.jp/api/v3/webhooks/${WEBHOOK_ID}`, { method: 'post', @@ -69,7 +46,7 @@ function sendErrorLogMessage(message: string) { headers: { 'Content-Type': 'text/plain; charset=utf-8', 'X-TRAQ-Signature': sign, - 'X-TRAQ-Channel-Id': ERROR_LOG_CHANNEL_ID + 'X-TRAQ-Channel-Id': channelID }, payload: message }) From 709fbd19cafed64248b7c3bf33d59f6f6d1356d1 Mon Sep 17 00:00:00 2001 From: pirosiki197 Date: Mon, 1 Jul 2024 19:02:30 +0900 Subject: [PATCH 3/3] fix: handle undefined headers --- src/index.ts | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/index.ts b/src/index.ts index 16e83db..f54d4f3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,7 +12,7 @@ function doPost(e: GoogleAppsScript.Events.DoPost) { const req: AlertRequest = JSON.parse(e.postData.contents) const alertMessages = req.alerts.map(alertToMessage) const text = alertMessages.join('\n\n') - const channelID = e.parameter.channelID + const channelID = e.parameter.channelID as string | undefined sendMessage(text, channelID) } @@ -38,16 +38,19 @@ ${link} `.trim() } -function sendMessage(message: string, channelID: string) { +function sendMessage(message: string, channelID: string | undefined) { const sign = computeSignature(message) + const headers: Record = { + 'Content-Type': 'text/plain; charset=utf-8', + 'X-TRAQ-Signature': sign + } + if (channelID !== undefined) { + headers['X-TRAQ-Channel-Id'] = channelID + } UrlFetchApp.fetch(`https://q.trap.jp/api/v3/webhooks/${WEBHOOK_ID}`, { method: 'post', contentType: 'text/plain; charset=utf-8', - headers: { - 'Content-Type': 'text/plain; charset=utf-8', - 'X-TRAQ-Signature': sign, - 'X-TRAQ-Channel-Id': channelID - }, + headers: headers, payload: message }) }