diff --git a/main.go b/main.go index 77a36b4..da522a3 100644 --- a/main.go +++ b/main.go @@ -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) @@ -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 { @@ -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 +} diff --git a/main_test.go b/main_test.go index 07be501..6c790ca 100644 --- a/main_test.go +++ b/main_test.go @@ -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)) }