|
| 1 | +const fs = require('fs'); |
| 2 | +const _ = require('lodash'); |
| 3 | +const childProcess = require('child_process'); |
| 4 | +const fetch = require('node-fetch'); |
| 5 | +const readline = require('readline'); |
| 6 | + |
| 7 | +function fetchLatestReleaseDate(version) { |
| 8 | + const relesae = childProcess.execSync(`gh release view ${version}`).toString(); |
| 9 | + const releaseMetaData = relesae.split('--')[0]; |
| 10 | + const createDate = _.flow(data => _.split(data, '\n'), |
| 11 | + linesData => _.find(linesData, l => l.startsWith('created')), |
| 12 | + createdData => _.split(createdData, '\t'), |
| 13 | + _.last)(releaseMetaData); |
| 14 | + |
| 15 | + return new Date(createDate); |
| 16 | +} |
| 17 | + |
| 18 | +function parsePR(prContent) { |
| 19 | + const sections = prContent.split('##'); |
| 20 | + |
| 21 | + const PRInfo = {}; |
| 22 | + sections.forEach(section => { |
| 23 | + const lines = section.trim().split('\r\n'); |
| 24 | + const title = lines.splice(0, 1)[0].toLowerCase(); |
| 25 | + const body = lines.join('\r\n'); |
| 26 | + if (title && body) { |
| 27 | + PRInfo[title] = body; |
| 28 | + } |
| 29 | + }); |
| 30 | + |
| 31 | + return PRInfo; |
| 32 | +} |
| 33 | + |
| 34 | +async function fetchMergedPRs(postMergedDate, repo, githubToken) { |
| 35 | + console.log('Find all merged PRs since - ', postMergedDate); |
| 36 | + const page = 1; |
| 37 | + // process.stderr.write(`Loading page ${page}..`); |
| 38 | + const url = `https://api.github.com/repos/${repo}/pulls?state=closed&base=master&page=${page}&per_page=100`; |
| 39 | + const headers = {Authorization: `token ${githubToken}`}; |
| 40 | + const response = await fetch(url, {headers}); |
| 41 | + const PRs = await response.json(); |
| 42 | + |
| 43 | + if (PRs.message) { |
| 44 | + console.log('\x1b[31m', 'Something went wrong', PRs.message); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + const relevantPRs = _.flow(prs => _.filter(prs, pr => !!pr.merged_at && new Date(pr.merged_at) > postMergedDate), |
| 49 | + prs => _.sortBy(prs, 'merged_at'), |
| 50 | + prs => |
| 51 | + _.map(prs, pr => ({ |
| 52 | + state: pr.state, |
| 53 | + merged_at: pr.merged_at, |
| 54 | + html_url: pr.html_url, |
| 55 | + branch: pr.head.ref, |
| 56 | + number: pr.number, |
| 57 | + title: pr.title, |
| 58 | + info: parsePR(pr.body) |
| 59 | + })))(PRs); |
| 60 | + |
| 61 | + return relevantPRs; |
| 62 | +} |
| 63 | + |
| 64 | +function isSilent(pr) { |
| 65 | + if (!pr.info.changelog) { |
| 66 | + return true; |
| 67 | + } else { |
| 68 | + const changelog = pr.info.changelog.toLowerCase(); |
| 69 | + if (changelog === 'none' || changelog === 'n/a') { |
| 70 | + return true; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return false; |
| 75 | +} |
| 76 | + |
| 77 | +function getPRsByType(PRs) { |
| 78 | + const silentPRs = [], |
| 79 | + features = [], |
| 80 | + web = [], |
| 81 | + fixes = [], |
| 82 | + infra = [], |
| 83 | + others = []; |
| 84 | + |
| 85 | + PRs.forEach(pr => { |
| 86 | + if (isSilent(pr)) { |
| 87 | + silentPRs.push(pr); |
| 88 | + } else if (pr.branch.startsWith('feat/')) { |
| 89 | + features.push(pr); |
| 90 | + } else if (pr.branch.startsWith('web/')) { |
| 91 | + web.push(pr); |
| 92 | + } else if (pr.branch.startsWith('fix/')) { |
| 93 | + fixes.push(pr); |
| 94 | + } else if (pr.branch.startsWith('infra/')) { |
| 95 | + infra.push(pr); |
| 96 | + } else { |
| 97 | + others.push(pr); |
| 98 | + } |
| 99 | + }); |
| 100 | + |
| 101 | + return {silentPRs, features, web, fixes, infra, others}; |
| 102 | +} |
| 103 | + |
| 104 | +function getLine(log, requester, prNumber) { |
| 105 | + return `• ${log}${requester}${prNumber} \n`; |
| 106 | +} |
| 107 | + |
| 108 | +function getAdditionalInfo(pr) { |
| 109 | + // TODO: Remove `jira issue` once fully migrated (has to remain for backwards compatibility) |
| 110 | + let additionalInfo = pr.info['jira issue'] || pr.info['Additional info']; |
| 111 | + if (additionalInfo === undefined || additionalInfo.toLowerCase() === 'none' || additionalInfo.includes('???')) { |
| 112 | + additionalInfo = ''; |
| 113 | + } else { |
| 114 | + additionalInfo = ` (${additionalInfo})`; |
| 115 | + } |
| 116 | + |
| 117 | + return additionalInfo; |
| 118 | +} |
| 119 | + |
| 120 | +function getEntry(pr) { |
| 121 | + let releaseNotes = ''; |
| 122 | + const log = pr.info.changelog || pr.title; |
| 123 | + const additionalInfo = getAdditionalInfo(pr); |
| 124 | + |
| 125 | + const prNumber = ` (#${pr.number})`; |
| 126 | + if (log.includes('\r\n')) { |
| 127 | + log.split('\r\n').forEach(l => { |
| 128 | + releaseNotes += getLine(l, additionalInfo, prNumber); |
| 129 | + }); |
| 130 | + } else { |
| 131 | + releaseNotes = getLine(log, additionalInfo, prNumber); |
| 132 | + } |
| 133 | + |
| 134 | + return releaseNotes; |
| 135 | +} |
| 136 | + |
| 137 | +function getTitle(title) { |
| 138 | + return `\n\n${title}\n\n`; |
| 139 | +} |
| 140 | + |
| 141 | +function getReleaseNotesForType(PRs, title) { |
| 142 | + let releaseNotes = getTitle(title); |
| 143 | + PRs.forEach(pr => { |
| 144 | + releaseNotes += getEntry(pr); |
| 145 | + }); |
| 146 | + |
| 147 | + return releaseNotes; |
| 148 | +} |
| 149 | + |
| 150 | +async function _generateReleaseNotes(latestVersion, newVersion, githubToken, prefix, repo, header) { |
| 151 | + const latestReleaseDate = fetchLatestReleaseDate(latestVersion); |
| 152 | + const PRs = await fetchMergedPRs(latestReleaseDate, repo, githubToken); |
| 153 | + if (!PRs) { |
| 154 | + return; |
| 155 | + } |
| 156 | + |
| 157 | + const {silentPRs, features, web, fixes, infra, others} = getPRsByType(PRs); |
| 158 | + |
| 159 | + let releaseNotes = header; |
| 160 | + |
| 161 | + releaseNotes += getTitle(':rocket: What’s New?'); |
| 162 | + |
| 163 | + releaseNotes += getReleaseNotesForType(features, ':gift: Features'); |
| 164 | + |
| 165 | + releaseNotes += getReleaseNotesForType(web, ':spider_web: Web support'); |
| 166 | + |
| 167 | + releaseNotes += getReleaseNotesForType(fixes, ':wrench: Fixes'); |
| 168 | + |
| 169 | + releaseNotes += getReleaseNotesForType(infra, ':gear: Maintenance & Infra'); |
| 170 | + |
| 171 | + releaseNotes += getTitle(':bulb: Deprecations & Migrations'); |
| 172 | + |
| 173 | + releaseNotes += getReleaseNotesForType(others, 'OTHERS'); |
| 174 | + |
| 175 | + releaseNotes += getReleaseNotesForType(silentPRs, |
| 176 | + '// Silent - these PRs did not have a changelog or were left out for some other reason, is it on purpose?'); |
| 177 | + |
| 178 | + fs.writeFileSync(`${process.env.HOME}/Downloads/${prefix}-release-notes_${newVersion}.txt`, releaseNotes, { |
| 179 | + encoding: 'utf8' |
| 180 | + }); |
| 181 | + |
| 182 | + console.log(`\x1b[1m\x1b[32m✔\x1b[0m \x1b[32m${prefix}-release-notes.txt was successfully written to ${process.env.HOME}/Downloads\x1b[0m \x1b[1m\x1b[32m✔\x1b[0m`); |
| 183 | +} |
| 184 | + |
| 185 | +async function generateReleaseNotes(latestVersion, newVersion, githubToken, prefix, repo, header = '') { |
| 186 | + let latestVer, newVer; |
| 187 | + const rl = readline.createInterface({ |
| 188 | + input: process.stdin, |
| 189 | + output: process.stdout |
| 190 | + }); |
| 191 | + |
| 192 | + rl.question(`What is the current version? `, currentV => { |
| 193 | + rl.question('What is the next version for release? ', newV => { |
| 194 | + latestVer = currentV || latestVersion; |
| 195 | + newVer = newV || newVersion; |
| 196 | + rl.close(); |
| 197 | + }); |
| 198 | + }); |
| 199 | + |
| 200 | + rl.on('close', () => { |
| 201 | + console.info(`Current latest version is v${latestVer}`); |
| 202 | + console.info(`Generating release notes out or PRs for v${newVer}`); |
| 203 | + _generateReleaseNotes(latestVer, newVer, githubToken, prefix, repo, header); |
| 204 | + }); |
| 205 | +} |
| 206 | + |
| 207 | +module.exports = {generateReleaseNotes}; |
0 commit comments