-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
106 lines (90 loc) · 2.63 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
var Repo = require( "git-tools" );
var isEmail = require( "sane-email-validation" );
exports.getCommits = getCommits;
exports.getCommitErrors = getCommitErrors;
function unique( array ) {
var elements = {};
return array.filter(function( element ) {
var exists = elements.hasOwnProperty( element );
elements[ element ] = true;
return !exists;
});
}
function parsePerson( person ) {
var matches = /^(.+)\s<([^>]+)>$/.exec( person );
return {
name: matches[ 1 ],
email: matches[ 2 ]
};
}
function getCommits( settings, callback ) {
var repo = new Repo( settings.path );
var committish = settings.committish || "master";
var exceptional = settings.exceptionalAuthors || {};
var delimiter = "---===DCO===---";
repo.exec( "log",
"--format=%H%n%aN <%aE>%n%cN <%cE>%n%b" + delimiter,
committish,
function( error, log ) {
if ( error ) {
return callback( error );
}
var commits = log.trim().split( delimiter );
commits.pop();
commits = commits.map(function( commit ) {
var body = commit.trim().split( "\n" );
var hash = body.shift();
var author = parsePerson( body.shift() );
var committer = parsePerson( body.shift() );
var signatures = body
.filter(function( line ) {
return line.substring( 0, 13 ) === "Signed-off-by";
})
.map(function( line ) {
return parsePerson( line.substring( 15 ) );
});
// If there are no signatures, but the author is exceptional,
// treat the commit as if the author had signed it
if ( !signatures.length &&
exceptional.hasOwnProperty( author.email ) &&
exceptional[ author.email ] === author.name ) {
signatures.push( author );
}
return {
hash: hash,
author: author,
committer: committer,
license: signatures
};
});
callback( null, commits );
});
}
function validatePerson( person, description ) {
var errors = [];
if ( !isEmail( person.email ) ) {
errors.push( description + " has invalid email address: " + person.email );
}
return errors;
}
function getCommitErrors( settings, callback ) {
getCommits( settings, function( error, commits ) {
if ( error ) {
return callback( error );
}
var errors = [];
commits.forEach(function( commit ) {
errors = errors.concat( validatePerson( commit.author, "Author" ) );
errors = errors.concat( validatePerson( commit.committer, "Committer" ) );
if ( !commit.license.length ) {
errors.push( "Missing DCO for " + commit.hash + "." );
} else {
commit.license.forEach(function( signature ) {
errors = errors.concat( validatePerson( signature, "Signer" ) );
});
}
});
errors = unique( errors );
callback( null, errors, commits );
});
}