Skip to content
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

Bitbucket support #4

Merged
merged 5 commits into from
Jan 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@ node_js:
- "8"
- "6"
- "4"
script: npm run test:coveralls
script: npm test
jobs:
include:
- stage: coveralls
node_js: "node"
script: npm run test:coveralls
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- Support for BitBucket

## [1.0.0] - 2017-12-12
### Added
Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# changelog-updater
[![npm package](https://img.shields.io/npm/v/changelog-updater.svg?style=flat-square)](https://www.npmjs.org/package/changelog-updater)
[![GitHub license](https://img.shields.io/github/license/nikolajevp/changelog-updater.svg)](https://github.com/nikolajevp/changelog-updater/blob/master/LICENSE)
[![Dependencies](https://img.shields.io/david/nikolajevp/changelog-updater.svg?style=flat-square)](https://david-dm.org/nikolajevp/changelog-updater)
[![Build Status](https://travis-ci.org/nikolajevp/changelog-updater.svg?branch=master)](https://travis-ci.org/nikolajevp/changelog-updater)
[![Coverage Status](https://coveralls.io/repos/github/nikolajevp/changelog-updater/badge.svg?branch=master)](https://coveralls.io/github/nikolajevp/changelog-updater?branch=master)
[![Dependencies](https://img.shields.io/david/nikolajevp/changelog-updater.svg?style=flat-square)](https://david-dm.org/nikolajevp/changelog-updater)
[![DevDependencies](https://img.shields.io/david/dev/nikolajevp/changelog-updater.svg?style=flat-square)](https://david-dm.org/nikolajevp/changelog-updater?type=dev)
[![GitHub license](https://img.shields.io/github/license/nikolajevp/changelog-updater.svg)](https://github.com/nikolajevp/changelog-updater/blob/master/LICENSE)
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)

Update [Unreleased] in CHANGELOG.md to current package version.
Expand Down
82 changes: 57 additions & 25 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,71 @@ const version = process.env.npm_package_version;
const date = new Date().toISOString().slice(0, 10);
const changelogFileName = 'CHANGELOG.md';

function updateChangelog() {
fs.readFile(changelogFileName, 'utf8', (err, changelog) => {
if (err) {
throw err;
}
function updateUpperSection(changelog) {
return changelog.replace(
/## \[Unreleased\]/,
`## [Unreleased]\n\n## [${version}] - ${date}`
);
}

function updateBottomSectionGithub(changelog) {
/**
* matches[0] = `[Unreleased]: https://github.com/nikolajevp/changelog-updater/compare/v1.0.0...HEAD`
* matches[1] = ` https://github.com/nikolajevp/changelog-updater/compare/`
* matches[2] = `1.0.0`
*/
const regex = /\[Unreleased\]:(.+\/)v(.+)\.\.\.HEAD/i;
const matches = changelog.match(regex);

if (matches) {
const url = matches[1];
const previousVersion = matches[2];
const compareToUnreleased = `[Unreleased]:${url}v${version}...HEAD`;
const compareToLatestVersion = `[${version}]:${url}v${previousVersion}...v${version}`;

changelog = changelog.replace(
/## \[Unreleased\]/,
`## [Unreleased]\n\n## [${version}] - ${date}`
regex,
`${compareToUnreleased}\n${compareToLatestVersion}`
);
}

return changelog;
}

/**
* unreleasedLink[0] = `[Unreleased]: https://github.com/nikolajevp/changelog-updater/compare/v1.0.0...HEAD`
* unreleasedLink[1] = ` https://github.com/nikolajevp/changelog-updater/compare/v1.0.0`
* unreleasedLink[2] = ` https://github.com/nikolajevp/changelog-updater/compare/`
*/
const unreleasedLink = changelog.match(
/\[Unreleased\]:((.+\/).+)\.\.\.HEAD/i
function updateBottomSectionBitbucket(changelog) {
/**
* matches[0] = `[Unreleased]: https://bitbucket.org/nikolajevp/changelog-updater/branches/compare/HEAD..v1.0.0`
* matches[1] = ` https://bitbucket.org/nikolajevp/changelog-updater/branches/compare/`
* matches[2] = `1.0.0`
*/
const regex = /\[Unreleased\]:(.+)HEAD\.\.v(.+)/i;
const matches = changelog.match(regex);

if (matches) {
const url = matches[1];
const previousVersion = matches[2];
const compareToUnreleased = `[Unreleased]:${url}HEAD..v${version}`;
const compareToLatestVersion = `[${version}]:${url}v${version}..v${previousVersion}`;

changelog = changelog.replace(
regex,
`${compareToUnreleased}\n${compareToLatestVersion}`
);
}

return changelog;
}

if (unreleasedLink) {
const dynamicResult = `[Unreleased]:${
unreleasedLink[2]
}v${version}...HEAD\n[${version}]:${
unreleasedLink[1]
}...v${version}`;

changelog = changelog.replace(
/\[Unreleased\]:((.+\/).+)\.\.\.HEAD/i,
dynamicResult
);
function updateChangelog() {
fs.readFile(changelogFileName, 'utf8', (err, changelog) => {
if (err) {
throw err;
}

changelog = updateUpperSection(changelog);
changelog = updateBottomSectionGithub(changelog);
changelog = updateBottomSectionBitbucket(changelog);

fs.writeFile(changelogFileName, changelog, 'utf8', (err) => {
if (err) {
throw err;
Expand Down
56 changes: 55 additions & 1 deletion lib/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe('updateChangelog', () => {
expect(updateChangelog).toThrow('could not write file');
});

it('Updates [Unreleased] section', () => {
it('Updates [Unreleased] section for Github', () => {
readFile
.mockClear()
.mockImplementation((changelogFileName, format, callback) =>
Expand Down Expand Up @@ -113,4 +113,58 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
[1.0.0]: https://github.com/nikolajevp/changelog-updater/compare/v0.1.0...v1.0.0
`);
});

it('Updates [Unreleased] section for Bitbucket', () => {
readFile
.mockClear()
.mockImplementation((changelogFileName, format, callback) =>
callback(
null,
`
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]

## 0.1.0 - 2017-12-09
### Added
- Initial commit

[Unreleased]: https://bitbucket.org/nikolajevp/changelog-updater/branches/compare/HEAD..v0.1.0
`
)
);

writeFile
.mockClear()
.mockImplementation(
(changelogFileName, changelog, format, callback) => {
return callback();
}
);

updateChangelog();

expect(writeFile.mock.calls[0][1]).toEqual(`
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [1.0.0] - 2017-12-12

## 0.1.0 - 2017-12-09
### Added
- Initial commit

[Unreleased]: https://bitbucket.org/nikolajevp/changelog-updater/branches/compare/HEAD..v1.0.0
[1.0.0]: https://bitbucket.org/nikolajevp/changelog-updater/branches/compare/v1.0.0..v0.1.0
`);
});
});
65 changes: 31 additions & 34 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
"dependencies": {},
"devDependencies": {
"coveralls": "^3.0.0",
"eslint": "^4.13.1",
"eslint": "^4.14.0",
"jest": "^21.2.1",
"prettier": "^1.9.1"
"prettier": "^1.9.2"
}
}