Skip to content
This repository has been archived by the owner on Jan 2, 2020. It is now read-only.

Add support for matching patterns. Such release/* or dev-*. #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,15 @@ To force checks (disables skipping), add the third param with `true`:
grunt.registerTask("deploy", ["test", "checkbranch:develop:true", "copy"]
```

Use the 'wildcard' `*` to match a branch prefix or suffix:

```js
grunt.registerTask("deploy", ["stage", "checkbranch:release/*", "copy"]
```


## Contributors ##

* [Dominykas Blyžė](https://www.dominykas.com/)
* [Daniel Lowes](https://github.com/Pleochism)
* [Stephen Harris](https://github.com/stephenharris)
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"test": "grunt --verbose"
},
"dependencies": {
"shelljs": "0.7.x"
"shelljs": "0.7.x",
"minimatch": "3.0.x"
},
"devDependencies": {
"buster": "0.7.x",
Expand Down
6 changes: 3 additions & 3 deletions tasks/checkbranch.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var shell = require("shelljs");
var minimatch = require("minimatch");

module.exports = function (grunt) {

Expand All @@ -25,12 +26,11 @@ module.exports = function (grunt) {
if (e) {
grunt.fail.fatal("Failed to detect the current branch");
}

var branch = branchOutput.trim();
if (!negate && branch !== expectedBranch) {
if (!negate && !minimatch(branch, expectedBranch)) {
grunt.fail.fatal("Only '" + expectedBranch + "' branch is allowed, and you're on '" + branch + "' branch.");
}
else if (negate && branch === expectedBranch) {
else if (negate && minimatch(branch, expectedBranch) ) {
grunt.fail.fatal("Anything except '" + expectedBranch + "' branch is allowed, and you're on '" + branch + "' branch.");
}

Expand Down
42 changes: 41 additions & 1 deletion test/checkbranch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,46 @@ buster.testCase("grunt-checkbranch", {
expect(output.stdout).toMatch(GRUNT_FATAL);
expect(output.stdout).toMatch("Failed to detect");
expect(output.code).toEqual(1, "Incorrect grunt output code");
}
},

"should proceed when branch matches pattern": function () {
shell.pushd('tmp');
shell.exec("git checkout -b release/1.2.3");
shell.popd();

var output = execGrunt("checkbranch:release/*");
expect(output.stdout).toMatch(GRUNT_SUCCESS);
expect(output.code).toEqual(0, "Incorrect grunt output code");
},

"should not proceed when branch does not match pattern": function () {
shell.pushd('tmp');
shell.exec("git checkout -b release");
shell.popd();

var output = execGrunt("checkbranch:release/*");
expect(output.stdout).toMatch(GRUNT_FATAL);
expect(output.code).toEqual(1, "Incorrect grunt output code");
},

"should proceed when branch does not match negated pattern": function () {
shell.pushd('tmp');
shell.exec("git checkout -b master");
shell.popd();

var output = execGrunt("checkbranch:!dev-*");
expect(output.stdout).toMatch(GRUNT_SUCCESS);
expect(output.code).toEqual(0, "Incorrect grunt output code");
},

"should not proceed when branch matches negated pattern": function () {
shell.pushd('tmp');
shell.exec("git checkout -b release/1.2.3");
shell.popd();

var output = execGrunt("checkbranch:!release/*");
expect(output.stdout).toMatch(GRUNT_FATAL);
expect(output.code).toEqual(1, "Incorrect grunt output code");
},

});