diff --git a/github-bot/src/main.ts b/github-bot/src/main.ts index a28e6c35d..cd0004ced 100644 --- a/github-bot/src/main.ts +++ b/github-bot/src/main.ts @@ -35,35 +35,42 @@ function ConvertGitHubURL(url: string) { return url.replace("https://api.github.com/repos/", "GitHub:"); } -app.webhooks.on( - ["pull_request.opened", "pull_request.edited"], - async ({ octokit, payload: body }) => { - try { - const filesChanged = (await fetch( - `https://api.github.com/repos/zkldi/Tachi/pulls/${body.number}/files` - ).then((r) => r.json())) as Array<{ filename: string }>; +async function sendDiff(octokit: any, payload: any) { + // post a link to the diff viewer in the PR comments. + return sendMsg( + mkSeedDiffViewMsg( + payload.pull_request.base.repo.url, + payload.pull_request.base.sha, + payload.pull_request.head.repo.url, + payload.pull_request.head.sha + ), + octokit, + payload + ); +} - // if any file modified in this pr is a collection - if (filesChanged.some((k) => k.filename.startsWith("database-seeds/collections"))) { - // post a link to the diff viewer in the PR comments. - await octokit.request("POST /repos/{owner}/{repo}/issues/{issue_number}/comments", { - owner: body.repository.owner.login, - repo: body.repository.name, - issue_number: body.pull_request.number, - body: mkSeedDiffViewMsg( - body.pull_request.base.repo.url, - body.pull_request.base.sha, - body.pull_request.head.repo.url, - body.pull_request.head.sha - ), - }); - } - } catch (err) { - await octokit.request("POST /repos/{owner}/{repo}/issues/{issue_number}/comments", { - owner: body.repository.owner.login, - repo: body.repository.name, - issue_number: body.pull_request.number, - body: `I failed horribly figuring out whether this was a seeds diff or not. I'm sorry! +async function sendMsg(message: string, octokit: any, payload: any) { + await octokit.request("POST /repos/{owner}/{repo}/issues/{issue_number}/comments", { + owner: payload.repository.owner.login, + repo: payload.repository.name, + issue_number: payload.pull_request.number, + body: message, + }); +} + +app.webhooks.on(["pull_request.opened", "pull_request.edited"], async ({ octokit, payload }) => { + try { + const filesChanged = (await fetch( + `https://api.github.com/repos/zkldi/Tachi/pulls/${payload.number}/files` + ).then((r) => r.json())) as Array<{ filename: string }>; + + // if any file modified in this pr is a collection + if (filesChanged.some((k) => k.filename.startsWith("database-seeds/collections"))) { + await sendDiff(octokit, payload); + } + } catch (err) { + await sendMsg( + `I failed horribly figuring out whether this was a seeds diff or not. I'm sorry! ***** Reason @@ -75,15 +82,43 @@ ${err} ***** ${mkSeedDiffViewMsg( - body.pull_request.base.repo.url, - body.pull_request.base.sha, - body.pull_request.head.repo.url, - body.pull_request.head.sha + payload.pull_request.base.repo.url, + payload.pull_request.base.sha, + payload.pull_request.head.repo.url, + payload.pull_request.head.sha )}`, - }); + octokit, + payload + ); + } +}); + +app.webhooks.on(["commit_comment.created"], async ({ octokit, payload }) => { + const body = payload.comment.body.trim(); + + if (body.startsWith("+bot")) { + const cmd = body.split(" ")[1]?.replace(/[^a-z]/u, ""); + + switch (cmd) { + case "ping": { + await sendMsg("pong!", octokit, payload); + break; + } + + case "diff": { + await sendDiff(octokit, payload); + break; + } + + default: + await sendMsg( + `No idea what to do with command \`${cmd}\`, sorry!`, + octokit, + payload + ); } } -); +}); const serverMiddleware = createNodeMiddleware(app); diff --git a/github-bot/tsconfig.json b/github-bot/tsconfig.json index 6570fe77f..ce059e614 100644 --- a/github-bot/tsconfig.json +++ b/github-bot/tsconfig.json @@ -1,7 +1,7 @@ { "extends": "../tsconfig.json", "compilerOptions": { - "target": "es5", + "target": "es6", "module": "commonjs", "lib": [ "es2015",