Skip to content

Commit

Permalink
Initial support for remote Links (web links)
Browse files Browse the repository at this point in the history
This adds a new command "remotelink" which permits adding a remote (or
web link) to an issue.

Fixes go-jira#112.

Signed-off-by: Jonathan Dowland <jon@dow.land>
  • Loading branch information
jmtd committed Feb 18, 2021
1 parent d7c9202 commit 778f3ae
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
34 changes: 34 additions & 0 deletions issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,40 @@ func LinkIssues(ua HttpClient, endpoint string, lip LinkIssueProvider) error {
return responseError(resp)
}

// https://developer.atlassian.com/cloud/jira/platform/rest/v2/api-group-issue-remote-links/#api-rest-api-2-issue-issueidorkey-remotelink-post
func (j *Jira) LinkRemoteIssue(issue string, url string, title string) error {
return LinkRemoteIssue(j.UA, j.Endpoint, issue, url, title)
}

func LinkRemoteIssue(ua HttpClient, endpoint string, issue string, url string, title string) error {
req := struct {
Object struct {
Url string `json:"url"`
Title string `json:"title"`
} `json:"object"`
}{}
req.Object.Url = url
req.Object.Title = title

encoded, err := json.Marshal(req)
if err != nil {
return err
}

uri := URLJoin(endpoint, "rest/api/2/issue", issue, "remotelink")
resp, err := ua.Post(uri, "application/json", bytes.NewBuffer(encoded))
if err != nil {
return err
}
defer resp.Body.Close()

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


// https://docs.atlassian.com/jira/REST/cloud/#api/2/issue-getTransitions
func (j *Jira) GetIssueTransitions(issue string) (*jiradata.TransitionsMeta, error) {
return GetIssueTransitions(j.UA, j.Endpoint, issue)
Expand Down
1 change: 1 addition & 0 deletions jiracmd/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func RegisterAllCommands() {
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "fields", Entry: CmdFieldsRegistry()})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "in-progress", Entry: CmdTransitionRegistry("Progress"), Aliases: []string{"prog", "progress"}})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "issuelink", Entry: CmdIssueLinkRegistry()})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "remotelink", Entry: CmdRemoteLinkRegistry()})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "issuelinktypes", Entry: CmdIssueLinkTypesRegistry()})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "issuetypes", Entry: CmdIssueTypesRegistry()})
jiracli.RegisterCommand(jiracli.CommandRegistry{Command: "labels add", Entry: CmdLabelsAddRegistry()})
Expand Down
56 changes: 56 additions & 0 deletions jiracmd/remotelink.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package jiracmd

import (
"github.com/coryb/figtree"
"github.com/coryb/oreo"

"github.com/go-jira/jira"
"github.com/go-jira/jira/jiracli"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)

type RemoteLinkOptions struct {
jiracli.CommonOptions `yaml:",inline" json:",inline" figtree:",inline"`
Issue string `yaml:"issue,omitempty" json:"issue,omitempty"`
Project string `yaml:"project,omitempty" json:"project,omitempty"`
// There is no existing jiradata definition for RemoteObject
URL string
Title string
}

func CmdRemoteLinkRegistry() *jiracli.CommandRegistryEntry {
opts := RemoteLinkOptions {}
return &jiracli.CommandRegistryEntry{
"Link an issue to a remote URI",
func(fig *figtree.FigTree, cmd *kingpin.CmdClause) error {
jiracli.LoadConfigs(cmd, fig, &opts)
return CmdRemoteLinkUsage(cmd, &opts)
},
func(o *oreo.Client, globals *jiracli.GlobalOptions) error {
opts.Issue = jiracli.FormatIssue(opts.Issue, opts.Project)
return CmdRemoteLink(o, globals, &opts)
},
}
}

func CmdRemoteLinkUsage(cmd *kingpin.CmdClause, opts *RemoteLinkOptions) error {
jiracli.BrowseUsage(cmd, &opts.CommonOptions)
jiracli.EditorUsage(cmd, &opts.CommonOptions)
jiracli.TemplateUsage(cmd, &opts.CommonOptions)

cmd.Arg("ISSUE", "issue").Required().StringVar(&opts.Issue)
cmd.Arg("TITLE", "Link title").Required().StringVar(&opts.Title)
cmd.Arg("URL", "Link URL").Required().StringVar(&opts.URL)

return nil
}

func CmdRemoteLink(o *oreo.Client, globals *jiracli.GlobalOptions, opts *RemoteLinkOptions) error {
if err := jira.LinkRemoteIssue(o, globals.Endpoint.Value, opts.Issue, opts.URL, opts.Title); err != nil {
return err
}
// unhandled if !globals.Quiet.Value {
// unhandled if opts.Browse.Value {

return nil
}

0 comments on commit 778f3ae

Please sign in to comment.