Skip to content
Merged
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
14 changes: 14 additions & 0 deletions server/templaterenderer/issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,18 @@ func TestIssueTemplates(t *testing.T) {
require.NoError(t, err)
require.Equal(t, expected, actual)
})

t.Run("RenderIssueCreatedEventNotificationForSubscribedChannels_WithImage", func(t *testing.T) {
// Test that images in issue descriptions are properly converted to markdown
// and don't leave orphan '>' blockquote characters
expected := "\n#### Bug with image attachment" +
"\n##### [\\[mattermost-plugin-bitbucket#2\\]](https://bitbucket.org/mattermost/mattermost-plugin-bitbucket/issues/2/bug-with-image)" +
"\n#new-issue by @testMmUser:" +
"\n>Here is a screenshot:![screenshot](https://bitbucket.org/repo/attachments/image.png)\n"

actual, err := tr.RenderIssueCreatedEventNotificationForSubscribedChannels(getTestIssueCreatedPayloadWithImage())

require.NoError(t, err)
require.Equal(t, expected, actual)
})
}
16 changes: 16 additions & 0 deletions server/templaterenderer/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,23 @@ func (tr *templateRenderer) init() {
selection.SetText(bitbucketNickname)
})

// Convert img tags to markdown image syntax to preserve images in the output
// This fixes the issue where images in descriptions cause '>' to appear in posts
doc.Find("img").Each(func(i int, selection *goquery.Selection) {
src := selection.AttrOr("src", "")
alt := selection.AttrOr("alt", "image")
if src != "" {
// Replace the img tag with markdown image syntax as plain text
markdownImg := "![" + alt + "](" + src + ")"
selection.ReplaceWithHtml(markdownImg)
} else {
// Remove img tags without src to prevent empty lines
selection.Remove()
}
})

// Text() returns only text, without HTML attributes or tags
// The markdown image syntax we inserted will be preserved since it's plain text
return doc.Text()
}

Expand Down
18 changes: 18 additions & 0 deletions server/templaterenderer/template_fixture_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ func getTestIssue() webhookpayload.Issue {
return issue
}

func getTestIssueWithImage() webhookpayload.Issue {
issue := webhookpayload.Issue{}
issue.ID = 2
issue.Title = "Bug with image attachment"
issue.Content.HTML = "<p>Here is a screenshot:</p><p><img src=\"https://bitbucket.org/repo/attachments/image.png\" alt=\"screenshot\"></p>"
issue.Links.HTML.Href = "https://bitbucket.org/mattermost/mattermost-plugin-bitbucket/issues/2/bug-with-image"

return issue
}

func getTestIssueCreatedPayload() webhookpayload.IssueCreatedPayload {
return webhookpayload.IssueCreatedPayload{
Repository: getTestRepository(),
Expand All @@ -64,6 +74,14 @@ func getTestIssueCreatedPayload() webhookpayload.IssueCreatedPayload {
}
}

func getTestIssueCreatedPayloadWithImage() webhookpayload.IssueCreatedPayload {
return webhookpayload.IssueCreatedPayload{
Repository: getTestRepository(),
Actor: getTestOwnerThatHasMmAccount(),
Issue: getTestIssueWithImage(),
}
}

func getTestIssueUpdatedPayload() webhookpayload.IssueUpdatedPayload {
return webhookpayload.IssueUpdatedPayload{
Repository: getTestRepository(),
Expand Down