Skip to content
This repository has been archived by the owner on Feb 5, 2018. It is now read-only.

Commit

Permalink
feat(index): add autoFix support that lowercases type/subject and pas…
Browse files Browse the repository at this point in the history
…s commit (#56)

* feat(index): add autoFix support that lowercases type and pass commit

* chore(readme): add as contributor

* feat(index): add autoFix support that lowercases first letter of subject

* docs(readme): documentation for autoFix for subject

* test(index): reset autoFix to true after each test

* chore(contributors): update contribution data

* docs(readme): update contribution data
  • Loading branch information
spirosikmd authored and Kent C. Dodds committed Jan 20, 2017
1 parent 082eed8 commit ca62533
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 9 deletions.
15 changes: 14 additions & 1 deletion .all-contributorsrc
Expand Up @@ -224,7 +224,20 @@
"name": "Bo Lingen",
"avatar_url": "https://avatars.githubusercontent.com/u/16605186?v=3",
"profile": "https://github.com/citycide",
"contributions": ["doc"]
"contributions": [
"doc"
]
},
{
"login": "spirosikmd",
"name": "Spyros Ioakeimidis",
"avatar_url": "https://avatars.githubusercontent.com/u/1057324?v=3",
"profile": "http://www.spyros.io",
"contributions": [
"code",
"doc",
"test"
]
}
]
}
13 changes: 9 additions & 4 deletions README.md
Expand Up @@ -7,7 +7,7 @@
[![downloads][downloads-badge]][npm-stat]
[![MIT License][license-badge]][LICENSE]

