|
| 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 | +const botNamePattern = /\[bot\]$/i |
| 8 | +const botEmailPattern = /\[bot\]@users\.noreply\.github\.com$/i |
| 9 | + |
| 10 | +// Extract email from an author string like "Name <email>" |
| 11 | +function parseEmail (authorStr) { |
| 12 | + if (!authorStr) return null |
| 13 | + const match = authorStr.match(/<([^>]+)>/) |
| 14 | + return match ? match[1].toLowerCase() : null |
| 15 | +} |
| 16 | + |
| 17 | +export default { |
| 18 | + id, |
| 19 | + meta: { |
| 20 | + description: 'enforce DCO sign-off', |
| 21 | + recommended: true |
| 22 | + }, |
| 23 | + defaults: {}, |
| 24 | + options: {}, |
| 25 | + validate: (context, rule) => { |
| 26 | + const parsed = context.toJSON() |
| 27 | + |
| 28 | + // Release commits generally won't have sign-offs |
| 29 | + if (parsed.release) { |
| 30 | + context.report({ |
| 31 | + id, |
| 32 | + message: 'skipping sign-off for release commit', |
| 33 | + string: '', |
| 34 | + level: 'skip' |
| 35 | + }) |
| 36 | + return |
| 37 | + } |
| 38 | + |
| 39 | + const signoffPattern = /^Signed-off-by: /i |
| 40 | + const validSignoff = /^Signed-off-by: .+ <[^@]+@[^@]+\.[^@]+>/i |
| 41 | + const signoffs = parsed.body |
| 42 | + .map((line, i) => [line, i]) |
| 43 | + .filter(([line]) => signoffPattern.test(line)) |
| 44 | + |
| 45 | + if (signoffs.length === 0) { |
| 46 | + context.report({ |
| 47 | + id, |
| 48 | + message: 'Commit must have a "Signed-off-by" trailer', |
| 49 | + string: '', |
| 50 | + level: 'fail' |
| 51 | + }) |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + // Check that at least one sign-off has a valid email |
| 56 | + const hasValid = signoffs.some(([line]) => validSignoff.test(line)) |
| 57 | + if (!hasValid) { |
| 58 | + const [line, lineNum] = signoffs[0] |
| 59 | + context.report({ |
| 60 | + id, |
| 61 | + message: '"Signed-off-by" trailer has invalid email', |
| 62 | + string: line, |
| 63 | + line: lineNum, |
| 64 | + column: 0, |
| 65 | + level: 'fail' |
| 66 | + }) |
| 67 | + return |
| 68 | + } |
| 69 | + |
| 70 | + // Check that no sign-off appears to be from a bot or AI agent. |
| 71 | + // Bots and AI agents are not permitted to sign off on commits. |
| 72 | + for (const [line, lineNum] of signoffs) { |
| 73 | + const match = line.match(signoffParts) |
| 74 | + if (!match) continue |
| 75 | + const name = match[1] |
| 76 | + const email = match[2] |
| 77 | + if (botNamePattern.test(name) || botEmailPattern.test(email)) { |
| 78 | + context.report({ |
| 79 | + id, |
| 80 | + message: '"Signed-off-by" must be from a human author, ' + |
| 81 | + 'not a bot or AI agent', |
| 82 | + string: line, |
| 83 | + line: lineNum, |
| 84 | + column: 0, |
| 85 | + level: 'warn' |
| 86 | + }) |
| 87 | + return |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + // When author info is available, warn if none of the sign-off emails |
| 92 | + // match the commit author email. This may indicate an automated tool |
| 93 | + // signed off on behalf of the author. |
| 94 | + const authorEmail = parseEmail(parsed.author) |
| 95 | + if (authorEmail) { |
| 96 | + const authorMatch = signoffs.some(([line]) => { |
| 97 | + const match = line.match(signoffParts) |
| 98 | + if (!match) return false |
| 99 | + return match[2].toLowerCase() === authorEmail |
| 100 | + }) |
| 101 | + if (!authorMatch) { |
| 102 | + context.report({ |
| 103 | + id, |
| 104 | + message: '"Signed-off-by" email does not match the ' + |
| 105 | + 'commit author email', |
| 106 | + string: signoffs[0][0], |
| 107 | + line: signoffs[0][1], |
| 108 | + column: 0, |
| 109 | + level: 'warn' |
| 110 | + }) |
| 111 | + return |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + context.report({ |
| 116 | + id, |
| 117 | + message: 'has valid Signed-off-by', |
| 118 | + string: '', |
| 119 | + level: 'pass' |
| 120 | + }) |
| 121 | + } |
| 122 | +} |
0 commit comments