github.com/twelho/conform@v0.0.0-20231016230407-c25e9238598a/internal/policy/commit/check_header_length.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  	"fmt"
     9  
    10  	"github.com/pkg/errors"
    11  
    12  	"github.com/twelho/conform/internal/policy"
    13  )
    14  
    15  // MaxNumberOfCommitCharacters is the default maximium number of characters
    16  // allowed in a commit header.
    17  var MaxNumberOfCommitCharacters = 89
    18  
    19  // HeaderLengthCheck enforces a maximum number of charcters on the commit
    20  // header.
    21  //
    22  //nolint:govet
    23  type HeaderLengthCheck struct {
    24  	headerLength int
    25  	errors       []error
    26  }
    27  
    28  // Name returns the name of the check.
    29  func (h HeaderLengthCheck) Name() string {
    30  	return "Header Length"
    31  }
    32  
    33  // Message returns to check message.
    34  func (h HeaderLengthCheck) Message() string {
    35  	return fmt.Sprintf("Header is %d characters", h.headerLength)
    36  }
    37  
    38  // Errors returns any violations of the check.
    39  func (h HeaderLengthCheck) Errors() []error {
    40  	return h.errors
    41  }
    42  
    43  // ValidateHeaderLength checks the header length.
    44  func (c Commit) ValidateHeaderLength() policy.Check { //nolint:ireturn
    45  	check := &HeaderLengthCheck{}
    46  
    47  	if c.Header.Length != 0 {
    48  		MaxNumberOfCommitCharacters = c.Header.Length
    49  	}
    50  
    51  	check.headerLength = len(c.header())
    52  	if check.headerLength > MaxNumberOfCommitCharacters {
    53  		check.errors = append(check.errors, errors.Errorf("Commit header is %d characters", check.headerLength))
    54  	}
    55  
    56  	return check
    57  }