Skip to content

Commit

Permalink
feat: add package for finding JIRA issue references
Browse files Browse the repository at this point in the history
  • Loading branch information
fallion committed Jun 17, 2020
1 parent 5f59188 commit 440541b
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
28 changes: 28 additions & 0 deletions pkg/jira/build_regex.go
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()
}
18 changes: 18 additions & 0 deletions pkg/jira/build_regex_test.go
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))
}
}
18 changes: 18 additions & 0 deletions pkg/jira/find_references.go
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
}
53 changes: 53 additions & 0 deletions pkg/jira/find_references_test.go
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)
}
}

1 comment on commit 440541b

@vercel
Copy link

@vercel vercel bot commented on 440541b Jun 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.