diff --git a/jira/cli/cli.go b/jira/cli/cli.go index da7b4e1..7652e5d 100644 --- a/jira/cli/cli.go +++ b/jira/cli/cli.go @@ -248,17 +248,19 @@ func (c *Cli) editTemplate(template string, tmpFilePrefix string, templateData m } else { edited = fixed.(map[string]interface{}) } - - mf := templateData["meta"].(map[string]interface{})["fields"] - f := edited["fields"].(map[string]interface{}) - for k, _ := range f { - if _, ok := mf.(map[string]interface{})[k]; !ok { - err := fmt.Errorf("Field %s is not editable", k) - log.Error("%s", err) - if promptYN("edit again?", true) { - continue + + if _, ok := templateData["meta"]; ok { + mf := templateData["meta"].(map[string]interface{})["fields"] + f := edited["fields"].(map[string]interface{}) + for k, _ := range f { + if _, ok := mf.(map[string]interface{})[k]; !ok { + err := fmt.Errorf("Field %s is not editable", k) + log.Error("%s", err) + if promptYN("edit again?", true) { + continue + } + return err } - return err } } diff --git a/jira/cli/commands.go b/jira/cli/commands.go index 4c0ebd5..e04afc8 100644 --- a/jira/cli/commands.go +++ b/jira/cli/commands.go @@ -312,7 +312,7 @@ func (c *Cli) CmdDups(duplicate string, issue string) error { func (c *Cli) CmdWatch(issue string, watcher string) error { - log.Debug("dups called") + log.Debug("watch called") json, err := jsonEncode(watcher); if err != nil { return err @@ -335,6 +335,7 @@ func (c *Cli) CmdWatch(issue string, watcher string) error { } func (c *Cli) CmdTransition(issue string, trans string) error { + log.Debug("transition called") uri := fmt.Sprintf("%s/rest/api/2/issue/%s/transitions", c.endpoint, issue) data, err := responseToJson(c.get(uri)); if err != nil { return err @@ -342,7 +343,7 @@ func (c *Cli) CmdTransition(issue string, trans string) error { transitions := data.(map[string]interface{})["transitions"].([]interface{}) var transId string - found := make([]string, len(transitions)) + found := make([]string, 0, len(transitions)) for _, transition := range transitions { name := transition.(map[string]interface{})["name"].(string) id := transition.(map[string]interface{})["id"].(string) @@ -395,3 +396,43 @@ func (c *Cli) CmdTransition(issue string, trans string) error { } return nil } + +func (c *Cli) CmdComment(issue string) error { + log.Debug("comment called") + + handlePost := func(json string) error { + log.Debug("JSON: %s", json) + uri := fmt.Sprintf("%s/rest/api/2/issue/%s/comment", c.endpoint, issue) + resp, err := c.post(uri, json); if err != nil { + return err + } + + if resp.StatusCode == 201 { + fmt.Printf("OK %s %s/browse/%s\n", issue, c.endpoint, issue) + return nil + } else { + logBuffer := bytes.NewBuffer(make([]byte,0)) + resp.Write(logBuffer) + err := fmt.Errorf("Unexpected Response From PUT") + log.Error("%s:\n%s", err, logBuffer) + return err + } + } + + if comment, ok := c.opts["comment"]; ok { + json, err := jsonEncode(map[string]interface{}{ + "body": comment, + }); if err != nil { + return err + } + return handlePost(json) + } else { + return c.editTemplate( + c.getTemplate(".jira.d/templates/comment", default_comment_template), + fmt.Sprintf("%s-create-", issue), + map[string]interface{}{}, + handlePost, + ) + } + return nil +} diff --git a/jira/cli/templates.go b/jira/cli/templates.go index 89ac6d5..d07f683 100644 --- a/jira/cli/templates.go +++ b/jira/cli/templates.go @@ -71,3 +71,7 @@ const default_create_template = `fields: customfield_10110: - name: ` + +const default_comment_template = `body: | + +` diff --git a/jira/main.go b/jira/main.go index 336e446..3ecbd77 100644 --- a/jira/main.go +++ b/jira/main.go @@ -41,9 +41,8 @@ Usage: jira [-v ...] [-u USER] [-e URI] reopen ISSUE [-m COMMENT] jira [-v ...] [-u USER] [-e URI] start ISSUE [-m COMMENT] jira [-v ...] [-u USER] [-e URI] stop ISSUE [-m COMMENT] + jira [-v ...] [-u USER] [-e URI] [-t FILE] comment ISSUE [-m COMMENT] - - jira TODO [-v ...] [-u USER] [-e URI] comment ISSUE [-m COMMENT] jira TODO [-v ...] [-u USER] [-e URI] take ISSUE jira TODO [-v ...] [-u USER] [-e URI] assign ISSUE ASSIGNEE @@ -215,6 +214,8 @@ Transition Options: err = c.CmdTransition(args["ISSUE"].(string), "start") } else if validCommand("stop") { err = c.CmdTransition(args["ISSUE"].(string), "stop") + } else if validCommand("comment") { + err = c.CmdComment(args["ISSUE"].(string)) } else if val, ok := args["ISSUE"]; ok { err = c.CmdView(val.(string)) }