From 46e93e14924df3adb6ae9354f447ca06ade6c65a Mon Sep 17 00:00:00 2001 From: Stefan Logue Date: Thu, 12 Dec 2024 20:01:06 +0000 Subject: [PATCH] fix(git): incorrect ticket number with different formats --- git.go | 19 +++++++++++-------- git_test.go | 26 ++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/git.go b/git.go index 27d67f3..650c622 100644 --- a/git.go +++ b/git.go @@ -30,8 +30,8 @@ func findGitDir() (string, error) { return strings.TrimSpace(string(out)), nil } -func matchTicketNumber(board string, msg string) bool { - match, _ := regexp.MatchString(fmt.Sprintf(`(?i)^%s-\d{1,}`, board), msg) +func checkBoardMatchesBranch(board string, msg string) bool { + match, _ := regexp.MatchString(fmt.Sprintf(`(?i)%s-\d{1,}`, board), msg) return match } @@ -42,23 +42,26 @@ func getGitTicketNumber(board string) string { if err != nil { return "" } - match := matchTicketNumber(board, string(out)) + match := checkBoardMatchesBranch(board, string(out)) if !match { cmd = exec.Command("git", "log", "-1", "--grep", board, "--oneline", "--format=%s") out, err = cmd.Output() if err != nil { return "" } - re := regexp.MustCompile(`(.*):.*`) - ticket := re.ReplaceAllString(string(out), "$1") - return strings.TrimSpace(ticket) } - re := regexp.MustCompile(fmt.Sprintf(`(?i)((%s-)\d{1,})|(.*)`, board)) - ticket := re.ReplaceAllString(string(out), "$1") + ticket := getTicketNumberFromString(string(out), board) return strings.TrimSpace(ticket) } +func getTicketNumberFromString(str string, sub string) string { + expr := fmt.Sprintf(`(?i).*((%s-)\d{1,})|(.*)`, sub) + + re := regexp.MustCompile(expr) + return re.ReplaceAllString(str, "$1") +} + // buildCommitCommand builds the git commit command func buildCommitCommand(msg string, body string, osArgs []string) ([]string, string) { args := append([]string{"commit", "-m", msg}, osArgs...) diff --git a/git_test.go b/git_test.go index 15beba1..876adbb 100644 --- a/git_test.go +++ b/git_test.go @@ -12,7 +12,7 @@ func TestBuildCommitCommand(t *testing.T) { } } -func TestMatchTicketNumber(t *testing.T) { +func TestCheckBoardMatchesBranch(t *testing.T) { cases := []struct { Desc string msg string @@ -24,16 +24,38 @@ func TestMatchTicketNumber(t *testing.T) { {"it should match with 4 digits", "TICKET-1234", true}, {"it should match with 5 digits", "TICKET-12345", true}, {"it should match with 6 digits", "TICKET-123456", true}, + {"it should match different case", "ticket-1234", true}, + {"it should match different format", "fix-for-TICKET-1234", true}, + {"it should not match with no digits", "TICKET-", false}, } for _, tc := range cases { t.Run(tc.Desc, func(t *testing.T) { - got := matchTicketNumber("TICKET", tc.msg) + got := checkBoardMatchesBranch("TICKET", tc.msg) assertEqualBools(t, tc.want, got) }) } } +func TestGetTicketNumberFromString(t *testing.T) { + cases := []struct { + Desc string + msg string + sub string + want string + }{ + {"it should return the ticket number", "TICKET-1234", "TICKET", "TICKET-1234"}, + {"it should return when ticket is not at the beginning", "this is a TICKET-1234", "TICKET", "TICKET-1234"}, + } + + for _, tc := range cases { + t.Run(tc.Desc, func(t *testing.T) { + got := getTicketNumberFromString(tc.msg, tc.sub) + assertEqualStrings(t, tc.want, got) + }) + } +} + func assertEqualStrings(t testing.TB, expected, got string) { t.Helper() if got != expected {