diff --git a/Makefile b/Makefile index c7007f1..114be1b 100644 --- a/Makefile +++ b/Makefile @@ -43,17 +43,12 @@ else GOBUILD=go build -v -ldflags "$(LDFLAGS) -s" endif -build: src/gopkg.in/Netflix-Skunkworks/go-jira.v0 +build: $(GOBUILD) -o '$(BIN)' cmd/jira/main.go debug: go build -v -o '$(BIN)' cmd/jira/main.go -src/%: - mkdir -p $(@D) - test -L $@ || ln -sf '../../..' $@ - go get -v $* $*/main - vet: @go vet . @go vet ./data diff --git a/cmd/jira/main.go b/cmd/jira/main.go index 7a6cd91..0640850 100644 --- a/cmd/jira/main.go +++ b/cmd/jira/main.go @@ -203,6 +203,19 @@ func main() { Command: "watch", Entry: cli.CmdWatchRegistry(), }, + jiracli.CommandRegistry{ + Command: "labels add", + Entry: cli.CmdLabelsAddRegistry(), + }, + jiracli.CommandRegistry{ + Command: "labels set", + Entry: cli.CmdLabelsAddRegistry(), + }, + jiracli.CommandRegistry{ + Command: "labels remove", + Entry: cli.CmdLabelsAddRegistry(), + Aliases: []string{"rm"}, + }, } cli.Register(app, registry) @@ -221,8 +234,6 @@ func main() { } // Usage: - // jira comment ISSUE [--noedit] - // jira (set,add,remove) labels ISSUE [LABEL] ... // jira take ISSUE // jira (assign|give) ISSUE [ASSIGNEE|--default] // jira unassign ISSUE @@ -277,9 +288,6 @@ func main() { // } // jiraCommands := map[string]string{ - // "comment": "comment", - // "label": "labels", - // "labels": "labels", // "component": "component", // "components": "components", // "take": "take", @@ -438,16 +446,6 @@ func main() { // switch command { // case "issuetypes": // err = c.CmdIssueTypes() - // case "comment": - // 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 "component": // requireArgs(2) // action := args[0] diff --git a/jiracli/commands.go b/jiracli/commands.go index 71b828e..1cde4ba 100644 --- a/jiracli/commands.go +++ b/jiracli/commands.go @@ -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") diff --git a/jiracli/labelsAdd.go b/jiracli/labelsAdd.go new file mode 100644 index 0000000..4515e19 --- /dev/null +++ b/jiracli/labelsAdd.go @@ -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 +} diff --git a/jiracli/labelsRemove.go b/jiracli/labelsRemove.go new file mode 100644 index 0000000..5aaebb4 --- /dev/null +++ b/jiracli/labelsRemove.go @@ -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 +} diff --git a/jiracli/labelsSet.go b/jiracli/labelsSet.go new file mode 100644 index 0000000..d0a368b --- /dev/null +++ b/jiracli/labelsSet.go @@ -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 +}