-
Notifications
You must be signed in to change notification settings - Fork 27.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add release hook to format the changelog by labels #14592
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f7cea07
Add release hook
rafaelalmeidatk 152ee06
Remove release.js exported config
rafaelalmeidatk 72eda2b
Add skip flag to release CLI
rafaelalmeidatk be8aa48
Use full flag name
rafaelalmeidatk 0c8ce94
Upgrade release
timneutkens 4b36fba
Merge branch 'canary' of github.com:vercel/next.js into rafaelalmeida…
timneutkens eb70336
Update yarn.lock
timneutkens File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
const fetch = require('node-fetch') | ||
|
||
// -------------------- | ||
// Repo | ||
|
||
const owner = 'vercel' | ||
const repo = 'next.js' | ||
|
||
// -------------------- | ||
// Config | ||
|
||
// section -> label | ||
const sectionLabelMap = { | ||
'Core Changes': 'type: next', | ||
'Documentation Changes': 'type: documentation', | ||
'Example Changes': 'type: example', | ||
} | ||
|
||
const fallbackSection = 'Misc Changes' | ||
|
||
// -------------------------------------------------- | ||
|
||
const prNumberRegex = /\(#([-0-9]+)\)/ | ||
|
||
const getCommitPullRequest = async (commit) => { | ||
const match = prNumberRegex.exec(commit.title) | ||
|
||
if (!match) { | ||
return null | ||
} | ||
|
||
const number = parseInt(match[1], 10) | ||
const url = `https://api.github.com/repos/${owner}/${repo}/pulls/${number}` | ||
const res = await fetch(url) | ||
|
||
if (!res.ok) { | ||
console.warn(`\n\n⚠️ Failed to fetch PR #${number}\n`) | ||
return null | ||
} | ||
|
||
return await res.json() | ||
} | ||
|
||
const getSectionForPullRequest = (pullRequest) => { | ||
const { labels } = pullRequest | ||
|
||
// sections defined first will take priority | ||
for (const [section, label] of Object.entries(sectionLabelMap)) { | ||
if (labels.some((prLabel) => prLabel.name === label)) { | ||
return section | ||
} | ||
} | ||
|
||
return null | ||
} | ||
|
||
const groupByLabels = async (commits, github) => { | ||
// Initialize the sections object with empty arrays | ||
const sections = Object.keys(sectionLabelMap).reduce((sections, section) => { | ||
sections[section] = [] | ||
return sections | ||
}, {}) | ||
sections.__fallback = [] | ||
|
||
for (const commit of commits) { | ||
const pullRequest = await getCommitPullRequest(commit, github) | ||
|
||
if (pullRequest) { | ||
const section = getSectionForPullRequest(pullRequest) | ||
|
||
if (section) { | ||
// Add the change to the respective section | ||
sections[section].push({ | ||
title: pullRequest.title, | ||
number: pullRequest.number, | ||
}) | ||
|
||
continue | ||
} | ||
|
||
// No section found, add it to the fallback section | ||
sections.__fallback.push({ | ||
title: pullRequest.title, | ||
number: pullRequest.number, | ||
}) | ||
|
||
continue | ||
} | ||
|
||
// No Pull Request found, add it to the fallback section but without the number | ||
sections.__fallback.push({ | ||
title: commit.title, | ||
}) | ||
} | ||
|
||
return sections | ||
} | ||
|
||
const buildChangelog = (sections, authors) => { | ||
let text = '' | ||
|
||
for (const section in sections) { | ||
const changes = sections[section] | ||
|
||
// No changes in this section? Don't render it | ||
if (changes.length === 0) { | ||
continue | ||
} | ||
|
||
const title = section === '__fallback' ? fallbackSection : section | ||
text += `### ${title}\n\n` | ||
|
||
for (const change of changes) { | ||
const numberText = change.number != null ? `: #${change.number}` : '' | ||
|
||
text += `- ${change.title}${numberText}\n` | ||
} | ||
|
||
text += '\n' | ||
} | ||
|
||
if (authors.size > 0) { | ||
text += '### Credits \n\n' | ||
text += 'Huge thanks to ' | ||
|
||
let index = 1 | ||
authors.forEach((author) => { | ||
// GitHub links usernames if prefixed with @ | ||
text += `@${author}` | ||
|
||
const penultimate = index === authors.size - 1 | ||
const notLast = index !== authors.size | ||
|
||
if (penultimate) { | ||
// Oxford comma is applied when list is bigger than 2 names | ||
if (authors.size > 2) { | ||
text += ',' | ||
} | ||
|
||
text += ' and ' | ||
} else if (notLast) { | ||
text += ', ' | ||
} | ||
|
||
index += 1 | ||
}) | ||
|
||
text += ' for helping!' | ||
text += '\n' | ||
} | ||
|
||
return text | ||
} | ||
|
||
module.exports = async (markdown, metadata) => { | ||
const { commits, authors } = metadata | ||
|
||
const sections = await groupByLabels(commits.all) | ||
const changelog = buildChangelog(sections, authors) | ||
|
||
return changelog | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this has to be changed somewhere else too for stable releases?