From 5d1a44644f1c8142266857629d8aef1fb4fc00ee Mon Sep 17 00:00:00 2001 From: zkldi <20380519+zkldi@users.noreply.github.com> Date: Thu, 25 Apr 2024 01:41:36 +0100 Subject: [PATCH] fix: handle unbelievably pernicious edge case in ghbot --- github-bot/src/main.ts | 49 +++++++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 12 deletions(-) diff --git a/github-bot/src/main.ts b/github-bot/src/main.ts index 8803be731..a28e6c35d 100644 --- a/github-bot/src/main.ts +++ b/github-bot/src/main.ts @@ -38,23 +38,48 @@ function ConvertGitHubURL(url: string) { app.webhooks.on( ["pull_request.opened", "pull_request.edited"], async ({ octokit, payload: body }) => { - const filesChanged = (await fetch( - `https://api.github.com/repos/zkldi/Tachi/pulls/${body.number}/files` - ).then((r) => r.json())) as Array<{ filename: string }>; + try { + const filesChanged = (await fetch( + `https://api.github.com/repos/zkldi/Tachi/pulls/${body.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"))) { - // post a link to the diff viewer in the PR comments. + // 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: mkSeedDiffViewMsg( - body.pull_request.base.repo.url, - body.pull_request.base.sha, - body.pull_request.head.repo.url, - body.pull_request.head.sha - ), + body: `I failed horribly figuring out whether this was a seeds diff or not. I'm sorry! + +***** +Reason + +\`\`\` +${err} +\`\`\` + +***** + +${mkSeedDiffViewMsg( + body.pull_request.base.repo.url, + body.pull_request.base.sha, + body.pull_request.head.repo.url, + body.pull_request.head.sha +)}`, }); } }