github.com/twelho/conform@v0.0.0-20231016230407-c25e9238598a/internal/policy/commit/check_dco.go (about)

     1  // This Source Code Form is subject to the terms of the Mozilla Public
     2  // License, v. 2.0. If a copy of the MPL was not distributed with this
     3  // file, You can obtain one at http://mozilla.org/MPL/2.0/.
     4  
     5  package commit
     6  
     7  import (
     8  	"regexp"
     9  	"strings"
    10  
    11  	"github.com/pkg/errors"
    12  
    13  	"github.com/twelho/conform/internal/policy"
    14  )
    15  
    16  // DCORegex is the regular expression used for Developer Certificate of Origin.
    17  var DCORegex = regexp.MustCompile(`^Signed-off-by: ([^<]+) <([^<>@]+@[^<>]+)>$`)
    18  
    19  // DCOCheck ensures that the commit message contains a
    20  // Developer Certificate of Origin.
    21  type DCOCheck struct {
    22  	errors []error
    23  }
    24  
    25  // Name returns the name of the check.
    26  func (d DCOCheck) Name() string {
    27  	return "DCO"
    28  }
    29  
    30  // Message returns to check message.
    31  func (d DCOCheck) Message() string {
    32  	if len(d.errors) != 0 {
    33  		return d.errors[0].Error()
    34  	}
    35  
    36  	return "Developer Certificate of Origin was found"
    37  }
    38  
    39  // Errors returns any violations of the check.
    40  func (d DCOCheck) Errors() []error {
    41  	return d.errors
    42  }
    43  
    44  // ValidateDCO checks the commit message for a Developer Certificate of Origin.
    45  func (c Commit) ValidateDCO() policy.Check { //nolint:ireturn
    46  	check := &DCOCheck{}
    47  
    48  	for _, line := range strings.Split(c.msg, "\n") {
    49  		if DCORegex.MatchString(strings.TrimSpace(line)) {
    50  			return check
    51  		}
    52  	}
    53  
    54  	check.errors = append(check.errors, errors.Errorf("Commit does not have a DCO"))
    55  
    56  	return check
    57  }