|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { execSync } from 'node:child_process'; |
| 4 | + |
| 5 | +function checkIfBeachballChangeNeeded() { |
| 6 | + console.log('Checking if beachball change files are needed...'); |
| 7 | + |
| 8 | + try { |
| 9 | + execSync('npx beachball check --verbose', { encoding: 'utf8', stdio: 'pipe' }); |
| 10 | + console.log('✅ Beachball check passed - no change files needed'); |
| 11 | + return false; |
| 12 | + } catch (error) { |
| 13 | + console.log('❌ Beachball check failed - change files needed'); |
| 14 | + return true; |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +function determineChangeType(commitMessage) { |
| 19 | + console.log(`Analyzing commit message: "${commitMessage}"`); |
| 20 | + |
| 21 | + // Search for keywords to determine change type. This is best effort and intended to be reviewed at PR time. |
| 22 | + if (/breaking|major/i.test(commitMessage)) { |
| 23 | + return 'major'; |
| 24 | + } else { |
| 25 | + return 'patch'; |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +function getLatestCommitMessage() { |
| 30 | + const commitMessage = execSync('git log -1 --pretty=format:"%s"', { encoding: 'utf8' }).trim(); |
| 31 | + return commitMessage.replace(/^"|"$/g, ''); // Remove surrounding quotes if present |
| 32 | +} |
| 33 | + |
| 34 | +function createBeachballChangeFile(commitMessage, changeType) { |
| 35 | + console.log(`Creating beachball change file with type: ${changeType}`); |
| 36 | + |
| 37 | + try { |
| 38 | + // Create change file using beachball CLI |
| 39 | + execSync('git config --local user.email "[email protected]"'); |
| 40 | + execSync('git config --local user.name "mc-npm"'); |
| 41 | + |
| 42 | + execSync(`npx beachball change --type "${changeType}" --message "${commitMessage}"`, { |
| 43 | + encoding: 'utf8', |
| 44 | + stdio: 'inherit', |
| 45 | + }); |
| 46 | + |
| 47 | + console.log('✅ Successfully created and committed beachball change files'); |
| 48 | + return true; |
| 49 | + } catch (error) { |
| 50 | + console.error('❌ Failed to create beachball change file:', error.message); |
| 51 | + return false; |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +try { |
| 56 | + const isDryRun = process.argv.includes('-n'); |
| 57 | + console.log('🤖 Starting dependabot beachball change file automation...'); |
| 58 | + |
| 59 | + // Step 1: Check if beachball change files are needed |
| 60 | + const needsChange = checkIfBeachballChangeNeeded(); |
| 61 | + |
| 62 | + if (!needsChange) { |
| 63 | + process.exit(0); |
| 64 | + } |
| 65 | + |
| 66 | + // Step 2: Get commit message and determine change type |
| 67 | + const commitMessage = getLatestCommitMessage(); |
| 68 | + const changeType = determineChangeType(commitMessage); |
| 69 | + |
| 70 | + console.log(`📝 Commit message: "${commitMessage}"`); |
| 71 | + console.log(`📊 Determined change type: ${changeType}`); |
| 72 | + |
| 73 | + // Step 3: Create beachball change file |
| 74 | + const changeFileCreated = createBeachballChangeFile(commitMessage, changeType, isDryRun); |
| 75 | + |
| 76 | + if (!changeFileCreated) { |
| 77 | + console.error('❌ Failed to create change file'); |
| 78 | + process.exit(1); |
| 79 | + } |
| 80 | + |
| 81 | + console.log('🎉 Successfully automated beachball change file creation! Pushing changes.'); |
| 82 | + if (!isDryRun) { |
| 83 | + execSync('git push'); |
| 84 | + } |
| 85 | +} catch (error) { |
| 86 | + console.error('❌ Error in dependabot beachball automation:', error.message); |
| 87 | + console.error(error.stack); |
| 88 | + process.exit(1); |
| 89 | +} |
0 commit comments