generated from MetaMask/metamask-module-template
-
-
Notifications
You must be signed in to change notification settings - Fork 4
package rename changelog validation #157
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 all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
a64929d
allows changelog to maintain the history when package has been renamed
kanthesha 9d93abb
Update src/changelog.ts
kanthesha 96cb711
Update src/changelog.ts
kanthesha 2ba442d
validation test added for renamed package changelog
kanthesha daf65a5
Update src/changelog-config.ts
kanthesha 694ac81
Update src/changelog-config.ts
kanthesha 6a43fba
command line params for version and tag prefix of the package before …
kanthesha 0dbe4fc
refactor unreleasedLinkReferenceDefinition
kanthesha dc83e68
refactor released link reference definition
kanthesha b51a4fa
Apply JSDoc syntax for optional params
MajorLift dee072d
rename versionBeforePkgRename and tagPrefixBeforePkgRename and releas…
kanthesha bac92c7
Revert "Apply JSDoc syntax for optional params
MajorLift 920aec1
added PackageRename type with version and tagPrefix properties
kanthesha 5b24367
parse changelog test
kanthesha b674e81
Merge branch 'main' into package-rename-validation
kanthesha c553d22
Update src/changelog.ts
kanthesha 3c4c682
Update src/changelog.ts
kanthesha 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 |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ import { | |
| unreleased, | ||
| Version, | ||
| } from './constants'; | ||
| import { PackageRename } from './shared-types'; | ||
|
|
||
| const changelogTitle = '# Changelog'; | ||
| const changelogDescription = `All notable changes to this project will be documented in this file. | ||
|
|
@@ -165,24 +166,50 @@ function getTagUrl(repoUrl: string, tag: string) { | |
| * @param repoUrl - The URL for the GitHub repository. | ||
| * @param tagPrefix - The prefix used in tags before the version number. | ||
| * @param releases - The releases to generate link definitions for. | ||
| * @param packageRename - The package rename properties | ||
| * An optional, which is required only in case of package renamed. | ||
| * @returns The stringified release link definitions. | ||
| */ | ||
| function stringifyLinkReferenceDefinitions( | ||
| repoUrl: string, | ||
| tagPrefix: string, | ||
| releases: ReleaseMetadata[], | ||
| packageRename?: PackageRename, | ||
| ) { | ||
| // A list of release versions in descending SemVer order | ||
| const descendingSemverVersions = releases | ||
| .map(({ version }) => version) | ||
| .sort((a: Version, b: Version) => { | ||
| return semver.gt(a, b) ? -1 : 1; | ||
| }); | ||
| const latestSemverVersion = descendingSemverVersions[0]; | ||
| // A list of release versions in chronological order | ||
| const chronologicalVersions = releases.map(({ version }) => version); | ||
| const hasReleases = chronologicalVersions.length > 0; | ||
| const unreleasedLinkReferenceDefinition = | ||
| getUnreleasedLinkReferenceDefinition( | ||
| repoUrl, | ||
| tagPrefix, | ||
| releases, | ||
| packageRename, | ||
| ); | ||
|
|
||
| const releaseLinkReferenceDefinitions = getReleaseLinkReferenceDefinitions( | ||
| repoUrl, | ||
| tagPrefix, | ||
| releases, | ||
| packageRename, | ||
| ).join('\n'); | ||
| return `${unreleasedLinkReferenceDefinition}\n${releaseLinkReferenceDefinitions}${ | ||
| releases.length > 0 ? '\n' : '' | ||
| }`; | ||
| } | ||
|
|
||
| /** | ||
| * Get a string of unreleased link reference definition. | ||
| * | ||
| * @param repoUrl - The URL for the GitHub repository. | ||
| * @param tagPrefix - The prefix used in tags before the version number. | ||
| * @param releases - The releases to generate link definitions for. | ||
| * @param packageRename - The package rename properties. | ||
| * @returns A unreleased link reference definition string. | ||
| */ | ||
| function getUnreleasedLinkReferenceDefinition( | ||
| repoUrl: string, | ||
| tagPrefix: string, | ||
| releases: ReleaseMetadata[], | ||
| packageRename?: PackageRename, | ||
| ): string { | ||
| // The "Unreleased" section represents all changes made since the *highest* | ||
| // release, not the most recent release. This is to accomodate patch releases | ||
| // of older versions that don't represent the latest set of changes. | ||
|
|
@@ -193,42 +220,102 @@ function stringifyLinkReferenceDefinitions( | |
| // | ||
| // If there have not been any releases yet, the repo URL is used directly as | ||
| // the link definition. | ||
| const unreleasedLinkReferenceDefinition = `[${unreleased}]: ${ | ||
|
|
||
| // A list of release versions in descending SemVer order | ||
| const descendingSemverVersions = releases | ||
| .map(({ version }) => version) | ||
| .sort((a: Version, b: Version) => { | ||
| return semver.gt(a, b) ? -1 : 1; | ||
| }); | ||
| const latestSemverVersion = descendingSemverVersions[0]; | ||
| const hasReleases = descendingSemverVersions.length > 0; | ||
| // if there is a package renamed, the tag prefix before the rename will be considered for compare | ||
| // [Unreleased]: https://github.com/ExampleUsernameOrOrganization/ExampleRepository/compare/[email protected] | ||
| const tagPrefixToCompare = | ||
| packageRename && packageRename.versionBeforeRename === latestSemverVersion | ||
| ? packageRename.tagPrefixBeforeRename | ||
| : tagPrefix; | ||
|
|
||
| return `[${unreleased}]: ${ | ||
| hasReleases | ||
| ? getCompareUrl(repoUrl, `${tagPrefix}${latestSemverVersion}`, 'HEAD') | ||
| ? getCompareUrl( | ||
| repoUrl, | ||
| `${tagPrefixToCompare}${latestSemverVersion}`, | ||
| 'HEAD', | ||
| ) | ||
| : withTrailingSlash(repoUrl) | ||
| }`; | ||
| } | ||
|
|
||
| /** | ||
| * Get a list of release link reference definitions. | ||
| * | ||
| * @param repoUrl - The URL for the GitHub repository. | ||
| * @param tagPrefix - The prefix used in tags before the version number. | ||
| * @param releases - The releases to generate link definitions for. | ||
| * @param packageRename - The package rename properties. | ||
| * @returns A list of release link reference definitions. | ||
| */ | ||
| function getReleaseLinkReferenceDefinitions( | ||
| repoUrl: string, | ||
| tagPrefix: string, | ||
| releases: ReleaseMetadata[], | ||
| packageRename?: PackageRename, | ||
| ): string[] { | ||
| // The "previous" release that should be used for comparison is not always | ||
| // the most recent release chronologically. The _highest_ version that is | ||
| // lower than the current release is used as the previous release, so that | ||
| // patch releases on older releases can be accomodated. | ||
| const releaseLinkReferenceDefinitions = releases | ||
| .map(({ version }) => { | ||
| let diffUrl; | ||
| if (version === chronologicalVersions[chronologicalVersions.length - 1]) { | ||
| diffUrl = getTagUrl(repoUrl, `${tagPrefix}${version}`); | ||
| const chronologicalVersions = releases.map(({ version }) => version); | ||
| let tagPrefixToCompare = tagPrefix; | ||
| const releaseLinkReferenceDefinitions = releases.map(({ version }) => { | ||
| let diffUrl; | ||
| // once the version matches with versionBeforeRename, rest of the lines in changelog will be assumed as migrated tags | ||
| if (packageRename && packageRename.versionBeforeRename === version) { | ||
| tagPrefixToCompare = packageRename.tagPrefixBeforeRename; | ||
| } | ||
|
|
||
| if (version === chronologicalVersions[chronologicalVersions.length - 1]) { | ||
| diffUrl = getTagUrl(repoUrl, `${tagPrefixToCompare}${version}`); | ||
| } else { | ||
| const versionIndex = chronologicalVersions.indexOf(version); | ||
| const previousVersion = chronologicalVersions | ||
| .slice(versionIndex) | ||
| .find((releaseVersion: Version) => { | ||
| return semver.gt(version, releaseVersion); | ||
| }); | ||
|
|
||
| if (previousVersion) { | ||
| if ( | ||
| packageRename && | ||
| packageRename.versionBeforeRename === previousVersion | ||
| ) { | ||
| // The package was renamed at this version | ||
| // (the tag prefix holds the new name). | ||
| diffUrl = getCompareUrl( | ||
| repoUrl, | ||
| `${packageRename.tagPrefixBeforeRename}${previousVersion}`, | ||
| `${tagPrefix}${version}`, | ||
| ); | ||
| } else { | ||
| // If the package was ever renamed, it was not renamed at this version, | ||
| // so use either the old tag prefix or the new tag prefix. | ||
| // If the package was never renamed, use the tag prefix as it is. | ||
| diffUrl = getCompareUrl( | ||
| repoUrl, | ||
| `${tagPrefixToCompare}${previousVersion}`, | ||
| `${tagPrefixToCompare}${version}`, | ||
| ); | ||
| } | ||
| } else { | ||
| const versionIndex = chronologicalVersions.indexOf(version); | ||
| const previousVersion = chronologicalVersions | ||
| .slice(versionIndex) | ||
| .find((releaseVersion: Version) => { | ||
| return semver.gt(version, releaseVersion); | ||
| }); | ||
| diffUrl = previousVersion | ||
| ? getCompareUrl( | ||
| repoUrl, | ||
| `${tagPrefix}${previousVersion}`, | ||
| `${tagPrefix}${version}`, | ||
| ) | ||
| : getTagUrl(repoUrl, `${tagPrefix}${version}`); | ||
| // This is the smallest release. | ||
| diffUrl = getTagUrl(repoUrl, `${tagPrefixToCompare}${version}`); | ||
| } | ||
| return `[${version}]: ${diffUrl}`; | ||
| }) | ||
| .join('\n'); | ||
| return `${unreleasedLinkReferenceDefinition}\n${releaseLinkReferenceDefinitions}${ | ||
| releases.length > 0 ? '\n' : '' | ||
| }`; | ||
| } | ||
| return `[${version}]: ${diffUrl}`; | ||
| }); | ||
|
|
||
| return releaseLinkReferenceDefinitions; | ||
| } | ||
|
|
||
| type AddReleaseOptions = { | ||
|
|
@@ -265,28 +352,35 @@ export default class Changelog { | |
|
|
||
| #formatter: Formatter; | ||
|
|
||
| readonly #packageRename: PackageRename | undefined; | ||
|
|
||
| /** | ||
| * Construct an empty changelog. | ||
| * | ||
| * @param options - Changelog options. | ||
| * @param options.repoUrl - The GitHub repository URL for the current project. | ||
| * @param options.tagPrefix - The prefix used in tags before the version number. | ||
| * @param options.formatter - A function that formats the changelog string. | ||
| * @param options.packageRename - The package rename properties. | ||
| * An optional, which is required only in case of package renamed. | ||
| */ | ||
| constructor({ | ||
| repoUrl, | ||
| tagPrefix = 'v', | ||
| formatter = (changelog) => changelog, | ||
| packageRename, | ||
| }: { | ||
| repoUrl: string; | ||
| tagPrefix?: string; | ||
| formatter?: Formatter; | ||
| packageRename?: PackageRename; | ||
| }) { | ||
| this.#releases = []; | ||
| this.#changes = { [unreleased]: {} }; | ||
| this.#repoUrl = repoUrl; | ||
| this.#tagPrefix = tagPrefix; | ||
| this.#formatter = formatter; | ||
| this.#packageRename = packageRename; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -468,6 +562,7 @@ ${stringifyLinkReferenceDefinitions( | |
| this.#repoUrl, | ||
| this.#tagPrefix, | ||
| this.#releases, | ||
| this.#packageRename, | ||
| )}`; | ||
|
|
||
| return this.#formatter(changelog); | ||
|
|
||
MajorLift marked this conversation as resolved.
Show resolved
Hide resolved
MajorLift marked this conversation as resolved.
Show resolved
Hide resolved
mcmire marked this conversation as resolved.
Show resolved
Hide resolved
|
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.