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 a927181db1
commit b0d4f7273d
2 changed files with 27 additions and 38 deletions
+16 -29
View File
@@ -419,44 +419,27 @@ func (c *Cli) CmdWatch(issue string) error {
return nil return nil
} }
func (c *Cli) CmdVote(issue string) error { func (c *Cli) CmdVote(issue string, up bool) error {
log.Debug("vote called") log.Debug("vote called, with up: %n", up)
uri := fmt.Sprintf("%s/rest/api/2/issue/%s/votes", c.endpoint, issue) uri := fmt.Sprintf("%s/rest/api/2/issue/%s/votes", c.endpoint, issue)
if c.getOptBool("dryrun", false) { if c.getOptBool("dryrun", false) {
if up {
log.Debug("POST: %s", "") log.Debug("POST: %s", "")
log.Debug("Dryrun mode, skipping POST") log.Debug("Dryrun mode, skipping POST")
return nil
}
resp, err := c.post(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)
}
} else { } 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("DELETE: %s", "")
log.Debug("Dryrun mode, skipping DELETE") log.Debug("Dryrun mode, skipping DELETE")
}
return nil return nil
} }
resp, err := c.delete(uri) var resp *http.Response
var err error
if up {
resp, err = c.post(uri, "")
} else {
resp, err = c.delete(uri)
}
if err != nil { if err != nil {
return err return err
} }
@@ -468,7 +451,11 @@ func (c *Cli) CmdUnvote(issue string) error {
} else { } else {
logBuffer := bytes.NewBuffer(make([]byte, 0)) logBuffer := bytes.NewBuffer(make([]byte, 0))
resp.Write(logBuffer) resp.Write(logBuffer)
err := fmt.Errorf("Unexpected Response From DELETE") if up {
err = fmt.Errorf("Unexpected Response From POST")
} else {
err = fmt.Errorf("Unexpected Response From DELETE")
}
log.Error("%s:\n%s", err, logBuffer) log.Error("%s:\n%s", err, logBuffer)
return err return err
} }
+7 -5
View File
@@ -57,6 +57,7 @@ Usage:
jira DUPLICATE dups ISSUE jira DUPLICATE dups ISSUE
jira BLOCKER blocks ISSUE jira BLOCKER blocks ISSUE
jira watch ISSUE [-w WATCHER] jira watch ISSUE [-w WATCHER]
jira vote ISSUE [--down]
jira (trans|transition) TRANSITION ISSUE [--noedit] <Edit Options> jira (trans|transition) TRANSITION ISSUE [--noedit] <Edit Options>
jira ack ISSUE [--edit] <Edit Options> jira ack ISSUE [--edit] <Edit Options>
jira close ISSUE [--edit] <Edit Options> jira close ISSUE [--edit] <Edit Options>
@@ -156,7 +157,6 @@ Command Options:
"req": "request", "req": "request",
"request": "request", "request": "request",
"vote": "vote", "vote": "vote",
"unvote": "unvote",
} }
defaults := map[string]interface{}{ defaults := map[string]interface{}{
@@ -208,6 +208,7 @@ Command Options:
"M|method=s": setopt, "M|method=s": setopt,
"S|saveFile=s": setopt, "S|saveFile=s": setopt,
"Q|quiet": setopt, "Q|quiet": setopt,
"down": setopt,
}) })
if err := op.ProcessAll(os.Args[1:]); err != nil { if err := op.ProcessAll(os.Args[1:]); err != nil {
@@ -408,10 +409,11 @@ Command Options:
err = c.CmdView(args[0]) err = c.CmdView(args[0])
case "vote": case "vote":
requireArgs(1) requireArgs(1)
err = c.CmdVote(args[0]) if val, ok := opts["down"]; ok {
case "unvote": err = c.CmdVote(args[0], !val.(bool))
requireArgs(1) } else {
err = c.CmdUnvote(args[0]) err = c.CmdVote(args[0], true)
}
case "request": case "request":
requireArgs(1) requireArgs(1)
data := "" data := ""