feat: non-ascii not in the first line of commit msg
This commit is contained in:
parent
5c2fe4ffcb
commit
dee296143f
|
@ -9,6 +9,41 @@ import (
|
|||
"github.com/go-git/go-git/v5"
|
||||
)
|
||||
|
||||
func checkMsg(msg string) bool {
|
||||
// List of prefixes to ignore in the commit message
|
||||
ignoredPrefixes := []string{
|
||||
"Co-authored-by:",
|
||||
"Reviewed-by:",
|
||||
"Co-committed-by:",
|
||||
"Reviewed-on:",
|
||||
}
|
||||
|
||||
// Split message by lines and ignore specific lines with prefixes
|
||||
lines := strings.Split(msg, "\n")
|
||||
for i, line := range lines {
|
||||
trimmedLine := strings.TrimSpace(line)
|
||||
ignore := false
|
||||
if i != 0 {
|
||||
for _, prefix := range ignoredPrefixes {
|
||||
if strings.HasPrefix(trimmedLine, prefix) {
|
||||
ignore = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if ignore {
|
||||
continue
|
||||
}
|
||||
// Check for non-ASCII characters in the rest of the lines
|
||||
for _, c := range line {
|
||||
if c > unicode.MaxASCII {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// nonAsciiMsg checks for non-ASCII characters in the commit message.
|
||||
// If the message starts with "Merge pull request", it skips the non-ASCII characters check.
|
||||
// Otherwise, it iterates over each character in the message and checks if it is a non-ASCII character.
|
||||
|
@ -35,43 +70,7 @@ func NonAsciiMsg(root string) error {
|
|||
}
|
||||
|
||||
msg := commit.Message
|
||||
if msg == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
var isCommitLegal bool = true
|
||||
// List of prefixes to ignore in the commit message
|
||||
ignoredPrefixes := []string{
|
||||
"Co-authored-by:",
|
||||
"Reviewed-by:",
|
||||
"Co-committed-by:",
|
||||
"Reviewed-on:",
|
||||
}
|
||||
|
||||
// Split message by lines and ignore specific lines with prefixes
|
||||
lines := strings.Split(msg, "\n")
|
||||
for _, line := range lines {
|
||||
trimmedLine := strings.TrimSpace(line)
|
||||
ignore := false
|
||||
for _, prefix := range ignoredPrefixes {
|
||||
if strings.HasPrefix(trimmedLine, prefix) {
|
||||
ignore = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if ignore {
|
||||
continue
|
||||
}
|
||||
// Check for non-ASCII characters in the rest of the lines
|
||||
for _, c := range line {
|
||||
if c > unicode.MaxASCII {
|
||||
isCommitLegal = false
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !isCommitLegal {
|
||||
if !checkMsg(msg) {
|
||||
return fmt.Errorf("Non-ASCII characters in commit messages:\n%s", msg)
|
||||
}
|
||||
return nil
|
||||
|
|
83
pkg/healthcheck/commit_test.go
Normal file
83
pkg/healthcheck/commit_test.go
Normal file
|
@ -0,0 +1,83 @@
|
|||
package healthcheck
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCheckMsg(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
message string
|
||||
expected bool
|
||||
}{
|
||||
{
|
||||
name: "Valid ASCII message",
|
||||
message: "This is a valid commit message",
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "Message with non-ASCII character",
|
||||
message: "This message contains a non-ASCII character: é",
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "Message with ignored prefix",
|
||||
message: "First line\nCo-authored-by: John Doe <john@example.com>\nThis is a valid message",
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "Message with ignored prefix and non-ASCII character in content",
|
||||
message: "First line\nCo-authored-by: John Doe <john@example.com>\nThis message has a non-ASCII character: ñ",
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "Message with ignored prefix in the first line",
|
||||
message: "Co-authored-by: Jöhn Döe <john@example.com>",
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "Multi-line message with all valid ASCII",
|
||||
message: "First line\nSecond line\nThird line",
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "Multi-line message with non-ASCII in middle",
|
||||
message: "First line\nSecond line with ö\nThird line",
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "Multi-line message with non-ASCII in the first line",
|
||||
message: "First line with ö\nSecond line",
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "Message with multiple ignored prefixes",
|
||||
message: "First line\nCo-authored-by: John Doe <john@example.com>\nReviewed-by: Jane Smith <jane@example.com>\nValid content",
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "Empty message",
|
||||
message: "",
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "Message with only whitespace",
|
||||
message: " \n \t ",
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "Message with non-ASCII after ignored prefix",
|
||||
message: "First line\nCo-authored-by: John Doe <john@example.com>\nReviewed-by: Jöhn Döe <john@example.com>",
|
||||
expected: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := checkMsg(tt.message)
|
||||
if result != tt.expected {
|
||||
t.Errorf("checkMsg(%q) = %v, want %v", tt.message, result, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user