Add component/components support: add and list for now.

This adds the basics of the component API:

* listing of components assigned to a project
* adding new components to a project.

The interface to 'add component' is similar to that of 'labels', as
previously discussed. We can implement remove/update later.
This commit is contained in:
Mike Pountney
2016-01-29 00:22:26 +00:00
parent f595801202
commit 595a5212b4
3 changed files with 79 additions and 0 deletions
+54
View File
@@ -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")
+21
View File
@@ -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))
+4
View File
@@ -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}}`