Skip to content

Commit

Permalink
Merge pull request #80 from draconisNoctis/master
Browse files Browse the repository at this point in the history
feat(validation): add branch validation via regular expression
  • Loading branch information
hdorgeval authored Oct 14, 2018
2 parents 95c6f55 + 9db25b1 commit 13b8e64
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ 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).

## [4.1.0] - 2018-10-13
### Added
- add branch validation via regular expression

## [4.0.1] - 2018-10-11
### Fixed
- fix: config helper hangs on Windows after answering first question
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ npm run publish-please
- **untrackedFiles** - Check that there are no untracked files in the working tree. Default: `true`.
- **gitTag** - Check that git tag matches version specified in the `package.json`. Default: `true`.
- **branch** - Check that current branch matches the specified branch. Default: `master`.
- You may also set the branch as a regular expression to be able to use pubish-please in a multiple branches scenario like `master` and `release`:
```js
/(master|release)/
```
- **sensitiveData** - Perform [audit for the sensitive data](#sensitive-information-audit). Default: `true`.
- **vulnerableDependencies** - Perform vulnerable dependencies check using `npm audit`. Default: `true` if npm version is 6.1.0 or above, `false` otherwise.
- you may prevent specific vulnerabilities to be reported by publish-please by creating a `.auditignore` file in the root of your project with content like the following:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "publish-please",
"version": "4.0.1",
"version": "4.1.0",
"description": "Safe and highly functional replacement for `npm publish`.",
"main": "./lib/index.js",
"bin": {
Expand Down
9 changes: 8 additions & 1 deletion src/validations/branch.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ module.exports = {
.replace('* ', '')
)
.then((branch) => {
if (branch !== expected)
if (expected.match(/^\/.*\/$/)) {
const regexp = new RegExp(
expected.replace(/^\/(.*)\/$/, '$1')
);
if (!regexp.test(branch)) {
throw `Expected branch to match ${expected}, but it was '${branch}'.`;
}
} else if (branch !== expected)
throw `Expected branch to be '${expected}', but it was '${branch}'.`;
});
},
Expand Down
77 changes: 77 additions & 0 deletions test/12-integration-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,29 @@ describe('Integration tests', () => {
)
));

it('Should validate the branch set in the configuration file via RegExp', () =>
exec('git checkout some-branch')
.then(() =>
publish(
getTestOptions({
set: {
validations: {
branch: '/(^master$|^release$)/',
},
},
})
)
)
.then(() => {
throw new Error('Promise rejection expected');
})
.catch((err) =>
assert.strictEqual(
err.message,
" * Expected branch to match /(^master$|^release$)/, but it was 'some-branch'."
)
));

it('Should expect the latest commit in the branch', () =>
exec('git checkout 15a1ef78338cf1fa60c318828970b2b3e70004d1')
.then(() =>
Expand Down Expand Up @@ -257,6 +280,60 @@ describe('Integration tests', () => {
)
));

it('Should pass branch validation via RegExp (master branch)', () =>
exec('git checkout master').then(() =>
publish(
getTestOptions({
set: {
publishCommand: echoPublishCommand,
validations: {
branch: '/(^master$|^release$)/',
},
},
})
)
));
it('Should pass branch validation via simple RegExp (master branch)', () =>
exec('git checkout master').then(() =>
publish(
getTestOptions({
set: {
publishCommand: echoPublishCommand,
validations: {
branch: '/(master|release)/',
},
},
})
)
));

it('Should pass branch validation via RegExp (release branch)', () =>
exec('git checkout -b release').then(() =>
publish(
getTestOptions({
set: {
publishCommand: echoPublishCommand,
validations: {
branch: '/(^master$|^release$)/',
},
},
})
)
));
it('Should pass branch validation via simple RegExp (hotfix branch)', () =>
exec('git checkout -b hotfix').then(() =>
publish(
getTestOptions({
set: {
publishCommand: echoPublishCommand,
validations: {
branch: '/(master|release|hotfix)/',
},
},
})
)
));

it('Should not validate if branch-validation is disabled', () =>
exec('git checkout some-branch').then(() =>
publish(
Expand Down

0 comments on commit 13b8e64

Please sign in to comment.