diff --git a/server/issue.go b/server/issue.go index d3911cc65..bc5902e96 100644 --- a/server/issue.go +++ b/server/issue.go @@ -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 { diff --git a/server/webhook.go b/server/webhook.go index 0cdfbf30b..6c29c6c84 100644 --- a/server/webhook.go +++ b/server/webhook.go @@ -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) } diff --git a/server/webhook_jira.go b/server/webhook_jira.go index c27a045ef..c7f90fdc7 100644 --- a/server/webhook_jira.go +++ b/server/webhook_jira.go @@ -8,6 +8,8 @@ import ( "strings" "github.com/andygrunwald/go-jira" + + "github.com/mattermost/mattermost-plugin-jira/server/utils/types" ) type JiraWebhook struct { @@ -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") diff --git a/server/webhook_worker.go b/server/webhook_worker.go index 982d7b721..a008e665f 100644 --- a/server/webhook_worker.go +++ b/server/webhook_worker.go @@ -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