Skip to content

Commit f28f04c

Browse files
committed
rules: add metadata-end rule
This rule verifies that there should be no non-empty lines after the commit metadata. It is disabled if the "validate-metadata" option is false. Fixes: #6 Semver: minor
1 parent 02073c3 commit f28f04c

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

lib/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ module.exports = class ValidateCommit extends EE {
2828
if (!this.opts['validate-metadata']) {
2929
this.disableRule('pr-url')
3030
this.disableRule('reviewers')
31+
this.disableRule('metadata-end')
3132
}
3233
}
3334

lib/rules/metadata-end.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'use strict'
2+
3+
const id = 'metadata-end'
4+
5+
module.exports = {
6+
id: id
7+
, meta: {
8+
description: 'enforce that metadata is at the end of commit messages'
9+
, recommended: true
10+
}
11+
, defaults: {}
12+
, options: {}
13+
, validate: (context, rule) => {
14+
const parsed = context.toJSON()
15+
const body = parsed.body
16+
const end = parsed.metadata.end
17+
if (end < body.length) {
18+
const extra = body.slice(end + 1)
19+
let lineNum = end + 1
20+
for (var i = 0; i < extra.length; i++) {
21+
if (extra[i]) {
22+
lineNum += i
23+
break
24+
}
25+
}
26+
27+
if (lineNum !== end + 1) {
28+
context.report({
29+
id: id
30+
, message: 'commit metadata at end of message'
31+
, string: body[lineNum]
32+
, line: lineNum
33+
, column: 0
34+
, level: 'fail'
35+
})
36+
return
37+
}
38+
}
39+
40+
context.report({
41+
id: id
42+
, message: 'metadata is at end of message'
43+
, string: ''
44+
, level: 'pass'
45+
})
46+
}
47+
}

test/validator.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,35 @@ Date: Thu Mar 3 10:10:46 2016 -0600
7676
The properties on memoryUsage were not checked before,
7777
this commit checks them.`
7878

79+
/* eslint-disable */
80+
const str6 = {
81+
"sha": "c5545f2c63fe30b0cfcdafab18c26df8286881d0",
82+
"url": "https://api.github.com/repos/nodejs/node/git/commits/c5545f2c63fe30b0cfcdafab18c26df8286881d0",
83+
"html_url": "https://github.com/nodejs/node/commit/c5545f2c63fe30b0cfcdafab18c26df8286881d0",
84+
"author": {
85+
"name": "Anna Henningsen",
86+
"email": "[email protected]",
87+
"date": "2016-09-13T10:57:49Z"
88+
},
89+
"committer": {
90+
"name": "Anna Henningsen",
91+
"email": "[email protected]",
92+
"date": "2016-09-19T12:50:57Z"
93+
},
94+
"tree": {
95+
"sha": "b505c0ffa0555730e9f4cdb391d1ebeb48bb2f59",
96+
"url": "https://api.github.com/repos/nodejs/node/git/trees/b505c0ffa0555730e9f4cdb391d1ebeb48bb2f59"
97+
},
98+
"message": "fs: fix handling of `uv_stat_t` fields\n\n`FChown` and `Chown` test that the `uid` and `gid` parameters\nthey receive are unsigned integers, but `Stat()` and `FStat()`\nwould return the corresponding fields of `uv_stat_t` as signed\nintegers. Applications which pass those these values directly\nto `Chown` may fail\n(e.g. for `nobody` on OS X, who has an `uid` of `-2`, see e.g.\nhttps://github.com/nodejs/node-v0.x-archive/issues/5890).\n\nThis patch changes the `Integer::New()` call for `uid` and `gid`\nto `Integer::NewFromUnsigned()`.\n\nAll other fields are kept as they are, for performance, but\nstrictly speaking the respective sizes of those\nfields aren’t specified, either.\n\nRef: https://github.com/npm/npm/issues/13918\nPR-URL: https://github.com/nodejs/node/pull/8515\nReviewed-By: Ben Noordhuis <[email protected]>\nReviewed-By: Sakthipriyan Vairamani <[email protected]>\nReviewed-By: James M Snell <[email protected]>\n\nundo accidental change to other fields of uv_fs_stat",
99+
"parents": [
100+
{
101+
"sha": "4e76bffc0c7076a5901179e70c7b8a8f9fcd22e4",
102+
"url": "https://api.github.com/repos/nodejs/node/git/commits/4e76bffc0c7076a5901179e70c7b8a8f9fcd22e4",
103+
"html_url": "https://github.com/nodejs/node/commit/4e76bffc0c7076a5901179e70c7b8a8f9fcd22e4"
104+
}
105+
]
106+
}
107+
/* eslint-enable */
79108

80109
test('Validator - misc', (t) => {
81110
const v = new Validator()
@@ -213,5 +242,29 @@ test('Validator - real commits', (t) => {
213242
})
214243
})
215244

245+
t.test('non empty lines after metadata', (tt) => {
246+
const v = new Validator()
247+
v.lint(str6)
248+
v.on('commit', (data) => {
249+
const c = data.commit.toJSON()
250+
tt.equal(c.sha, 'c5545f2c63fe30b0cfcdafab18c26df8286881d0', 'sha')
251+
tt.equal(c.date, '2016-09-13T10:57:49Z', 'date')
252+
tt.deepEqual(c.subsystems, ['fs'], 'subsystems')
253+
tt.equal(c.prUrl, 'https://github.com/nodejs/node/pull/8515', 'pr')
254+
tt.equal(c.revert, false, 'revert')
255+
const msgs = data.messages
256+
const filtered = msgs.filter((item) => {
257+
return item.level === 'fail'
258+
})
259+
tt.equal(filtered.length, 1, 'messages.length')
260+
const item = filtered[0]
261+
tt.equal(item.id, 'metadata-end', 'id')
262+
tt.equal(item.message, 'commit metadata at end of message', 'message')
263+
tt.equal(item.line, 22, 'line')
264+
tt.equal(item.column, 0, 'column')
265+
tt.end()
266+
})
267+
})
268+
216269
t.end()
217270
})

0 commit comments

Comments
 (0)