|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | +const { execSync } = require('child_process'); |
| 4 | + |
| 5 | +async function run() { |
| 6 | + const apiKey = process.env.GEMINI_API_KEY; |
| 7 | + if (!apiKey) { |
| 8 | + console.error('Error: GEMINI_API_KEY environment variable is not set.'); |
| 9 | + process.exit(1); |
| 10 | + } |
| 11 | + |
| 12 | + // 1. Get commit history since last tag |
| 13 | + let commitLog = ''; |
| 14 | + try { |
| 15 | + // Try to get the tag before the current one (which is HEAD) |
| 16 | + const lastTag = execSync('git describe --tags --abbrev=0 HEAD^ 2>/dev/null || git describe --tags --abbrev=0 HEAD~1 2>/dev/null').toString().trim(); |
| 17 | + console.log(`Generating changelog since tag: ${lastTag}`); |
| 18 | + commitLog = execSync(`git log ${lastTag}..HEAD --oneline`).toString().trim(); |
| 19 | + } catch (error) { |
| 20 | + console.log('No previous tag found or error fetching it. Fetching last 50 commits instead.'); |
| 21 | + try { |
| 22 | + commitLog = execSync('git log --oneline -n 50').toString().trim(); |
| 23 | + } catch (gitError) { |
| 24 | + console.error('Failed to retrieve git log:', gitError.message); |
| 25 | + commitLog = 'Initial release / No commit logs found.'; |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + if (!commitLog) { |
| 30 | + commitLog = 'No commits found since the last release.'; |
| 31 | + } |
| 32 | + |
| 33 | + // 2. Read prompt template |
| 34 | + const templatePath = path.join(__dirname, '..', 'release-prompt-template.md'); |
| 35 | + if (!fs.existsSync(templatePath)) { |
| 36 | + console.error(`Error: Template file not found at ${templatePath}`); |
| 37 | + process.exit(1); |
| 38 | + } |
| 39 | + |
| 40 | + let prompt = fs.readFileSync(templatePath, 'utf8'); |
| 41 | + prompt = prompt.replace('{{COMMIT_LOG}}', commitLog); |
| 42 | + |
| 43 | + console.log('--- Sending Prompt to Gemini API ---'); |
| 44 | + console.log(prompt); |
| 45 | + console.log('------------------------------------'); |
| 46 | + |
| 47 | + // 3. Request Gemini API |
| 48 | + const url = `https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent?key=${apiKey}`; |
| 49 | + const requestBody = { |
| 50 | + contents: [ |
| 51 | + { |
| 52 | + parts: [ |
| 53 | + { |
| 54 | + text: prompt |
| 55 | + } |
| 56 | + ] |
| 57 | + } |
| 58 | + ] |
| 59 | + }; |
| 60 | + |
| 61 | + try { |
| 62 | + const response = await fetch(url, { |
| 63 | + method: 'POST', |
| 64 | + headers: { |
| 65 | + 'Content-Type': 'application/json' |
| 66 | + }, |
| 67 | + body: JSON.stringify(requestBody) |
| 68 | + }); |
| 69 | + |
| 70 | + if (!response.ok) { |
| 71 | + const errorText = await response.text(); |
| 72 | + throw new Error(`Gemini API returned status ${response.status}: ${errorText}`); |
| 73 | + } |
| 74 | + |
| 75 | + const data = await response.json(); |
| 76 | + const generatedText = data.candidates?.[0]?.content?.parts?.[0]?.text; |
| 77 | + |
| 78 | + if (!generatedText) { |
| 79 | + throw new Error('Invalid response structure or empty content from Gemini API'); |
| 80 | + } |
| 81 | + |
| 82 | + let finalNotes = generatedText; |
| 83 | + const githubRepository = process.env.GITHUB_REPOSITORY || 'owner/repo'; |
| 84 | + const releaseVersion = process.env.GITHUB_REF_NAME || 'v1.0.0'; |
| 85 | + |
| 86 | + finalNotes = finalNotes |
| 87 | + .replace(/\{\{GITHUB_REPOSITORY\}\}/g, githubRepository) |
| 88 | + .replace(/\{\{RELEASE_VERSION\}\}/g, releaseVersion); |
| 89 | + |
| 90 | + // 4. Output the release notes to a file for use in next steps |
| 91 | + const outputPath = path.join(process.cwd(), 'release_notes.md'); |
| 92 | + fs.writeFileSync(outputPath, finalNotes, 'utf8'); |
| 93 | + console.log(`Release notes successfully generated and written to ${outputPath}`); |
| 94 | + } catch (error) { |
| 95 | + console.error('Failed to generate release notes:', error.message); |
| 96 | + // Fallback release notes file to prevent workflow failure |
| 97 | + const outputPath = path.join(process.cwd(), 'release_notes.md'); |
| 98 | + fs.writeFileSync(outputPath, `### Commits in this Release\n\n\`\`\`\n${commitLog}\n\`\`\``, 'utf8'); |
| 99 | + console.log(`Fallback release notes written to ${outputPath}`); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +run(); |
0 commit comments