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.
This commit is contained in:
Mike Pountney
2016-01-24 02:23:08 +00:00
parent 7b651d73c2
commit 797edefc3f
2 changed files with 27 additions and 38 deletions
+20 -33
View File
@@ -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
}
+7 -5
View File
@@ -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] <Edit Options>
jira ack ISSUE [--edit] <Edit Options>
jira close ISSUE [--edit] <Edit Options>
@@ -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 := ""