Skip to content

Commit

Permalink
add watch command
Browse files Browse the repository at this point in the history
  • Loading branch information
coryb committed Aug 14, 2017
1 parent 8b863d2 commit ec0ac3c
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 83 deletions.
36 changes: 5 additions & 31 deletions cmd/jira/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ func main() {
Command: "rank",
Entry: cli.CmdRankRegistry(),
},
jiracli.CommandRegistry{
Command: "watch",
Entry: cli.CmdWatchRegistry(),
},
}

cli.Register(app, registry)
Expand All @@ -211,29 +215,8 @@ func main() {
if err != nil {
log.Fatalf("%s", err)
}
// user := os.Getenv("USER")
// home := os.Getenv("HOME")
// defaultQueryFields := "summary,created,updated,priority,status,reporter,assignee"
// defaultSort := "priority asc, key"
// defaultMaxResults := 500

// usage := func(ok bool) {
// printer := fmt.Printf
// if !ok {
// printer = func(format string, args ...interface{}) (int, error) {
// return fmt.Fprintf(os.Stderr, format, args...)
// }
// defer func() {
// os.Exit(1)
// }()
// } else {
// defer func() {
// os.Exit(0)
// }()
// }
// output := fmt.Sprintf(`

// Usage:
// jira watch ISSUE [-w WATCHER] [--remove]
// jira comment ISSUE [--noedit] <Edit Options>
// jira (set,add,remove) labels ISSUE [LABEL] ...
// jira take ISSUE
Expand Down Expand Up @@ -269,8 +252,6 @@ func main() {
// -q --query=JQL Jira Query Language expression for the search
// -r --reporter=USER Reporter to search for
// -s --sort=ORDER For list operations, sort issues (default: %s)
// -w --watcher=USER Watcher to add to issue (default: %s)
// or Watcher to search for

// Edit Options:
// -m --comment=COMMENT Comment message for transition
Expand All @@ -292,7 +273,6 @@ func main() {
// }

// jiraCommands := map[string]string{
// "watch": "watch",
// "comment": "comment",
// "label": "labels",
// "labels": "labels",
Expand Down Expand Up @@ -345,7 +325,6 @@ func main() {
// "c|component=s": setopt,
// "a|assignee=s": setopt,
// "i|issuetype=s": setopt,
// "w|watcher=s": setopt,
// "remove": setopt,
// "r|reporter=s": setopt,
// "f|queryfields=s": setopt,
Expand Down Expand Up @@ -455,11 +434,6 @@ func main() {
// switch command {
// case "issuetypes":
// err = c.CmdIssueTypes()
// case "watch":
// requireArgs(1)
// watcher := c.GetOptString("watcher", opts["user"].(string))
// remove := c.GetOptBool("remove", false)
// err = c.CmdWatch(args[0], watcher, remove)
// case "comment":
// requireArgs(1)
// setEditing(true)
Expand Down
30 changes: 30 additions & 0 deletions issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,3 +369,33 @@ func (j *Jira) RankIssues(rrp RankRequestProvider) error {
}
return responseError(resp)
}

// https://docs.atlassian.com/jira/REST/cloud/#api/2/issue-addWatcher
func (j *Jira) IssueAddWatcher(issue, user string) error {
uri := fmt.Sprintf("%s/rest/api/2/issue/%s/watchers", j.Endpoint, issue)
resp, err := j.UA.Post(uri, "application/json", strings.NewReader(fmt.Sprintf("%q", user)))
if err != nil {
return err
}
defer resp.Body.Close()

if resp.StatusCode == 204 {
return nil
}
return responseError(resp)
}

// https://docs.atlassian.com/jira/REST/cloud/#api/2/issue-addWatcher
func (j *Jira) IssueRemoveWatcher(issue, user string) error {
uri := fmt.Sprintf("%s/rest/api/2/issue/%s/watchers?username=%s", j.Endpoint, issue, user)
resp, err := j.UA.Delete(uri)
if err != nil {
return err
}
defer resp.Body.Close()

if resp.StatusCode == 204 {
return nil
}
return responseError(resp)
}
52 changes: 0 additions & 52 deletions jiracli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,58 +116,6 @@ func (jc *JiraCli) Register(app *kingpin.Application, reg []CommandRegistry) {
// return runTemplate(c.getTemplate("components"), data, nil)
// }

// // CmdWatch will add the given watcher to the issue (or remove the watcher
// // given the 'remove' flag)
// func (c *Cli) CmdWatch(issue string, watcher string, remove bool) error {
// log.Debugf("watch called: watcher: %q, remove: %n", watcher, remove)

// var uri string
// json, err := jsonEncode(watcher)
// if err != nil {
// return err
// }

// if c.getOptBool("dryrun", false) {
// if !remove {
// log.Debugf("POST: %s", json)
// log.Debugf("Dryrun mode, skipping POST")
// } else {
// log.Debugf("DELETE: %s", watcher)
// log.Debugf("Dryrun mode, skipping POST")
// }
// return nil
// }

// var resp *http.Response
// if !remove {
// uri = fmt.Sprintf("%s/rest/api/2/issue/%s/watchers", c.endpoint, issue)
// resp, err = c.post(uri, json)
// } else {
// uri = fmt.Sprintf("%s/rest/api/2/issue/%s/watchers?username=%s", c.endpoint, issue, watcher)
// resp, err = c.delete(uri)
// }
// if err != nil {
// return err
// }
// if resp.StatusCode == 204 {
// c.Browse(issue)
// if !c.GetOptBool("quiet", false) {
// fmt.Printf("OK %s %s/browse/%s\n", issue, c.endpoint, issue)
// }
// } else {
// logBuffer := bytes.NewBuffer(make([]byte, 0))
// resp.Write(logBuffer)
// if !remove {
// err = fmt.Errorf("Unexpected Response From POST")
// } else {
// err = fmt.Errorf("Unexpected Response From DELETE")
// }
// log.Errorf("%s:\n%s", err, logBuffer)
// return err
// }
// return nil
// }

// // CmdComment will open up editor with "comment" template and submit
// // YAML output to jira
// func (c *Cli) CmdComment(issue string) error {
Expand Down
74 changes: 74 additions & 0 deletions jiracli/watch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package jiracli

import (
"fmt"

kingpin "gopkg.in/alecthomas/kingpin.v2"
)

type WatchAction int

const (
WatcherAdd WatchAction = iota
WatcherRemove
)

type WatchOptions struct {
GlobalOptions
Issue string
Watcher string
Action WatchAction
}

func (jc *JiraCli) CmdWatchRegistry() *CommandRegistryEntry {
opts := WatchOptions{
GlobalOptions: GlobalOptions{},
Action: WatcherAdd,
}

return &CommandRegistryEntry{
"Add/Remove watcher to issue",
func() error {
return jc.CmdWatch(&opts)
},
func(cmd *kingpin.CmdClause) error {
return jc.CmdWatchUsage(cmd, &opts)
},
}
}

func (jc *JiraCli) CmdWatchUsage(cmd *kingpin.CmdClause, opts *WatchOptions) error {
if err := jc.GlobalUsage(cmd, &opts.GlobalOptions); err != nil {
return err
}
cmd.Flag("remove", "remove watcher from issue").Short('r').PreAction(func(ctx *kingpin.ParseContext) error {
opts.Action = WatcherRemove
return nil
}).Bool()
cmd.Arg("ISSUE", "issue to add watcher").Required().StringVar(&opts.Issue)
cmd.Arg("WATCHER", "username of watcher to add to issue").StringVar(&opts.Watcher)
return nil
}

// CmdWatch will add the given watcher to the issue (or remove the watcher
// with the 'remove' flag)
func (jc *JiraCli) CmdWatch(opts *WatchOptions) error {
if opts.Watcher == "" {
opts.Watcher = opts.User
}
if opts.Action == WatcherAdd {
if err := jc.IssueAddWatcher(opts.Issue, opts.Watcher); err != nil {
return err
}
} else {
if err := jc.IssueRemoveWatcher(opts.Issue, opts.Watcher); err != nil {
return err
}
}

fmt.Printf("OK %s %s/browse/%s\n", opts.Issue, jc.Endpoint, opts.Issue)

// FIXME implement browse

return nil
}

0 comments on commit ec0ac3c

Please sign in to comment.