2015-10-05 20:16:52 +00:00
|
|
|
package dco
|
|
|
|
|
|
|
|
import (
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/vbatts/git-validation/git"
|
|
|
|
"github.com/vbatts/git-validation/validate"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
validate.RegisterRule(DcoRule)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2015-10-05 22:55:05 +00:00
|
|
|
// ValidDCO is the regexp for signed off DCO
|
2015-10-05 20:16:52 +00:00
|
|
|
ValidDCO = regexp.MustCompile(`^Signed-off-by: ([^<]+) <([^<>@]+@[^<>]+)>$`)
|
2015-10-05 22:55:05 +00:00
|
|
|
// DcoRule is the rule being registered
|
|
|
|
DcoRule = validate.Rule{
|
2015-10-05 20:16:52 +00:00
|
|
|
Name: "DCO",
|
|
|
|
Description: "makes sure the commits are signed",
|
|
|
|
Run: ValidateDCO,
|
2017-10-09 19:49:59 +00:00
|
|
|
Default: true,
|
2015-10-05 20:16:52 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2016-04-05 14:06:48 +00:00
|
|
|
// ValidateDCO checks that the commit has been signed off, per the DCO process
|
2017-10-09 19:49:59 +00:00
|
|
|
func ValidateDCO(r validate.Rule, c git.CommitEntry) (vr validate.Result) {
|
2015-10-05 20:16:52 +00:00
|
|
|
vr.CommitEntry = c
|
|
|
|
if len(strings.Split(c["parent"], " ")) > 1 {
|
|
|
|
vr.Pass = true
|
|
|
|
vr.Msg = "merge commits do not require DCO"
|
|
|
|
return vr
|
|
|
|
}
|
|
|
|
|
|
|
|
hasValid := false
|
|
|
|
for _, line := range strings.Split(c["body"], "\n") {
|
|
|
|
if ValidDCO.MatchString(line) {
|
|
|
|
hasValid = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !hasValid {
|
|
|
|
vr.Pass = false
|
|
|
|
vr.Msg = "does not have a valid DCO"
|
|
|
|
} else {
|
|
|
|
vr.Pass = true
|
|
|
|
vr.Msg = "has a valid DCO"
|
|
|
|
}
|
|
|
|
|
|
|
|
return vr
|
|
|
|
}
|