From fcef9b12046970698f46d718e97bc380047039ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Lutz=20Bru=CC=88ggen?= Date: Mon, 15 Oct 2018 11:50:22 +0200 Subject: [PATCH] Add -one flag to require for only one issue to exist When the flag is set we succeed as soon as we find an issue, instead of failing as soon as we don't. If the loop ran through without terminating we fail because we apparently didn't find any issue. --- main.go | 27 +++++++++++++++++++++------ main_test.go | 2 +- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index 7012af6..502c44a 100644 --- a/main.go +++ b/main.go @@ -25,6 +25,7 @@ func main() { jiraPassword = flag.String("pass", "", "JIRA Password.") issueMessage = flag.String("issues", "", "Message to retrieve the issues from.") inputStdin = flag.Bool("stdin", false, "If set to true you can stream \"-issues\" to stdin instead of an argument. If set \"-issues\" will be ignored.") + checkOnlyOne = flag.Bool("one", false, "If set to true jitic will succeed as soon as one issue is found in the remote Jira instance.") flagVersion = flag.Bool("version", false, "Outputs the version number and exits.") flagVerbose = flag.Bool("verbose", false, "If activated more information will be written to stdout .") ) @@ -71,12 +72,26 @@ func main() { // Loop over all issues and check if they are correct / valid for _, issueFromUser := range issues { - err := checkIfIssueExists(issueFromUser, jiraClient) + found, err := checkIfIssueExists(issueFromUser, jiraClient) if err != nil { - logger.Fatal(err) + if *checkOnlyOne { + logger.Print(err) + } else { + logger.Fatal(err) + } + } else if found && *checkOnlyOne { + logger.Printf("Found JIRA-Issue '%s'", issueFromUser) + os.Exit(0) } } + // if we went through the whole loop with checkOnlyOne == true, then we + // haven't found any issue. + if *checkOnlyOne { + logger.Printf("None of these issues existed in JIRA: %v", issues) + os.Exit(1) + } + os.Exit(0) } @@ -113,20 +128,20 @@ func getIssuesOutOfMessage(projects []string, message string) []string { // checkIfIssueExists checks if issue exists in the JIRA instance. // If not an error will be returned. -func checkIfIssueExists(issue string, jiraClient *jira.Client) error { +func checkIfIssueExists(issue string, jiraClient *jira.Client) (bool, error) { JIRAIssue, resp, err := jiraClient.Issue.Get(issue, nil) if c := resp.StatusCode; err != nil || (c < 200 || c > 299) { - return fmt.Errorf("JIRA Request for issue %s returned %s (%d)", issue, resp.Status, resp.StatusCode) + return false, fmt.Errorf("JIRA Request for issue %s returned %s (%d)", issue, resp.Status, resp.StatusCode) } // Make issues uppercase to be able to compare Web-1234 with WEB-1234 upperIssue := strings.ToUpper(issue) upperJIRAIssue := strings.ToUpper(JIRAIssue.Key) if upperIssue != upperJIRAIssue { - return fmt.Errorf("Issue %s is not the same as %s (provided by JIRA)", upperIssue, upperJIRAIssue) + return false, fmt.Errorf("Issue %s is not the same as %s (provided by JIRA)", upperIssue, upperJIRAIssue) } - return nil + return true, nil } // getJIRAClient will return a valid JIRA api client. diff --git a/main_test.go b/main_test.go index e629222..4230cec 100644 --- a/main_test.go +++ b/main_test.go @@ -48,7 +48,7 @@ func TestGetJIRAClient_IssueDoesNotExist(t *testing.T) { w.WriteHeader(http.StatusNotFound) }) - err = checkIfIssueExists("WEB-1234", c) + _, err = checkIfIssueExists("WEB-1234", c) if err == nil { t.Error("No error occuered. Expected a 404") }