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

[GH-167] Render JIRA webhook comments as quoted messages #467

Merged
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
25 changes: 13 additions & 12 deletions server/webhook_http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type testWebhookWrapper struct {
func (wh testWebhookWrapper) Events() StringSet {
return wh.Webhook.Events()
}

func (wh *testWebhookWrapper) PostToChannel(p *Plugin, channelId, fromUserId string) (*model.Post, int, error) {
post, status, err := wh.Webhook.PostToChannel(p, channelId, fromUserId)
if post != nil {
Expand Down Expand Up @@ -308,14 +309,14 @@ func TestWebhookHTTP(t *testing.T) {
Request: testWebhookRequest("webhook-cloud-comment-created.json"),
ExpectedSlackAttachment: true,
ExpectedHeadline: "Test User **commented** on story [TES-41: Unit test summary 1](https://some-instance-test.atlassian.net/browse/TES-41)",
ExpectedText: "Added a comment",
ExpectedText: "> Added a comment",
CurrentInstance: true,
},
"CLOUD comment updated": {
Request: testWebhookRequest("webhook-cloud-comment-updated.json"),
ExpectedSlackAttachment: true,
ExpectedHeadline: "Test User **edited comment** in story [TES-41: Unit test summary 1](https://some-instance-test.atlassian.net/browse/TES-41)",
ExpectedText: "Added a comment, then edited it",
ExpectedText: "> Added a comment, then edited it",
CurrentInstance: true,
},
"CLOUD comment deleted": {
Expand All @@ -327,14 +328,14 @@ func TestWebhookHTTP(t *testing.T) {
Request: testWebhookRequest("webhook-server-issue-updated-commented-1.json"),
ExpectedSlackAttachment: true,
ExpectedHeadline: "Lev Brouk **commented** on story [PRJX-14: As a user, I can find important items on the board by using the customisable ...](http://sales-jira.centralus.cloudapp.azure.com:8080/browse/PRJX-14)",
ExpectedText: "unik",
ExpectedText: "> unik",
CurrentInstance: true,
},
"SERVER (old version) issue commented (no issue_event_type_name)": {
Request: testWebhookRequest("webhook-server-old-issue-updated-no-event-type-commented.json"),
ExpectedSlackAttachment: true,
ExpectedHeadline: "Lev Brouk **commented** on story [PRJX-14: As a user, I can find important items on the board by using the customisable ...](http://sales-jira.centralus.cloudapp.azure.com:8080/browse/PRJX-14)",
ExpectedText: "unik",
ExpectedText: "> unik",
CurrentInstance: true,
},
"SERVER issue comment deleted": {
Expand All @@ -353,21 +354,21 @@ func TestWebhookHTTP(t *testing.T) {
Request: testWebhookRequest("webhook-server-issue-updated-comment-edited.json"),
ExpectedSlackAttachment: true,
ExpectedHeadline: "Lev Brouk **edited comment** in story [PRJX-14: As a user, I can find important items on the board by using the customisable ...](http://sales-jira.centralus.cloudapp.azure.com:8080/browse/PRJX-14)",
ExpectedText: "and higher eeven higher",
ExpectedText: "> and higher eeven higher",
CurrentInstance: true,
},
"SERVER (old version) issue comment edited (no issue_event_type_name)": {
Request: testWebhookRequest("webhook-server-old-issue-updated-no-event-type-comment-edited.json"),
ExpectedSlackAttachment: true,
ExpectedHeadline: "Lev Brouk **edited comment** in story [PRJX-14: As a user, I can find important items on the board by using the customisable ...](http://sales-jira.centralus.cloudapp.azure.com:8080/browse/PRJX-14)",
ExpectedText: "and higher eeven higher",
ExpectedText: "> and higher eeven higher",
CurrentInstance: true,
},
"SERVER issue commented notify": {
Request: testWebhookRequest("webhook-server-issue-updated-commented-2.json"),
ExpectedSlackAttachment: true,
ExpectedHeadline: "Test User **commented** on improvement [PRJA-42: test for notifications](http://test-server.azure.com:8080/browse/PRJA-42)",
ExpectedText: "This is a test comment. We should act on it right away.",
ExpectedText: "> This is a test comment. We should act on it right away.",
CurrentInstance: true,
},
"SERVER: ignored comment created": {
Expand Down Expand Up @@ -543,14 +544,14 @@ func TestWebhookHTTP(t *testing.T) {
Request: testWebhookRequest("webhook-cloud-comment-created.json"),
ExpectedSlackAttachment: true,
ExpectedHeadline: "Test User **commented** on story [TES-41: Unit test summary 1](https://some-instance-test.atlassian.net/browse/TES-41)",
ExpectedText: "Added a comment",
ExpectedText: "> Added a comment",
CurrentInstance: false,
},
"CLOUD comment updated - no Instance": {
Request: testWebhookRequest("webhook-cloud-comment-updated.json"),
ExpectedSlackAttachment: true,
ExpectedHeadline: "Test User **edited comment** in story [TES-41: Unit test summary 1](https://some-instance-test.atlassian.net/browse/TES-41)",
ExpectedText: "Added a comment, then edited it",
ExpectedText: "> Added a comment, then edited it",
CurrentInstance: false,
},
"CLOUD comment deleted - no Instance": {
Expand All @@ -562,7 +563,7 @@ func TestWebhookHTTP(t *testing.T) {
Request: testWebhookRequest("webhook-server-issue-updated-commented-1.json"),
ExpectedSlackAttachment: true,
ExpectedHeadline: "Lev Brouk **commented** on story [PRJX-14: As a user, I can find important items on the board by using the customisable ...](http://sales-jira.centralus.cloudapp.azure.com:8080/browse/PRJX-14)",
ExpectedText: "unik",
ExpectedText: "> unik",
CurrentInstance: false,
},
"SERVER issue comment deleted - no Instance": {
Expand All @@ -575,14 +576,14 @@ func TestWebhookHTTP(t *testing.T) {
Request: testWebhookRequest("webhook-server-issue-updated-comment-edited.json"),
ExpectedSlackAttachment: true,
ExpectedHeadline: "Lev Brouk **edited comment** in story [PRJX-14: As a user, I can find important items on the board by using the customisable ...](http://sales-jira.centralus.cloudapp.azure.com:8080/browse/PRJX-14)",
ExpectedText: "and higher eeven higher",
ExpectedText: "> and higher eeven higher",
CurrentInstance: false,
},
"SERVER issue commented notify - no Instance": {
Request: testWebhookRequest("webhook-server-issue-updated-commented-2.json"),
ExpectedSlackAttachment: true,
ExpectedHeadline: "Test User **commented** on improvement [PRJA-42: test for notifications](http://test-server.azure.com:8080/browse/PRJA-42)",
ExpectedText: "This is a test comment. We should act on it right away.",
ExpectedText: "> This is a test comment. We should act on it right away.",
CurrentInstance: false,
},
"SERVER: ignored comment created - no Instance": {
Expand Down
8 changes: 6 additions & 2 deletions server/webhook_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ func parseWebhookCommentCreated(jwh *JiraWebhook) (Webhook, error) {
JiraWebhook: jwh,
eventTypes: NewStringSet(eventCreatedComment),
headline: fmt.Sprintf("%s **commented** on %s", commentAuthor, jwh.mdKeySummaryLink()),
text: truncate(jwh.Comment.Body, 3000),
text: truncate(quoteIssueComment(jwh.Comment.Body), 3000),
}

appendCommentNotifications(wh, "**mentioned** you in a new comment on")
Expand Down Expand Up @@ -309,6 +309,10 @@ func appendCommentNotifications(wh *webhook, verb string) {
})
}

func quoteIssueComment(comment string) string {
return fmt.Sprintf("> %s", comment)
}

func parseWebhookCommentDeleted(jwh *JiraWebhook) (Webhook, error) {
if jwh.Issue.ID == "" {
return nil, ErrWebhookIgnored
Expand Down Expand Up @@ -341,7 +345,7 @@ func parseWebhookCommentUpdated(jwh *JiraWebhook) (Webhook, error) {
JiraWebhook: jwh,
eventTypes: NewStringSet(eventUpdatedComment),
headline: fmt.Sprintf("%s **edited comment** in %s", mdUser(&jwh.Comment.UpdateAuthor), jwh.mdKeySummaryLink()),
text: truncate(jwh.Comment.Body, 3000),
text: truncate(quoteIssueComment(jwh.Comment.Body), 3000),
}

appendCommentNotifications(wh, "**mentioned** you in a comment update on")
Expand Down
19 changes: 19 additions & 0 deletions server/webhook_parser_misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package main
import (
"io/ioutil"
"os"
"strings"
"testing"

jira "github.com/andygrunwald/go-jira"
Expand Down Expand Up @@ -117,3 +118,21 @@ func TestJiraLink(t *testing.T) {
jwh.Issue.Self = "http://localhost:8080/foo/bar/rest/api/2/issue/10006"
assert.Equal(t, "[1](http://localhost:8080/foo/bar/QWERTY)", jwh.mdJiraLink("1", "/QWERTY"))
}

func TestWebhookQuotedComment(t *testing.T) {
for _, value := range []string{
"testdata/webhook-server-issue-updated-commented-3.json",
"testdata/webhook-server-issue-updated-comment-edited.json",
} {
f, err := os.Open(value)
require.NoError(t, err)
defer f.Close()
bb, err := ioutil.ReadAll(f)
require.Nil(t, err)
wh, err := ParseWebhook(bb)
require.NoError(t, err)
w := wh.(*webhook)
require.NotNil(t, w)
assert.True(t, strings.HasPrefix(w.text, ">"))
}
}