File size: 7,840 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
#!/usr/bin/env node

/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

/**
 * Script for commenting back to original PR with patch release results.
 * Used by the patch release workflow (step 3).
 */

import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';

async function main() {
  const argv = await yargs(hideBin(process.argv))
    .option('original-pr', {
      description: 'The original PR number to comment on',
      type: 'number',
      demandOption: !process.env.GITHUB_ACTIONS,
    })
    .option('success', {
      description: 'Whether the release succeeded',
      type: 'boolean',
    })
    .option('release-version', {
      description: 'The release version (e.g., 0.5.4)',
      type: 'string',
      demandOption: !process.env.GITHUB_ACTIONS,
    })
    .option('release-tag', {
      description: 'The release tag (e.g., v0.5.4)',
      type: 'string',
    })
    .option('npm-tag', {
      description: 'The npm tag (latest or preview)',
      type: 'string',
    })
    .option('channel', {
      description: 'The channel (stable or preview)',
      type: 'string',
      choices: ['stable', 'preview'],
    })
    .option('dry-run', {
      description: 'Whether this was a dry run',
      type: 'boolean',
      default: false,
    })
    .option('test', {
      description: 'Test mode - validate logic without GitHub API calls',
      type: 'boolean',
      default: false,
    })
    .example(
      '$0 --original-pr 8655 --success --release-version "0.5.4" --channel stable --test',
      'Test success comment',
    )
    .example(
      '$0 --original-pr 8655 --no-success --channel preview --test',
      'Test failure comment',
    )
    .help()
    .alias('help', 'h').argv;

  const testMode = argv.test || process.env.TEST_MODE === 'true';

  // Initialize GitHub API client only if not in test mode
  let github;
  if (!testMode) {
    const { Octokit } = await import('@octokit/rest');
    github = new Octokit({
      auth: process.env.GITHUB_TOKEN,
    });
  }

  const repo = {
    owner: process.env.GITHUB_REPOSITORY_OWNER || 'google-gemini',
    repo: process.env.GITHUB_REPOSITORY_NAME || 'gemini-cli',
  };

  // Get inputs from CLI args or environment
  const originalPr = argv.originalPr || process.env.ORIGINAL_PR;
  const success =
    argv.success !== undefined ? argv.success : process.env.SUCCESS === 'true';
  const releaseVersion = argv.releaseVersion || process.env.RELEASE_VERSION;
  const releaseTag =
    argv.releaseTag ||
    process.env.RELEASE_TAG ||
    (releaseVersion ? `v${releaseVersion}` : null);
  const npmTag =
    argv.npmTag ||
    process.env.NPM_TAG ||
    (argv.channel === 'stable' ? 'latest' : 'preview');
  const channel = argv.channel || process.env.CHANNEL || 'stable';
  const dryRun = argv.dryRun || process.env.DRY_RUN === 'true';
  const runId = process.env.GITHUB_RUN_ID || '12345678';
  const raceConditionFailure = process.env.RACE_CONDITION_FAILURE === 'true';

  // Current version info for race condition failures
  const currentReleaseVersion = process.env.CURRENT_RELEASE_VERSION;
  const currentReleaseTag = process.env.CURRENT_RELEASE_TAG;
  const currentPreviousTag = process.env.CURRENT_PREVIOUS_TAG;

  if (!originalPr) {
    console.log('No original PR specified, skipping comment');
    return;
  }

  console.log(
    `Commenting on original PR ${originalPr} with ${success ? 'success' : 'failure'} status`,
  );

  if (testMode) {
    console.log('\nπŸ§ͺ TEST MODE - No API calls will be made');
    console.log('\nπŸ“‹ Inputs:');
    console.log(`  - Original PR: ${originalPr}`);
    console.log(`  - Success: ${success}`);
    console.log(`  - Release Version: ${releaseVersion}`);
    console.log(`  - Release Tag: ${releaseTag}`);
    console.log(`  - NPM Tag: ${npmTag}`);
    console.log(`  - Channel: ${channel}`);
    console.log(`  - Dry Run: ${dryRun}`);
    console.log(`  - Run ID: ${runId}`);
  }

  let commentBody;

  if (success) {
    commentBody = `βœ… **[Step 4/4] Patch Release Complete!**

**πŸ“¦ Release Details:**
- **Version**: [\`${releaseVersion}\`](https://github.com/${repo.owner}/${repo.repo}/releases/tag/${releaseTag})
- **NPM Tag**: \`${npmTag}\`
- **Channel**: \`${channel}\`
- **Dry Run**: ${dryRun}

**πŸŽ‰ Status:** Your patch has been successfully released and published to npm!

**πŸ“ What's Available:**
- **GitHub Release**: [View release ${releaseTag}](https://github.com/${repo.owner}/${repo.repo}/releases/tag/${releaseTag})
- **NPM Package**: \`npm install @google/gemini-cli@${npmTag}\`

**πŸ”— Links:**
- [GitHub Release](https://github.com/${repo.owner}/${repo.repo}/releases/tag/${releaseTag})
- [This release workflow run](https://github.com/${repo.owner}/${repo.repo}/actions/runs/${runId})
- [Workflow History](https://github.com/${repo.owner}/${repo.repo}/actions/workflows/release-patch-3-release.yml)`;
  } else if (raceConditionFailure) {
    commentBody = `⚠️ **[Step 4/4] Patch Release Cancelled - Concurrent Release Detected**

**🚦 What Happened:**
Another patch release completed while this one was in progress, causing a version conflict.

**πŸ“‹ Details:**
- **Originally planned**: \`${releaseVersion || 'Unknown'}\`
- **Channel**: \`${channel}\`
- **Issue**: Version numbers are no longer sequential due to concurrent releases

**πŸ“Š Current State:**${
      currentReleaseVersion
        ? `
- **Latest ${channel} version**: \`${currentPreviousTag?.replace(/^v/, '') || 'unknown'}\`
- **Next patch should be**: \`${currentReleaseVersion}\`
- **New release tag**: \`${currentReleaseTag || 'unknown'}\``
        : `
- **Status**: Version information updated since this release was triggered`
    }

**πŸ”„ Next Steps:**
1. **Request a new patch** - The version calculation will now be correct
2. No action needed on your part - simply request the patch again
3. The system detected this automatically to prevent invalid releases

**πŸ’‘ Why This Happens:**
Multiple patch releases can't run simultaneously. When they do, the second one is automatically cancelled to maintain version consistency.

**πŸ”— Details:**
- [This release workflow run](https://github.com/${repo.owner}/${repo.repo}/actions/runs/${runId})
- [Workflow History](https://github.com/${repo.owner}/${repo.repo}/actions/workflows/release-patch-3-release.yml)`;
  } else {
    commentBody = `❌ **[Step 4/4] Patch Release Failed!**

**πŸ“‹ Details:**
- **Version**: \`${releaseVersion || 'Unknown'}\`
- **Channel**: \`${channel}\`
- **Error**: The patch release workflow encountered an error

**πŸ” Next Steps:**
1. Check the workflow logs for detailed error information
2. The maintainers have been notified via automatic issue creation
3. You may need to retry the patch once the issue is resolved

**πŸ”— Troubleshooting:**
- [This release workflow run](https://github.com/${repo.owner}/${repo.repo}/actions/runs/${runId})
- [View workflow logs](https://github.com/${repo.owner}/${repo.repo}/actions/runs/${runId})
- [Workflow History](https://github.com/${repo.owner}/${repo.repo}/actions/workflows/release-patch-3-release.yml)`;
  }

  if (testMode) {
    console.log('\nπŸ’¬ Would post comment:');
    console.log('----------------------------------------');
    console.log(commentBody);
    console.log('----------------------------------------');
    console.log('\nβœ… Comment generation working correctly!');
  } else if (github) {
    await github.rest.issues.createComment({
      owner: repo.owner,
      repo: repo.repo,
      issue_number: parseInt(originalPr),
      body: commentBody,
    });

    console.log(`Successfully commented on PR ${originalPr}`);
  } else {
    console.log('No GitHub client available');
  }
}

main().catch((error) => {
  console.error('Error commenting on PR:', error);
  process.exit(1);
});