diff --git a/NodeSrv/apps/healthcheck-survey-bot/scripts/deploy-workflow.js b/NodeSrv/apps/healthcheck-survey-bot/scripts/deploy-workflow.js new file mode 100644 index 00000000..1685344a --- /dev/null +++ b/NodeSrv/apps/healthcheck-survey-bot/scripts/deploy-workflow.js @@ -0,0 +1,41 @@ +// scripts/deploy-workflow.js +const fs = require("node:fs"); +const path = require("node:path"); +const { request } = require("./n8n-api"); + +async function main() { + const [, , filePath, ...rest] = process.argv; + if (!filePath) { + console.error("使い方: node scripts/deploy-workflow.js [--id=<既存workflowId>]"); + process.exit(1); + } + + const idArg = rest.find((a) => a.startsWith("--id=")); + const existingId = idArg ? idArg.slice("--id=".length) : null; + + const fullPath = path.resolve(filePath); + const definition = JSON.parse(fs.readFileSync(fullPath, "utf8")); + const body = { + name: definition.name, + nodes: definition.nodes, + connections: definition.connections, + settings: definition.settings || {}, + }; + + const { status, body: result } = existingId + ? await request("PUT", `/workflows/${existingId}`, body) + : await request("POST", "/workflows", body); + + console.log("HTTP status:", status); + console.log(JSON.stringify(result, null, 2)); + + if (status >= 200 && status < 300 && result.id) { + console.log(`\nワークフローID: ${result.id}`); + console.log("README.mdの「n8nリソースID一覧」へ記録すること。"); + } +} + +main().catch((err) => { + console.error(err); + process.exit(1); +}); diff --git a/NodeSrv/apps/healthcheck-survey-bot/scripts/n8n-api.js b/NodeSrv/apps/healthcheck-survey-bot/scripts/n8n-api.js new file mode 100644 index 00000000..d34371a2 --- /dev/null +++ b/NodeSrv/apps/healthcheck-survey-bot/scripts/n8n-api.js @@ -0,0 +1,30 @@ +// scripts/n8n-api.js +const N8N_BASE_URL = "https://n8n32.next-hd.net/api/v1"; +const N8N_API_KEY = process.env.N8N_API_KEY; + +if (!N8N_API_KEY) { + throw new Error( + "環境変数 N8N_API_KEY が未設定です。NodeSrv/apps/n8n/docs/n8n-guide.md 2章のPublic API Keyを設定してください。" + ); +} + +async function request(method, path, body) { + const res = await fetch(`${N8N_BASE_URL}${path}`, { + method, + headers: { + "X-N8N-API-KEY": N8N_API_KEY, + "Content-Type": "application/json", + }, + body: body ? JSON.stringify(body) : undefined, + }); + const text = await res.text(); + let json; + try { + json = text ? JSON.parse(text) : null; + } catch { + json = text; + } + return { status: res.status, body: json }; +} + +module.exports = { request };