generated from fallion/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add package for finding JIRA issue references
- Loading branch information
Showing
4 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package jira | ||
|
||
import "strings" | ||
|
||
func buildRegex(keys []string) string { | ||
// This is the default regex for JIRA tickets. Please see https://community.atlassian.com/t5/Bitbucket-questions/Regex-pattern-to-match-JIRA-issue-key/qaq-p/233319 for reference | ||
if len(keys) == 0 { | ||
return `([A-Z]+-[\d]+)` | ||
} | ||
|
||
result := strings.Builder{} | ||
|
||
result.WriteString("(") | ||
|
||
for index, key := range keys { | ||
result.WriteString(key) | ||
|
||
if (index + 1) != len(keys) { | ||
result.WriteString("|") | ||
} | ||
} | ||
|
||
result.WriteString(")") | ||
|
||
result.WriteString("-[0-9]+") | ||
|
||
return result.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package jira | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestBuildRegex(t *testing.T) { | ||
tests := map[string][]string{ | ||
`([A-Z]+-[\d]+)`: {}, | ||
`(TEST|PROJ|FEOP)-[0-9]+`: {"TEST", "PROJ", "FEOP"}, | ||
} | ||
|
||
for expected, test := range tests { | ||
assert.Equal(t, expected, buildRegex(test)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package jira | ||
|
||
import ( | ||
"regexp" | ||
) | ||
|
||
// FindReferences scans a given message looking for all issues that match the keys. Default to the JIRA default regex if no keys are provided. | ||
func FindReferences(keys []string, message string) ([]string, error) { | ||
regex, err := regexp.Compile(buildRegex(keys)) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
matches := regex.FindAllString(message, -1) | ||
|
||
return matches, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package jira | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestFindReferences(t *testing.T) { | ||
tests := []struct { | ||
expected []string | ||
keys []string | ||
message string | ||
}{ | ||
{ | ||
expected: []string{"TES-1"}, | ||
keys: nil, | ||
message: "TES-1: added a tes feature", | ||
}, | ||
{ | ||
expected: []string{"TES-1"}, | ||
keys: []string{"TES"}, | ||
message: "TES-1: added a tes feature", | ||
}, | ||
{ | ||
expected: nil, | ||
keys: []string{"TES"}, | ||
message: "REST-1: added a tes feature", | ||
}, | ||
{ | ||
expected: []string{"REST-1", "TEST-2"}, | ||
keys: []string{"TEST", "REST"}, | ||
message: "REST-1: added a tes feature TEST-2", | ||
}, | ||
{ | ||
expected: []string{"TEST-2"}, | ||
keys: []string{"TEST"}, | ||
message: "REST-1: added a tes feature TEST-2", | ||
}, | ||
{ | ||
expected: []string{"REST-1", "TEST-2"}, | ||
keys: nil, | ||
message: "REST-1: added a tes feature TEST-2", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
references, err := FindReferences(test.keys, test.message) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, test.expected, references) | ||
} | ||
} |
440541b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to following URLs: