From dee296143f4a23dd41d2d5363feab474f6288d9c Mon Sep 17 00:00:00 2001 From: Boming Zhang Date: Fri, 4 Oct 2024 02:06:32 -0400 Subject: [PATCH] feat: non-ascii not in the first line of commit msg --- pkg/healthcheck/commit.go | 73 +++++++++++++++--------------- pkg/healthcheck/commit_test.go | 83 ++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+), 37 deletions(-) create mode 100644 pkg/healthcheck/commit_test.go diff --git a/pkg/healthcheck/commit.go b/pkg/healthcheck/commit.go index 375046e..4bfecac 100644 --- a/pkg/healthcheck/commit.go +++ b/pkg/healthcheck/commit.go @@ -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 diff --git a/pkg/healthcheck/commit_test.go b/pkg/healthcheck/commit_test.go new file mode 100644 index 0000000..a987a78 --- /dev/null +++ b/pkg/healthcheck/commit_test.go @@ -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 \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 \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 ", + 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 \nReviewed-by: Jane Smith \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 \nReviewed-by: Jöhn Döe ", + 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) + } + }) + } +}