Skip to content

Commit

Permalink
add components command
Browse files Browse the repository at this point in the history
  • Loading branch information
coryb committed Aug 20, 2017
1 parent cc90610 commit 0bd3ca2
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 48 deletions.
9 changes: 4 additions & 5 deletions cmd/jira/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ func main() {
Command: "component add",
Entry: cli.CmdComponentAddRegistry(),
},
jiracli.CommandRegistry{
Command: "components",
Entry: cli.CmdComponentsRegistry(),
},
}

cli.Register(app, registry)
Expand All @@ -251,7 +255,6 @@ func main() {
}

// Usage:
// jira components [-p PROJECT]
// jira issuetypes [-p PROJECT]
// jira export-templates [-d DIR] [-t template]
// jira (b|browse) ISSUE
Expand Down Expand Up @@ -301,7 +304,6 @@ func main() {
// }

// jiraCommands := map[string]string{
// "components": "components",
// "issuetypes": "issuetypes",
// "export-templates": "export-templates",
// "browse": "browse",
Expand Down Expand Up @@ -454,9 +456,6 @@ func main() {
// switch command {
// case "issuetypes":
// err = c.CmdIssueTypes()
// case "components":
// project := opts["project"].(string)
// err = c.CmdComponents(project)
// case "browse":
// requireArgs(1)
// opts["browse"] = true
Expand Down
2 changes: 1 addition & 1 deletion jiracli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func (jc *JiraCli) editLoop(opts *GlobalOptions, input interface{}, output inter
// etc. We need to do this because jira will reject json documents
// with empty arrays, or empty strings typically. So here we process
// the data to a raw interface{} then we fixup the yaml parsed
// inferface, then we serialize to a new yaml document ... then is
// interface, then we serialize to a new yaml document ... then is
// parsed as the original document to populate the output struct. Phew.
var raw interface{}
if err := yaml.Unmarshal(data, &raw); err != nil {
Expand Down
11 changes: 0 additions & 11 deletions jiracli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,6 @@ func (jc *JiraCli) Register(app *kingpin.Application, reg []CommandRegistry) {
// return ""
// }

// // CmdComponents sends component data for given project and sends to the "components" template
// func (c *Cli) CmdComponents(project string) error {
// log.Debugf("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)
// }

// // CmdExportTemplates will export the default templates to the template directory.
// func (c *Cli) CmdExportTemplates() error {
// dir := c.opts["directory"].(string)
Expand Down
36 changes: 9 additions & 27 deletions jiracli/componentAdd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,18 @@ import (

type ComponentAddOptions struct {
GlobalOptions
Overrides map[string]string
jiradata.Component
}

func (jc *JiraCli) CmdComponentAddRegistry() *CommandRegistryEntry {
opts := ComponentAddOptions{
GlobalOptions: GlobalOptions{
Template: "component-add",
},
Overrides: map[string]string{},
}

return &CommandRegistryEntry{
"ComponentAdd issue",
"Add component",
func() error {
return jc.CmdComponentAdd(&opts)
},
Expand All @@ -38,38 +37,21 @@ func (jc *JiraCli) CmdComponentAddUsage(cmd *kingpin.CmdClause, opts *ComponentA
jc.EditorUsage(cmd, &opts.GlobalOptions)
jc.TemplateUsage(cmd, &opts.GlobalOptions)
cmd.Flag("noedit", "Disable opening the editor").BoolVar(&opts.SkipEditing)
cmd.Flag("project", "project to create component in").Short('p').PreAction(func(ctx *kingpin.ParseContext) error {
opts.Overrides["project"] = flagValue(ctx, "project")
return nil
}).String()
cmd.Flag("name", "name of component").Short('n').PreAction(func(ctx *kingpin.ParseContext) error {
opts.Overrides["name"] = flagValue(ctx, "name")
return nil
}).String()
cmd.Flag("description", "description of component").Short('d').PreAction(func(ctx *kingpin.ParseContext) error {
opts.Overrides["description"] = flagValue(ctx, "description")
return nil
}).String()
cmd.Flag("lead", "person that acts as lead for component").Short('l').PreAction(func(ctx *kingpin.ParseContext) error {
opts.Overrides["lead"] = flagValue(ctx, "lead")
return nil
}).String()
cmd.Flag("project", "project to create component in").Short('p').StringVar(&opts.Project)
cmd.Flag("name", "name of component").Short('n').StringVar(&opts.Name)
cmd.Flag("description", "description of component").Short('d').StringVar(&opts.Description)
cmd.Flag("lead", "person that acts as lead for component").Short('l').StringVar(&opts.LeadUserName)
return nil
}

// CmdComponentAdd sends the provided overrides to the "component-add" template for editing, then
// will parse the edited document as YAML and submit the document to jira.
func (jc *JiraCli) CmdComponentAdd(opts *ComponentAddOptions) error {
input := struct {
Overrides map[string]string
}{
opts.Overrides,
}

var err error
component := &jiradata.Component{}
err = jc.editLoop(&opts.GlobalOptions, &input, component, func() error {
component, err = jc.CreateComponent(component)
var resp *jiradata.Component
err = jc.editLoop(&opts.GlobalOptions, &opts.Component, component, func() error {
resp, err = jc.CreateComponent(component)
return err
})
if err != nil {
Expand Down
52 changes: 52 additions & 0 deletions jiracli/components.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package jiracli

import (
"fmt"

kingpin "gopkg.in/alecthomas/kingpin.v2"
)

type ComponentsOptions struct {
GlobalOptions
Project string
}

func (jc *JiraCli) CmdComponentsRegistry() *CommandRegistryEntry {
opts := ComponentsOptions{
GlobalOptions: GlobalOptions{
Template: "components",
},
}

return &CommandRegistryEntry{
"Show components for a project",
func() error {
return jc.CmdComponents(&opts)
},
func(cmd *kingpin.CmdClause) error {
return jc.CmdComponentsUsage(cmd, &opts)
},
}
}

func (jc *JiraCli) CmdComponentsUsage(cmd *kingpin.CmdClause, opts *ComponentsOptions) error {
if err := jc.GlobalUsage(cmd, &opts.GlobalOptions); err != nil {
return err
}
jc.TemplateUsage(cmd, &opts.GlobalOptions)
cmd.Flag("project", "project to list components").Short('p').StringVar(&opts.Project)

return nil
}

// CmdComponents will get available components for project and send to the "components" template
func (jc *JiraCli) CmdComponents(opts *ComponentsOptions) error {
if opts.Project == "" {
return fmt.Errorf("Project Required.")
}
data, err := jc.GetProjectComponents(opts.Project)
if err != nil {
return err
}
return jc.runTemplate(opts.Template, data, nil)
}
8 changes: 4 additions & 4 deletions jiracli/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,10 @@ const defaultComponentsTemplate = `{{ range . }}{{.id }}: {{.name}}
{{end}}`

const defaultComponentAddTemplate = `{{/* compoinent add template */ -}}
project: {{or .overrides.project ""}}
name: {{or .overrides.name ""}}
description: {{or .overrides.description ""}}
leadUserName: {{or .overrides.lead ""}}
project: {{or .project ""}}
name: {{or .name ""}}
description: {{or .description ""}}
leadUserName: {{or .leadUserName ""}}
`

const defaultIssuetypesTemplate = `{{ range .projects }}{{ range .issuetypes }}{{color "+bh"}}{{.name | append ":" | printf "%-13s" }}{{color "reset"}} {{.description}}
Expand Down
23 changes: 23 additions & 0 deletions project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package jira

import (
"fmt"

"gopkg.in/Netflix-Skunkworks/go-jira.v1/jiradata"
)

// https://docs.atlassian.com/jira/REST/cloud/#api/2/project-getProjectComponents
func (j *Jira) GetProjectComponents(project string) (*jiradata.Components, error) {
uri := fmt.Sprintf("%s/rest/api/2/project/%s/components", j.Endpoint, project)
resp, err := j.UA.GetJSON(uri)
if err != nil {
return nil, err
}
defer resp.Body.Close()

if resp.StatusCode == 200 {
results := jiradata.Components{}
return &results, readJSON(resp.Body, &results)
}
return nil, responseError(resp)
}

0 comments on commit 0bd3ca2

Please sign in to comment.