github.com/autonomy/conform@v0.1.0-alpha.16/internal/policy/commit/check_body.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  	"strings"
     9  
    10  	"github.com/autonomy/conform/internal/policy"
    11  	"github.com/pkg/errors"
    12  )
    13  
    14  // RequiredBodyThreshold is the default minimum number of line changes required
    15  // to trigger the body check.
    16  var RequiredBodyThreshold = 10
    17  
    18  // Body enforces a maximum number of charcters on the commit
    19  // header.
    20  type Body struct {
    21  	errors []error
    22  }
    23  
    24  // Name returns the name of the check.
    25  func (h Body) Name() string {
    26  	return "Commit Body"
    27  }
    28  
    29  // Message returns to check message.
    30  func (h Body) Message() string {
    31  	if len(h.errors) != 0 {
    32  		return h.errors[0].Error()
    33  	}
    34  	return "Commit body is valid"
    35  }
    36  
    37  // Errors returns any violations of the check.
    38  func (h Body) Errors() []error {
    39  	return h.errors
    40  }
    41  
    42  // ValidateBody checks the header length.
    43  func (c Commit) ValidateBody() policy.Check {
    44  	check := &Body{}
    45  
    46  	if c.HeaderLength != 0 {
    47  		MaxNumberOfCommitCharacters = c.HeaderLength
    48  	}
    49  
    50  	lines := strings.Split(strings.TrimPrefix(c.msg, "\n"), "\n")
    51  	valid := false
    52  	for _, line := range lines[1:] {
    53  		if DCORegex.MatchString(strings.TrimSpace(line)) {
    54  			continue
    55  		}
    56  		if line != "" {
    57  			valid = true
    58  			break
    59  		}
    60  	}
    61  
    62  	if !valid {
    63  		check.errors = append(check.errors, errors.New("Commit body is empty"))
    64  	}
    65  
    66  	return check
    67  }