aboutsummaryrefslogtreecommitdiff
path: root/test/tools/vendor/github.com/vbatts/git-validation/rules/dco/dco.go
blob: a42ea06acdd210359b3b16620457ec5330f4f264 (plain)
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
package dco

import (
	"regexp"
	"strings"

	"github.com/vbatts/git-validation/git"
	"github.com/vbatts/git-validation/validate"
)

func init() {
	validate.RegisterRule(DcoRule)
}

var (
	// ValidDCO is the regexp for signed off DCO
	ValidDCO = regexp.MustCompile(`^Signed-off-by: ([^<]+) <([^<>@]+@[^<>]+)>$`)
	// DcoRule is the rule being registered
	DcoRule = validate.Rule{
		Name:        "DCO",
		Description: "makes sure the commits are signed",
		Run:         ValidateDCO,
		Default:     true,
	}
)

// ValidateDCO checks that the commit has been signed off, per the DCO process
func ValidateDCO(r validate.Rule, c git.CommitEntry) (vr validate.Result) {
	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
}