From 60e4925fe477c9aa1afa9736f4cc66ab0a1e4b2b Mon Sep 17 00:00:00 2001 From: Cory Bennett Date: Fri, 13 Feb 2015 13:16:18 -0800 Subject: [PATCH] add options to ls to allow for dynamically creating some simple JQL --- jira/cli/commands.go | 48 ++++++++++++++++++++++++++++++++------------ jira/main.go | 22 +++++++++----------- 2 files changed, 45 insertions(+), 25 deletions(-) diff --git a/jira/cli/commands.go b/jira/cli/commands.go index cbc5d52..a6ff432 100644 --- a/jira/cli/commands.go +++ b/jira/cli/commands.go @@ -57,25 +57,47 @@ func (c *Cli) CmdFields() error { func (c *Cli) CmdList() error { log.Debug("list called") - if query, ok := c.opts["query"]; !ok { - log.Error("No query argument found, either use --query or set query attribute in .jira file") - return fmt.Errorf("Missing query") - } else { - json, err := jsonEncode(map[string]string{ - "jql": query, - "startAt": "0", - "maxResults": "500", - }); if err != nil { + var query string + var ok bool + // project = BAKERY and status not in (Resolved, Closed) + if query, ok = c.opts["query"]; !ok { + qbuff := bytes.NewBufferString("status not in (Resolved, Closed)") + if project, ok := c.opts["project"]; !ok { + err := fmt.Errorf("Missing required arguments, either 'query' or 'project' are required") + log.Error("%s", err) return err + } else { + qbuff.WriteString(fmt.Sprintf(" AND project = '%s'", project)) + } + + if component, ok := c.opts["component"]; ok { + qbuff.WriteString(fmt.Sprintf(" AND component = '%s'", component)) } - uri := fmt.Sprintf("%s/rest/api/2/search", c.endpoint) - data, err := responseToJson(c.post(uri, json)); if err != nil { - return err + if assignee, ok := c.opts["assignee"]; ok { + qbuff.WriteString(fmt.Sprintf(" AND assignee = '%s'", assignee)) } - return runTemplate(c.getTemplate(".jira.d/templates/list", default_list_template), data, nil) + if issuetype, ok := c.opts["issuetype"]; ok { + qbuff.WriteString(fmt.Sprintf(" AND issuetype = '%s'", issuetype)) + } + query = qbuff.String() } + + json, err := jsonEncode(map[string]string{ + "jql": query, + "startAt": "0", + "maxResults": "500", + }); if err != nil { + return err + } + + uri := fmt.Sprintf("%s/rest/api/2/search", c.endpoint) + data, err := responseToJson(c.post(uri, json)); if err != nil { + return err + } + + return runTemplate(c.getTemplate(".jira.d/templates/list", default_list_template), data, nil) } func (c *Cli) CmdView(issue string) error { diff --git a/jira/main.go b/jira/main.go index f3089e0..84f022d 100644 --- a/jira/main.go +++ b/jira/main.go @@ -20,7 +20,7 @@ func main() { Usage: jira [-v ...] [-u USER] [-e URI] [-t FILE] fields jira [-v ...] [-u USER] [-e URI] [-t FILE] login - jira [-v ...] [-u USER] [-e URI] [-t FILE] (ls|list) [-q JQL] + jira [-v ...] [-u USER] [-e URI] [-t FILE] (ls|list) ( [-q JQL] | [-p PROJECT] [-c COMPONENT] [-a ASSIGNEE] [-i ISSUETYPE]) jira [-v ...] [-u USER] [-e URI] [-t FILE] view ISSUE jira [-v ...] [-u USER] [-e URI] [-t FILE] issuelinktypes jira [-v ...] [-u USER] [-e URI] [-t FILE] transmeta ISSUE @@ -53,23 +53,22 @@ General Options: -e --endpoint=URI URI to use for jira (default: https://jira) -t --template=FILE Template file to use for output/editing -List Options: - -q --query=JQL Jira Query Language expression for the search - -Create Options: - -p --project=PROJECT Jira Project Name +Command Options: + -a --assignee=USER Username assigned the issue + -q --query=JQL Jira Query Language expression for the search + -c --component=COMPONENT Component to Search for + -p --project=PROJECT Project to Search for -i --issuetype=ISSUETYPE Jira Issue Type (default: Bug) -o --override=KEY:VAL Set custom key/value pairs - -Watch Options: -w --watcher=USER Watcher to add to issue (default: %s) - -Transition Options: -m --comment=COMMENT Comment message for transition `, user, user) - args, _ := docopt.Parse(usage, nil, true, "0.0.1", false, false) + args, err := docopt.Parse(usage, nil, true, "0.0.1", false, false); if err != nil { + log.Error("Failed to parse options: %s", err) + os.Exit(1) + } logBackend := logging.NewLogBackend(os.Stderr, "", 0) logging.SetBackend( logging.NewBackendFormatter( @@ -152,7 +151,6 @@ Transition Options: return dflt } - var err error if validCommand("login") { err = c.CmdLogin() } else if validCommand("fields") {