Skip to content

Commit

Permalink
Add 'labels' command to set/add/remove labels
Browse files Browse the repository at this point in the history
This adds 'labels' command, which allows for the setting, addition and removal
of labels on an issue.

'set' action resets the issue labels to the list provided.
'add' action adds the supplied labels to the issue
'remove' action removes the supplied labels from the issue

The API already gracefully handles duplication, removal of non-existant labels, and the
supplying of an empty list of labels (which is useful in the case of 'set')

Eg

jira labels TEST-123 add label1 label2 label3
jira labels TEST-123 remove label1 label2
jira labels TEST-123 set label1 label2 label3
jira labels TEST-123 set # clears any labels on the issue
jira labels TEST-123 add # no-op
jira labels TEST-123 remove # no-op

This mirrors the functionality of the API.
  • Loading branch information
Mike Pountney committed Dec 24, 2015
1 parent 425b571 commit 230b52d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
65 changes: 65 additions & 0 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,71 @@ func (c *Cli) CmdComment(issue string) error {
return nil
}

func (c *Cli) CmdLabels(issue string, command string, labels []string) error {
log.Debug("label called")

if command != "add" && command != "remove" && command != "set" {
return fmt.Errorf("command must be 'add', 'set' or 'remove': %q is invalid", command)
}

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 command == "set" {
labelsCommands := make([]map[string][]string, 1)
labelsCommands[0] = map[string][]string{
"set": labels,
}
labels_json, err = jsonEncode(map[string]interface{}{
"labels": labelsCommands,
})
} else {
labelsCommands := make([]map[string]string, len(labels))
for i, label := range labels {
labelCommandMap := map[string]string{
command: label,
}
labelsCommands[i] = labelCommandMap
}
labels_json, err = jsonEncode(map[string]interface{}{
"labels": labelsCommands,
})
}
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: 8 additions & 2 deletions main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
)

var (
log = logging.MustGetLogger("jira")
format = "%{color}%{time:2006-01-02T15:04:05.000Z07:00} %{level:-5s} [%{shortfile}]%{color:reset} %{message}"
log = logging.MustGetLogger("jira")
format = "%{color}%{time:2006-01-02T15:04:05.000Z07:00} %{level:-5s} [%{shortfile}]%{color:reset} %{message}"
)

func main() {
Expand Down 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 labels ISSUE set,add,remove [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 @@ -377,6 +380,9 @@ Command Options:
requireArgs(1)
setEditing(true)
err = c.CmdComment(args[0])
case "labels":
requireArgs(2)
err = c.CmdLabels(args[0], args[1], args[2:])
case "take":
requireArgs(1)
err = c.CmdAssign(args[0], opts["user"].(string))
Expand Down

0 comments on commit 230b52d

Please sign in to comment.