-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(policy): add checks for header case and last character
It's common for commit message guidelines to include restrictions on the leading case and trailing punctuation of the header; this adds checks to enforce those. The desired case can be specified as either "upper" or "lower"; the punctuation check is implemented as a general check for disallowing the last character in the header to be from any given set of characters. Now that there are several header-related checks, this also adds a level to the config to put all of those checks in one place in the configuration. Signed-off-by: Danny Zhu <[email protected]>
- Loading branch information
1 parent
a55d411
commit fa7df19
Showing
7 changed files
with
180 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package commit | ||
|
||
import ( | ||
"unicode" | ||
"unicode/utf8" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/talos-systems/conform/internal/policy" | ||
) | ||
|
||
// HeaderCaseCheck enforces the case of the first word in the header. | ||
type HeaderCaseCheck struct { | ||
headerCase string | ||
errors []error | ||
} | ||
|
||
// Name returns the name of the check. | ||
func (h HeaderCaseCheck) Name() string { | ||
return "Header Case" | ||
} | ||
|
||
// Message returns to check message. | ||
func (h HeaderCaseCheck) Message() string { | ||
if len(h.errors) != 0 { | ||
return h.errors[0].Error() | ||
} | ||
return "Header case is valid" | ||
} | ||
|
||
// Errors returns any violations of the check. | ||
func (h HeaderCaseCheck) Errors() []error { | ||
return h.errors | ||
} | ||
|
||
// ValidateHeaderCase checks the header length. | ||
func (c Commit) ValidateHeaderCase() policy.Check { | ||
check := &HeaderCaseCheck{headerCase: c.Header.Case} | ||
|
||
firstWord, err := c.firstWord() | ||
if err != nil { | ||
check.errors = append(check.errors, err) | ||
return check | ||
} | ||
|
||
first, _ := utf8.DecodeRuneInString(firstWord) | ||
if first == utf8.RuneError { | ||
check.errors = append(check.errors, errors.New("Header does not start with valid UTF-8 text")) | ||
return check | ||
} | ||
|
||
var valid bool | ||
switch c.Header.Case { | ||
case "upper": | ||
valid = unicode.IsUpper(first) | ||
case "lower": | ||
valid = unicode.IsLower(first) | ||
default: | ||
check.errors = append(check.errors, errors.Errorf("Invalid configured case %s", c.Header.Case)) | ||
return check | ||
} | ||
if !valid { | ||
check.errors = append(check.errors, errors.Errorf("Commit header case is not %s", c.Header.Case)) | ||
} | ||
return check | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package commit | ||
|
||
import ( | ||
"strings" | ||
"unicode/utf8" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/talos-systems/conform/internal/policy" | ||
) | ||
|
||
// HeaderLastCharacterCheck enforces that the last character of the header isn't in some set. | ||
type HeaderLastCharacterCheck struct { | ||
errors []error | ||
} | ||
|
||
// Name returns the name of the check. | ||
func (h HeaderLastCharacterCheck) Name() string { | ||
return "Header Last Character" | ||
} | ||
|
||
// Message returns to check message. | ||
func (h HeaderLastCharacterCheck) Message() string { | ||
if len(h.errors) != 0 { | ||
return h.errors[0].Error() | ||
} | ||
return "Header last character is valid" | ||
} | ||
|
||
// Errors returns any violations of the check. | ||
func (h HeaderLastCharacterCheck) Errors() []error { | ||
return h.errors | ||
} | ||
|
||
// ValidateHeaderLastCharacter checks the last character of the header. | ||
func (c Commit) ValidateHeaderLastCharacter() policy.Check { | ||
check := &HeaderLastCharacterCheck{} | ||
|
||
switch last, _ := utf8.DecodeLastRuneInString(c.header()); { | ||
case last == utf8.RuneError: | ||
check.errors = append(check.errors, errors.New("Header does not end with valid UTF-8 text")) | ||
case strings.ContainsRune(c.Header.InvalidLastCharacters, last): | ||
check.errors = append(check.errors, errors.Errorf("Commit header ends in %q", last)) | ||
} | ||
|
||
return check | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters