Skip to content

Commit 356cfb5

Browse files
ajoslinpkozlowski-opensource
authored andcommitted
chore(build): Add 'enforce' task, custom changelog template
1 parent 201fdf8 commit 356cfb5

File tree

3 files changed

+138
-2
lines changed

3 files changed

+138
-2
lines changed

Gruntfile.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ module.exports = function(grunt) {
133133
},
134134
changelog: {
135135
options: {
136-
dest: 'CHANGELOG.md'
136+
dest: 'CHANGELOG.md',
137+
templateFile: 'misc/changelog.tpl.md'
137138
}
138139
},
139140
shell: {
@@ -172,7 +173,7 @@ module.exports = function(grunt) {
172173

173174
//register before and after test tasks so we've don't have to change cli
174175
//options on the goole's CI server
175-
grunt.registerTask('before-test', ['jshint', 'html2js']);
176+
grunt.registerTask('before-test', ['enforce', 'jshint', 'html2js']);
176177
grunt.registerTask('after-test', ['build', 'copy']);
177178

178179
//Rename our watch task to 'delta', then make actual 'watch'
@@ -183,6 +184,13 @@ module.exports = function(grunt) {
183184
// Default task.
184185
grunt.registerTask('default', ['before-test', 'test', 'after-test']);
185186

187+
grunt.registerTask('enforce', 'Install commit message enforce script if it doesn\'t exist', function() {
188+
if (!grunt.file.exists('.git/hooks/commit-msg')) {
189+
grunt.file.copy('misc/validate-commit-msg.js', '.git/hooks/commit-msg');
190+
require('fs').chmodSync('.git/hooks/commit-msg', '0755');
191+
}
192+
});
193+
186194
//Common ui.bootstrap module containing all modules for src and templates
187195
//findModule: Adds a given module to config
188196
var foundModules = {};

misc/changelog.tpl.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
# <%= version%> (<%= today%>)
3+
4+
<% if (_(changelog.feat).size() > 0) { %> ## Features
5+
<% _(changelog.feat).forEach(function(changes, scope) { %>
6+
- **<%= scope%>:**
7+
<% changes.forEach(function(change) { %> - <%= change.msg%> (<%= helpers.commitLink(change.sha1) %>)
8+
<% }); %>
9+
<% }); %> <% } %>
10+
11+
<% if (_(changelog.fix).size() > 0) { %> ## Fixes
12+
<% _(changelog.fix).forEach(function(changes, scope) { %>
13+
- **<%= scope%>:**
14+
<% changes.forEach(function(change) { %> - <%= change.msg%> (<%= helpers.commitLink(change.sha1) %>)
15+
<% }); %>
16+
<% }); %> <% } %>
17+
18+
<% if (_(changelog.breaking).size() > 0) { %> ## Breaking Changes
19+
<% _(changelog.breaking).forEach(function(changes, scope) { %>
20+
- **<%= scope%>:**
21+
<% changes.forEach(function(change) { %> <%= change.msg%>
22+
<% }); %>
23+
<% }); %> <% } %>

misc/validate-commit-msg.js

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* Git COMMIT-MSG hook for validating commit message
5+
* See https://docs.google.com/document/d/1rk04jEuGfk9kYzfqCuOlPTSJw3hEDZJTBN5E5f1SALo/edit
6+
*
7+
* Installation:
8+
* >> cd <angular-repo>
9+
* >> ln -s validate-commit-msg.js .git/hooks/commit-msg
10+
*/
11+
var fs = require('fs');
12+
var util = require('util');
13+
14+
15+
var MAX_LENGTH = 70;
16+
var PATTERN = /^(?:fixup!\s*)?(\w*)(\((\w+)\))?\: (.*)$/;
17+
var IGNORED = /^WIP\:/;
18+
var TYPES = {
19+
feat: true,
20+
fix: true,
21+
docs: true,
22+
style: true,
23+
refactor: true,
24+
test: true,
25+
chore: true,
26+
revert: true
27+
};
28+
29+
30+
var error = function() {
31+
// gitx does not display it
32+
// http://gitx.lighthouseapp.com/projects/17830/tickets/294-feature-display-hook-error-message-when-hook-fails
33+
// https://groups.google.com/group/gitx/browse_thread/thread/a03bcab60844b812
34+
console.error('INVALID COMMIT MSG: ' + util.format.apply(null, arguments));
35+
};
36+
37+
38+
var validateMessage = function(message) {
39+
var isValid = true;
40+
41+
if (IGNORED.test(message)) {
42+
console.log('Commit message validation ignored.');
43+
return true;
44+
}
45+
46+
if (message.length > MAX_LENGTH) {
47+
error('is longer than %d characters !', MAX_LENGTH);
48+
isValid = false;
49+
}
50+
51+
var match = PATTERN.exec(message);
52+
53+
if (!match) {
54+
error('does not match "<type>(<scope>): <subject>" ! was: "' + message + '"\nNote: <scope> must be only letters.');
55+
return false;
56+
}
57+
58+
var type = match[1];
59+
var scope = match[3];
60+
var subject = match[4];
61+
62+
if (!TYPES.hasOwnProperty(type)) {
63+
error('"%s" is not allowed type !', type);
64+
return false;
65+
}
66+
67+
// Some more ideas, do want anything like this ?
68+
// - allow only specific scopes (eg. fix(docs) should not be allowed ?
69+
// - auto correct the type to lower case ?
70+
// - auto correct first letter of the subject to lower case ?
71+
// - auto add empty line after subject ?
72+
// - auto remove empty () ?
73+
// - auto correct typos in type ?
74+
// - store incorrect messages, so that we can learn
75+
76+
return isValid;
77+
};
78+
79+
80+
var firstLineFromBuffer = function(buffer) {
81+
return buffer.toString().split('\n').shift();
82+
};
83+
84+
85+
86+
// publish for testing
87+
exports.validateMessage = validateMessage;
88+
89+
// hacky start if not run by jasmine :-D
90+
if (process.argv.join('').indexOf('jasmine-node') === -1) {
91+
var commitMsgFile = process.argv[2];
92+
var incorrectLogFile = commitMsgFile.replace('COMMIT_EDITMSG', 'logs/incorrect-commit-msgs');
93+
94+
fs.readFile(commitMsgFile, function(err, buffer) {
95+
var msg = firstLineFromBuffer(buffer);
96+
97+
if (!validateMessage(msg)) {
98+
fs.appendFile(incorrectLogFile, msg + '\n', function() {
99+
process.exit(1);
100+
});
101+
} else {
102+
process.exit(0);
103+
}
104+
});
105+
}

0 commit comments

Comments
 (0)