Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'vote' and 'unvote' #26

Merged
merged 3 commits into from
Jan 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,24 @@ func (c *Cli) put(uri string, content string) (*http.Response, error) {
return c.makeRequestWithContent("PUT", uri, content)
}

func (c *Cli) delete(uri string) (*http.Response, error) {
method := "DELETE"
req, _ := http.NewRequest(method, uri, nil)
log.Info("%s %s", req.Method, req.URL.String())
if resp, err := c.makeRequest(req); err != nil {
return nil, err
} else {
if resp.StatusCode == 401 {
if err := c.CmdLogin(); err != nil {
return nil, err
}
req, _ = http.NewRequest(method, uri, nil)
return c.makeRequest(req)
}
return resp, err
}
}

func (c *Cli) makeRequestWithContent(method string, uri string, content string) (*http.Response, error) {
buffer := bytes.NewBufferString(content)
req, _ := http.NewRequest(method, uri, buffer)
Expand Down
43 changes: 43 additions & 0 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,49 @@ func (c *Cli) CmdWatch(issue string) error {
return nil
}

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) {
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
}
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
}
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 {
logBuffer := bytes.NewBuffer(make([]byte, 0))
resp.Write(logBuffer)
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
}
return nil
}

func (c *Cli) CmdTransition(issue string, trans string) error {
log.Debug("transition called")
uri := fmt.Sprintf("%s/rest/api/2/issue/%s/transitions?expand=transitions.fields", c.endpoint, issue)
Expand Down
10 changes: 10 additions & 0 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 @@ -155,6 +156,7 @@ Command Options:
"login": "login",
"req": "request",
"request": "request",
"vote": "vote",
}

defaults := map[string]interface{}{
Expand Down Expand Up @@ -206,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 @@ -404,6 +407,13 @@ Command Options:
case "view":
requireArgs(1)
err = c.CmdView(args[0])
case "vote":
requireArgs(1)
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