From 094d5c644189cdfaeae8dd7fd6a4122b4c8d70da Mon Sep 17 00:00:00 2001 From: Muunatic Date: Tue, 22 Aug 2023 13:07:48 +0700 Subject: [PATCH 1/3] feat: implement automated stale label for inactive pull requests --- src/index.ts | 147 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 141 insertions(+), 6 deletions(-) diff --git a/src/index.ts b/src/index.ts index fdf4d65..8500ecc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -191,8 +191,8 @@ module.exports = (app: Probot) => { if (context.payload.sender.login != context.payload.repository.owner.login) { if (context.payload.repository.html_url == "https://github.com/Typeslint/github-AutoResponse") { await context.octokit.pulls.listFiles({ - owner: 'Typeslint', - repo: 'github-AutoResponse', + owner: "Typeslint", + repo: "github-AutoResponse", pull_number: context.payload.number }).then((res) => { return res.data; @@ -211,14 +211,14 @@ module.exports = (app: Probot) => { ); let shaRef: string; await octokit.rest.pulls.get({ - owner: 'Typeslint', - repo: 'github-AutoResponse', + owner: "Typeslint", + repo: "github-AutoResponse", pull_number: context.payload.number }).then(async (res) => { shaRef = res.data.head.sha; await octokit.rest.repos.getContent({ - owner: 'Typeslint', - repo: 'github-AutoResponse', + owner: "Typeslint", + repo: "github-AutoResponse", ref: shaRef, path: "tsconfig.json" }).then(async (res) => { @@ -1084,4 +1084,139 @@ 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")) { + 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); + } + } 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: ["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")) { + 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); + } + } 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: ["Closed"] + }); + 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; + } + } + }); + }, 3600000); + }; From 93c117ef7eec524bc7a4f7e4289ce3f8933f5c41 Mon Sep 17 00:00:00 2001 From: wakame Date: Tue, 22 Aug 2023 15:28:47 +0700 Subject: [PATCH 2/3] chore: remove unused method Co-authored-by: Haruyaki <52639021+HarunamiYaki@users.noreply.github.com> --- src/index.ts | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/src/index.ts b/src/index.ts index 8500ecc..68723b3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1120,20 +1120,6 @@ module.exports = (app: Probot) => { state: "closed" }).then(async (res) => { if (res.data.labels.find((a) => a.name == "Stale")) { - 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); - } - } else { - prsReviewers.push("@Muunatic"); - } - }); await octokit.rest.issues.addLabels({ owner: res.data.base.repo.owner.login, repo: res.data.base.repo.name, From de1bd6de602c221ebde211bc36525d532f44e15b Mon Sep 17 00:00:00 2001 From: Muunatic Date: Tue, 22 Aug 2023 15:35:31 +0700 Subject: [PATCH 3/3] chore: correct label assignment --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 68723b3..1179153 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1173,7 +1173,7 @@ module.exports = (app: Probot) => { owner: res.data.base.repo.owner.login, repo: res.data.base.repo.name, issue_number: res.data.number, - labels: ["Closed"] + labels: ["Stale"] }); await octokit.rest.issues.createComment({ owner: res.data.base.repo.owner.login,