From 6abc7de59426778f2d799928201ff6a3f33a0100 Mon Sep 17 00:00:00 2001 From: Bernhard Kaszt Date: Tue, 31 Jul 2018 16:18:33 +0200 Subject: [PATCH] Implement adding links to the Github issues --- lib/clients/jira.go | 49 +++++++++++++++++++++++++++++++++++++++++++++ lib/issues.go | 11 ++++++++++ 2 files changed, 60 insertions(+) diff --git a/lib/clients/jira.go b/lib/clients/jira.go index 33f7b6a..4b9436b 100644 --- a/lib/clients/jira.go +++ b/lib/clients/jira.go @@ -51,6 +51,7 @@ type JIRAClient interface { UpdateIssue(issue jira.Issue) (jira.Issue, error) CreateComment(issue jira.Issue, comment github.IssueComment, github GitHubClient) (jira.Comment, error) UpdateComment(issue jira.Issue, id string, comment github.IssueComment, github GitHubClient) (jira.Comment, error) + CreateIssueRemoteLink(issue jira.Issue, remoteLink RemoteLink) (error) } // NewJIRAClient creates a new JIRAClient and configures it with @@ -207,6 +208,15 @@ func (j realJIRAClient) CreateIssue(issue jira.Issue) (jira.Issue, error) { return *is, nil } +type RemoteLink struct { + Object RemoteLinkObject `json:"object" structs:"object"` +} + +type RemoteLinkObject struct { + Url string `json:"url" structs:"url"` + Title string `json:"title" structs:"title"` +} + // UpdateIssue updates a given issue (identified by the Key field of the provided // issue object) with the fields on the provided issue. It returns the updated // issue as it exists on JIRA. @@ -335,6 +345,29 @@ func (j realJIRAClient) UpdateComment(issue jira.Issue, id string, comment githu return *co, nil } +// CreateIssueLink creates an issue remotelink +func (j realJIRAClient) CreateIssueRemoteLink(issue jira.Issue, remoteLink RemoteLink) (error) { + log := j.config.GetLogger() + + req, err := j.client.NewRequest("POST", fmt.Sprintf("rest/api/2/issue/%s/remotelink", issue.Key), remoteLink) + if err != nil { + log.Errorf("Error creating remotelink: %s", err) + return err + } + + _, res, err := j.request(func() (interface{}, *jira.Response, error) { + res, err := j.client.Do(req, nil) + return nil, res, err + }) + + if err != nil { + log.Errorf("Error creating remotelink: %v", err) + return getErrorBody(j.config, res) + } + + return nil +} + // request takes an API function from the JIRA library // and calls it with exponential backoff. If the function succeeds, it // returns the expected value and the JIRA API response, as well as a nil @@ -632,3 +665,19 @@ func (j dryrunJIRAClient) request(f func() (interface{}, *jira.Response, error)) return ret, res, backoffErr } + +// CreateIssue prints out the fields that would be set on a new issue were +// it to be created according to the provided issue object. It returns the +// provided issue object as-is. +func (j dryrunJIRAClient) CreateIssueRemoteLink(issue jira.Issue, remoteLink RemoteLink) (error) { + log := j.config.GetLogger() + + log.Info("") + log.Info("Create new JIRA issue remotelink:") + log.Infof(" Issue: %s", issue.Key) + log.Infof(" Url: %s", remoteLink.Object.Url) + log.Infof(" Title: %d", remoteLink.Object.Title) + log.Info("") + + return nil +} diff --git a/lib/issues.go b/lib/issues.go index 20bdf38..0dca000 100644 --- a/lib/issues.go +++ b/lib/issues.go @@ -1,6 +1,7 @@ package lib import ( + "strconv" "strings" "time" @@ -209,6 +210,16 @@ func CreateIssue(config cfg.Config, issue github.Issue, ghClient clients.GitHubC return err } + user, repo := config.GetRepo() + url := "https://github.com/" + user + "/" + repo + "/issues/" + strconv.Itoa(issue.GetNumber()) + remoteLink := clients.RemoteLink { + Object: clients.RemoteLinkObject { + Url: url, + Title: url, + }, + } + jClient.CreateIssueRemoteLink(jIssue, remoteLink) + log.Debugf("Created JIRA issue %s!", jIssue.Key) if err := CompareComments(config, issue, jIssue, ghClient, jClient); err != nil {