|
| 1 | +#!/usr/bin/env node |
| 2 | +import { execFileSync } from 'node:child_process'; |
| 3 | +import { writeFileSync } from 'node:fs'; |
| 4 | +import { resolve } from 'node:path'; |
| 5 | +import { |
| 6 | + extractIssueRefs, |
| 7 | + normalizeTag, |
| 8 | + readChangelogSection, |
| 9 | + renderAcknowledgements, |
| 10 | + renderReleaseNotes, |
| 11 | +} from './lib/release-notes.mjs'; |
| 12 | + |
| 13 | +function parseArgs(argv) { |
| 14 | + const args = {}; |
| 15 | + |
| 16 | + for (let index = 2; index < argv.length; index += 1) { |
| 17 | + const key = argv[index]; |
| 18 | + const value = argv[index + 1]; |
| 19 | + |
| 20 | + if (!key.startsWith('--')) continue; |
| 21 | + if (!value || value.startsWith('--')) { |
| 22 | + throw new Error(`Missing value for ${key}`); |
| 23 | + } |
| 24 | + |
| 25 | + args[key.slice(2)] = value; |
| 26 | + index += 1; |
| 27 | + } |
| 28 | + |
| 29 | + return args; |
| 30 | +} |
| 31 | + |
| 32 | +function gitLines(...args) { |
| 33 | + const output = execFileSync('git', args, { |
| 34 | + cwd: resolve('.'), |
| 35 | + encoding: 'utf8', |
| 36 | + stdio: ['ignore', 'pipe', 'pipe'], |
| 37 | + }); |
| 38 | + |
| 39 | + return output |
| 40 | + .split(/\r?\n/) |
| 41 | + .map((line) => line.trim()) |
| 42 | + .filter(Boolean); |
| 43 | +} |
| 44 | + |
| 45 | +function gitText(...args) { |
| 46 | + return execFileSync('git', args, { |
| 47 | + cwd: resolve('.'), |
| 48 | + encoding: 'utf8', |
| 49 | + stdio: ['ignore', 'pipe', 'pipe'], |
| 50 | + }).trim(); |
| 51 | +} |
| 52 | + |
| 53 | +function previousTag(tag) { |
| 54 | + const tags = gitLines('tag', '--sort=creatordate'); |
| 55 | + const index = tags.indexOf(tag); |
| 56 | + if (index <= 0) return ''; |
| 57 | + return tags[index - 1]; |
| 58 | +} |
| 59 | + |
| 60 | +function compareUrl(repo, fromTag, toTag) { |
| 61 | + if (!repo || !fromTag || !toTag) return ''; |
| 62 | + return `https://github.com/${repo}/compare/${fromTag}...${toTag}`; |
| 63 | +} |
| 64 | + |
| 65 | +async function fetchReference(repo, number, token) { |
| 66 | + const response = await fetch(`https://api.github.com/repos/${repo}/issues/${number}`, { |
| 67 | + headers: { |
| 68 | + Accept: 'application/vnd.github+json', |
| 69 | + 'User-Agent': 'hello2cc-release-notes', |
| 70 | + ...(token ? { Authorization: `Bearer ${token}` } : {}), |
| 71 | + }, |
| 72 | + }); |
| 73 | + |
| 74 | + if (!response.ok) { |
| 75 | + throw new Error(`GitHub API returned ${response.status} for #${number}`); |
| 76 | + } |
| 77 | + |
| 78 | + const payload = await response.json(); |
| 79 | + return { |
| 80 | + number, |
| 81 | + kind: payload.pull_request ? 'pr' : 'issue', |
| 82 | + login: payload.user?.login || '', |
| 83 | + title: String(payload.title || '').trim(), |
| 84 | + html_url: String(payload.html_url || '').trim(), |
| 85 | + }; |
| 86 | +} |
| 87 | + |
| 88 | +async function resolveAcknowledgementRefs(repo, numbers) { |
| 89 | + if (!repo || numbers.length === 0) return []; |
| 90 | + |
| 91 | + const token = process.env.GH_TOKEN || process.env.GITHUB_TOKEN || ''; |
| 92 | + const refs = []; |
| 93 | + |
| 94 | + for (const number of numbers) { |
| 95 | + try { |
| 96 | + refs.push(await fetchReference(repo, number, token)); |
| 97 | + } catch (error) { |
| 98 | + process.stderr.write(`warning: ${error.message}\n`); |
| 99 | + refs.push({ |
| 100 | + number, |
| 101 | + kind: 'issue', |
| 102 | + login: '', |
| 103 | + title: '', |
| 104 | + html_url: repo ? `https://github.com/${repo}/issues/${number}` : '', |
| 105 | + }); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return refs; |
| 110 | +} |
| 111 | + |
| 112 | +async function main() { |
| 113 | + const args = parseArgs(process.argv); |
| 114 | + const tag = normalizeTag(args.tag || process.env.TAG_NAME || process.env.GITHUB_REF_NAME || ''); |
| 115 | + const repo = String(args.repo || process.env.GITHUB_REPOSITORY || '').trim(); |
| 116 | + const outputFile = String(args.output || '').trim(); |
| 117 | + const changelogPath = resolve(args.changelog || 'CHANGELOG.md'); |
| 118 | + |
| 119 | + const section = readChangelogSection(changelogPath, tag); |
| 120 | + if (!section?.body?.trim()) { |
| 121 | + throw new Error(`Missing CHANGELOG section for ${tag}. Add "## ${tag.replace(/^v/, '')} - YYYY-MM-DD" with bullet points before publishing.`); |
| 122 | + } |
| 123 | + |
| 124 | + const fromTag = previousTag(tag); |
| 125 | + const commitText = fromTag ? gitText('log', '--format=%B', `${fromTag}..${tag}`) : gitText('log', '--format=%B', '-n', '1', tag); |
| 126 | + const issueNumbers = extractIssueRefs(section.body, commitText); |
| 127 | + const acknowledgementRefs = await resolveAcknowledgementRefs(repo, issueNumbers); |
| 128 | + const notes = renderReleaseNotes({ |
| 129 | + section, |
| 130 | + acknowledgements: renderAcknowledgements(acknowledgementRefs), |
| 131 | + compareUrl: compareUrl(repo, fromTag, tag), |
| 132 | + }); |
| 133 | + |
| 134 | + if (outputFile) { |
| 135 | + writeFileSync(outputFile, notes, 'utf8'); |
| 136 | + return; |
| 137 | + } |
| 138 | + |
| 139 | + process.stdout.write(notes); |
| 140 | +} |
| 141 | + |
| 142 | +await main(); |
0 commit comments