-
-
Notifications
You must be signed in to change notification settings - Fork 54
feat!: parse trailers using git if available, allow longer lines
#144
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
Open
aduh95
wants to merge
3
commits into
nodejs:main
Choose a base branch
from
aduh95:parse-trailers-with-git
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| import { spawnSync } from 'node:child_process' | ||
| import Base from 'gitlint-parser-base' | ||
|
|
||
| const revertRE = /Revert "(.*)"$/ | ||
| const workingRE = /Working on v([\d]+)\.([\d]+).([\d]+)$/ | ||
| const releaseRE = /([\d]{4})-([\d]{2})-([\d]{2}),? Version/ | ||
| const reviewedByRE = /^Reviewed-By: (.*)$/ | ||
| const fixesRE = /^Fixes: (.*)$/ | ||
| const prUrlRE = /^PR-URL: (.*)$/ | ||
| const refsRE = /^Refs?: (.*)$/ | ||
|
|
||
| export default class Parser extends Base { | ||
| constructor (str, validator) { | ||
| super(str, validator) | ||
| this.subsystems = [] | ||
| this.fixes = [] | ||
| this.prUrl = null | ||
| this.refs = [] | ||
| this.reviewers = [] | ||
| this._metaStart = 0 | ||
| this._metaEnd = 0 | ||
| this._parse() | ||
| } | ||
|
|
||
| _setMetaStart (n) { | ||
| if (this._metaStart) return | ||
| this._metaStart = n | ||
| } | ||
|
|
||
| _setMetaEnd (n) { | ||
| if (n < this._metaEnd) return | ||
| this._metaEnd = n | ||
| } | ||
|
|
||
| _parseTrailers (body) { | ||
| const interpretTrailers = commitMessage => spawnSync('git', [ | ||
| 'interpret-trailers', '--only-trailers', '--only-input', '--no-divider' | ||
| ], { | ||
| encoding: 'utf-8', | ||
| input: `'dummy subject\n\n${commitMessage.join('\n')}\n` | ||
| }).stdout | ||
|
|
||
| let originalTrailers | ||
| try { | ||
| originalTrailers = interpretTrailers(body).trim() | ||
| } catch (err) { | ||
| console.warn('git is not available, trailers detection might be a bit ' + | ||
| 'off which is acceptable in most cases', err) | ||
| return body | ||
| } | ||
| const trailerFreeBody = body.slice(1) // clone, and remove the first empty line | ||
| const stillInTrailers = () => { | ||
| const result = interpretTrailers(trailerFreeBody) | ||
| return result.length && originalTrailers.startsWith(result.trim()) | ||
| } | ||
| for (let i = trailerFreeBody.length - 1; stillInTrailers(); i--) { | ||
| // Remove last line until git no longer detects any trailers | ||
| trailerFreeBody.pop() | ||
| } | ||
| this._metaStart = trailerFreeBody.length + 1 // the subject line needs to be counted | ||
| for (let i = trailerFreeBody.length - 1; trailerFreeBody[i] === ''; i--) { | ||
| // Remove additional empty line(s) | ||
| trailerFreeBody.pop() | ||
| } | ||
| this._metaEnd = body.length - 1 | ||
| this.trailerFreeBody = trailerFreeBody | ||
| return (this.trailers = originalTrailers.split('\n')) | ||
| } | ||
|
|
||
| _parse () { | ||
| const revert = this.isRevert() | ||
| if (!revert) { | ||
| this.subsystems = getSubsystems(this.title || '') | ||
| } else { | ||
| const matches = this.title.match(revertRE) | ||
| if (matches) { | ||
| const title = matches[1] | ||
| this.subsystems = getSubsystems(title) | ||
| } | ||
| } | ||
|
|
||
| const trailers = this._parseTrailers(this.body) | ||
|
|
||
| for (let i = 0; i < trailers.length; i++) { | ||
| const line = trailers[i] | ||
| const reviewedBy = reviewedByRE.exec(line) | ||
| if (reviewedBy) { | ||
| this._setMetaStart(i) | ||
| this._setMetaEnd(i) | ||
| this.reviewers.push(reviewedBy[1]) | ||
| continue | ||
| } | ||
|
|
||
| const fixes = fixesRE.exec(line) | ||
| if (fixes) { | ||
| this._setMetaStart(i) | ||
| this._setMetaEnd(i) | ||
| this.fixes.push(fixes[1]) | ||
| continue | ||
| } | ||
|
|
||
| const prUrl = prUrlRE.exec(line) | ||
| if (prUrl) { | ||
| this._setMetaStart(i) | ||
| this._setMetaEnd(i) | ||
| this.prUrl = prUrl[1] | ||
| continue | ||
| } | ||
|
|
||
| const refs = refsRE.exec(line) | ||
| if (refs) { | ||
| this._setMetaStart(i) | ||
| this._setMetaEnd(i) | ||
| this.refs.push(refs[1]) | ||
| continue | ||
| } | ||
|
|
||
| if (this._metaStart && !this._metaEnd) { this._setMetaEnd(i) } | ||
| } | ||
| } | ||
|
|
||
| isRevert () { | ||
| return revertRE.test(this.title) | ||
| } | ||
|
|
||
| isWorkingCommit () { | ||
| return workingRE.test(this.title) | ||
| } | ||
|
|
||
| isReleaseCommit () { | ||
| return releaseRE.test(this.title) | ||
| } | ||
|
|
||
| toJSON () { | ||
| return { | ||
| sha: this.sha, | ||
| title: this.title, | ||
| subsystems: this.subsystems, | ||
| author: this.author, | ||
| date: this.date, | ||
| fixes: this.fixes, | ||
| refs: this.refs, | ||
| prUrl: this.prUrl, | ||
| reviewers: this.reviewers, | ||
| body: this.body, | ||
| trailers: this.trailers, | ||
| trailerFreeBody: this.trailerFreeBody, | ||
| revert: this.isRevert(), | ||
| release: this.isReleaseCommit(), | ||
| working: this.isWorkingCommit(), | ||
| metadata: { | ||
| start: this._metaStart, | ||
| end: this._metaEnd | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function getSubsystems (str) { | ||
| str = str || '' | ||
| const colon = str.indexOf(':') | ||
| if (colon === -1) { | ||
| return [] | ||
| } | ||
|
|
||
| const subStr = str.slice(0, colon) | ||
| const subs = subStr.split(',') | ||
| return subs.map((item) => { | ||
| return item.trim() | ||
| }) | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Derived from https://github.com/git-lint/gitlint-parser-node/blob/v1.1.0/index.js (MIT license)