| 43 | // -- |
| 44 | |
| 45 | async function markStale(owner: string, repo: string) { |
| 46 | const staleDays = lifecycle.find((l) => l.label === "stale")!.days; |
| 47 | const cutoff = new Date(); |
| 48 | cutoff.setDate(cutoff.getDate() - staleDays); |
| 49 | |
| 50 | let labeled = 0; |
| 51 | |
| 52 | console.log(`\n=== marking stale (${staleDays}d inactive) ===`); |
| 53 | |
| 54 | for (let page = 1; page <= 10; page++) { |
| 55 | const issues = await githubRequest<any[]>( |
| 56 | `/repos/${owner}/${repo}/issues?state=open&sort=updated&direction=asc&per_page=100&page=${page}` |
| 57 | ); |
| 58 | if (issues.length === 0) break; |
| 59 | |
| 60 | for (const issue of issues) { |
| 61 | if (issue.pull_request) continue; |
| 62 | if (issue.locked) continue; |
| 63 | if (issue.assignees?.length > 0) continue; |
| 64 | |
| 65 | const updatedAt = new Date(issue.updated_at); |
| 66 | if (updatedAt > cutoff) return labeled; |
| 67 | |
| 68 | const alreadyStale = issue.labels?.some( |
| 69 | (l: any) => l.name === "stale" || l.name === "autoclose" |
| 70 | ); |
| 71 | if (alreadyStale) continue; |
| 72 | |
| 73 | const thumbsUp = issue.reactions?.["+1"] ?? 0; |
| 74 | if (thumbsUp >= STALE_UPVOTE_THRESHOLD) continue; |
| 75 | |
| 76 | const base = `/repos/${owner}/${repo}/issues/${issue.number}`; |
| 77 | |
| 78 | if (DRY_RUN) { |
| 79 | const age = Math.floor((Date.now() - updatedAt.getTime()) / 86400000); |
| 80 | console.log(`#${issue.number}: would label stale (${age}d inactive) — ${issue.title}`); |
| 81 | } else { |
| 82 | await githubRequest(`${base}/labels`, "POST", { labels: ["stale"] }); |
| 83 | console.log(`#${issue.number}: labeled stale — ${issue.title}`); |
| 84 | } |
| 85 | labeled++; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | return labeled; |
| 90 | } |
| 91 | |
| 92 | async function closeExpired(owner: string, repo: string) { |
| 93 | let closed = 0; |