Skip to content

Commit b2bf5e0

Browse files
committed
Add Signed-off-by rule
Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-By: Opencode/Opus 4.6
1 parent 450f858 commit b2bf5e0

File tree

7 files changed

+813
-9
lines changed

7 files changed

+813
-9
lines changed

lib/rules/signed-off-by.js

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
const id = 'signed-off-by'
2+
3+
// Matches the name and email from a Signed-off-by line
4+
const signoffParts = /^Signed-off-by: (.*) <([^>]+)>/i
5+
6+
// Bot/AI patterns: name ending in [bot], or GitHub bot noreply emails
7+
// This is an imperfect heuristic, but there's no standard way to identify
8+
// bot commits in a reliable, generic way.
9+
const botNamePattern = /\[bot\]$/i
10+
const botEmailPattern = /\[bot\]@users\.noreply\.github\.com$/i
11+
12+
// Matches "Name <email>" author strings
13+
const authorPattern = /^(.*?)\s*<([^>]+)>/
14+
const signoffPattern = /^Signed-off-by: /i
15+
const validSignoff = /^Signed-off-by: .+ <[^@]+@[^@]+\.[^@]+>/i
16+
17+
// Parse name and email from an author string like "Name <email>"
18+
function parseAuthor (authorStr) {
19+
if (!authorStr) return null
20+
const match = authorStr.match(authorPattern)
21+
if (!match) return null // Purely defensive check here
22+
return { name: match[1].trim(), email: match[2].toLowerCase() }
23+
}
24+
25+
// Check if an author string looks like a bot
26+
function isBotAuthor (authorStr) {
27+
const author = parseAuthor(authorStr)
28+
if (!author) return false
29+
return botNamePattern.test(author.name) || botEmailPattern.test(author.email)
30+
}
31+
32+
export default {
33+
id,
34+
meta: {
35+
description: 'enforce DCO sign-off',
36+
recommended: true
37+
},
38+
defaults: {},
39+
options: {},
40+
validate: (context, rule) => {
41+
const parsed = context.toJSON()
42+
43+
// Release commits generally won't have sign-offs
44+
if (parsed.release) {
45+
context.report({
46+
id,
47+
message: 'skipping sign-off for release commit',
48+
string: '',
49+
level: 'skip'
50+
})
51+
return
52+
}
53+
54+
const signoffs = parsed.body
55+
.map((line, i) => [line, i])
56+
.filter(([line]) => signoffPattern.test(line))
57+
58+
// Bot-authored commits don't need a sign-off.
59+
// If they have one, warn; otherwise pass.
60+
if (isBotAuthor(parsed.author)) {
61+
if (signoffs.length === 0) {
62+
context.report({
63+
id,
64+
message: 'skipping sign-off for bot commit',
65+
string: '',
66+
level: 'pass'
67+
})
68+
} else {
69+
for (const [line, lineNum] of signoffs) {
70+
context.report({
71+
id,
72+
message: 'bot commit should not have a "Signed-off-by" trailer',
73+
string: line,
74+
line: lineNum,
75+
column: 0,
76+
level: 'warn'
77+
})
78+
}
79+
}
80+
return
81+
}
82+
83+
// Assume it's not a bot commit... a Signed-off-by trailer is required.
84+
if (signoffs.length === 0) {
85+
context.report({
86+
id,
87+
message: 'Commit must have a "Signed-off-by" trailer',
88+
string: '',
89+
level: 'fail'
90+
})
91+
return
92+
}
93+
94+
// Flag every sign-off that has an invalid email format.
95+
// Collect valid sign-offs for further checks.
96+
const valid = []
97+
for (const [line, lineNum] of signoffs) {
98+
if (validSignoff.test(line)) {
99+
valid.push([line, lineNum])
100+
} else {
101+
context.report({
102+
id,
103+
message: '"Signed-off-by" trailer has invalid email',
104+
string: line,
105+
line: lineNum,
106+
column: 0,
107+
level: 'fail'
108+
})
109+
}
110+
}
111+
112+
if (valid.length === 0) {
113+
// All sign-offs had invalid emails; already reported above.
114+
return
115+
}
116+
117+
// Flag any sign-off that appears to be from a bot or AI agent.
118+
// Bots and AI agents are not permitted to sign off on commits.
119+
// Collect non-bot sign-offs for further checks. If the commit
120+
// itself appears to be from a bot, the case is handled above.
121+
const human = []
122+
for (const [line, lineNum] of valid) {
123+
const { 1: name, 2: email } = line.match(signoffParts)
124+
if (botNamePattern.test(name) || botEmailPattern.test(email)) {
125+
context.report({
126+
id,
127+
message: '"Signed-off-by" must be from a human author, ' +
128+
'not a bot or AI agent',
129+
string: line,
130+
line: lineNum,
131+
column: 0,
132+
level: 'warn'
133+
})
134+
} else {
135+
human.push([line, lineNum])
136+
}
137+
}
138+
139+
// All sign-offs appear to be from bots; already reported above.
140+
// If there are no human sign-offs, fail.
141+
if (human.length === 0) {
142+
context.report({
143+
id,
144+
message: 'Commit must have a "Signed-off-by" trailer from a human author',
145+
string: '',
146+
level: 'fail'
147+
})
148+
return
149+
}
150+
151+
// When author info is available, warn if none of the human sign-off
152+
// emails match the commit author email. This may indicate an automated
153+
// tool signed off on behalf of the author.
154+
const authorEmail = parseAuthor(parsed.author)?.email
155+
if (authorEmail) {
156+
const authorMatch = human.some(([line]) => {
157+
const { 2: email } = line.match(signoffParts)
158+
return email.toLowerCase() === authorEmail
159+
})
160+
if (!authorMatch) {
161+
context.report({
162+
id,
163+
message: '"Signed-off-by" email does not match the ' +
164+
'commit author email',
165+
string: human[0][0],
166+
line: human[0][1],
167+
column: 0,
168+
level: 'warn'
169+
})
170+
return
171+
}
172+
}
173+
174+
context.report({
175+
id,
176+
message: 'has valid Signed-off-by',
177+
string: '',
178+
level: 'pass'
179+
})
180+
}
181+
}

