add take, unassign and assign|give commands

This commit is contained in:
Cory Bennett
2017-08-19 16:02:29 -07:00
parent 916186161a
commit 959524af23
7 changed files with 157 additions and 133 deletions
+56
View File
@@ -0,0 +1,56 @@
package jiracli
import (
"fmt"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)
type AssignOptions struct {
GlobalOptions
Issue string
Assignee string
}
func (jc *JiraCli) CmdAssignRegistry() *CommandRegistryEntry {
opts := AssignOptions{}
return &CommandRegistryEntry{
"Assign user to issue",
func() error {
return jc.CmdAssign(&opts)
},
func(cmd *kingpin.CmdClause) error {
return jc.CmdAssignUsage(cmd, &opts)
},
}
}
func (jc *JiraCli) CmdAssignUsage(cmd *kingpin.CmdClause, opts *AssignOptions) error {
if err := jc.GlobalUsage(cmd, &opts.GlobalOptions); err != nil {
return err
}
cmd.Flag("default", "use default user for assignee").PreAction(func(ctx *kingpin.ParseContext) error {
if flagValue(ctx, "default") == "true" {
opts.Assignee = "-1"
}
return nil
}).Bool()
cmd.Arg("ISSUE", "issue to assign").Required().StringVar(&opts.Issue)
cmd.Arg("ASSIGNEE", "user to assign to issue").StringVar(&opts.Assignee)
return nil
}
// CmdAssign will assign an issue to a user
func (jc *JiraCli) CmdAssign(opts *AssignOptions) error {
err := jc.IssueAssign(opts.Issue, opts.Assignee)
if err != nil {
return err
}
fmt.Printf("OK %s %s/browse/%s\n", opts.Issue, jc.Endpoint, opts.Issue)
// FIXME implement browse
return nil
}
-114
View File
@@ -160,120 +160,6 @@ func (jc *JiraCli) Register(app *kingpin.Application, reg []CommandRegistry) {
// return nil
// }
// // CmdLabels will add, remove or set labels on a given issue
// func (c *Cli) CmdLabels(action string, issue string, labels []string) error {
// log.Debugf("label called")
// if action != "add" && action != "remove" && action != "set" {
// return fmt.Errorf("action must be 'add', 'set' or 'remove': %q is invalid", action)
// }
// handlePut := func(json string) error {
// uri := fmt.Sprintf("%s/rest/api/2/issue/%s", c.endpoint, issue)
// if c.getOptBool("dryrun", false) {
// log.Debugf("PUT: %s", json)
// log.Debugf("Dryrun mode, skipping POST")
// return nil
// }
// resp, err := c.put(uri, json)
// if err != nil {
// return err
// }
// if resp.StatusCode == 204 {
// c.Browse(issue)
// if !c.GetOptBool("quiet", false) {
// fmt.Printf("OK %s %s/browse/%s\n", issue, c.endpoint, issue)
// }
// return nil
// }
// logBuffer := bytes.NewBuffer(make([]byte, 0))
// resp.Write(logBuffer)
// err = fmt.Errorf("Unexpected Response From PUT")
// log.Errorf("%s:\n%s", err, logBuffer)
// return err
// }
// var labelsJSON string
// var err error
// if action == "set" {
// labelsActions := make([]map[string][]string, 1)
// labelsActions[0] = map[string][]string{
// "set": labels,
// }
// labelsJSON, err = jsonEncode(map[string]interface{}{
// "labels": labelsActions,
// })
// } else {
// labelsActions := make([]map[string]string, len(labels))
// for i, label := range labels {
// labelActionMap := map[string]string{
// action: label,
// }
// labelsActions[i] = labelActionMap
// }
// labelsJSON, err = jsonEncode(map[string]interface{}{
// "labels": labelsActions,
// })
// }
// if err != nil {
// return err
// }
// json := fmt.Sprintf("{ \"update\": %s }", labelsJSON)
// return handlePut(json)
// }
// // CmdAssign will assign the given user to be the owner of the given issue
// func (c *Cli) CmdAssign(issue string, user string) error {
// log.Debugf("assign called")
// var userVal interface{} = user
// // https://docs.atlassian.com/jira/REST/cloud/#api/2/issue-assign
// // If the name is "-1" automatic assignee is used. A null name will remove the assignee.
// if user == "" {
// userVal = nil
// }
// if c.GetOptBool("default", false) {
// userVal = "-1"
// }
// json, err := jsonEncode(map[string]interface{}{
// "name": userVal,
// })
// if err != nil {
// return err
// }
// uri := fmt.Sprintf("%s/rest/api/2/issue/%s/assignee", c.endpoint, issue)
// if c.getOptBool("dryrun", false) {
// log.Debugf("PUT: %s", json)
// log.Debugf("Dryrun mode, skipping PUT")
// return nil
// }
// resp, err := c.put(uri, json)
// if err != nil {
// return err
// }
// if resp.StatusCode == 204 {
// c.Browse(issue)
// if !c.GetOptBool("quiet", false) {
// fmt.Printf("OK %s %s/browse/%s\n", issue, c.endpoint, issue)
// }
// } else {
// logBuffer := bytes.NewBuffer(make([]byte, 0))
// resp.Write(logBuffer)
// err := fmt.Errorf("Unexpected Response From PUT")
// log.Errorf("%s:\n%s", err, logBuffer)
// return err
// }
// return nil
// }
// func (c *Cli) CmdUnassign(issue string) error {
// return c.CmdAssign(issue, "")
// }
// // CmdExportTemplates will export the default templates to the template directory.
// func (c *Cli) CmdExportTemplates() error {
// dir := c.opts["directory"].(string)
+26
View File
@@ -0,0 +1,26 @@
package jiracli
import kingpin "gopkg.in/alecthomas/kingpin.v2"
func (jc *JiraCli) CmdTakeRegistry() *CommandRegistryEntry {
opts := AssignOptions{}
return &CommandRegistryEntry{
"Assign issue to yourself",
func() error {
opts.Assignee = opts.User
return jc.CmdAssign(&opts)
},
func(cmd *kingpin.CmdClause) error {
return jc.CmdAssignUsage(cmd, &opts)
},
}
}
func (jc *JiraCli) CmdTakeUsage(cmd *kingpin.CmdClause, opts *AssignOptions) error {
if err := jc.GlobalUsage(cmd, &opts.GlobalOptions); err != nil {
return err
}
cmd.Arg("ISSUE", "issue to assign").Required().StringVar(&opts.Issue)
return nil
}
+25
View File
@@ -0,0 +1,25 @@
package jiracli
import kingpin "gopkg.in/alecthomas/kingpin.v2"
func (jc *JiraCli) CmdUnassignRegistry() *CommandRegistryEntry {
opts := AssignOptions{}
return &CommandRegistryEntry{
"Unassign an issue",
func() error {
return jc.CmdAssign(&opts)
},
func(cmd *kingpin.CmdClause) error {
return jc.CmdAssignUsage(cmd, &opts)
},
}
}
func (jc *JiraCli) CmdUnassignUsage(cmd *kingpin.CmdClause, opts *AssignOptions) error {
if err := jc.GlobalUsage(cmd, &opts.GlobalOptions); err != nil {
return err
}
cmd.Arg("ISSUE", "issue to unassign").Required().StringVar(&opts.Issue)
return nil
}