|
| 1 | +// npx ts-node .github/bot/src/milestone-manager.ts $version |
| 2 | +// This script creates a milestone for the published version and moves all issues/PRs from "Planned" milestone to it |
| 3 | + |
| 4 | +import { octokit, owner, repo } from "./util/octokit"; |
| 5 | + |
| 6 | +async function main() { |
| 7 | + const version = process.argv[2]; |
| 8 | + |
| 9 | + if (!version) { |
| 10 | + console.error( |
| 11 | + "Usage: npx ts-node .github/bot/src/milestone-manager.ts <version>" |
| 12 | + ); |
| 13 | + process.exit(1); |
| 14 | + } |
| 15 | + |
| 16 | + console.log(`Managing milestone for version: ${version}`); |
| 17 | + |
| 18 | + try { |
| 19 | + // 1. Create a new milestone for the version |
| 20 | + console.log(`Creating milestone: ${version}`); |
| 21 | + |
| 22 | + let newMilestone; |
| 23 | + try { |
| 24 | + const { data: milestone } = await octokit.issues.createMilestone({ |
| 25 | + owner, |
| 26 | + repo, |
| 27 | + title: version, |
| 28 | + description: `Release milestone for version ${version}`, |
| 29 | + state: "open", |
| 30 | + }); |
| 31 | + newMilestone = milestone; |
| 32 | + console.log( |
| 33 | + `✓ Created milestone: ${milestone.title} (#${milestone.number})` |
| 34 | + ); |
| 35 | + } catch (error: any) { |
| 36 | + if (error.status === 422) { |
| 37 | + // Milestone already exists, fetch it |
| 38 | + console.log(`Milestone ${version} already exists, fetching it...`); |
| 39 | + const { data: milestones } = await octokit.issues.listMilestones({ |
| 40 | + owner, |
| 41 | + repo, |
| 42 | + state: "open", |
| 43 | + }); |
| 44 | + newMilestone = milestones.find((m) => m.title === version); |
| 45 | + if (!newMilestone) { |
| 46 | + throw new Error(`Could not find existing milestone: ${version}`); |
| 47 | + } |
| 48 | + console.log( |
| 49 | + `✓ Found existing milestone: ${newMilestone.title} (#${newMilestone.number})` |
| 50 | + ); |
| 51 | + } else { |
| 52 | + throw error; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // 2. Find the "Planned" milestone |
| 57 | + console.log(`Looking for "Planned" milestone...`); |
| 58 | + const { data: milestones } = await octokit.issues.listMilestones({ |
| 59 | + owner, |
| 60 | + repo, |
| 61 | + state: "open", |
| 62 | + }); |
| 63 | + |
| 64 | + const plannedMilestone = milestones.find((m) => m.title === "Planned"); |
| 65 | + if (!plannedMilestone) { |
| 66 | + console.log(`⚠ No "Planned" milestone found, nothing to move`); |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + console.log(`✓ Found "Planned" milestone (#${plannedMilestone.number})`); |
| 71 | + |
| 72 | + // 3. Get all issues and PRs from the "Planned" milestone |
| 73 | + console.log(`Fetching issues and PRs from "Planned" milestone...`); |
| 74 | + const { data: issues } = await octokit.issues.listForRepo({ |
| 75 | + owner, |
| 76 | + repo, |
| 77 | + milestone: plannedMilestone.number.toString(), |
| 78 | + state: "all", |
| 79 | + per_page: 100, |
| 80 | + }); |
| 81 | + |
| 82 | + if (issues.length === 0) { |
| 83 | + console.log(`⚠ No issues or PRs found in "Planned" milestone`); |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + console.log(`✓ Found ${issues.length} items in "Planned" milestone`); |
| 88 | + |
| 89 | + // 4. Move each issue/PR to the new milestone |
| 90 | + let movedCount = 0; |
| 91 | + for (const issue of issues) { |
| 92 | + try { |
| 93 | + await octokit.issues.update({ |
| 94 | + owner, |
| 95 | + repo, |
| 96 | + issue_number: issue.number, |
| 97 | + milestone: newMilestone.number, |
| 98 | + }); |
| 99 | + |
| 100 | + const itemType = issue.pull_request ? "PR" : "issue"; |
| 101 | + console.log(`✓ Moved ${itemType} #${issue.number}: ${issue.title}`); |
| 102 | + movedCount++; |
| 103 | + } catch (error: any) { |
| 104 | + console.error(`✗ Failed to move #${issue.number}: ${error.message}`); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + console.log( |
| 109 | + `\n🎉 Successfully moved ${movedCount}/${issues.length} items from "Planned" to "${version}" milestone` |
| 110 | + ); |
| 111 | + } catch (error: any) { |
| 112 | + console.error(`❌ Error managing milestone: ${error.message}`); |
| 113 | + process.exit(1); |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +main(); |
0 commit comments