Skip to content

Commit a4d67a3

Browse files
authored
fix(git): incorrect ticket number with different formats (#47)
1 parent caf67fd commit a4d67a3

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

git.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ func findGitDir() (string, error) {
3030
return strings.TrimSpace(string(out)), nil
3131
}
3232

33-
func matchTicketNumber(board string, msg string) bool {
34-
match, _ := regexp.MatchString(fmt.Sprintf(`(?i)^%s-\d{1,}`, board), msg)
33+
func checkBoardMatchesBranch(board string, msg string) bool {
34+
match, _ := regexp.MatchString(fmt.Sprintf(`(?i)%s-\d{1,}`, board), msg)
3535
return match
3636
}
3737

@@ -42,23 +42,26 @@ func getGitTicketNumber(board string) string {
4242
if err != nil {
4343
return ""
4444
}
45-
match := matchTicketNumber(board, string(out))
45+
match := checkBoardMatchesBranch(board, string(out))
4646
if !match {
4747
cmd = exec.Command("git", "log", "-1", "--grep", board, "--oneline", "--format=%s")
4848
out, err = cmd.Output()
4949
if err != nil {
5050
return ""
5151
}
52-
re := regexp.MustCompile(`(.*):.*`)
53-
ticket := re.ReplaceAllString(string(out), "$1")
54-
return strings.TrimSpace(ticket)
5552
}
5653

57-
re := regexp.MustCompile(fmt.Sprintf(`(?i)((%s-)\d{1,})|(.*)`, board))
58-
ticket := re.ReplaceAllString(string(out), "$1")
54+
ticket := getTicketNumberFromString(string(out), board)
5955
return strings.TrimSpace(ticket)
6056
}
6157

58+
func getTicketNumberFromString(str string, sub string) string {
59+
expr := fmt.Sprintf(`(?i).*((%s-)\d{1,})|(.*)`, sub)
60+
61+
re := regexp.MustCompile(expr)
62+
return re.ReplaceAllString(str, "$1")
63+
}
64+
6265
// buildCommitCommand builds the git commit command
6366
func buildCommitCommand(msg string, body string, osArgs []string) ([]string, string) {
6467
args := append([]string{"commit", "-m", msg}, osArgs...)

git_test.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func TestBuildCommitCommand(t *testing.T) {
1212
}
1313
}
1414

15-
func TestMatchTicketNumber(t *testing.T) {
15+
func TestCheckBoardMatchesBranch(t *testing.T) {
1616
cases := []struct {
1717
Desc string
1818
msg string
@@ -24,16 +24,38 @@ func TestMatchTicketNumber(t *testing.T) {
2424
{"it should match with 4 digits", "TICKET-1234", true},
2525
{"it should match with 5 digits", "TICKET-12345", true},
2626
{"it should match with 6 digits", "TICKET-123456", true},
27+
{"it should match different case", "ticket-1234", true},
28+
{"it should match different format", "fix-for-TICKET-1234", true},
29+
{"it should not match with no digits", "TICKET-", false},
2730
}
2831

2932
for _, tc := range cases {
3033
t.Run(tc.Desc, func(t *testing.T) {
31-
got := matchTicketNumber("TICKET", tc.msg)
34+
got := checkBoardMatchesBranch("TICKET", tc.msg)
3235
assertEqualBools(t, tc.want, got)
3336
})
3437
}
3538
}
3639

40+
func TestGetTicketNumberFromString(t *testing.T) {
41+
cases := []struct {
42+
Desc string
43+
msg string
44+
sub string
45+
want string
46+
}{
47+
{"it should return the ticket number", "TICKET-1234", "TICKET", "TICKET-1234"},
48+
{"it should return when ticket is not at the beginning", "this is a TICKET-1234", "TICKET", "TICKET-1234"},
49+
}
50+
51+
for _, tc := range cases {
52+
t.Run(tc.Desc, func(t *testing.T) {
53+
got := getTicketNumberFromString(tc.msg, tc.sub)
54+
assertEqualStrings(t, tc.want, got)
55+
})
56+
}
57+
}
58+
3759
func assertEqualStrings(t testing.TB, expected, got string) {
3860
t.Helper()
3961
if got != expected {

0 commit comments

Comments
 (0)