|
| 1 | +import { execSync } from "node:child_process"; |
| 2 | +import { parseArgs } from "node:util"; |
| 3 | + |
| 4 | +const CI = process.env.CI === "true"; |
| 5 | + |
| 6 | +let oneYearAgo = new Date(Date.now() - 365 * 24 * 60 * 60 * 1000); |
| 7 | + |
| 8 | +const { values: args } = parseArgs({ |
| 9 | + options: { |
| 10 | + date: { |
| 11 | + type: "string", |
| 12 | + short: "d", |
| 13 | + default: new Date(oneYearAgo).toISOString().substring(0, 10), |
| 14 | + }, |
| 15 | + }, |
| 16 | + strict: true, |
| 17 | +}); |
| 18 | + |
| 19 | +if (!args.date || !/^\d{4}-\d{2}-\d{2}$/.test(args.date)) { |
| 20 | + console.error("Missing or invalid date - expected YYYY-MM-DD."); |
| 21 | + process.exit(1); |
| 22 | +} else { |
| 23 | + run(); |
| 24 | +} |
| 25 | + |
| 26 | +async function run() { |
| 27 | + /** @type {(ms: number) => Promise<void>} */ |
| 28 | + let sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); |
| 29 | + |
| 30 | + /** @type {(q: string) => string} */ |
| 31 | + let getIssuesCmd = (q) => `gh issue list --search "${q}" --json number`; |
| 32 | + |
| 33 | + /** @type {(n: number) => string} */ |
| 34 | + let getCommentCmd = (n) => |
| 35 | + `gh issue comment ${n} -F ./scripts/close-v2-issues-comment.md`; |
| 36 | + |
| 37 | + /** @type {(n: number) => string} */ |
| 38 | + let getCloseCmd = (n) => `gh issue close ${n} -r "not planned"`; |
| 39 | + |
| 40 | + let issueCmd = getIssuesCmd(`is:issue state:open updated:<${args.date}`); |
| 41 | + console.log(`Executing command: ${issueCmd}`); |
| 42 | + let result = execSync(issueCmd).toString(); |
| 43 | + console.log(`Result: ${result}`); |
| 44 | + |
| 45 | + let issues = JSON.parse(result).map((i) => i.number); |
| 46 | + if (issues.length > 50) { |
| 47 | + console.log("❌ Refusing to close more than 50 issues at once, exiting."); |
| 48 | + process.exit(1); |
| 49 | + } |
| 50 | + |
| 51 | + console.log(`Parsed ${issues.length} issues`); |
| 52 | + |
| 53 | + for (let issue of issues) { |
| 54 | + console.log(`--- Processing issue #${issue} ---`); |
| 55 | + let commentResult = runCmdIfTokenExists(getCommentCmd(issue)); |
| 56 | + console.log(`Commented on issue #${issue}: ${commentResult}`); |
| 57 | + await sleep(250); |
| 58 | + |
| 59 | + runCmdIfTokenExists(getCloseCmd(issue)); |
| 60 | + // No log here since the GH CLI already logs for issue close |
| 61 | + await sleep(250); |
| 62 | + } |
| 63 | + |
| 64 | + console.log("Done!"); |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * @param {string} cmd |
| 69 | + * @return {string} |
| 70 | + */ |
| 71 | +function runCmdIfTokenExists(cmd) { |
| 72 | + if (CI) { |
| 73 | + console.log(); |
| 74 | + return execSync(cmd).toString(); |
| 75 | + } else { |
| 76 | + console.log(`⚠️ Local run, skipping command: ${cmd}`); |
| 77 | + return "<skipped>"; |
| 78 | + } |
| 79 | +} |
0 commit comments