[![All Contributors](https://img.shields.io/badge/all_contributors-22-orange.svg?style=flat-square)](#contributors)
[![All Contributors](https://img.shields.io/badge/all_contributors-23-orange.svg?style=flat-square)](#contributors)
[![PRs Welcome][prs-badge]][prs]
[![Donate][donate-badge]][donate]
[![Code of Conduct][coc-badge]][coc]
Expand Down Expand Up @@ -40,7 +40,8 @@ You can specify options in `package.json`
"maxSubjectLength": 100, // default
"subjectPattern": ".+", // default
"subjectPatternErrorMsg": "subject does not match subject pattern!", // default
"helpMessage": "" //default
"helpMessage": "", // default
"autoFix": false // default
}
}
}
Expand Down Expand Up @@ -76,7 +77,11 @@ If `subjectPattern` is provided, this message will be displayed if the commit me

If provided, the helpMessage string is displayed when a commit message is not valid. This allows projects to provide a better developer experience for new contributors.

The `helpMessage` also supports interpoling a single `%s` with the original commit message.
The `helpMessage` also supports interpolating a single `%s` with the original commit message.

#### autoFix

If this is set to `true`, type will be auto fixed to all lowercase, subject first letter will be lowercased, and the commit will pass (assuming there's nothing else wrong with it).

### Other notes

Expand Down Expand Up @@ -122,7 +127,7 @@ Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
| [<img src="https://avatars.githubusercontent.com/u/2112202?v=3" width="100px;"/><br /><sub>Shawn Erquhart</sub>](http://www.professant.com)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=erquhart) [📖](https://github.com/kentcdodds/validate-commit-msg/commits?author=erquhart) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=erquhart) | [<img src="https://avatars.githubusercontent.com/u/194482?v=3" width="100px;"/><br /><sub>Tushar Mathur</sub>](http://tusharm.com)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=tusharmath) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=tusharmath) | [<img src="https://avatars.githubusercontent.com/u/904007?v=3" width="100px;"/><br /><sub>Jason Dreyzehner</sub>](https://twitter.com/bitjson)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=bitjson) [📖](https://github.com/kentcdodds/validate-commit-msg/commits?author=bitjson) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=bitjson) | [<img src="https://avatars.githubusercontent.com/u/9654923?v=3" width="100px;"/><br /><sub>Abimbola Idowu</sub>](http://twitter.com/hisabimbola)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=hisabimbola) | [<img src="https://avatars.githubusercontent.com/u/2212006?v=3" width="100px;"/><br /><sub>Gleb Bahmutov</sub>](https://glebbahmutov.com/)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=bahmutov) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=bahmutov) | [<img src="https://avatars.githubusercontent.com/u/332905?v=3" width="100px;"/><br /><sub>Dennis</sub>](http://dennis.io)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=ds82) | [<img src="https://avatars.githubusercontent.com/u/6425649?v=3" width="100px;"/><br /><sub>Matt Lewis</sub>](https://mattlewis.me/)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=mattlewis92) |
| [<img src="https://avatars.githubusercontent.com/u/323761?v=3" width="100px;"/><br /><sub>Tom Vincent</sub>](https://tlvince.com)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=tlvince) | [<img src="https://avatars.githubusercontent.com/u/615381?v=3" width="100px;"/><br /><sub>Anders D. Johnson</sub>](https://andrz.me/)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=adjohnson916) [📖](https://github.com/kentcdodds/validate-commit-msg/commits?author=adjohnson916) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=adjohnson916) | [<img src="https://avatars.githubusercontent.com/u/1643758?v=3" width="100px;"/><br /><sub>James Zetlen</sub>](jameszetlen.com)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=zetlen) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=zetlen) | [<img src="https://avatars.githubusercontent.com/u/235784?v=3" width="100px;"/><br /><sub>Paul Bienkowski</sub>](http://opatut.de)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=opatut) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=opatut) | [<img src="https://avatars.githubusercontent.com/u/324073?v=3" width="100px;"/><br /><sub>Barney Scott</sub>](https://github.com/bmds)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=bmds) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=bmds) | [<img src="https://avatars.githubusercontent.com/u/5572221?v=3" width="100px;"/><br /><sub>Emmanuel Murillo Sánchez</sub>](https://github.com/Emmurillo)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=Emmurillo) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=Emmurillo) | [<img src="https://avatars.githubusercontent.com/u/968267?v=3" width="100px;"/><br /><sub>Hans Kristian Flaatten</sub>](https://starefossen.github.io)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=Starefossen) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=Starefossen) |
| [<img src="https://avatars.githubusercontent.com/u/16605186?v=3" width="100px;"/><br /><sub>Bo Lingen</sub>](https://github.com/citycide)<br />[📖](https://github.com/kentcdodds/validate-commit-msg/commits?author=citycide) |
| [<img src="https://avatars.githubusercontent.com/u/16605186?v=3" width="100px;"/><br /><sub>Bo Lingen</sub>](https://github.com/citycide)<br />[📖](https://github.com/kentcdodds/validate-commit-msg/commits?author=citycide) | [<img src="https://avatars.githubusercontent.com/u/1057324?v=3" width="100px;"/><br /><sub>Spyros Ioakeimidis</sub>](http://www.spyros.io)<br />[💻](https://github.com/kentcdodds/validate-commit-msg/commits?author=spirosikmd) [📖](https://github.com/kentcdodds/validate-commit-msg/commits?author=spirosikmd) [⚠️](https://github.com/kentcdodds/validate-commit-msg/commits?author=spirosikmd) |
<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. Contributions of any kind welcome!
19 changes: 17 additions & 2 deletions index.js
Expand Up @@ -83,11 +83,20 @@ var validateMessage = function(raw) {
isValid = false;
}

// If should auto fix type then do it here
if (config.autoFix) {
type = lowercase(type);
}

if (types !== '*' && types.indexOf(type) === -1) {
error('"%s" is not allowed type ! Valid types are: %s', type, types.join(', '));
isValid = false;
}

if (config.autoFix) {
subject = lowercaseFirstLetter(subject);
}

if (!SUBJECT_PATTERN.exec(subject)) {
error(SUBJECT_PATTERN_ERROR_MSG);
isValid = false;
Expand All @@ -97,8 +106,6 @@ var validateMessage = function(raw) {
// Some more ideas, do want anything like this ?
// - Validate the rest of the message (body, footer, BREAKING CHANGE annotations)
// - allow only specific scopes (eg. fix(docs) should not be allowed ?
// - auto correct the type to lower case ?
// - auto correct first letter of the subject to lower case ?
// - auto add empty line after subject ?
// - auto remove empty () ?
// - auto correct typos in type ?
Expand Down Expand Up @@ -183,3 +190,11 @@ function getGitFolder()

return gitDirLocation;
}

function lowercase(string) {
return string.toLowerCase();
}

function lowercaseFirstLetter(string) {
return lowercase(string.charAt(0)) + string.slice(1);
}
16 changes: 16 additions & 0 deletions index.test.js
Expand Up @@ -39,6 +39,10 @@ describe('validate-commit-msg.js', function() {
}
});

afterEach(function() {
m.config.autoFix = false;
});

describe('validateMessage', function() {

it('should be valid', function() {
Expand Down Expand Up @@ -216,6 +220,18 @@ describe('validate-commit-msg.js', function() {
expect(errors).to.deep.equal(['INVALID COMMIT MSG: subject does not match subject pattern!']);
expect(logs).to.deep.equal([msg]);
});

it('should lowercase type when autoFix is true and make it valid', function() {
m.config.autoFix = true;
m.config.subjectPattern = /^a.*Z$/;
var msg = 'Chore(build): A something Z';
expect(m.validateMessage(msg)).to.equal(VALID);
});

it('should show invalid when autoFix is false and type starts with capital letter', function() {
var msg = 'Chore(build): A something Z';
expect(m.validateMessage(msg)).to.equal(INVALID);
});
});

describe('handle .git as folder', function()
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -68,7 +68,8 @@
"revert",
"custom"
],
"warnOnFail": false
"warnOnFail": false,
"autoFix": false
}
},
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Expand Up @@ -50,7 +50,7 @@ align-text@^0.1.1, align-text@^0.1.3:
longest "^1.0.1"
repeat-string "^1.5.2"

all-contributors-cli@^3.0.7:
all-contributors-cli@3.0.7:
version "3.0.7"
resolved "https://registry.yarnpkg.com/all-contributors-cli/-/all-contributors-cli-3.0.7.tgz#8c1b791e744893c0a254ee6793776eaa1bdf13cc"
dependencies:
Expand Down

0 comments on commit ca62533

Please sign in to comment.