From 388b9cb7839de4afbc39ff8b26490d71a5ec5d75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Lutz=20Bru=CC=88ggen?= Date: Mon, 15 Oct 2018 11:35:15 +0200 Subject: [PATCH 1/2] Renamed checkIfIssue function to checkIfIssueExists This is a more appropiate name as the function only checks if the issue exists, and doesn't have any more criteria to validate it. --- main.go | 6 +++--- main_test.go | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index da522a3..7012af6 100644 --- a/main.go +++ b/main.go @@ -71,7 +71,7 @@ func main() { // Loop over all issues and check if they are correct / valid for _, issueFromUser := range issues { - err := checkIfIssue(issueFromUser, jiraClient) + err := checkIfIssueExists(issueFromUser, jiraClient) if err != nil { logger.Fatal(err) } @@ -111,9 +111,9 @@ func getIssuesOutOfMessage(projects []string, message string) []string { return issues } -// checkIfIssue checks if issue exists in the JIRA instance. +// checkIfIssueExists checks if issue exists in the JIRA instance. // If not an error will be returned. -func checkIfIssue(issue string, jiraClient *jira.Client) error { +func checkIfIssueExists(issue string, jiraClient *jira.Client) 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) diff --git a/main_test.go b/main_test.go index 2666c3b..e629222 100644 --- a/main_test.go +++ b/main_test.go @@ -35,7 +35,7 @@ func teardown() { server.Close() } -func TestGetJIRAClient_IssueNotExists(t *testing.T) { +func TestGetJIRAClient_IssueDoesNotExist(t *testing.T) { setup() defer teardown() @@ -48,7 +48,7 @@ func TestGetJIRAClient_IssueNotExists(t *testing.T) { w.WriteHeader(http.StatusNotFound) }) - err = checkIfIssue("WEB-1234", c) + err = checkIfIssueExists("WEB-1234", c) if err == nil { t.Error("No error occuered. Expected a 404") } 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 2/2] 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") }