Skip to content

Commit

Permalink
Amend vote/unvote to be vote/vote --down
Browse files Browse the repository at this point in the history
This simplifies the interface to voting, as per the discussion in
#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.
  • Loading branch information
Mike Pountney committed Jan 24, 2016
1 parent 7b651d7 commit 797edef
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 37 deletions.
51 changes: 19 additions & 32 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,44 +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, "")
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)
}
var resp *http.Response
var err error
if up {
resp, err = c.post(uri, "")
} 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)
}
resp, err := c.delete(uri)
if err != nil {
return err
}
Expand All @@ -468,7 +451,11 @@ func (c *Cli) CmdUnvote(issue string) error {
} else {
logBuffer := bytes.NewBuffer(make([]byte, 0))
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)
return err
}
Expand Down
12 changes: 7 additions & 5 deletions main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -156,7 +157,6 @@ Command Options:
"req": "request",
"request": "request",
"vote": "vote",
"unvote": "unvote",
}

defaults := map[string]interface{}{
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 := ""
Expand Down

0 comments on commit 797edef

Please sign in to comment.