From 2badeac40ee7f68b8461fe47d6b7d6d0847726b8 Mon Sep 17 00:00:00 2001 From: Ryunosuke Tokinaga <59079411+toririm@users.noreply.github.com> Date: Sat, 21 Sep 2024 02:55:08 +0900 Subject: [PATCH] =?UTF-8?q?Slack=E9=80=9A=E7=9F=A5=E5=9F=BA=E7=9B=A4?= =?UTF-8?q?=E3=81=AE=E4=BD=9C=E6=88=90=20(#135)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit close #133 --- .github/workflows/firebase-hosting-merge.yml | 2 ++ .../workflows/firebase-hosting-pull-request.yml | 2 ++ .gitignore | 3 ++- app/lib/webhook.ts | 17 +++++++++++++++++ app/routes/items/actions/addItem.ts | 7 ++++++- app/vite-env.d.ts | 9 +++++++++ 6 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 app/lib/webhook.ts create mode 100644 app/vite-env.d.ts diff --git a/.github/workflows/firebase-hosting-merge.yml b/.github/workflows/firebase-hosting-merge.yml index f41e4c10..8e65e772 100644 --- a/.github/workflows/firebase-hosting-merge.yml +++ b/.github/workflows/firebase-hosting-merge.yml @@ -24,6 +24,8 @@ jobs: if: steps.cache-node-modules.outputs.cache-hit != 'true' run: bun install --frozen-lockfile - run: bun run build + env: + VITE_WEBHOOK_URL: ${{ secrets.WEBHOOK_URL }} - uses: FirebaseExtended/action-hosting-deploy@v0 with: repoToken: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/firebase-hosting-pull-request.yml b/.github/workflows/firebase-hosting-pull-request.yml index 62218681..fa94bbb5 100644 --- a/.github/workflows/firebase-hosting-pull-request.yml +++ b/.github/workflows/firebase-hosting-pull-request.yml @@ -26,6 +26,8 @@ jobs: if: steps.cache-node-modules.outputs.cache-hit != 'true' run: bun install --frozen-lockfile - run: bun run build + env: + VITE_WEBHOOK_URL: ${{ secrets.WEBHOOK_URL }} - uses: FirebaseExtended/action-hosting-deploy@v0 with: repoToken: ${{ secrets.GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore index 7fe07520..2350078d 100644 --- a/.gitignore +++ b/.gitignore @@ -3,7 +3,8 @@ node_modules /.cache /build /public/build -.env +.env* +!.env.example .firebase .firebaserc diff --git a/app/lib/webhook.ts b/app/lib/webhook.ts new file mode 100644 index 00000000..3ae01d55 --- /dev/null +++ b/app/lib/webhook.ts @@ -0,0 +1,17 @@ +export const sendSlackMessage = async (message: string) => { + const webhookUrl = import.meta.env.VITE_WEBHOOK_URL; + + if (!webhookUrl) { + throw new Error("WEBHOOK_URL is not defined"); + } + + const response = await fetch(webhookUrl, { + method: "POST", + body: JSON.stringify({ text: message }), + }); + + if (!response.ok) { + console.log(response); + throw new Error("Failed to send message"); + } +}; diff --git a/app/routes/items/actions/addItem.ts b/app/routes/items/actions/addItem.ts index d8ebc655..dbece28b 100644 --- a/app/routes/items/actions/addItem.ts +++ b/app/routes/items/actions/addItem.ts @@ -1,5 +1,6 @@ import { parseWithZod } from "@conform-to/zod"; import { type ClientActionFunction, json } from "@remix-run/react"; +import { sendSlackMessage } from "~/lib/webhook"; import { ItemEntity, itemSchema } from "~/models/item"; import { itemRepository } from "~/repositories/item"; @@ -14,7 +15,11 @@ export const addItem: ClientActionFunction = async ({ request }) => { } const newItem = ItemEntity.createNew(submission.value); - const savedItem = await itemRepository.save(newItem); + const itemSavePromise = itemRepository.save(newItem); + const webhookSendPromise = sendSlackMessage( + `新しいアイテムが追加されました!\n${newItem.name}`, + ); + const [savedItem] = await Promise.all([itemSavePromise, webhookSendPromise]); console.log("Document written with ID: ", savedItem.id); return json(submission.reply({ resetForm: true }), { status: 200 }); diff --git a/app/vite-env.d.ts b/app/vite-env.d.ts new file mode 100644 index 00000000..8bb23b20 --- /dev/null +++ b/app/vite-env.d.ts @@ -0,0 +1,9 @@ +/// + +interface ImportMetaEnv { + readonly VITE_WEBHOOK_URL: string; +} + +interface ImportMeta { + readonly env: ImportMetaEnv; +}