|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const { execSync } = require('child_process') |
| 4 | + |
| 5 | +// Parse command line arguments |
| 6 | +const args = process.argv.slice(2) |
| 7 | +const versionType = args[0] || 'patch' // patch, minor, major, or specific version like 1.2.3 |
| 8 | + |
| 9 | +function log(message) { |
| 10 | + console.log(`${message}`) |
| 11 | +} |
| 12 | + |
| 13 | +function error(message) { |
| 14 | + console.error(`❌ ${message}`) |
| 15 | + process.exit(1) |
| 16 | +} |
| 17 | + |
| 18 | +function formatTimestamp() { |
| 19 | + const now = new Date() |
| 20 | + const options = { |
| 21 | + month: 'long', |
| 22 | + day: 'numeric', |
| 23 | + hour: '2-digit', |
| 24 | + minute: '2-digit', |
| 25 | + second: '2-digit', |
| 26 | + timeZoneName: 'short', |
| 27 | + } |
| 28 | + return now.toLocaleDateString('en-US', options) |
| 29 | +} |
| 30 | + |
| 31 | +function checkGitHubToken() { |
| 32 | + const token = process.env.CODEBUFF_GITHUB_TOKEN |
| 33 | + if (!token) { |
| 34 | + error( |
| 35 | + 'CODEBUFF_GITHUB_TOKEN environment variable is required but not set.\n' + |
| 36 | + 'Please set it with your GitHub personal access token or use the infisical setup.' |
| 37 | + ) |
| 38 | + } |
| 39 | + |
| 40 | + // Set GITHUB_TOKEN for compatibility with existing curl commands |
| 41 | + process.env.GITHUB_TOKEN = token |
| 42 | + return token |
| 43 | +} |
| 44 | + |
| 45 | +async function triggerWorkflow(versionType) { |
| 46 | + if (!process.env.GITHUB_TOKEN) { |
| 47 | + error('GITHUB_TOKEN environment variable is required but not set') |
| 48 | + } |
| 49 | + |
| 50 | + try { |
| 51 | + // Use workflow filename instead of ID |
| 52 | + const triggerCmd = `curl -s -w "HTTP Status: %{http_code}" -X POST \ |
| 53 | + -H "Accept: application/vnd.github.v3+json" \ |
| 54 | + -H "Authorization: token ${process.env.GITHUB_TOKEN}" \ |
| 55 | + -H "Content-Type: application/json" \ |
| 56 | + https://api.github.com/repos/CodebuffAI/codebuff/actions/workflows/sdk-release-prod.yml/dispatches \ |
| 57 | + -d '{"ref":"main","inputs":{"version_type":"${versionType}"}}'` |
| 58 | + |
| 59 | + const response = execSync(triggerCmd, { encoding: 'utf8' }) |
| 60 | + |
| 61 | + // Check if response contains error message |
| 62 | + if (response.includes('workflow_dispatch')) { |
| 63 | + log(`⚠️ Workflow dispatch failed: ${response}`) |
| 64 | + log('The workflow may need to be updated on GitHub. Continuing anyway...') |
| 65 | + log( |
| 66 | + 'Please manually trigger the workflow at: https://github.com/CodebuffAI/codebuff/actions/workflows/sdk-release-prod.yml', |
| 67 | + ) |
| 68 | + } else { |
| 69 | + log('🎉 SDK release workflow triggered!') |
| 70 | + } |
| 71 | + } catch (err) { |
| 72 | + log(`⚠️ Failed to trigger workflow automatically: ${err.message}`) |
| 73 | + log( |
| 74 | + 'You may need to trigger it manually at: https://github.com/CodebuffAI/codebuff/actions/workflows/sdk-release-prod.yml', |
| 75 | + ) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +async function main() { |
| 80 | + log('🚀 Initiating SDK release...') |
| 81 | + log(`Date: ${formatTimestamp()}`) |
| 82 | + |
| 83 | + // Check for local GitHub token |
| 84 | + checkGitHubToken() |
| 85 | + log('✅ Using local CODEBUFF_GITHUB_TOKEN') |
| 86 | + |
| 87 | + log(`Version bump type: ${versionType}`) |
| 88 | + |
| 89 | + // Trigger the workflow |
| 90 | + await triggerWorkflow(versionType) |
| 91 | + |
| 92 | + log('') |
| 93 | + log('Monitor progress at: https://github.com/CodebuffAI/codebuff/actions') |
| 94 | +} |
| 95 | + |
| 96 | +main().catch((err) => { |
| 97 | + error(`SDK release failed: ${err.message}`) |
| 98 | +}) |
0 commit comments