lib/validator.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import metadataEnd from './rules/metadata-end.js'
1111
import prUrl from './rules/pr-url.js'
1212
import reviewers from './rules/reviewers.js'
1313
import subsystem from './rules/subsystem.js'
14+
import signedOffBy from './rules/signed-off-by.js'
1415
import titleFormat from './rules/title-format.js'
1516
import titleLength from './rules/title-length.js'
1617

@@ -22,6 +23,7 @@ const RULES = {
2223
'metadata-end': metadataEnd,
2324
'pr-url': prUrl,
2425
reviewers,
26+
'signed-off-by': signedOffBy,
2527
subsystem,
2628
'title-format': titleFormat,
2729
'title-length': titleLength

test/cli-test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ test('Test cli flags', (t) => {
155155
t.test('test stdin with valid JSON', (tt) => {
156156
const validCommit = {
157157
id: '2b98d02b52',
158-
message: 'stream: make null an invalid chunk to write in object mode\n\nthis harmonizes behavior between readable, writable, and transform\nstreams so that they all handle nulls in object mode the same way by\nconsidering them invalid chunks.\n\nPR-URL: https://github.com/nodejs/node/pull/6170\nReviewed-By: James M Snell <jasnell@gmail.com>\nReviewed-By: Matteo Collina <matteo.collina@gmail.com>'
158+
message: 'stream: make null an invalid chunk to write in object mode\n\nthis harmonizes behavior between readable, writable, and transform\nstreams so that they all handle nulls in object mode the same way by\nconsidering them invalid chunks.\n\nSigned-off-by: Calvin Metcalf <cmetcalf@appgeo.com>\nPR-URL: https://github.com/nodejs/node/pull/6170\nReviewed-By: James M Snell <jasnell@gmail.com>\nReviewed-By: Matteo Collina <matteo.collina@gmail.com>'
159159
}
160160
const input = JSON.stringify([validCommit])
161161

@@ -211,11 +211,11 @@ test('Test cli flags', (t) => {
211211
const commits = [
212212
{
213213
id: 'commit1',
214-
message: 'doc: update README\n\nPR-URL: https://github.com/nodejs/node/pull/1111\nReviewed-By: Someone <someone@example.com>'
214+
message: 'doc: update README\n\nSigned-off-by: Someone <someone@example.com>\nPR-URL: https://github.com/nodejs/node/pull/1111\nReviewed-By: Someone <someone@example.com>'
215215
},
216216
{
217217
id: 'commit2',
218-
message: 'test: add new test case\n\nPR-URL: https://github.com/nodejs/node/pull/2222\nReviewed-By: Someone <someone@example.com>'
218+
message: 'test: add new test case\n\nSigned-off-by: Someone <someone@example.com>\nPR-URL: https://github.com/nodejs/node/pull/2222\nReviewed-By: Someone <someone@example.com>'
219219
}
220220
]
221221
const input = JSON.stringify(commits)
@@ -337,7 +337,7 @@ test('Test cli flags', (t) => {
337337
t.test('test stdin with --no-validate-metadata', (tt) => {
338338
const commit = {
339339
id: 'novalidate',
340-
message: 'doc: update README\n\nThis commit has no PR-URL or reviewers'
340+
message: 'doc: update README\n\nThis commit has no PR-URL or reviewers\n\nSigned-off-by: Someone <someone@example.com>'
341341
}
342342
const input = JSON.stringify([commit])
343343

test/fixtures/commit.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"sha": "d3f20ccfaa7b0919a7c5a472e344b7de8829b30c",
1717
"url": "https://api.github.com/repos/nodejs/node/git/trees/d3f20ccfaa7b0919a7c5a472e344b7de8829b30c"
1818
},
19-
"message": "stream: make null an invalid chunk to write in object mode\n\nthis harmonizes behavior between readable, writable, and transform\nstreams so that they all handle nulls in object mode the same way by\nconsidering them invalid chunks.\n\nPR-URL: https://github.com/nodejs/node/pull/6170\nReviewed-By: James M Snell <jasnell@gmail.com>\nReviewed-By: Matteo Collina <matteo.collina@gmail.com>",
19+
"message": "stream: make null an invalid chunk to write in object mode\n\nthis harmonizes behavior between readable, writable, and transform\nstreams so that they all handle nulls in object mode the same way by\nconsidering them invalid chunks.\n\nSigned-off-by: Calvin Metcalf <cmetcalf@appgeo.com>\nPR-URL: https://github.com/nodejs/node/pull/6170\nReviewed-By: James M Snell <jasnell@gmail.com>\nReviewed-By: Matteo Collina <matteo.collina@gmail.com>",
2020
"parents": [
2121
{
2222
"sha": "ec2822adaad76b126b5cccdeaa1addf2376c9aa6",

test/fixtures/pr.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"email": "cmetcalf@appgeo.com",
1313
"date": "2016-04-13T16:33:55Z"
1414
},
15-
"message": "stream: make null an invalid chunk to write in object mode\n\nthis harmonizes behavior between readable, writable, and transform\nstreams so that they all handle nulls in object mode the same way by\nconsidering them invalid chunks.\n\nPR-URL: https://github.com/nodejs/node/pull/6170\nReviewed-By: James M Snell <jasnell@gmail.com>\nReviewed-By: Matteo Collina <matteo.collina@gmail.com>",
15+
"message": "stream: make null an invalid chunk to write in object mode\n\nthis harmonizes behavior between readable, writable, and transform\nstreams so that they all handle nulls in object mode the same way by\nconsidering them invalid chunks.\n\nSigned-off-by: Calvin Metcalf <cmetcalf@appgeo.com>\nPR-URL: https://github.com/nodejs/node/pull/6170\nReviewed-By: James M Snell <jasnell@gmail.com>\nReviewed-By: Matteo Collina <matteo.collina@gmail.com>",
1616
"tree": {
1717
"sha": "e4f9381fdd77d1fd38fe27a80dc43486ac732d48",
1818
"url": "https://api.github.com/repos/nodejs/node/git/trees/e4f9381fdd77d1fd38fe27a80dc43486ac732d48"

0 commit comments

Comments
 (0)