Skip to content

Commit

Permalink
Added support for generating the regular expression based on JIRA pro…
Browse files Browse the repository at this point in the history
…jects
  • Loading branch information
andygrunwald committed Dec 1, 2017
1 parent 148c6a7 commit 61cb4bc
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 18 deletions.
42 changes: 34 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,27 @@ func main() {
logger.Fatal("No JIRA Instance provided. Please set the URL of the JIRA instance by -url parameter.")
}

jiraClient, err := getJIRAClient(*jiraURL, *jiraUsername, *jiraPassword)
if err != nil {
logger.Fatal(err)
}

projects, err := getProjectsFromJIRA(jiraClient)
if err != nil {
logger.Fatal(err)
}
if len(projects) == 0 {
logger.Fatal("0 projects retrieved from JIRA. Without projects, we are not able to operate.")
}

issueText := getTextToAnalyze(*issueMessage, *inputStdin)
issues := getIssuesOutOfMessage(issueText)
issues := getIssuesOutOfMessage(projects, issueText)

// If we don`t get any issues, exit
if len(issues) == 0 {
logger.Fatalf("No JIRA-Issue(s) found in text '%s'.", issueText)
}

jiraClient, err := getJIRAClient(*jiraURL, *jiraUsername, *jiraPassword)
if err != nil {
logger.Fatal(err)
}

// Loop over all issues and check if they are correct / valid
for _, issueFromUser := range issues {
err := checkIfIssue(issueFromUser, jiraClient)
Expand All @@ -84,9 +92,13 @@ func main() {
//
// @link https://confluence.atlassian.com/display/STASHKB/Integrating+with+custom+JIRA+issue+key
// @link https://answers.atlassian.com/questions/325865/regex-pattern-to-match-jira-issue-key
func getIssuesOutOfMessage(message string) []string {
func getIssuesOutOfMessage(projects []string, message string) []string {
var issues []string
re := regexp.MustCompile("(?i)([A-Z]+)-(\\d+)")

projectList := strings.Join(projects, "|")
expression := fmt.Sprintf("(?i)(%s)-(\\d+)", projectList)

re := regexp.MustCompile(expression)

parts := re.FindAllStringSubmatch(message, -1)
for _, v := range parts {
Expand Down Expand Up @@ -163,3 +175,17 @@ func getTextToAnalyze(argText string, inputStdin bool) string {

return text
}

func getProjectsFromJIRA(jiraClient *jira.Client) ([]string, error) {
list, _, err := jiraClient.Project.GetList()
if err != nil {
return []string{}, err
}

projects := []string{}
for _, p := range *list {
projects = append(projects, p.Key)
}

return projects, nil
}
21 changes: 11 additions & 10 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,21 @@ func TestGetJIRAClient_IssueNotExists(t *testing.T) {

func TestGetIssuesOutOfMessage(t *testing.T) {
dataProvider := []struct {
Message string
Result []string
Message string
Projects []string
Result []string
}{
{"WEB-22861 remove authentication prod build for now", []string{"WEB-22861"}},
{"[WEB-22861] remove authentication prod build for now", []string{"WEB-22861"}},
{"WEB-4711 SYS-1234 PRD-5678 remove authentication prod build for now", []string{"WEB-4711", "SYS-1234", "PRD-5678"}},
{"[SCC-27] Replace deprecated autoloader strategy PSR-0 with PSR-4", []string{"SCC-27", "PSR-4"}},
{"WeB-4711 sys-1234 PRD-5678 remove authentication prod build for now", []string{"WeB-4711", "sys-1234", "PRD-5678"}},
{"TASKLESS: Removes duplicated comment code.", nil},
{"This is a commit message and we applied the PHP standard PSR-0 to the codebase", nil},
{"WEB-22861 remove authentication prod build for now", []string{"WEB"}, []string{"WEB-22861"}},
{"[WEB-22861] remove authentication prod build for now", []string{"WEB"}, []string{"WEB-22861"}},
{"WEB-4711 SYS-1234 PRD-5678 remove authentication prod build for now", []string{"WEB", "SYS", "PRD"}, []string{"WEB-4711", "SYS-1234", "PRD-5678"}},
{"[SCC-27] Replace deprecated autoloader strategy PSR-0 with PSR-4", []string{"SCC"}, []string{"SCC-27"}},
{"WeB-4711 sys-1234 PRD-5678 remove authentication prod build for now", []string{"WEB", "SYS", "PRD"}, []string{"WeB-4711", "sys-1234", "PRD-5678"}},
{"TASKLESS: Removes duplicated comment code.", []string{"WEB"}, nil},
{"This is a commit message and we applied the PHP standard PSR-0 to the codebase", []string{"WEB"}, nil},
}

for _, data := range dataProvider {
res := getIssuesOutOfMessage(data.Message)
res := getIssuesOutOfMessage(data.Projects, data.Message)
if reflect.DeepEqual(data.Result, res) == false {
t.Errorf("Test failed, expected: '%+v' (%d), got: '%+v' (%d)", data.Result, len(data.Result), res, len(res))
}
Expand Down

0 comments on commit 61cb4bc

Please sign in to comment.