File size: 11,006 Bytes
7a1ad33 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 | #!/usr/bin/env node
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* Script for patch release trigger workflow (step 2).
* Handles channel detection, workflow dispatch, and user feedback.
*/
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
/**
* Extract base version, original pr, and originalPr info from hotfix branch name.
* Formats:
* - New NEW: hotfix/v0.5.3/v0.5.4/preview/cherry-pick-abc/pr-1234 -> v0.5.4, preview, 1234
* - New format: hotfix/v0.5.3/preview/cherry-pick-abc -> v0.5.3 and preview
* - Old format: hotfix/v0.5.3/cherry-pick-abc -> v0.5.3 and stable (default)
* We check the formats from newest to oldest. If the channel found is invalid,
* an error is thrown.
*/
function getBranchInfo({ branchName, context }) {
const parts = branchName.split('/');
const version = parts[1];
let prNum;
let channel = 'stable'; // default for old format
if (parts.length >= 6 && (parts[3] === 'stable' || parts[3] === 'preview')) {
channel = parts[3];
const prMatch = parts[5].match(/pr-(\d+)/);
prNum = prMatch[1];
} else if (
parts.length >= 4 &&
(parts[2] === 'stable' || parts[2] === 'preview')
) {
// New format with explicit channel
channel = parts[2];
} else if (context.eventName === 'workflow_dispatch') {
// Manual dispatch, infer from version name
channel = version.includes('preview') ? 'preview' : 'stable';
}
// Validate channel
if (channel !== 'stable' && channel !== 'preview') {
throw new Error(
`Invalid channel: ${channel}. Must be 'stable' or 'preview'.`,
);
}
return { channel, prNum, version };
}
async function main() {
const argv = await yargs(hideBin(process.argv))
.option('head-ref', {
description:
'The hotfix branch name (e.g., hotfix/v0.5.3/preview/cherry-pick-abc1234)',
type: 'string',
demandOption: !process.env.GITHUB_ACTIONS,
})
.option('pr-body', {
description: 'The PR body content',
type: 'string',
default: '',
})
.option('dry-run', {
description: 'Run in test mode without actually triggering workflows',
type: 'boolean',
default: false,
})
.option('test', {
description: 'Test mode - validate logic without GitHub API calls',
type: 'boolean',
default: false,
})
.option('force-skip-tests', {
description: 'Skip the "Run Tests" step in testing',
type: 'boolean',
default: false,
})
.option('environment', {
choices: ['prod', 'dev'],
type: 'string',
default: process.env.ENVIRONMENT || 'prod',
})
.example(
'$0 --head-ref "hotfix/v0.5.3/preview/cherry-pick-abc1234" --test',
'Test channel detection logic',
)
.example(
'$0 --head-ref "hotfix/v0.5.3/stable/cherry-pick-abc1234" --dry-run',
'Test with GitHub API in dry-run mode',
)
.help()
.alias('help', 'h').argv;
const testMode = argv.test || process.env.TEST_MODE === 'true';
const context = {
eventName: process.env.GITHUB_EVENT_NAME || 'pull_request',
repo: {
owner: process.env.GITHUB_REPOSITORY_OWNER || 'google-gemini',
repo: process.env.GITHUB_REPOSITORY_NAME || 'gemini-cli',
},
payload: JSON.parse(process.env.GITHUB_EVENT_PAYLOAD || '{}'),
};
// Get inputs from CLI args or environment
const headRef = argv.headRef || process.env.HEAD_REF;
const environment = argv.environment;
const body = argv.prBody || process.env.PR_BODY || '';
const isDryRun = argv.dryRun || body.includes('[DRY RUN]');
const forceSkipTests =
argv.forceSkipTests || process.env.FORCE_SKIP_TESTS === 'true';
const runId = process.env.GITHUB_RUN_ID || '0';
if (!headRef) {
throw new Error(
'head-ref is required. Use --head-ref or set HEAD_REF environment variable.',
);
}
console.log(`Processing patch trigger for branch: ${headRef}`);
const { prNum, version, channel } = getBranchInfo({
branchName: headRef,
context,
});
let originalPr = prNum;
console.log(`Found originalPr: ${prNum} from hotfix branch`);
// Fallback to using PR search (inconsistent) if no pr found in branch name.
if (!testMode && !originalPr) {
try {
console.log('Looking for original PR using search...');
const { execFileSync } = await import('node:child_process');
// Split search string into searchArgs to prevent triple escaping on the quoted filters
const searchArgs =
`repo:${context.repo.owner}/${context.repo.repo} is:pr in:comments "${headRef}"`.split(
' ',
);
console.log('Search args:', searchArgs);
// Use gh CLI to search for PRs with comments referencing the hotfix branch
const result = execFileSync(
'gh',
[
'search',
'prs',
'--json',
'number,title',
'--limit',
'1',
...searchArgs,
'Patch PR Created',
],
{
encoding: 'utf8',
env: { ...process.env, GH_TOKEN: process.env.GITHUB_TOKEN },
},
);
const searchResults = JSON.parse(result);
if (searchResults && searchResults.length > 0) {
originalPr = searchResults[0].number;
console.log(`Found original PR: #${originalPr}`);
} else {
console.log('Could not find a matching original PR via search.');
}
} catch (e) {
console.log('Could not determine original PR:', e.message);
}
}
if (!originalPr && testMode) {
console.log('Skipping original PR lookup (test mode)');
originalPr = 8655; // Mock for testing
}
if (!originalPr) {
throw new Error(
'Could not find the original PR for this patch. Cannot proceed with release.',
);
}
const releaseRef = `release/${version}-pr-${originalPr}`;
const workflowId =
context.eventName === 'pull_request'
? 'release-patch-3-release.yml'
: process.env.WORKFLOW_ID || 'release-patch-3-release.yml';
console.log(`Detected channel: ${channel}, version: ${version}`);
console.log(`Release ref: ${releaseRef}`);
console.log(`Workflow ID: ${workflowId}`);
console.log(`Dry run: ${isDryRun}`);
if (testMode) {
console.log('\nπ§ͺ TEST MODE - No API calls will be made');
console.log('\nπ Parsed Results:');
console.log(` - Branch: ${headRef}`);
console.log(
` - Channel: ${channel} β npm tag: ${channel === 'stable' ? 'latest' : 'preview'}`,
);
console.log(` - Version: ${version}`);
console.log(` - Release ref: ${releaseRef}`);
console.log(` - Workflow: ${workflowId}`);
console.log(` - Dry run: ${isDryRun}`);
console.log('\nβ
Channel detection logic working correctly!');
return;
}
// Trigger the release workflow
console.log(`Triggering release workflow: ${workflowId}`);
if (!testMode) {
try {
const { execFileSync } = await import('node:child_process');
const args = [
'workflow',
'run',
workflowId,
'--ref',
'main',
'--field',
`type=${channel}`,
'--field',
`dry_run=${isDryRun.toString()}`,
'--field',
`force_skip_tests=${forceSkipTests.toString()}`,
'--field',
`release_ref=${releaseRef}`,
'--field',
`environment=${environment}`,
'--field',
originalPr ? `original_pr=${originalPr.toString()}` : 'original_pr=',
];
console.log(`Running command: gh ${args.join(' ')}`);
execFileSync('gh', args, {
stdio: 'inherit',
env: { ...process.env, GH_TOKEN: process.env.GITHUB_TOKEN },
});
console.log('β
Workflow dispatch completed successfully');
} catch (e) {
console.error('β Failed to dispatch workflow:', e.message);
throw e;
}
} else {
console.log('β
Would trigger workflow with inputs:', {
type: channel,
dry_run: isDryRun.toString(),
force_skip_tests: forceSkipTests.toString(),
release_ref: releaseRef,
original_pr: originalPr ? originalPr.toString() : '',
});
}
// Comment back to original PR if we found it
if (originalPr) {
console.log(`Commenting on original PR ${originalPr}...`);
const npmTag = channel === 'stable' ? 'latest' : 'preview';
const commentBody = `π **[Step 3/4] Patch Release ${environment === 'prod' ? 'Waiting for Approval' : 'Triggered'}!**
**π Release Details:**
- **Environment**: \`${environment}\`
- **Channel**: \`${channel}\` β publishing to npm tag \`${npmTag}\`
- **Version**: \`${version}\`
- **Hotfix PR**: Merged β
- **Release Branch**: [\`${releaseRef}\`](https://github.com/${context.repo.owner}/${context.repo.repo}/tree/${releaseRef})
**β³ Status:** The patch release has been triggered${environment === 'prod' ? ' and is waiting for deployment approval. Please visit the specific workflow run link below and approve the deployment' : ''}. You'll receive another update when it completes.
**π Track Progress:**
- [View release workflow history](https://github.com/${context.repo.owner}/${context.repo.repo}/actions/workflows/${workflowId})
- [This trigger workflow run](https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${runId})`;
if (!testMode) {
let tempDir;
try {
const { execFileSync } = await import('node:child_process');
const { writeFileSync, mkdtempSync } = await import('node:fs');
const { join } = await import('node:path');
const { tmpdir } = await import('node:os');
// Create secure temporary directory and file
tempDir = mkdtempSync(join(tmpdir(), 'patch-trigger-'));
const tempFile = join(tempDir, 'comment.md');
writeFileSync(tempFile, commentBody);
execFileSync(
'gh',
['pr', 'comment', originalPr.toString(), '--body-file', tempFile],
{
stdio: 'inherit',
env: { ...process.env, GH_TOKEN: process.env.GITHUB_TOKEN },
},
);
console.log('β
Comment posted successfully');
} catch (e) {
console.error('β Failed to post comment:', e.message);
// Don't throw here since the main workflow dispatch succeeded
} finally {
// Clean up temp directory and all its contents
if (tempDir) {
try {
const { rmSync } = await import('node:fs');
rmSync(tempDir, { recursive: true, force: true });
} catch (cleanupError) {
console.warn(
'β οΈ Failed to clean up temp directory:',
cleanupError.message,
);
}
}
}
} else {
console.log('β
Would post comment:', commentBody);
}
}
console.log('Patch trigger completed successfully!');
}
main().catch((error) => {
console.error('Error in patch trigger:', error);
process.exit(1);
});
|