Skip to content

Commit

Permalink
[MI-3282] Added logic to expand issue and issue with DM notification …
Browse files Browse the repository at this point in the history
…for comment webhook notification (#58)

* [MI-3282] Added logic to expand issue in comment webhook notification

* [MI-3282] Fix lint errors

* [MI-3315] Fixed issue: comment DM notification not working due to invalid API call

* [MI-3282] Review fixes

* [MI-3282] Review fix
  • Loading branch information
raghavaggarwal2308 authored Jul 25, 2023
1 parent fdbdc0d commit 0186841
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
24 changes: 24 additions & 0 deletions server/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,30 @@ func getIssueCustomFieldValue(issue *jira.Issue, key string) StringSet {
return nil
}

func (p *Plugin) getIssueDataForCloudWebhook(instance Instance, issueKey string) (*jira.Issue, error) {
ci, ok := instance.(*cloudInstance)
if !ok {
return nil, errors.Errorf("must be a Jira cloud instance, is %s", instance.Common().Type)
}

jiraClient, err := ci.getClientForBot()
if err != nil {
return nil, err
}

issue, resp, err := jiraClient.Issue.Get(issueKey, nil)
if err != nil {
switch {
case resp == nil:
return nil, errors.WithMessage(userFriendlyJiraError(nil, err), "request to Jira failed")
case resp.StatusCode == http.StatusNotFound || resp.StatusCode == http.StatusUnauthorized:
return nil, errors.New(`we couldn't find the issue key, or the cloud "bot" client does not have the appropriate permissions to view the issue`)
}
}

return issue, nil
}

func getIssueFieldValue(issue *jira.Issue, key string) StringSet {
key = strings.ToLower(key)
switch key {
Expand Down
2 changes: 1 addition & 1 deletion server/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (wh *webhook) PostNotifications(p *Plugin, instanceID types.ID) ([]*model.P

isCommentEvent := wh.Events().Intersection(commentEvents).Len() > 0
if isCommentEvent {
err = client.RESTGet(notification.commentSelf, nil, &struct{}{})
err = client.RESTGet(fmt.Sprintf("/2/issue/%s/comment/%s", wh.Issue.ID, wh.Comment.ID), nil, &struct{}{})
} else {
_, err = client.GetIssue(wh.Issue.ID, nil)
}
Expand Down
46 changes: 46 additions & 0 deletions server/webhook_jira.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"strings"

"github.com/andygrunwald/go-jira"

"github.com/mattermost/mattermost-plugin-jira/server/utils/types"
)

type JiraWebhook struct {
Expand All @@ -29,6 +31,50 @@ type JiraWebhook struct {
IssueEventTypeName string `json:"issue_event_type_name"`
}

func (jwh *JiraWebhook) expandIssue(p *Plugin, instanceID types.ID) error {
instance, err := p.instanceStore.LoadInstance(instanceID)
if err != nil {
return err
}

// Jira Cloud comment event. We need to fetch issue data because it is not expanded in webhook payload.
isCommentEvent := jwh.WebhookEvent == commentCreated || jwh.WebhookEvent == commentUpdated || jwh.WebhookEvent == commentDeleted
if isCommentEvent && instance.Common().IsCloudInstance() {
if _, ok := instance.(*cloudInstance); ok {
issue, err := p.getIssueDataForCloudWebhook(instance, jwh.Issue.ID)
if err != nil {
return err
}

jwh.Issue = *issue
} else if _, ok := instance.(*cloudOAuthInstance); ok {
mmUserID, err := p.userStore.LoadMattermostUserID(instanceID, jwh.Comment.Author.AccountID)
if err != nil {
return err
}

conn, err := p.userStore.LoadConnection(instance.GetID(), mmUserID)
if err != nil {
return err
}

client, err := instance.GetClient(conn)
if err != nil {
return err
}

issue, err := client.GetIssue(jwh.Issue.ID, nil)
if err != nil {
return err
}

jwh.Issue = *issue
}
}

return nil
}

func (jwh *JiraWebhook) mdJiraLink(title, suffix string) string {
// Use Self URL only to extract the full hostname from it
pos := strings.LastIndex(jwh.Issue.Self, "/rest/api")
Expand Down
4 changes: 4 additions & 0 deletions server/webhook_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ func (ww webhookWorker) process(msg *webhookMessage) (err error) {
}

v := wh.(*webhook)
if err = v.JiraWebhook.expandIssue(ww.p, msg.InstanceID); err != nil {
return err
}

channelsSubscribed, err := ww.p.getChannelsSubscribed(v, msg.InstanceID)
if err != nil {
return err
Expand Down

0 comments on commit 0186841

Please sign in to comment.