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