feat: add option to generate explicit URLs in the release notes (#16126)

This commit is contained in:
Charles Kerr 2018-12-19 09:48:01 -06:00 committed by Shelley Vohr
parent ddc38eda26
commit df0381e76c
2 changed files with 33 additions and 23 deletions

View file

@ -120,7 +120,7 @@ const getPreviousPoint = async (point) => {
} }
} }
async function getReleaseNotes (range) { async function getReleaseNotes (range, explicitLinks) {
const rangeList = range.split('..') || ['HEAD'] const rangeList = range.split('..') || ['HEAD']
const to = rangeList.pop() const to = rangeList.pop()
const from = rangeList.pop() || (await getPreviousPoint(to)) const from = rangeList.pop() || (await getPreviousPoint(to))
@ -128,7 +128,7 @@ async function getReleaseNotes (range) {
const notes = await notesGenerator.get(from, to) const notes = await notesGenerator.get(from, to)
const ret = { const ret = {
text: notesGenerator.render(notes) text: notesGenerator.render(notes, explicitLinks)
} }
if (notes.unknown.length) { if (notes.unknown.length) {
@ -139,13 +139,15 @@ async function getReleaseNotes (range) {
} }
async function main () { async function main () {
if (process.argv.length > 3) { // TODO: minimist/commander
console.log('Use: script/release-notes/index.js [tag | tag1..tag2]') const explicitLinks = process.argv.slice(2).some(arg => arg === '--explicit-links')
if (process.argv.length > 4) {
console.log('Use: script/release-notes/index.js [--explicit-links] [tag | tag1..tag2]')
return 1 return 1
} }
const range = process.argv[2] || 'HEAD' const range = process.argv[2] || 'HEAD'
const notes = await getReleaseNotes(range) const notes = await getReleaseNotes(range, explicitLinks)
console.log(notes.text) console.log(notes.text)
if (notes.warning) { if (notes.warning) {
throw new Error(notes.warning) throw new Error(notes.warning)

View file

@ -496,7 +496,28 @@ const getNotes = async (fromRef, toRef) => {
**** Render **** Render
***/ ***/
const renderCommit = commit => { const renderLink = (commit, explicitLinks) => {
let link
const pr = commit.originalPr
if (pr) {
const { owner, repo, number } = pr
const url = `https://github.com/${owner}/${repo}/pull/${number}`
const text = owner === 'electron' && repo === 'electron'
? `#${number}`
: `${owner}/${repo}#${number}`
link = explicitLinks ? `[${text}](${url})` : text
} else {
const { owner, repo, hash } = commit
const url = `https://github.com/${owner}/${repo}/commit/${hash}`
const text = owner === 'electron' && repo === 'electron'
? `${hash.slice(0, 8)}`
: `${owner}/${repo}@${hash.slice(0, 8)}`
link = explicitLinks ? `[${text}](${url})` : text
}
return link
}
const renderCommit = (commit, explicitLinks) => {
// clean up the note // clean up the note
let note = commit.note || commit.subject let note = commit.note || commit.subject
note = note.trim() note = note.trim()
@ -535,21 +556,12 @@ const renderCommit = commit => {
} }
} }
// make a GH-markdown-friendly link const link = renderLink(commit, explicitLinks)
let link
const pr = commit.originalPr
if (!pr) {
link = `https://github.com/${commit.owner}/${commit.repo}/commit/${commit.hash}`
} else if (pr.owner === 'electron' && pr.repo === 'electron') {
link = `#${pr.number}`
} else {
link = `[${pr.owner}/${pr.repo}:${pr.number}](https://github.com/${pr.owner}/${pr.repo}/pull/${pr.number})`
}
return { note, link } return { note, link }
} }
const renderNotes = notes => { const renderNotes = (notes, explicitLinks) => {
const rendered = [ `# Release Notes for ${notes.ref}\n\n` ] const rendered = [ `# Release Notes for ${notes.ref}\n\n` ]
const renderSection = (title, commits) => { const renderSection = (title, commits) => {
@ -557,7 +569,7 @@ const renderNotes = notes => {
return return
} }
const notes = new Map() const notes = new Map()
for (const note of commits.map(commit => renderCommit(commit))) { for (const note of commits.map(commit => renderCommit(commit, explicitLinks))) {
if (!notes.has(note.note)) { if (!notes.has(note.note)) {
notes.set(note.note, [note.link]) notes.set(note.note, [note.link])
} else { } else {
@ -576,11 +588,7 @@ const renderNotes = notes => {
renderSection('Other Changes', notes.other) renderSection('Other Changes', notes.other)
if (notes.docs.length) { if (notes.docs.length) {
const docs = notes.docs.map(commit => { const docs = notes.docs.map(commit => renderLink(commit, explicitLinks)).sort()
return commit.pr && commit.pr.number
? `#${commit.pr.number}`
: `https://github.com/electron/electron/commit/${commit.hash}`
}).sort()
rendered.push('## Documentation\n\n', ` * Documentation changes: ${docs.join(', ')}\n`, '\n') rendered.push('## Documentation\n\n', ` * Documentation changes: ${docs.join(', ')}\n`, '\n')
} }