generated from MetaMask/metamask-module-template
-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The validate command will validate the formatting of the changelog. If the `--rc` flag is used, it will also ensure that the current version is included as a release header, and that there are no unreleased changes. The CLI command displays a rudimentary diff if formatting problems are detected. This can be improved later with colours, and with highlighting within each line to show what has changed. It also doesn't yet highlight whitespace changes. The validation logic is also exposed via the API as a function called `validateChangelog`. It doesn't include the fancy diff output, but it does throw an error with all of the required information for someone to construct the same output.
- Loading branch information
Showing
5 changed files
with
186 additions
and
17 deletions.
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
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 |
---|---|---|
@@ -1,5 +1,11 @@ | ||
const { updateChangelog } = require('./updateChangelog'); | ||
const { | ||
ChangelogFormattingError, | ||
validateChangelog, | ||
} = require('./validateChangelog'); | ||
|
||
module.exports = { | ||
ChangelogFormattingError, | ||
updateChangelog, | ||
validateChangelog, | ||
}; |
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,74 @@ | ||
const { parseChangelog } = require('./parseChangelog'); | ||
|
||
/** | ||
* @typedef {import('./constants.js').Version} Version | ||
*/ | ||
|
||
/** | ||
* Represents a formatting error in a changelog. | ||
*/ | ||
class ChangelogFormattingError extends Error { | ||
/** | ||
* @param {Object} options | ||
* @param {string} options.validChangelog - The string contents of the well- | ||
* formatted changelog. | ||
* @param {string} options.invalidChangelog - The string contents of the | ||
* malformed changelog. | ||
*/ | ||
constructor({ validChangelog, invalidChangelog }) { | ||
super('Changelog is not well-formatted'); | ||
this.data = { | ||
validChangelog, | ||
invalidChangelog, | ||
}; | ||
} | ||
} | ||
|
||
/** | ||
* Validates that a changelog is well-formatted. | ||
* @param {Object} options | ||
* @param {string} options.changelogContent - The current changelog | ||
* @param {Version} options.currentVersion - The current version | ||
* @param {string} options.repoUrl - The GitHub repository URL for the current | ||
* project. | ||
* @param {boolean} options.isReleaseCandidate - Denotes whether the current | ||
* project is in the midst of release preparation or not. If this is set, this | ||
* command will also ensure the current version is represented in the | ||
* changelog with a release header, and that there are no unreleased changes | ||
* present. | ||
*/ | ||
function validateChangelog({ | ||
changelogContent, | ||
currentVersion, | ||
repoUrl, | ||
isReleaseCandidate, | ||
}) { | ||
const changelog = parseChangelog({ changelogContent, repoUrl }); | ||
|
||
// Ensure release header exists, if necessary | ||
if ( | ||
isReleaseCandidate && | ||
!changelog | ||
.getReleases() | ||
.find((release) => release.version === currentVersion) | ||
) { | ||
throw new Error( | ||
`Current version missing from changelog: '${currentVersion}'`, | ||
); | ||
} | ||
|
||
const hasUnreleasedChanges = changelog.getUnreleasedChanges().length !== 0; | ||
if (isReleaseCandidate && hasUnreleasedChanges) { | ||
throw new Error('Unreleased changes present in the changelog'); | ||
} | ||
|
||
const validChangelog = changelog.toString(); | ||
if (validChangelog !== changelogContent) { | ||
throw new ChangelogFormattingError({ | ||
validChangelog, | ||
invalidChangelog: changelogContent, | ||
}); | ||
} | ||
} | ||
|
||
module.exports = { validateChangelog, ChangelogFormattingError }; |
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