Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(git): incorrect ticket number with different formats #47

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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...)
Expand Down
26 changes: 24 additions & 2 deletions git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
Loading