github.com/autonomy/conform@v0.1.0-alpha.16/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/autonomy/conform/internal/policy"
    12  	"github.com/pkg/errors"
    13  )
    14  
    15  // DCORegex is the regular expression used for Developer Certificate of Origin.
    16  var DCORegex = regexp.MustCompile(`^Signed-off-by: ([^<]+) <([^<>@]+@[^<>]+)>$`)
    17  
    18  // DCOCheck ensures that the commit message contains a
    19  // Developer Certificate of Origin.
    20  type DCOCheck struct {
    21  	errors []error
    22  }
    23  
    24  // Name returns the name of the check.
    25  func (d DCOCheck) Name() string {
    26  	return "DCO"
    27  }
    28  
    29  // Message returns to check message.
    30  func (d DCOCheck) Message() string {
    31  	if len(d.errors) != 0 {
    32  		return d.errors[0].Error()
    33  	}
    34  	return "Developer Certificate of Origin was found"
    35  }
    36  
    37  // Errors returns any violations of the check.
    38  func (d DCOCheck) Errors() []error {
    39  	return d.errors
    40  }
    41  
    42  // ValidateDCO checks the commit message for a Developer Certificate of Origin.
    43  func (c Commit) ValidateDCO() policy.Check {
    44  	check := &DCOCheck{}
    45  	for _, line := range strings.Split(c.msg, "\n") {
    46  		if DCORegex.MatchString(strings.TrimSpace(line)) {
    47  			return check
    48  		}
    49  	}
    50  
    51  	check.errors = append(check.errors, errors.Errorf("Commit does not have a DCO"))
    52  
    53  	return check
    54  }