diff --git a/package.json b/package.json index aee3bcb..9309532 100644 --- a/package.json +++ b/package.json @@ -5,20 +5,18 @@ "build": "tsc" }, "dependencies": { - "@types/koa": "2.11.6", + "@types/koa": "2.15.0", "@types/koa-bodyparser": "5.0.2", - "@types/koa-router": "7.4.1", - "@types/node": "14.14.9", - "@types/request": "2.48.5", - "crypto": "1.0.1", - "koa": "2.13.0", - "koa-bodyparser": "4.3.0", - "koa-router": "10.0.0", - "request": "2.88.2", - "ts-node": "9.0.0", - "typescript": "4.1.2" + "@types/koa-router": "7.4.8", + "@types/node": "22.14.1", + "@types/request": "2.48.12", + "koa": "2.16.1", + "koa-bodyparser": "4.4.1", + "koa-router": "13.0.1", + "ts-node": "10.9.2", + "typescript": "5.8.3" }, "devDependencies": { - "@octokit/webhooks": "^13.2.7" + "@octokit/webhooks": "13.8.0" } } diff --git a/src/index.ts b/src/index.ts index 2e5af43..14c2d70 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,8 +3,7 @@ import { EventEmitter } from 'events'; import * as Koa from 'koa'; import * as Router from 'koa-router'; import * as bodyParser from 'koa-bodyparser'; -import * as request from 'request'; -import crypto = require('crypto'); +import * as crypto from 'node:crypto'; import type { EventPayloadMap } from '@octokit/webhooks/dist-types/generated/webhook-identifiers'; const config = require('../config.json'); @@ -19,15 +18,23 @@ class WebhookEventEmitter extends EventEmitter { const handler = new WebhookEventEmitter(); const post = async (text: string, home = true) => { - request.post(config.instance + '/api/notes/create', { - json: { + const instance = config.instance.endsWith('/') + ? config.instance.substring(0, config.instance.length - 1) + : config.instance; + + await fetch(`${instance}/api/notes/create`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ i: config.i, text, visibility: home ? 'home' : 'public', noExtractMentions: true, - noExtractHashtags: true - } - }); + noExtractHashtags: true, + }), + }) }; const app = new Koa(); @@ -40,7 +47,7 @@ const router = new Router(); router.post('/github', ctx => { const body = JSON.stringify(ctx.request.body); const hash = crypto.createHmac('sha1', secret).update(body).digest('hex'); - const sig1 = Buffer.from(ctx.headers['x-hub-signature']); + const sig1 = Buffer.from(ctx.headers['x-hub-signature'] as string); const sig2 = Buffer.from(`sha1=${hash}`); // シグネチャ比較 @@ -59,7 +66,7 @@ const server = http.createServer(app.callback()); server.listen(config.port); -handler.on('status', event => { +handler.on('status', async event => { const state = event.state; switch (state) { case 'error': @@ -68,26 +75,24 @@ handler.on('status', event => { const parent = commit.parents[0]; // Fetch parent status - request({ - url: `${parent.url}/statuses`, - proxy: config.proxy, + await fetch(`${parent.url}/statuses`, { + method: "GET", headers: { 'User-Agent': 'misskey' - } - }, (err, res, body) => { - if (err) { - console.error(err); - return; - } - const parentStatuses = JSON.parse(body); + }, + }).then(async res => { + const parentStatuses = await res.json() const parentState = parentStatuses[0]?.state; const stillFailed = parentState === 'failure' || parentState === 'error'; if (stillFailed) { - post(`⚠️ **BUILD STILL FAILED** ⚠️: ?[${commit.commit.message}](${commit.html_url})`); + await post(`⚠️ **BUILD STILL FAILED** ⚠️: ?[${commit.commit.message}](${commit.html_url})`); } else { - post(`🚨 **BUILD FAILED** 🚨: → ?[${commit.commit.message}](${commit.html_url}) ←`); + await post(`🚨 **BUILD FAILED** 🚨: → ?[${commit.commit.message}](${commit.html_url}) ←`); } - }); + }).catch(err => { + console.error(err); + }) + break; } });