adding labels [add|set|remove] commands

This commit is contained in:
Cory Bennett
2017-08-19 15:09:47 -07:00
parent 47a5ce25bc
commit 916186161a
6 changed files with 186 additions and 69 deletions
-48
View File
@@ -116,54 +116,6 @@ func (jc *JiraCli) Register(app *kingpin.Application, reg []CommandRegistry) {
// return runTemplate(c.getTemplate("components"), data, nil)
// }
// // CmdComment will open up editor with "comment" template and submit
// // YAML output to jira
// func (c *Cli) CmdComment(issue string) error {
// log.Debugf("comment called")
// handlePost := func(json string) error {
// uri := fmt.Sprintf("%s/rest/api/2/issue/%s/comment", c.endpoint, issue)
// if c.getOptBool("dryrun", false) {
// log.Debugf("POST: %s", json)
// log.Debugf("Dryrun mode, skipping POST")
// return nil
// }
// resp, err := c.post(uri, json)
// if err != nil {
// return err
// }
// if resp.StatusCode == 201 {
// 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 POST")
// log.Errorf("%s:\n%s", err, logBuffer)
// return err
// }
// if comment, ok := c.opts["comment"]; ok && comment != "" {
// json, err := jsonEncode(map[string]interface{}{
// "body": comment,
// })
// if err != nil {
// return err
// }
// return handlePost(json)
// }
// return c.editTemplate(
// c.getTemplate("comment"),
// fmt.Sprintf("%s-create-", issue),
// map[string]interface{}{},
// handlePost,
// )
// }
// // CmdComponent will add a new component to given project
// func (c *Cli) CmdComponent(action string, project string, name string, desc string, lead string) error {
// log.Debugf("component called")
+58
View File
@@ -0,0 +1,58 @@
package jiracli
import (
"fmt"
"gopkg.in/Netflix-Skunkworks/go-jira.v1/jiradata"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)
type LabelsAddOptions struct {
GlobalOptions
Issue string
Labels []string
}
func (jc *JiraCli) CmdLabelsAddRegistry() *CommandRegistryEntry {
opts := LabelsAddOptions{}
return &CommandRegistryEntry{
"Add labels to an issue",
func() error {
return jc.CmdLabelsAdd(&opts)
},
func(cmd *kingpin.CmdClause) error {
return jc.CmdLabelsAddUsage(cmd, &opts)
},
}
}
func (jc *JiraCli) CmdLabelsAddUsage(cmd *kingpin.CmdClause, opts *LabelsAddOptions) error {
if err := jc.GlobalUsage(cmd, &opts.GlobalOptions); err != nil {
return err
}
cmd.Arg("ISSUE", "issue id to modify labels").Required().StringVar(&opts.Issue)
cmd.Arg("LABEL", "label to add to issue").Required().StringsVar(&opts.Labels)
return nil
}
// CmdLabels will add labels on a given issue
func (jc *JiraCli) CmdLabelsAdd(opts *LabelsAddOptions) error {
ops := jiradata.FieldOperations{}
for _, label := range opts.Labels {
ops = append(ops, jiradata.FieldOperation{
"add": label,
})
}
issueUpdate := jiradata.IssueUpdate{
Update: jiradata.FieldOperationsMap{
"labels": ops,
},
}
err := jc.EditIssue(opts.Issue, &issueUpdate)
if err != nil {
return err
}
fmt.Printf("OK %s %s/browse/%s\n", opts.Issue, jc.Endpoint, opts.Issue)
return nil
}
+58
View File
@@ -0,0 +1,58 @@
package jiracli
import (
"fmt"
"gopkg.in/Netflix-Skunkworks/go-jira.v1/jiradata"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)
type LabelsRemoveOptions struct {
GlobalOptions
Issue string
Labels []string
}
func (jc *JiraCli) CmdLabelsRemoveRegistry() *CommandRegistryEntry {
opts := LabelsRemoveOptions{}
return &CommandRegistryEntry{
"Remove labels from an issue",
func() error {
return jc.CmdLabelsRemove(&opts)
},
func(cmd *kingpin.CmdClause) error {
return jc.CmdLabelsRemoveUsage(cmd, &opts)
},
}
}
func (jc *JiraCli) CmdLabelsRemoveUsage(cmd *kingpin.CmdClause, opts *LabelsRemoveOptions) error {
if err := jc.GlobalUsage(cmd, &opts.GlobalOptions); err != nil {
return err
}
cmd.Arg("ISSUE", "issue id to modify labels").Required().StringVar(&opts.Issue)
cmd.Arg("LABEL", "label to remove from issue").Required().StringsVar(&opts.Labels)
return nil
}
// CmdLabels will remove labels on a given issue
func (jc *JiraCli) CmdLabelsRemove(opts *LabelsRemoveOptions) error {
ops := jiradata.FieldOperations{}
for _, label := range opts.Labels {
ops = append(ops, jiradata.FieldOperation{
"remove": label,
})
}
issueUpdate := jiradata.IssueUpdate{
Update: jiradata.FieldOperationsMap{
"labels": ops,
},
}
err := jc.EditIssue(opts.Issue, &issueUpdate)
if err != nil {
return err
}
fmt.Printf("OK %s %s/browse/%s\n", opts.Issue, jc.Endpoint, opts.Issue)
return nil
}
+56
View File
@@ -0,0 +1,56 @@
package jiracli
import (
"fmt"
"gopkg.in/Netflix-Skunkworks/go-jira.v1/jiradata"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)
type LabelsSetOptions struct {
GlobalOptions
Issue string
Labels []string
}
func (jc *JiraCli) CmdLabelsSetRegistry() *CommandRegistryEntry {
opts := LabelsSetOptions{}
return &CommandRegistryEntry{
"Set labels on an issue",
func() error {
return jc.CmdLabelsSet(&opts)
},
func(cmd *kingpin.CmdClause) error {
return jc.CmdLabelsSetUsage(cmd, &opts)
},
}
}
func (jc *JiraCli) CmdLabelsSetUsage(cmd *kingpin.CmdClause, opts *LabelsSetOptions) error {
if err := jc.GlobalUsage(cmd, &opts.GlobalOptions); err != nil {
return err
}
cmd.Arg("ISSUE", "issue id to modify labels").Required().StringVar(&opts.Issue)
cmd.Arg("LABEL", "label to set on issue").Required().StringsVar(&opts.Labels)
return nil
}
// CmdLabels will set labels on a given issue
func (jc *JiraCli) CmdLabelsSet(opts *LabelsSetOptions) error {
issueUpdate := jiradata.IssueUpdate{
Update: jiradata.FieldOperationsMap{
"labels": jiradata.FieldOperations{
jiradata.FieldOperation{
"set": opts.Labels,
},
},
},
}
err := jc.EditIssue(opts.Issue, &issueUpdate)
if err != nil {
return err
}
fmt.Printf("OK %s %s/browse/%s\n", opts.Issue, jc.Endpoint, opts.Issue)
return nil
}