|
| 1 | +#!/usr/bin/env bun |
| 2 | + |
| 3 | +import { consola } from "consola"; |
| 4 | +import type { |
| 5 | + GitHubComment, |
| 6 | + GitHubIssue, |
| 7 | + GitHubReaction, |
| 8 | +} from "./lib/github.js"; |
| 9 | +import { |
| 10 | + API_BASE, |
| 11 | + fetchAllPages, |
| 12 | + fetchGitHub, |
| 13 | + GITHUB_REPOSITORY_NAME, |
| 14 | + GITHUB_REPOSITORY_OWNER, |
| 15 | + getIssueComments, |
| 16 | +} from "./lib/github.js"; |
| 17 | + |
| 18 | +const THREE_DAYS_MS = 3 * 24 * 60 * 60 * 1000; |
| 19 | + |
| 20 | +async function getOpenIssuesOlderThan3Days(): Promise<GitHubIssue[]> { |
| 21 | + const threeDaysAgo = new Date(Date.now() - THREE_DAYS_MS); |
| 22 | + const url = `${API_BASE}/issues?state=open&per_page=100&sort=created&direction=asc`; |
| 23 | + |
| 24 | + const issues = await fetchAllPages<GitHubIssue>(url); |
| 25 | + |
| 26 | + return issues.filter((issue) => { |
| 27 | + if (issue.pull_request) return false; |
| 28 | + return new Date(issue.created_at) < threeDaysAgo; |
| 29 | + }); |
| 30 | +} |
| 31 | + |
| 32 | +async function getCommentReactions( |
| 33 | + commentId: number, |
| 34 | +): Promise<GitHubReaction[]> { |
| 35 | + const url = `${API_BASE}/issues/comments/${commentId}/reactions?per_page=100`; |
| 36 | + return fetchAllPages<GitHubReaction>(url); |
| 37 | +} |
| 38 | + |
| 39 | +async function closeIssue(issueNumber: number, reason: string): Promise<void> { |
| 40 | + await fetchGitHub(`${API_BASE}/issues/${issueNumber}/labels`, { |
| 41 | + method: "POST", |
| 42 | + body: JSON.stringify({ labels: ["duplicate"] }), |
| 43 | + }); |
| 44 | + |
| 45 | + await fetchGitHub(`${API_BASE}/issues/${issueNumber}`, { |
| 46 | + method: "PATCH", |
| 47 | + body: JSON.stringify({ |
| 48 | + state: "closed", |
| 49 | + state_reason: "not_planned", |
| 50 | + }), |
| 51 | + }); |
| 52 | + |
| 53 | + await fetchGitHub(`${API_BASE}/issues/${issueNumber}/comments`, { |
| 54 | + method: "POST", |
| 55 | + body: JSON.stringify({ |
| 56 | + body: reason, |
| 57 | + }), |
| 58 | + }); |
| 59 | +} |
| 60 | + |
| 61 | +async function hasActivityAfterComment( |
| 62 | + issue: GitHubIssue, |
| 63 | + botCommentDate: Date, |
| 64 | +): Promise<boolean> { |
| 65 | + const comments = await getIssueComments(issue.number); |
| 66 | + |
| 67 | + const laterComments = comments.filter((comment) => { |
| 68 | + if (comment.user?.login.endsWith("[bot]")) return false; |
| 69 | + const commentDate = new Date(comment.created_at); |
| 70 | + return commentDate > botCommentDate; |
| 71 | + }); |
| 72 | + |
| 73 | + return laterComments.length > 0; |
| 74 | +} |
| 75 | + |
| 76 | +async function hasCreatorThumbsDown( |
| 77 | + issue: GitHubIssue, |
| 78 | + botComment: GitHubComment, |
| 79 | +): Promise<boolean> { |
| 80 | + if (!issue.user) { |
| 81 | + return false; |
| 82 | + } |
| 83 | + |
| 84 | + const reactions = await getCommentReactions(botComment.id); |
| 85 | + |
| 86 | + return reactions.some( |
| 87 | + (reaction) => |
| 88 | + reaction.content === "-1" && reaction.user?.login === issue.user?.login, |
| 89 | + ); |
| 90 | +} |
| 91 | + |
| 92 | +async function main(): Promise<void> { |
| 93 | + consola.info("Starting auto-close duplicates script..."); |
| 94 | + consola.info( |
| 95 | + `Repository: ${GITHUB_REPOSITORY_OWNER}/${GITHUB_REPOSITORY_NAME}`, |
| 96 | + ); |
| 97 | + |
| 98 | + const issues = await getOpenIssuesOlderThan3Days(); |
| 99 | + consola.info(`Found ${issues.length} open issues older than 3 days`); |
| 100 | + |
| 101 | + let processedCount = 0; |
| 102 | + let closedCount = 0; |
| 103 | + |
| 104 | + for (const issue of issues) { |
| 105 | + processedCount++; |
| 106 | + consola.info(`Processing issue #${issue.number}: ${issue.title}`); |
| 107 | + |
| 108 | + const comments = await getIssueComments(issue.number); |
| 109 | + |
| 110 | + const botComment = comments.find( |
| 111 | + (comment) => |
| 112 | + comment.user?.login === "github-actions[bot]" && |
| 113 | + comment.body.includes("<!-- ai-duplicate-check -->"), |
| 114 | + ); |
| 115 | + |
| 116 | + if (!botComment) { |
| 117 | + consola.info(` No duplicate bot comment found, skipping`); |
| 118 | + await new Promise((resolve) => setTimeout(resolve, 1000)); |
| 119 | + continue; |
| 120 | + } |
| 121 | + |
| 122 | + const botCommentDate = new Date(botComment.created_at); |
| 123 | + const now = new Date(); |
| 124 | + const timeSinceComment = now.getTime() - botCommentDate.getTime(); |
| 125 | + |
| 126 | + if (timeSinceComment < THREE_DAYS_MS) { |
| 127 | + consola.info(` Bot comment is less than 3 days old, skipping`); |
| 128 | + await new Promise((resolve) => setTimeout(resolve, 1000)); |
| 129 | + continue; |
| 130 | + } |
| 131 | + |
| 132 | + const hasActivity = await hasActivityAfterComment(issue, botCommentDate); |
| 133 | + if (hasActivity) { |
| 134 | + consola.info(` Has activity after bot comment, skipping`); |
| 135 | + await new Promise((resolve) => setTimeout(resolve, 1000)); |
| 136 | + continue; |
| 137 | + } |
| 138 | + |
| 139 | + const hasThumbsDown = await hasCreatorThumbsDown(issue, botComment); |
| 140 | + if (hasThumbsDown) { |
| 141 | + consola.info(` Creator reacted with thumbs down, skipping`); |
| 142 | + await new Promise((resolve) => setTimeout(resolve, 1000)); |
| 143 | + continue; |
| 144 | + } |
| 145 | + |
| 146 | + consola.info(` Closing issue #${issue.number} as duplicate`); |
| 147 | + await closeIssue( |
| 148 | + issue.number, |
| 149 | + "This issue has been automatically closed as a duplicate. It was marked as a duplicate over 3 days ago with no further activity. If you believe this was closed in error, please comment and we'll re-evaluate.", |
| 150 | + ); |
| 151 | + |
| 152 | + closedCount++; |
| 153 | + |
| 154 | + await new Promise((resolve) => setTimeout(resolve, 1000)); |
| 155 | + } |
| 156 | + |
| 157 | + consola.info("\n=== Summary ==="); |
| 158 | + consola.info(`Processed issues: ${processedCount}`); |
| 159 | + consola.info(`Closed issues: ${closedCount}`); |
| 160 | +} |
| 161 | + |
| 162 | +try { |
| 163 | + await main(); |
| 164 | +} catch (error) { |
| 165 | + consola.error("Error running auto-close script:", error); |
| 166 | + process.exit(1); |
| 167 | +} |
0 commit comments