|
| 1 | +#!/usr/bin/env bun |
| 2 | + |
| 3 | +const repo = "anomalyco/opencode" |
| 4 | +const days = 60 |
| 5 | +const msg = |
| 6 | + "To stay organized issues are automatically closed after 90 days of no activity. If the issue is still relevant please open a new one." |
| 7 | + |
| 8 | +const token = process.env.GITHUB_TOKEN |
| 9 | +if (!token) { |
| 10 | + console.error("GITHUB_TOKEN environment variable is required") |
| 11 | + process.exit(1) |
| 12 | +} |
| 13 | + |
| 14 | +const cutoff = new Date(Date.now() - days * 24 * 60 * 60 * 1000) |
| 15 | + |
| 16 | +type Issue = { |
| 17 | + number: number |
| 18 | + updated_at: string |
| 19 | +} |
| 20 | + |
| 21 | +const headers = { |
| 22 | + Authorization: `Bearer ${token}`, |
| 23 | + "Content-Type": "application/json", |
| 24 | + Accept: "application/vnd.github+json", |
| 25 | + "X-GitHub-Api-Version": "2022-11-28", |
| 26 | +} |
| 27 | + |
| 28 | +async function close(num: number) { |
| 29 | + const base = `https://api.github.com/repos/${repo}/issues/${num}` |
| 30 | + |
| 31 | + const comment = await fetch(`${base}/comments`, { |
| 32 | + method: "POST", |
| 33 | + headers, |
| 34 | + body: JSON.stringify({ body: msg }), |
| 35 | + }) |
| 36 | + if (!comment.ok) throw new Error(`Failed to comment #${num}: ${comment.status} ${comment.statusText}`) |
| 37 | + |
| 38 | + const patch = await fetch(base, { |
| 39 | + method: "PATCH", |
| 40 | + headers, |
| 41 | + body: JSON.stringify({ state: "closed", state_reason: "not_planned" }), |
| 42 | + }) |
| 43 | + if (!patch.ok) throw new Error(`Failed to close #${num}: ${patch.status} ${patch.statusText}`) |
| 44 | + |
| 45 | + console.log(`Closed https://github.com/${repo}/issues/${num}`) |
| 46 | +} |
| 47 | + |
| 48 | +async function main() { |
| 49 | + let page = 1 |
| 50 | + let closed = 0 |
| 51 | + |
| 52 | + while (true) { |
| 53 | + const res = await fetch( |
| 54 | + `https://api.github.com/repos/${repo}/issues?state=open&sort=updated&direction=asc&per_page=100&page=${page}`, |
| 55 | + { headers }, |
| 56 | + ) |
| 57 | + if (!res.ok) throw new Error(res.statusText) |
| 58 | + |
| 59 | + const all = (await res.json()) as Issue[] |
| 60 | + if (all.length === 0) break |
| 61 | + |
| 62 | + const stale: number[] = [] |
| 63 | + for (const i of all) { |
| 64 | + const updated = new Date(i.updated_at) |
| 65 | + if (updated < cutoff) { |
| 66 | + stale.push(i.number) |
| 67 | + } else { |
| 68 | + console.log(`\nFound fresh issue #${i.number}, stopping`) |
| 69 | + if (stale.length > 0) { |
| 70 | + await Promise.all(stale.map(close)) |
| 71 | + closed += stale.length |
| 72 | + } |
| 73 | + console.log(`Closed ${closed} issues total`) |
| 74 | + return |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + if (stale.length > 0) { |
| 79 | + await Promise.all(stale.map(close)) |
| 80 | + closed += stale.length |
| 81 | + } |
| 82 | + |
| 83 | + page++ |
| 84 | + } |
| 85 | + |
| 86 | + console.log(`Closed ${closed} issues total`) |
| 87 | +} |
| 88 | + |
| 89 | +main().catch((err) => { |
| 90 | + console.error("Error:", err) |
| 91 | + process.exit(1) |
| 92 | +}) |
0 commit comments