From b363962cfbcb35d093e7dc853423dca3f15bfac5 Mon Sep 17 00:00:00 2001 From: Stephen Harris Date: Mon, 5 Sep 2016 16:28:01 +0100 Subject: [PATCH 1/2] Add support for matching patterns. Such release/* or dev-*. This allows users to exclude a 'family' of branches without knowing the branch names. For example, allowing only deployment to a staging envrionment from a branch beginning with "release/" --- package.json | 3 ++- tasks/checkbranch.js | 6 +++--- test/checkbranch.test.js | 42 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 46 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index a326c4c..e61aeed 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/tasks/checkbranch.js b/tasks/checkbranch.js index 0c1471e..46289bb 100644 --- a/tasks/checkbranch.js +++ b/tasks/checkbranch.js @@ -1,6 +1,7 @@ 'use strict'; var shell = require("shelljs"); +var minimatch = require("minimatch"); module.exports = function (grunt) { @@ -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."); } diff --git a/test/checkbranch.test.js b/test/checkbranch.test.js index dfbbd57..df0fdde 100644 --- a/test/checkbranch.test.js +++ b/test/checkbranch.test.js @@ -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"); + }, }); \ No newline at end of file From 3b9a5a2823e2ea64503c659bc083bb9e64b2a699 Mon Sep 17 00:00:00 2001 From: Stephen Harris Date: Thu, 6 Oct 2016 14:42:24 +0100 Subject: [PATCH 2/2] Update readme with branch pattern matching example --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 2ab8607..8d4d246 100644 --- a/README.md +++ b/README.md @@ -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)