Skip to content

Commit

Permalink
jira/comment: Improve regex matching (#211)
Browse files Browse the repository at this point in the history
Update regular expression to match with:
- comments created by bots (`GitHubUser.GetName()` can be empty, making the regex not match with the comment)
- comments with newlines

---------

Signed-off-by: Laszlo Gecse <lgecse@cisco.com>
Co-authored-by: Stephen Augustus <justaugustus@users.noreply.github.com>
  • Loading branch information
lgecse and justaugustus committed Nov 18, 2023
1 parent bcde120 commit 8e2dc44
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/jira/comment/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
// GitHub Comment ID (\1), the GitHub username (\2), the GitHub real name (\3, if it exists),
// the time the comment was posted (\3 or \4), and the body of the comment (\4 or \5).
var jCommentRegex = regexp.MustCompile(
`^Comment \[\(ID (\d+)\)\|.*?] from GitHub user \[(.+)\|.*?] \((.+)\) at (.+):\n\n(.+)$`,
`^Comment \[\(ID (\d+)\)\|.*?] from GitHub user \[(.+)\|.*?] (?:\(?(.+)\) |.*)at (.+):\n\n([\S\s]*)*?$`,
)

// jCommentIDRegex just matches the beginning of a generated Jira comment. It's a smaller,
Expand Down
67 changes: 67 additions & 0 deletions internal/jira/comment/comment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ const testComment = `Comment [(ID 484163403)|https://github.com] from GitHub use
Bla blibidy bloo bla`

//nolint:lll
const testCommentNewLine = `Comment [(ID 484163403)|https://github.com] from GitHub user [bilbo-baggins|https://github.com/bilbo-baggins] (Bilbo Baggins) at 16:27 PM, May 31 2020:
Bla blibidy bloo bla
bla bla`

//nolint:lll
const testCommentUnnamed = `Comment [(ID 123456789)|https://github.com] from GitHub user [smaug-bot|https://github.com/smaug-bot] at 16:27 PM, Jan 22 2021:
rawr`

func TestJiraCommentRegex(t *testing.T) {
fields := jCommentRegex.FindStringSubmatch(testComment)

Expand Down Expand Up @@ -50,3 +61,59 @@ func TestJiraCommentRegex(t *testing.T) {
t.Fatalf("Expected field[5] = Bla blibidy bloo bla; Got field[5] = %s", fields[5])
}
}

func TestJiraCommentRegexNewLine(t *testing.T) {
fields := jCommentRegex.FindStringSubmatch(testCommentNewLine)

if len(fields) != 6 {
t.Fatalf("Regex failed to parse fields %v", fields)
}

if fields[1] != "484163403" {
t.Fatalf("Expected field[1] = 484163403; Got field[1] = %s", fields[1])
}

if fields[2] != "bilbo-baggins" {
t.Fatalf("Expected field[2] = bilbo-baggins; Got field[2] = %s", fields[2])
}

if fields[3] != "Bilbo Baggins" {
t.Fatalf("Expected field[3] = Bilbo Baggins; Got field[3] = %s", fields[3])
}

if fields[4] != "16:27 PM, May 31 2020" {
t.Fatalf("Expected field[4] = 16:27 PM, May 31 2020; Got field[4] = %s", fields[4])
}

if fields[5] != "Bla blibidy bloo bla\nbla bla" {
t.Fatalf("Expected field[5] = Bla blibidy bloo bla\nbla bla; Got field[5] = %s", fields[5])
}
}

func TestJiraCommentRegexUnnamed(t *testing.T) {
fields := jCommentRegex.FindStringSubmatch(testCommentUnnamed)

if len(fields) != 6 {
t.Fatalf("Regex failed to parse fields %v", fields)
}

if fields[1] != "123456789" {
t.Fatalf("Expected field[1] = 123456789; Got field[1] = %s", fields[1])
}

if fields[2] != "smaug-bot" {
t.Fatalf("Expected field[2] = smaug-bot; Got field[2] = %s", fields[2])
}

if fields[3] != "" {
t.Fatalf("Expected field[3] = ; Got field[3] = %s", fields[3])
}

if fields[4] != "16:27 PM, Jan 22 2021" {
t.Fatalf("Expected field[4] = 16:27 PM, Jan 22 2021; Got field[4] = %s", fields[4])
}

if fields[5] != "rawr" {
t.Fatalf("Expected field[5] = rawr; Got field[5] = %s", fields[5])
}
}

0 comments on commit 8e2dc44

Please sign in to comment.