Skip to content

Commit

Permalink
Merge pull request #21 from mikepea/label_command
Browse files Browse the repository at this point in the history
Add 'labels' command to set/add/remove labels
  • Loading branch information
coryb committed Jan 4, 2016
2 parents 8712b4a + 303784f commit 6734532
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
65 changes: 65 additions & 0 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,71 @@ func (c *Cli) CmdComment(issue string) error {
return nil
}

func (c *Cli) CmdLabels(action string, issue string, labels []string) error {
log.Debug("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 {
log.Debug("JSON: %s", json)
uri := fmt.Sprintf("%s/rest/api/2/issue/%s", c.endpoint, issue)
if c.getOptBool("dryrun", false) {
log.Debug("PUT: %s", json)
log.Debug("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.opts["quiet"].(bool) {
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
}
}

var labels_json string
var err error
if action == "set" {
labelsActions := make([]map[string][]string, 1)
labelsActions[0] = map[string][]string{
"set": labels,
}
labels_json, 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
}
labels_json, err = jsonEncode(map[string]interface{}{
"labels": labelsActions,
})
}
if err != nil {
return err
}
json := fmt.Sprintf("{ \"update\": %s }", labels_json)
return handlePut(json)

}

func (c *Cli) CmdAssign(issue string, user string) error {
log.Debug("assign called")

Expand Down
10 changes: 10 additions & 0 deletions main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Usage:
jira start ISSUE [--edit] <Edit Options>
jira stop ISSUE [--edit] <Edit Options>
jira comment ISSUE [--noedit] <Edit Options>
jira (set,add,remove) labels ISSUE [LABEL] ...
jira take ISSUE
jira (assign|give) ISSUE ASSIGNEE
jira fields
Expand Down Expand Up @@ -136,6 +137,8 @@ Command Options:
"start": "start",
"stop": "stop",
"comment": "comment",
"label": "labels",
"labels": "labels",
"take": "take",
"assign": "assign",
"give": "assign",
Expand Down Expand Up @@ -216,6 +219,7 @@ Command Options:
args = args[1:]
} else if len(args) > 1 {
// look at second arg for "dups" and "blocks" commands
// also for 'set/add/remove' actions like 'labels'
if alias, ok := jiraCommands[args[1]]; ok {
command = alias
args = append(args[:1], args[2:]...)
Expand Down Expand Up @@ -377,6 +381,12 @@ Command Options:
requireArgs(1)
setEditing(true)
err = c.CmdComment(args[0])
case "labels":
requireArgs(2)
action := args[0]
issue := args[1]
labels := args[2:]
err = c.CmdLabels(action, issue, labels)
case "take":
requireArgs(1)
err = c.CmdAssign(args[0], opts["user"].(string))
Expand Down

0 comments on commit 6734532

Please sign in to comment.