From 797edefc3fb433678b600ed888dceef560a8f146 Mon Sep 17 00:00:00 2001 From: Mike Pountney Date: Sun, 24 Jan 2016 02:23:08 +0000 Subject: [PATCH] Amend vote/unvote to be vote/vote --down This simplifies the interface to voting, as per the discussion in https://github.com/Netflix-Skunkworks/go-jira/pull/26 Basically, DRY out the logic into a single CmdVote function based around an 'up' bool. Similarly, make a single CLI command with an option to do the downvote. --- commands.go | 53 ++++++++++++++++++++-------------------------------- main/main.go | 12 +++++++----- 2 files changed, 27 insertions(+), 38 deletions(-) diff --git a/commands.go b/commands.go index 335f216..f628f56 100644 --- a/commands.go +++ b/commands.go @@ -419,16 +419,27 @@ func (c *Cli) CmdWatch(issue string) error { return nil } -func (c *Cli) CmdVote(issue string) error { - log.Debug("vote called") +func (c *Cli) CmdVote(issue string, up bool) error { + log.Debug("vote called, with up: %n", up) uri := fmt.Sprintf("%s/rest/api/2/issue/%s/votes", c.endpoint, issue) if c.getOptBool("dryrun", false) { - log.Debug("POST: %s", "") - log.Debug("Dryrun mode, skipping POST") + if up { + log.Debug("POST: %s", "") + log.Debug("Dryrun mode, skipping POST") + } else { + log.Debug("DELETE: %s", "") + log.Debug("Dryrun mode, skipping DELETE") + } return nil } - resp, err := c.post(uri, "") + var resp *http.Response + var err error + if up { + resp, err = c.post(uri, "") + } else { + resp, err = c.delete(uri) + } if err != nil { return err } @@ -440,35 +451,11 @@ func (c *Cli) CmdVote(issue string) error { } 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) CmdUnvote(issue string) error { - log.Debug("unvote called") - - uri := fmt.Sprintf("%s/rest/api/2/issue/%s/votes", c.endpoint, issue) - if c.getOptBool("dryrun", false) { - log.Debug("DELETE: %s", "") - log.Debug("Dryrun mode, skipping DELETE") - return nil - } - resp, err := c.delete(uri) - 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) + if up { + err = fmt.Errorf("Unexpected Response From POST") + } else { + err = fmt.Errorf("Unexpected Response From DELETE") } - } else { - logBuffer := bytes.NewBuffer(make([]byte, 0)) - resp.Write(logBuffer) - err := fmt.Errorf("Unexpected Response From DELETE") log.Error("%s:\n%s", err, logBuffer) return err } diff --git a/main/main.go b/main/main.go index a724c09..ff1f27b 100644 --- a/main/main.go +++ b/main/main.go @@ -57,6 +57,7 @@ Usage: jira DUPLICATE dups ISSUE jira BLOCKER blocks ISSUE jira watch ISSUE [-w WATCHER] + jira vote ISSUE [--down] jira (trans|transition) TRANSITION ISSUE [--noedit] jira ack ISSUE [--edit] jira close ISSUE [--edit] @@ -156,7 +157,6 @@ Command Options: "req": "request", "request": "request", "vote": "vote", - "unvote": "unvote", } defaults := map[string]interface{}{ @@ -208,6 +208,7 @@ Command Options: "M|method=s": setopt, "S|saveFile=s": setopt, "Q|quiet": setopt, + "down": setopt, }) if err := op.ProcessAll(os.Args[1:]); err != nil { @@ -408,10 +409,11 @@ Command Options: err = c.CmdView(args[0]) case "vote": requireArgs(1) - err = c.CmdVote(args[0]) - case "unvote": - requireArgs(1) - err = c.CmdUnvote(args[0]) + if val, ok := opts["down"]; ok { + err = c.CmdVote(args[0], !val.(bool)) + } else { + err = c.CmdVote(args[0], true) + } case "request": requireArgs(1) data := ""