Skip to content
This repository has been archived by the owner on Sep 21, 2023. It is now read-only.

Implement adding links to the Github issues #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
49 changes: 49 additions & 0 deletions lib/clients/jira.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
11 changes: 11 additions & 0 deletions lib/issues.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lib

import (
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -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 {
Expand Down