Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue: Comment notification not working after implementing OAuth2 and Added PKCE code for OAuth2 #953

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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
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
}

Comment on lines +739 to +762
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did this function exist somewhere else earlier? Was it deleted at some point? I'm asking because there is no red diff related to this function in this commit

Copy link
Contributor Author

@raghavaggarwal2308 raghavaggarwal2308 Jul 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mickmister Yes, it was removed earlier.

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{}{})
Copy link
Contributor

@mickmister mickmister Jul 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering if this works for all 3 Jira instance types. This is my main remaining concern here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mickmister I have tested this code on Jira cloud-oauth and server instances but since the Jira JWT is not working for us, we were not able to test it there.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I'm hesitant to introduce this change for Jira server. We need to make sure it works on Jira server v7 before release. If it works for Jira 9, it likely works for Jira 7, but we should avoid a regression on Jira server for something related to Jira cloud. Maybe we can wrap this in a conditional block "if cloud" etc. Please let me know your thoughts on risks of both options if you can

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mickmister Added the conditional block

} 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