Skip to content

Commit

Permalink
Fix version URLs in gitlab repos (#12)
Browse files Browse the repository at this point in the history
* Fix version URLs in gitlab repos where HEAD is not available in compare range

- Add head option, defaults to 'HEAD'
- Use the value of head option where HEAD was hardcoded
- Add a test with custom head

* Readme: Apply consistent style to default values in options table
  • Loading branch information
tamaracha authored Jun 6, 2021
1 parent c2a593f commit 9290fb4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ In [release-it](https://github.com/release-it/release-it) config:

| option | default value | description |
| -------------- | -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------ |
| filename | `CHANGELOG.md` | File with changelogs. |
| filename | `'CHANGELOG.md'` | File with changelogs. |
| strictLatest | `true` | Entry of latest version must be present in order to get correct changelog. Set this option to `false` if you expect latest version without logs. |
| addUnreleased | `false` | It leaves "Unreleased" title row if set to `true`. |
| keepUnreleased | `false` | It leaves "Unreleased" title row unchanged if set to `true`. |
| addVersionUrl | `false` | Links the version to the according changeset. |
| head | `'HEAD'` | The git revision the new version tag is compared to in the Unreleased URL. |
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ const getFormattedDate = () => {
class KeepAChangelog extends Plugin {
async init() {
await super.init();
const { filename, strictLatest, addUnreleased, keepUnreleased, addVersionUrl } = this.options;
const { filename, strictLatest, addUnreleased, keepUnreleased, addVersionUrl, head } = this.options;

this.filename = filename || 'CHANGELOG.md';
this.strictLatest = strictLatest === undefined ? true : Boolean(strictLatest);
this.addUnreleased = addUnreleased === undefined ? false : Boolean(addUnreleased);
this.keepUnreleased = keepUnreleased === undefined ? false : Boolean(keepUnreleased);
this.addVersionUrl = addVersionUrl === undefined ? false : Boolean(addVersionUrl);
this.head = head || 'HEAD'

this.changelogPath = path.resolve(this.filename);
this.changelogContent = fs.readFileSync(this.changelogPath, 'utf-8');
Expand Down Expand Up @@ -72,10 +73,10 @@ class KeepAChangelog extends Plugin {
const repositoryUrl = `https://${repo.host}/${repo.repository}`;

// Add or update the Unreleased link
const unreleasedUrl = `${repositoryUrl}/compare/${tagName}...HEAD`;
const unreleasedUrl = `${repositoryUrl}/compare/${tagName}...${this.head}`;
const unreleasedLink = `[Unreleased]: ${unreleasedUrl}`;
if (updatedChangelog.includes('[Unreleased]:')) {
updatedChangelog = updatedChangelog.replace(/\[Unreleased\]\:.*HEAD/, unreleasedLink);
updatedChangelog = updatedChangelog.replace(new RegExp('\\[Unreleased\\]\\:.*' + this.head), unreleasedLink);
} else {
updatedChangelog = `${updatedChangelog}${this.EOL}${this.EOL}${unreleasedLink}`;
}
Expand Down
20 changes: 20 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ mock({
'./CHANGELOG-UNRELEASED.md': '## [Unreleased]\n\n* Item A\n* Item B\n\n## [1.0.0] - 2020-05-02\n\n* Item C\n* Item D',
'./CHANGELOG-VERSION_URL.md':
'## [Unreleased]\n\n* Item A\n* Item B\n\n## [1.0.0] - 2020-05-02\n\n* Item C\n* Item D\n\n[Unreleased]: https://github.com/release-it/release-it/compare/1.0.0..HEAD\n[1.0.0]: https://github.com/release-it/release-it/compare/0.0.0...1.0.0',
'./CHANGELOG-VERSION_URL_HEAD.md':
'## [Unreleased]\n\n* Item A\n* Item B\n\n## [1.0.0] - 2020-05-02\n\n* Item C\n* Item D\n\n[Unreleased]: https://github.com/release-it/release-it/compare/1.0.0..main\n[1.0.0]: https://github.com/release-it/release-it/compare/0.0.0...1.0.0',
'./CHANGELOG-VERSION_URL_UNRELEASED.md':
'## [Unreleased]\n\n* Item A\n* Item B\n\n## [1.0.0] - 2020-05-02\n\n* Item C\n* Item D\n\n[Unreleased]: https://github.com/user/project/compare/1.0.0..HEAD\n[1.0.0]: https://github.com/user/project/compare/0.0.0...1.0.0',
'./CHANGELOG-VERSION_URL_NEW.md': '## [Unreleased]\n\n* Item A\n* Item B'
Expand Down Expand Up @@ -146,6 +148,24 @@ test('should add links to the end of the file', async t => {
);
});

test('should add links with custom head to the end of the file', async t => {
const options = { [namespace]: { filename: 'CHANGELOG-VERSION_URL_HEAD.md', addVersionUrl: true, head: 'main' } };
const plugin = factory(Plugin, { namespace, options });
plugin.config.setContext({
latestTag: '1.0.0',
repo: {
host: 'github.com',
repository: 'release-it/release-it'
}
});
await runTasks(plugin);
assert.equal(plugin.getChangelog(), '* Item A\n* Item B');
assert.match(
readFile('./CHANGELOG-VERSION_URL_HEAD.md'),
/^## \[1\.0\.1] - [0-9]{4}-[0-9]{2}-[0-9]{2}\n\n\* Item A\n\* Item B\n\n## \[1\.0\.0] - 2020-05-02\n\n\* Item C\n*\* Item D\n\n\[Unreleased]: https:\/\/github\.com\/release-it\/release-it\/compare\/1\.0\.1\.\.\.main\n\[1\.0\.1]: https:\/\/github\.com\/release-it\/release-it\/compare\/1\.0\.0\.\.\.1\.0\.1\n\[1\.0\.0]: https:\/\/github\.com\/release-it\/release-it\/compare\/0\.0\.0\.\.\.1\.0\.0\n$/
);
});

test('should add unreleased section and links to the end of the file', async t => {
const options = {
[namespace]: { filename: 'CHANGELOG-VERSION_URL_UNRELEASED.md', addVersionUrl: true, addUnreleased: true }
Expand Down

0 comments on commit 9290fb4

Please sign in to comment.