diff --git a/cli.go b/cli.go index 6d8baad8..bd18a8c6 100644 --- a/cli.go +++ b/cli.go @@ -14,6 +14,7 @@ import ( "net/url" "os" "os/exec" + "path" "runtime" "strings" "time" @@ -87,6 +88,7 @@ func (c *Cli) saveCookies(cookies []*http.Cookie) { } jsonWrite(c.cookieFile, mergedCookies) } else { + mkdir(path.Dir(c.cookieFile)) jsonWrite(c.cookieFile, cookies) } } diff --git a/commands.go b/commands.go index 589f3b67..f453b22f 100644 --- a/commands.go +++ b/commands.go @@ -2,6 +2,7 @@ package jira import ( "bytes" + "errors" "fmt" "golang.org/x/crypto/ssh/terminal" "net/http" @@ -200,6 +201,16 @@ func (c *Cli) CmdCreateMeta() error { return runTemplate(c.getTemplate("createmeta"), data, nil) } +func (c *Cli) CmdComponents(project string) error { + log.Debug("Components called") + uri := fmt.Sprintf("%s/rest/api/2/project/%s/components", c.endpoint, project) + data, err := responseToJson(c.get(uri)) + if err != nil { + return err + } + return runTemplate(c.getTemplate("components"), data, nil) +} + func (c *Cli) CmdTransitions(issue string) error { log.Debug("Transitions called") c.Browse(issue) @@ -606,6 +617,49 @@ func (c *Cli) CmdComment(issue string) error { return nil } +func (c *Cli) CmdComponent(action string, project string, name string, desc string, lead string) error { + log.Debug("component called") + + switch action { + case "add": + default: + return errors.New(fmt.Sprintf("CmdComponent: %q is not a valid action", action)) + } + + json, err := jsonEncode(map[string]interface{}{ + "name": name, + "description": desc, + "leadUserName": lead, + "project": project, + }) + if err != nil { + return err + } + + uri := fmt.Sprintf("%s/rest/api/2/component", c.endpoint) + if c.getOptBool("dryrun", false) { + log.Debug("POST: %s", json) + log.Debug("Dryrun mode, skipping POST") + return nil + } + resp, err := c.post(uri, json) + if err != nil { + return err + } + if resp.StatusCode == 201 { + if !c.opts["quiet"].(bool) { + fmt.Printf("OK %s %s\n", project, name) + } + } else { + logBuffer := bytes.NewBuffer(make([]byte, 0)) + resp.Write(logBuffer) + err := fmt.Errorf("Unexpected Response From POST") + log.Error("%s:\n%s", err, logBuffer) + return err + } + return nil +} + func (c *Cli) CmdLabels(action string, issue string, labels []string) error { log.Debug("label called") diff --git a/main/main.go b/main/main.go index f58979c7..ec119e75 100644 --- a/main/main.go +++ b/main/main.go @@ -73,6 +73,8 @@ Usage: jira issuelinktypes jira transmeta ISSUE jira editmeta ISSUE + jira add component [-p PROJECT] NAME DESCRIPTION LEAD + jira components [-p PROJECT] jira issuetypes [-p PROJECT] jira createmeta [-p PROJECT] [-i ISSUETYPE] jira transitions ISSUE @@ -141,6 +143,8 @@ Command Options: "comment": "comment", "label": "labels", "labels": "labels", + "component": "component", + "components": "components", "take": "take", "assign": "assign", "give": "assign", @@ -395,6 +399,23 @@ Command Options: issue := args[1] labels := args[2:] err = c.CmdLabels(action, issue, labels) + case "component": + requireArgs(2) + action := args[0] + project := opts["project"].(string) + name := args[1] + var lead string + var description string + if len(args) > 2 { + description = args[2] + } + if len(args) > 3 { + lead = args[2] + } + err = c.CmdComponent(action, project, name, description, lead) + case "components": + project := opts["project"].(string) + err = c.CmdComponents(project) case "take": requireArgs(1) err = c.CmdAssign(args[0], opts["user"].(string)) diff --git a/templates.go b/templates.go index a0cb81e6..253a3065 100644 --- a/templates.go +++ b/templates.go @@ -12,6 +12,7 @@ var all_templates = map[string]string{ "view": default_view_template, "edit": default_edit_template, "transitions": default_transitions_template, + "components": default_components_template, "issuetypes": default_issuetypes_template, "create": default_create_template, "comment": default_comment_template, @@ -82,6 +83,9 @@ fields: const default_transitions_template = `{{ range .transitions }}{{.id }}: {{.name}} {{end}}` +const default_components_template = `{{ range . }}{{.id }}: {{.name}} +{{end}}` + const default_issuetypes_template = `{{ range .projects }}{{ range .issuetypes }}{{color "+bh"}}{{.name | append ":" | printf "%-13s" }}{{color "reset"}} {{.description}} {{end}}{{end}}`