Skip to content

Commit

Permalink
Fix NPE if reporter is blank during issue creation (#1033)
Browse files Browse the repository at this point in the history
  • Loading branch information
hanzei authored Mar 13, 2024
1 parent d08f7f7 commit a1ee605
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 8 deletions.
18 changes: 10 additions & 8 deletions server/issue_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ func mdKeySummaryLink(issue *jira.Issue, instance Instance) string {
return fmt.Sprintf("[%s: %s (%s)](%s%s)", issue.Key, issue.Fields.Summary, issue.Fields.Status.Name, instance.GetJiraBaseURL(), "/browse/"+issue.Key)
}

func reporterSummary(issue *jira.Issue) string {
avatarLink := fmt.Sprintf("![avatar](%s =30x30)", issue.Fields.Reporter.AvatarUrls.One6X16)
reporterSummary := avatarLink + " " + issue.Fields.Reporter.Name
func reporterSummary(reporter *jira.User) string {
avatarLink := fmt.Sprintf("![avatar](%s =30x30)", reporter.AvatarUrls.One6X16)
reporterSummary := avatarLink + " " + reporter.Name
return reporterSummary
}

Expand Down Expand Up @@ -103,11 +103,13 @@ func asSlackAttachment(instance Instance, client Client, issue *jira.Issue, show
})
}

fields = append(fields, &model.SlackAttachmentField{
Title: "Reporter",
Value: reporterSummary(issue),
Short: true,
})
if issue.Fields.Reporter != nil {
fields = append(fields, &model.SlackAttachmentField{
Title: "Reporter",
Value: reporterSummary(issue.Fields.Reporter),
Short: true,
})
}

var actions []*model.PostAction
var err error
Expand Down
160 changes: 160 additions & 0 deletions server/issue_parser_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
// See License for license information.
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.

package main

import (
"fmt"
"testing"

jira "github.com/andygrunwald/go-jira"
"github.com/mattermost/mattermost/server/public/model"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestAsSlackAttachment(t *testing.T) {
instance := testInstance2
client := testClient{}

for name, tc := range map[string]struct {
issue *jira.Issue
showActions bool
expectedAttachment *model.SlackAttachment
}{
"minimum data": {
issue: &jira.Issue{
Key: "MM-57208",
Fields: &jira.IssueFields{
Summary: "",
Status: &jira.Status{
Name: "Open",
},
},
},
expectedAttachment: &model.SlackAttachment{
Text: "[MM-57208: (Open)](jiraurl2/browse/MM-57208)",
Color: "#95b7d0",
},
},
"with summary": {
issue: &jira.Issue{
Key: "MM-57208",
Fields: &jira.IssueFields{
Summary: "A Summary",
Status: &jira.Status{
Name: "Open",
},
},
},
expectedAttachment: &model.SlackAttachment{
Text: "[MM-57208: A Summary (Open)](jiraurl2/browse/MM-57208)",
Color: "#95b7d0",
},
},
"with Assignee, Priority and Reporter": {
issue: &jira.Issue{
Key: "MM-57208",
Fields: &jira.IssueFields{
Priority: &jira.Priority{
Name: "Very important",
},
Assignee: &jira.User{
DisplayName: "The Assignee",
},
Description: "A Description",
Summary: "A Summary",
Reporter: &jira.User{
Name: "The Reporter",
AvatarUrls: jira.AvatarUrls{
One6X16: "https://example.org/icon",
},
},
Status: &jira.Status{
Name: "Open",
},
},
},
expectedAttachment: &model.SlackAttachment{
Text: "[MM-57208: A Summary (Open)](jiraurl2/browse/MM-57208)\n\nA Description\n",
Color: "#95b7d0",
Fields: []*model.SlackAttachmentField{
{
Title: "Assignee",
Value: "The Assignee",
Short: true,
},
{
Title: "Priority",
Value: "Very important",
Short: true,
},
{
Title: "Reporter",
Value: "![avatar](https://example.org/icon =30x30) The Reporter",
Short: true,
},
},
},
},
"with actions": {
issue: &jira.Issue{
ID: "some ID",
Key: "MM-57208",
Fields: &jira.IssueFields{
Summary: "",
Status: &jira.Status{
Name: "Open",
},
},
},
showActions: true,
expectedAttachment: &model.SlackAttachment{
Text: "[MM-57208: (Open)](jiraurl2/browse/MM-57208)",
Color: "#95b7d0",
Actions: []*model.PostAction{
{
Type: "select",
Name: "Transition issue",
Options: []*model.PostActionOptions{
{
Value: "To Do",
},
{
Value: "In Progress",
},
{
Value: "In Testing",
},
},
Integration: &model.PostActionIntegration{
URL: fmt.Sprintf("/plugins/%s/api/v2/transition", manifest.Id),
Context: map[string]any{
"issue_key": "some ID",
"instance_id": testInstance2.GetID().String(),
},
},
},
{
Type: "button",
Name: "Share publicly",
Integration: &model.PostActionIntegration{
URL: fmt.Sprintf("/plugins/%s/api/v2/share-issue-publicly", manifest.Id),
Context: map[string]any{
"issue_key": "some ID",
"instance_id": testInstance2.GetID().String(),
},
},
},
},
},
},
} {
t.Run(name, func(t *testing.T) {
attachments, err := asSlackAttachment(instance, client, tc.issue, tc.showActions)
assert.NoError(t, err)
require.Len(t, attachments, 1)
assert.Equal(t, tc.expectedAttachment, attachments[0])
})
}
}

0 comments on commit a1ee605

Please sign in to comment.