close-issues.ts 2.7 KB

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