|
1 | 1 | import { |
| 2 | + checkRulesetExists, |
2 | 3 | ciWorkflowExists, |
| 4 | + enableStackCheckProtection, |
3 | 5 | getBranchProtectionUrl, |
4 | 6 | getRepoInfoFromRemote, |
| 7 | + JJ, |
5 | 8 | setupCI, |
6 | 9 | shellExecutor, |
7 | 10 | } from "@array/core"; |
8 | | -import { cyan, dim, formatSuccess, yellow } from "../utils/output"; |
| 11 | +import { cyan, dim, formatError, formatSuccess, yellow } from "../utils/output"; |
| 12 | +import { confirm } from "../utils/prompt"; |
9 | 13 |
|
10 | 14 | export async function ci(): Promise<void> { |
11 | 15 | const cwd = process.cwd(); |
12 | 16 |
|
| 17 | + // Create workflow file if not exists |
13 | 18 | if (ciWorkflowExists(cwd)) { |
14 | | - console.log(yellow("Stack check workflow already exists")); |
15 | | - console.log(dim(" .github/workflows/array-stack-check.yml")); |
| 19 | + console.log(dim("Stack check workflow already exists")); |
| 20 | + } else { |
| 21 | + const result = setupCI(cwd); |
| 22 | + if (result.created) { |
| 23 | + console.log( |
| 24 | + formatSuccess("Created .github/workflows/array-stack-check.yml"), |
| 25 | + ); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + const repoInfo = await getRepoInfo(cwd); |
| 30 | + if (!repoInfo) { |
16 | 31 | console.log(); |
17 | | - await printInstructions(cwd); |
| 32 | + console.log(yellow("Could not determine repository.")); |
| 33 | + console.log( |
| 34 | + dim( |
| 35 | + "Manually add 'Stack Check' as a required status check in GitHub settings.", |
| 36 | + ), |
| 37 | + ); |
18 | 38 | return; |
19 | 39 | } |
20 | 40 |
|
21 | | - const result = setupCI(cwd); |
| 41 | + console.log(); |
22 | 42 |
|
23 | | - if (result.created) { |
24 | | - console.log(formatSuccess("Created .github/workflows/array-stack-check.yml")); |
25 | | - console.log(); |
26 | | - await printInstructions(cwd); |
| 43 | + // Check if ruleset already exists to show appropriate prompt |
| 44 | + const rulesetExists = await checkRulesetExists( |
| 45 | + repoInfo.owner, |
| 46 | + repoInfo.repo, |
| 47 | + shellExecutor, |
| 48 | + cwd, |
| 49 | + ); |
| 50 | + |
| 51 | + const prompt = rulesetExists |
| 52 | + ? "Update ruleset to latest? (needs admin access)" |
| 53 | + : "Enable 'Stack Check' as required? (needs admin access)"; |
| 54 | + |
| 55 | + const shouldProceed = await confirm(prompt); |
| 56 | + |
| 57 | + if (shouldProceed) { |
| 58 | + const success = await tryEnableProtection(cwd, repoInfo, rulesetExists); |
| 59 | + if (success) return; |
27 | 60 | } |
| 61 | + |
| 62 | + // Show manual URL if they declined or API failed |
| 63 | + console.log(); |
| 64 | + const url = getBranchProtectionUrl(repoInfo.owner, repoInfo.repo); |
| 65 | + console.log("To enable manually, create a ruleset:"); |
| 66 | + console.log(cyan(` ${url}`)); |
| 67 | + console.log( |
| 68 | + dim(" → Add 'Require status checks' → Type 'Stack Check' → Create"), |
| 69 | + ); |
28 | 70 | } |
29 | 71 |
|
30 | | -async function printInstructions(cwd: string): Promise<void> { |
| 72 | +async function getRepoInfo( |
| 73 | + cwd: string, |
| 74 | +): Promise<{ owner: string; repo: string } | null> { |
31 | 75 | const remoteResult = await shellExecutor.execute( |
32 | 76 | "git", |
33 | 77 | ["config", "--get", "remote.origin.url"], |
34 | 78 | { cwd }, |
35 | 79 | ); |
36 | 80 |
|
37 | | - if (remoteResult.exitCode === 0) { |
38 | | - const repoInfo = getRepoInfoFromRemote(remoteResult.stdout.trim()); |
39 | | - if (repoInfo.ok) { |
40 | | - const url = getBranchProtectionUrl(repoInfo.value.owner, repoInfo.value.repo); |
41 | | - console.log("To enforce stack ordering, add 'Stack Check' as a required check:"); |
42 | | - console.log(cyan(` ${url}`)); |
43 | | - console.log(dim(" → Edit rule for 'main' → Require status checks → Add 'Stack Check'")); |
44 | | - return; |
| 81 | + if (remoteResult.exitCode !== 0) return null; |
| 82 | + |
| 83 | + const repoInfo = getRepoInfoFromRemote(remoteResult.stdout.trim()); |
| 84 | + return repoInfo.ok ? repoInfo.value : null; |
| 85 | +} |
| 86 | + |
| 87 | +async function tryEnableProtection( |
| 88 | + cwd: string, |
| 89 | + repoInfo: { owner: string; repo: string }, |
| 90 | + isUpdate: boolean, |
| 91 | +): Promise<boolean> { |
| 92 | + const jj = new JJ({ cwd }); |
| 93 | + const trunk = jj.getTrunk(); |
| 94 | + |
| 95 | + console.log( |
| 96 | + dim(isUpdate ? "Updating ruleset..." : `Creating ruleset for ${trunk}...`), |
| 97 | + ); |
| 98 | + |
| 99 | + const result = await enableStackCheckProtection( |
| 100 | + { owner: repoInfo.owner, repo: repoInfo.repo, trunk }, |
| 101 | + shellExecutor, |
| 102 | + cwd, |
| 103 | + ); |
| 104 | + |
| 105 | + const rulesetsUrl = `https://github.com/${repoInfo.owner}/${repoInfo.repo}/settings/rules`; |
| 106 | + |
| 107 | + if (result.success) { |
| 108 | + if (result.updated) { |
| 109 | + console.log(formatSuccess(`Updated ruleset 'Array Stack Check'`)); |
| 110 | + } else if (result.alreadyEnabled) { |
| 111 | + console.log(formatSuccess(`Ruleset 'Array Stack Check' already exists`)); |
| 112 | + } else { |
| 113 | + console.log(formatSuccess(`Created ruleset 'Array Stack Check'`)); |
45 | 114 | } |
| 115 | + console.log(); |
| 116 | + console.log( |
| 117 | + `PRs in a stack will now be blocked until their downstack PRs are merged.`, |
| 118 | + ); |
| 119 | + console.log(); |
| 120 | + console.log(`View or edit the ruleset:`); |
| 121 | + console.log(cyan(` ${rulesetsUrl}`)); |
| 122 | + return true; |
46 | 123 | } |
47 | 124 |
|
48 | | - console.log("To enforce stack ordering:"); |
49 | | - console.log(dim(" 1. Go to Repo Settings → Branches → Branch protection rules")); |
50 | | - console.log(dim(" 2. Edit rule for 'main' (or create one)")); |
51 | | - console.log(dim(" 3. Enable 'Require status checks to pass'")); |
52 | | - console.log(dim(" 4. Search for 'Stack Check' and add it")); |
| 125 | + console.log(formatError(result.error ?? "Failed to create ruleset")); |
| 126 | + return false; |
53 | 127 | } |
0 commit comments