-
Notifications
You must be signed in to change notification settings - Fork 503
feat!: support multiple commits in footer #686
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
805d648
feat: implementing parsing of multiple footer commits
2156d2f
test: add test for footers that contain footers
c6d9af9
deps: update to latest parser
a71a84f
test: add additional test based on review
72c1e80
Merge branch 'master' into go-multi-ball
4058257
chore: address code review
493a9eb
Merge branch 'master' into go-multi-ball
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -19,18 +19,21 @@ const visitWithAncestors = require('unist-util-visit-parents'); | |
| const NUMBER_REGEX = /^[0-9]+$/; | ||
| import * as parser from '@conventional-commits/parser'; | ||
|
|
||
| type SummaryNode = | ||
| type SummaryNodes = | ||
| | parser.Type | ||
| | parser.Scope | ||
| | parser.BreakingChange | ||
| | parser.Text; | ||
| type FooterNodes = | ||
| | parser.Type | ||
| | parser.Scope | ||
| | parser.BreakingChange | ||
| | parser.Separator | ||
| | parser.Text | ||
| | parser.Newline; | ||
|
|
||
| // Converts conventional commit AST into conventional-changelog's | ||
| // output format, see: https://www.npmjs.com/package/conventional-commits-parser | ||
| export default function toConventionalChangelogFormat( | ||
| ast: parser.Message | ||
| ): parser.ConventionalChangelogCommit { | ||
| const cc: parser.ConventionalChangelogCommit = { | ||
| function getBlankConventionalCommit(): parser.ConventionalChangelogCommit { | ||
| return { | ||
| body: '', | ||
| subject: '', | ||
| type: '', | ||
|
|
@@ -43,6 +46,15 @@ export default function toConventionalChangelogFormat( | |
| header: '', | ||
| footer: null, | ||
| }; | ||
| } | ||
|
|
||
| // Converts conventional commit AST into conventional-changelog's | ||
| // output format, see: https://www.npmjs.com/package/conventional-commits-parser | ||
| export default function toConventionalChangelogFormat( | ||
| ast: parser.Message | ||
| ): parser.ConventionalChangelogCommit[] { | ||
| const commits: parser.ConventionalChangelogCommit[] = []; | ||
| const headerCommit = getBlankConventionalCommit(); | ||
| // Separate the body and summary nodes, this simplifies the subsequent | ||
| // tree walking logic: | ||
| let body; | ||
|
|
@@ -59,22 +71,22 @@ export default function toConventionalChangelogFormat( | |
| }); | ||
|
|
||
| // <type>, "(", <scope>, ")", ["!"], ":", <whitespace>*, <text> | ||
| visit(summary, (node: SummaryNode) => { | ||
| visit(summary, (node: SummaryNodes) => { | ||
| switch (node.type) { | ||
| case 'type': | ||
| cc.type = node.value; | ||
| cc.header += node.value; | ||
| headerCommit.type = node.value; | ||
| headerCommit.header += node.value; | ||
| break; | ||
| case 'scope': | ||
| cc.scope = node.value; | ||
| cc.header += `(${node.value})`; | ||
| headerCommit.scope = node.value; | ||
| headerCommit.header += `(${node.value})`; | ||
| break; | ||
| case 'breaking-change': | ||
| cc.header += '!'; | ||
| headerCommit.header += '!'; | ||
| break; | ||
| case 'text': | ||
| cc.subject = node.value; | ||
| cc.header += `: ${node.value}`; | ||
| headerCommit.subject = node.value; | ||
| headerCommit.header += `: ${node.value}`; | ||
| break; | ||
| default: | ||
| break; | ||
|
|
@@ -83,10 +95,8 @@ export default function toConventionalChangelogFormat( | |
|
|
||
| // [<any body-text except pre-footer>] | ||
| if (body) { | ||
| visit(body, 'text', (node: parser.Text) => { | ||
| // TODO(@bcoe): once we have \n tokens in tree we can drop this: | ||
| if (cc.body !== '') cc.body += '\n'; | ||
| cc.body += node.value; | ||
| visit(body, ['text', 'newline'], (node: parser.Text) => { | ||
| headerCommit.body += node.value; | ||
| }); | ||
| } | ||
|
|
||
|
|
@@ -104,39 +114,35 @@ export default function toConventionalChangelogFormat( | |
| if (!parent) { | ||
| return; | ||
| } | ||
| let startCollecting = false; | ||
| switch (parent.type) { | ||
| case 'summary': | ||
| breaking.text = cc.subject; | ||
| breaking.text = headerCommit.subject; | ||
| break; | ||
| case 'body': | ||
| breaking.text = ''; | ||
| // We treat text from the BREAKING CHANGE marker forward as | ||
| // the breaking change notes: | ||
| visit( | ||
| parent, | ||
| ['text', 'breaking-change'], | ||
| ['text', 'newline'], | ||
| (node: parser.Text | parser.BreakingChange) => { | ||
| // TODO(@bcoe): once we have \n tokens in tree we can drop this: | ||
| if (startCollecting && node.type === 'text') { | ||
| if (breaking.text !== '') breaking.text += '\n'; | ||
| breaking.text += node.value; | ||
| } else if (node.type === 'breaking-change') { | ||
| startCollecting = true; | ||
| } | ||
| breaking.text += node.value; | ||
| } | ||
| ); | ||
| break; | ||
| case 'token': | ||
| // If the '!' breaking change marker is used, the breaking change | ||
| // will be identified when the footer is parsed as a commit: | ||
| if (!node.value.includes('BREAKING')) return; | ||
| parent = ancestors.pop(); | ||
| visit(parent, 'text', (node: parser.Text) => { | ||
| visit(parent, ['text', 'newline'], (node: parser.Text) => { | ||
| breaking.text = node.value; | ||
| }); | ||
| break; | ||
| } | ||
| } | ||
| ); | ||
| if (breaking.text !== '') cc.notes.push(breaking); | ||
| if (breaking.text !== '') headerCommit.notes.push(breaking); | ||
|
|
||
| // Populates references array from footers: | ||
| // references: [{ | ||
|
|
@@ -183,9 +189,60 @@ export default function toConventionalChangelogFormat( | |
| ); | ||
| // TODO(@bcoe): how should references like "Refs: v8:8940" work. | ||
| if (hasRefSepartor && reference.issue.match(NUMBER_REGEX)) { | ||
| cc.references.push(reference); | ||
| headerCommit.references.push(reference); | ||
| } | ||
| }); | ||
|
|
||
| return cc; | ||
| /* | ||
| * Split footers that resemble commits into additional commits, e.g., | ||
| * chore: multiple commits | ||
| * chore(recaptchaenterprise): migrate recaptchaenterprise to the Java microgenerator | ||
| * Committer: @miraleung | ||
| * PiperOrigin-RevId: 345559154 | ||
| * ... | ||
| */ | ||
| visitWithAncestors( | ||
| ast, | ||
| ['type'], | ||
| (node: parser.Type, ancestors: parser.Node[]) => { | ||
| let parent = ancestors.pop(); | ||
| if (!parent) { | ||
| return; | ||
| } | ||
| if (parent.type === 'token') { | ||
| parent = ancestors.pop(); | ||
| let footerText = ''; | ||
| visit( | ||
| parent, | ||
| ['type', 'scope', 'breaking-change', 'separator', 'text', 'newline'], | ||
| (node: FooterNodes) => { | ||
| switch (node.type) { | ||
| case 'scope': | ||
| footerText += `(${node.value})`; | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is necessary because we don't currently include |
||
| break; | ||
| case 'separator': | ||
| // Footers of the form Fixes #99, should not be parsed. | ||
| if (node.value.includes('#')) return; | ||
| footerText += `${node.value} `; | ||
| break; | ||
| default: | ||
| footerText += node.value; | ||
| break; | ||
| } | ||
| } | ||
| ); | ||
| try { | ||
| for (const commit of toConventionalChangelogFormat( | ||
| parser.parser(footerText) | ||
| )) { | ||
| commits.push(commit); | ||
| } | ||
| } catch (err) { | ||
| // Footer does not appear to be an additional commit. | ||
| } | ||
| } | ||
| } | ||
| ); | ||
| commits.push(headerCommit); | ||
| return commits; | ||
| } | ||
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.
Can you add the return type?