From 8644d8c907123d37b7df555dabbb7de76491e22a Mon Sep 17 00:00:00 2001 From: Muunatic Date: Mon, 18 Sep 2023 19:18:31 +0700 Subject: [PATCH] refactor: avoid no-misused-promises --- src/index.ts | 230 ++++++++++++++++++++++++++------------------------- 1 file changed, 116 insertions(+), 114 deletions(-) diff --git a/src/index.ts b/src/index.ts index 8273e24..9e29392 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -import fetch from "node-fetch"; +import fetch, { Response } from "node-fetch"; import { Probot } from "probot"; import { Octokit } from "octokit"; import { createAppAuth } from "@octokit/auth-app"; @@ -32,7 +32,7 @@ module.exports = (app: Probot) => { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token } - }).then((res) => { + }).then((res: Response) => { return res.json(); }).then((res: [getEvent]) => { let i: number; @@ -842,7 +842,7 @@ module.exports = (app: Probot) => { issue_number: context.payload.pull_request.number }).then(async (res) => { if (res.data.find(a => a.name == "Requested Changes")) { - context.octokit.issues.removeLabel( + await context.octokit.issues.removeLabel( context.issue({ name: 'Requested Changes' }) @@ -940,7 +940,7 @@ module.exports = (app: Probot) => { case "User": // Merge pull request if (context.payload.comment.body.toLowerCase() == "ready to merge") { - context.octokit.pulls.get({ + await context.octokit.pulls.get({ repo: context.payload.repository.name, owner: context.payload.repository.owner.login, pull_number: context.payload.issue.number @@ -1105,123 +1105,125 @@ module.exports = (app: Probot) => { }); // Stale PRs - setInterval(async () => { - octokit.rest.repos.listForUser({ - username: "Muunatic", - type: "owner" - }).then(async (res) => { - for (let i = 0; i < res.data.length; i++) { - if (res.data[i].fork === false) { - octokit.rest.pulls.list({ - owner: res.data[i].owner.login, - repo: res.data[i].name - }).then(async (res) => { - for (let i = 0; i < res.data.length; i++) { - const currentRepo = res.data[i].base.repo.name; - const currentOwner = res.data[i].base.repo.owner.login; - const currentPRs = res.data[i].number; - if (res.data[i].state.toLowerCase() == "open" && res.data[i].draft == false) { - if (res.data[i].user?.type.toLowerCase() == "user") { - octokit.rest.pulls.listCommits({ - owner: currentOwner, - repo: currentRepo, - pull_number: currentPRs - }).then((res) => { - const currentDate = new Date(); - const createdAt = new Date(res.data[res.data.length - 1].commit.author?.date as string); - const staleDate = new Date(createdAt.getTime() + 7 * 24 * 60 * 60 * 1000); - const closeDate = new Date(createdAt.getTime() + 30 * 24 * 60 * 60 * 1000); - if (currentDate > staleDate) { - if (currentDate > closeDate) { - octokit.rest.pulls.update({ - owner: currentOwner, - repo: currentRepo, - pull_number: currentPRs, - state: "closed" - }).then(async (res) => { - if (res.data.labels.find((a) => a.name == "Stale")) { - await octokit.rest.issues.addLabels({ - owner: res.data.base.repo.owner.login, - repo: res.data.base.repo.name, - issue_number: res.data.number, - labels: ["Closed"] - }); - await octokit.rest.issues.removeLabel({ - owner: res.data.base.repo.owner.login, - repo: res.data.base.repo.name, - issue_number: res.data.number, - name: "Pending" - }); - await octokit.rest.issues.createComment({ - owner: res.data.base.repo.owner.login, - repo: res.data.base.repo.name, - issue_number: res.data.number, - body: `Hello @${res.data.user?.login}! We've observed that your pull requests have remained inactive for the last 30 days. Due to this prolonged inactivity, we've had to close these PRs. If you still intend to pursue these changes, please feel free to create new PRs.` - }); - octokit.rest.issues.lock({ - owner: currentOwner, - repo: currentRepo, - issue_number: currentPRs, - lock_reason: "resolved" - }); - } else { - return; - } - }); - } else { - octokit.rest.pulls.get({ - owner: currentOwner, - repo: currentRepo, - pull_number: currentPRs - }).then(async (res) => { - if (res.data.labels.find((a) => a.name == "Stale")) { - return; - } else { - const prsReviewers: string[] = []; - octokit.rest.pulls.listReviewComments({ - owner: res.data.base.repo.owner.login, - repo: res.data.base.repo.name, - pull_number: res.data.number - }).then(async (res) => { - if (res.data.length !== 0) { - for (let i = 0; i < res.data.length; i++) { - prsReviewers.push("@" + res.data[i].user?.login); + setInterval(() => { + (async () => { + await octokit.rest.repos.listForUser({ + username: "Muunatic", + type: "owner" + }).then(async (res) => { + for (let i = 0; i < res.data.length; i++) { + if (res.data[i].fork === false) { + await octokit.rest.pulls.list({ + owner: res.data[i].owner.login, + repo: res.data[i].name + }).then(async (res) => { + for (let i = 0; i < res.data.length; i++) { + const currentRepo = res.data[i].base.repo.name; + const currentOwner = res.data[i].base.repo.owner.login; + const currentPRs = res.data[i].number; + if (res.data[i].state.toLowerCase() == "open" && res.data[i].draft == false) { + if (res.data[i].user?.type.toLowerCase() == "user") { + await octokit.rest.pulls.listCommits({ + owner: currentOwner, + repo: currentRepo, + pull_number: currentPRs + }).then(async (res) => { + const currentDate = new Date(); + const createdAt = new Date(res.data[res.data.length - 1].commit.author?.date as string); + const staleDate = new Date(createdAt.getTime() + 7 * 24 * 60 * 60 * 1000); + const closeDate = new Date(createdAt.getTime() + 30 * 24 * 60 * 60 * 1000); + if (currentDate > staleDate) { + if (currentDate > closeDate) { + await octokit.rest.pulls.update({ + owner: currentOwner, + repo: currentRepo, + pull_number: currentPRs, + state: "closed" + }).then(async (res) => { + if (res.data.labels.find((a) => a.name == "Stale")) { + await octokit.rest.issues.addLabels({ + owner: res.data.base.repo.owner.login, + repo: res.data.base.repo.name, + issue_number: res.data.number, + labels: ["Closed"] + }); + await octokit.rest.issues.removeLabel({ + owner: res.data.base.repo.owner.login, + repo: res.data.base.repo.name, + issue_number: res.data.number, + name: "Pending" + }); + await octokit.rest.issues.createComment({ + owner: res.data.base.repo.owner.login, + repo: res.data.base.repo.name, + issue_number: res.data.number, + body: `Hello @${res.data.user?.login}! We've observed that your pull requests have remained inactive for the last 30 days. Due to this prolonged inactivity, we've had to close these PRs. If you still intend to pursue these changes, please feel free to create new PRs.` + }); + await octokit.rest.issues.lock({ + owner: currentOwner, + repo: currentRepo, + issue_number: currentPRs, + lock_reason: "resolved" + }); + } else { + return; + } + }); + } else { + await octokit.rest.pulls.get({ + owner: currentOwner, + repo: currentRepo, + pull_number: currentPRs + }).then(async (res) => { + if (res.data.labels.find((a) => a.name == "Stale")) { + return; + } else { + const prsReviewers: string[] = []; + await octokit.rest.pulls.listReviewComments({ + owner: res.data.base.repo.owner.login, + repo: res.data.base.repo.name, + pull_number: res.data.number + }).then((res) => { + if (res.data.length !== 0) { + for (let i = 0; i < res.data.length; i++) { + prsReviewers.push("@" + res.data[i].user?.login); + } + } else { + prsReviewers.push("@Muunatic"); } - } else { - prsReviewers.push("@Muunatic"); - } - }); - await octokit.rest.issues.addLabels({ - owner: res.data.base.repo.owner.login, - repo: res.data.base.repo.name, - issue_number: res.data.number, - labels: ["Stale"] - }); - await octokit.rest.issues.createComment({ - owner: res.data.base.repo.owner.login, - repo: res.data.base.repo.name, - issue_number: res.data.number, - body: `Hello @${res.data.user?.login}! We noticed that your pull request has been inactive for the past 7 days. If you've already received a response from the maintainer, please ensure that you review and address the feedback provided. If not, please reach out to the maintainer to seek clarification or assistance if needed.\n\n Additionally, we'd appreciate it if ${prsReviewers.join(", ")} could also take a moment to review the pull request and provide feedback.` - }); - } - }); + }); + await octokit.rest.issues.addLabels({ + owner: res.data.base.repo.owner.login, + repo: res.data.base.repo.name, + issue_number: res.data.number, + labels: ["Stale"] + }); + await octokit.rest.issues.createComment({ + owner: res.data.base.repo.owner.login, + repo: res.data.base.repo.name, + issue_number: res.data.number, + body: `Hello @${res.data.user?.login}! We noticed that your pull request has been inactive for the past 7 days. If you've already received a response from the maintainer, please ensure that you review and address the feedback provided. If not, please reach out to the maintainer to seek clarification or assistance if needed.\n\n Additionally, we'd appreciate it if ${prsReviewers.join(", ")} could also take a moment to review the pull request and provide feedback.` + }); + } + }); + } + } else { + return; } - } else { - return; - } - }); + }); + } else { + continue; + } } else { continue; } - } else { - continue; } - } - }); - } else { - continue; + }); + } else { + continue; + } } - } + }); }); }, 3600000);