switch to gh cli instead of api (#8795)

This commit is contained in:
matt korwel
2025-09-18 19:18:27 -07:00
committed by GitHub
parent ec01b1f29f
commit 9954b03ced
+74 -31
View File
@@ -65,14 +65,8 @@ async function main() {
const testMode = argv.test || process.env.TEST_MODE === 'true'; const testMode = argv.test || process.env.TEST_MODE === 'true';
// Initialize GitHub API client only if not in test mode // GitHub CLI is available in the workflow environment
let github; const hasGitHubCli = !testMode;
if (!testMode) {
const { Octokit } = await import('@octokit/rest');
github = new Octokit({
auth: process.env.GH_TOKEN || process.env.GITHUB_TOKEN,
});
}
// Get inputs from CLI args or environment // Get inputs from CLI args or environment
const originalPr = argv.originalPr || process.env.ORIGINAL_PR; const originalPr = argv.originalPr || process.env.ORIGINAL_PR;
@@ -95,7 +89,7 @@ async function main() {
`Analyzing patch creation result for PR ${originalPr} (exit code: ${exitCode})`, `Analyzing patch creation result for PR ${originalPr} (exit code: ${exitCode})`,
); );
const [owner, repo] = repository.split('/'); const [_owner, _repo] = repository.split('/');
const npmTag = channel === 'stable' ? 'latest' : 'preview'; const npmTag = channel === 'stable' ? 'latest' : 'preview';
if (testMode) { if (testMode) {
@@ -189,18 +183,42 @@ ${hasConflicts ? '4' : '3'}. You'll receive updates here when the release comple
**🔗 Track Progress:** **🔗 Track Progress:**
- [View hotfix PR #${mockPrNumber}](${mockPrUrl})`; - [View hotfix PR #${mockPrNumber}](${mockPrUrl})`;
} else if (github) { } else if (hasGitHubCli) {
// Find the actual PR for the new branch // Find the actual PR for the new branch using gh CLI
try { try {
const prList = await github.rest.pulls.list({ const { spawnSync } = await import('node:child_process');
owner, const result = spawnSync(
repo, 'gh',
head: `${owner}:${branch}`, [
state: 'open', 'pr',
}); 'list',
'--head',
branch,
'--state',
'open',
'--json',
'number,title,url',
'--limit',
'1',
],
{ encoding: 'utf8' },
);
if (prList.data.length > 0) { if (result.error) {
const pr = prList.data[0]; throw result.error;
}
if (result.status !== 0) {
throw new Error(
`gh pr list failed with status ${result.status}: ${result.stderr}`,
);
}
const prListOutput = result.stdout;
const prList = JSON.parse(prListOutput);
if (prList.length > 0) {
const pr = prList[0];
const hasConflicts = const hasConflicts =
logContent.includes('Cherry-pick has conflicts') || logContent.includes('Cherry-pick has conflicts') ||
pr.title.includes('[CONFLICTS]'); pr.title.includes('[CONFLICTS]');
@@ -211,15 +229,15 @@ ${hasConflicts ? '4' : '3'}. You'll receive updates here when the release comple
- **Channel**: \`${channel}\` → will publish to npm tag \`${npmTag}\` - **Channel**: \`${channel}\` → will publish to npm tag \`${npmTag}\`
- **Commit**: \`${commit}\` - **Commit**: \`${commit}\`
- **Hotfix Branch**: [\`${branch}\`](https://github.com/${repository}/tree/${branch}) - **Hotfix Branch**: [\`${branch}\`](https://github.com/${repository}/tree/${branch})
- **Hotfix PR**: [#${pr.number}](${pr.html_url})${hasConflicts ? '\n- **⚠️ Status**: Cherry-pick conflicts detected - manual resolution required' : ''} - **Hotfix PR**: [#${pr.number}](${pr.url})${hasConflicts ? '\n- **⚠️ Status**: Cherry-pick conflicts detected - manual resolution required' : ''}
**📝 Next Steps:** **📝 Next Steps:**
1. ${hasConflicts ? '⚠️ **Resolve conflicts** in the hotfix PR first' : 'Review and approve the hotfix PR'}: [#${pr.number}](${pr.html_url})${hasConflicts ? '\n2. **Test your changes** after resolving conflicts' : ''} 1. ${hasConflicts ? '⚠️ **Resolve conflicts** in the hotfix PR first' : 'Review and approve the hotfix PR'}: [#${pr.number}](${pr.url})${hasConflicts ? '\n2. **Test your changes** after resolving conflicts' : ''}
${hasConflicts ? '3' : '2'}. Once merged, the patch release will automatically trigger ${hasConflicts ? '3' : '2'}. Once merged, the patch release will automatically trigger
${hasConflicts ? '4' : '3'}. You'll receive updates here when the release completes ${hasConflicts ? '4' : '3'}. You'll receive updates here when the release completes
**🔗 Track Progress:** **🔗 Track Progress:**
- [View hotfix PR #${pr.number}](${pr.html_url})`; - [View hotfix PR #${pr.number}](${pr.url})`;
} else { } else {
// Fallback if PR not found yet // Fallback if PR not found yet
commentBody = `🚀 **Patch PR Created!** commentBody = `🚀 **Patch PR Created!**
@@ -275,17 +293,42 @@ No output was generated during patch creation.
console.log(commentBody); console.log(commentBody);
console.log('----------------------------------------'); console.log('----------------------------------------');
console.log('\n✅ Comment generation working correctly!'); console.log('\n✅ Comment generation working correctly!');
} else if (github) { } else if (hasGitHubCli) {
await github.rest.issues.createComment({ const { spawnSync } = await import('node:child_process');
owner, const { writeFileSync, unlinkSync } = await import('node:fs');
repo, const { join } = await import('node:path');
issue_number: parseInt(originalPr),
body: commentBody,
});
console.log(`Successfully commented on PR ${originalPr}`); // Write comment to temporary file to avoid shell escaping issues
const tmpFile = join(process.cwd(), `comment-${Date.now()}.md`);
writeFileSync(tmpFile, commentBody);
try {
const result = spawnSync(
'gh',
['pr', 'comment', originalPr.toString(), '--body-file', tmpFile],
{
stdio: 'inherit',
},
);
if (result.error) {
throw result.error;
}
if (result.status !== 0) {
throw new Error(`gh pr comment failed with status ${result.status}`);
}
console.log(`Successfully commented on PR ${originalPr}`);
} finally {
// Clean up temp file
try {
unlinkSync(tmpFile);
} catch (_e) {
// Ignore cleanup errors
}
}
} else { } else {
console.log('No GitHub client available'); console.log('No GitHub CLI available');
} }
} }