PLT-7212: fix missing webhook post attachments (#7011)
* fix missing webhook post attachments * make ProcessSlackAttachments return a new slice instead of modifying it
Этот коммит содержится в:
коммит произвёл
Christopher Speller
родитель
dc884983e6
Коммит
df1ff4ec97
47
app/webhook_test.go
Обычный файл
47
app/webhook_test.go
Обычный файл
@@ -0,0 +1,47 @@
|
|||||||
|
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
|
||||||
|
// See License.txt for license information.
|
||||||
|
|
||||||
|
package app
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/mattermost/platform/model"
|
||||||
|
"github.com/mattermost/platform/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCreateWebhookPost(t *testing.T) {
|
||||||
|
th := Setup().InitBasic()
|
||||||
|
defer TearDown()
|
||||||
|
|
||||||
|
enableIncomingHooks := utils.Cfg.ServiceSettings.EnableIncomingWebhooks
|
||||||
|
defer func() {
|
||||||
|
utils.Cfg.ServiceSettings.EnableIncomingWebhooks = enableIncomingHooks
|
||||||
|
utils.SetDefaultRolesBasedOnConfig()
|
||||||
|
}()
|
||||||
|
utils.Cfg.ServiceSettings.EnableIncomingWebhooks = true
|
||||||
|
utils.SetDefaultRolesBasedOnConfig()
|
||||||
|
|
||||||
|
hook, err := CreateIncomingWebhookForChannel(th.BasicUser.Id, th.BasicChannel, &model.IncomingWebhook{ChannelId: th.BasicChannel.Id})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err.Error())
|
||||||
|
}
|
||||||
|
defer DeleteIncomingWebhook(hook.Id)
|
||||||
|
|
||||||
|
post, err := CreateWebhookPost(hook.UserId, hook.TeamId, th.BasicChannel.Id, "foo", "user", "http://iconurl", model.StringInterface{
|
||||||
|
"attachments": []*model.SlackAttachment{
|
||||||
|
&model.SlackAttachment{
|
||||||
|
Text: "text",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}, model.POST_SLACK_ATTACHMENT)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, k := range []string{"from_webhook", "attachments"} {
|
||||||
|
if _, ok := post.Props[k]; !ok {
|
||||||
|
t.Fatal(k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -14,12 +14,12 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type CommandResponse struct {
|
type CommandResponse struct {
|
||||||
ResponseType string `json:"response_type"`
|
ResponseType string `json:"response_type"`
|
||||||
Text string `json:"text"`
|
Text string `json:"text"`
|
||||||
Username string `json:"username"`
|
Username string `json:"username"`
|
||||||
IconURL string `json:"icon_url"`
|
IconURL string `json:"icon_url"`
|
||||||
GotoLocation string `json:"goto_location"`
|
GotoLocation string `json:"goto_location"`
|
||||||
Attachments SlackAttachments `json:"attachments"`
|
Attachments []*SlackAttachment `json:"attachments"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *CommandResponse) ToJson() string {
|
func (o *CommandResponse) ToJson() string {
|
||||||
@@ -40,7 +40,7 @@ func CommandResponseFromJson(data io.Reader) *CommandResponse {
|
|||||||
}
|
}
|
||||||
|
|
||||||
o.Text = ExpandAnnouncement(o.Text)
|
o.Text = ExpandAnnouncement(o.Text)
|
||||||
o.Attachments.Process()
|
o.Attachments = ProcessSlackAttachments(o.Attachments)
|
||||||
|
|
||||||
return &o
|
return &o
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,13 +28,13 @@ type IncomingWebhook struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type IncomingWebhookRequest struct {
|
type IncomingWebhookRequest struct {
|
||||||
Text string `json:"text"`
|
Text string `json:"text"`
|
||||||
Username string `json:"username"`
|
Username string `json:"username"`
|
||||||
IconURL string `json:"icon_url"`
|
IconURL string `json:"icon_url"`
|
||||||
ChannelName string `json:"channel"`
|
ChannelName string `json:"channel"`
|
||||||
Props StringInterface `json:"props"`
|
Props StringInterface `json:"props"`
|
||||||
Attachments SlackAttachments `json:"attachments"`
|
Attachments []*SlackAttachment `json:"attachments"`
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *IncomingWebhook) ToJson() string {
|
func (o *IncomingWebhook) ToJson() string {
|
||||||
@@ -209,7 +209,7 @@ func IncomingWebhookRequestFromJson(data io.Reader) *IncomingWebhookRequest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
o.Text = ExpandAnnouncement(o.Text)
|
o.Text = ExpandAnnouncement(o.Text)
|
||||||
o.Attachments.Process()
|
o.Attachments = ProcessSlackAttachments(o.Attachments)
|
||||||
|
|
||||||
return o
|
return o
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,8 +33,6 @@ type SlackAttachmentField struct {
|
|||||||
Short bool `json:"short"`
|
Short bool `json:"short"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type SlackAttachments []*SlackAttachment
|
|
||||||
|
|
||||||
// To mention @channel via a webhook in Slack, the message should contain
|
// To mention @channel via a webhook in Slack, the message should contain
|
||||||
// <!channel>, as explained at the bottom of this article:
|
// <!channel>, as explained at the bottom of this article:
|
||||||
// https://get.slack.help/hc/en-us/articles/202009646-Making-announcements
|
// https://get.slack.help/hc/en-us/articles/202009646-Making-announcements
|
||||||
@@ -51,9 +49,9 @@ func ExpandAnnouncement(text string) string {
|
|||||||
// can be found in the text attribute, or in the pretext, text, title and value
|
// can be found in the text attribute, or in the pretext, text, title and value
|
||||||
// attributes of the attachment structure. The Slack attachment structure is
|
// attributes of the attachment structure. The Slack attachment structure is
|
||||||
// documented here: https://api.slack.com/docs/attachments
|
// documented here: https://api.slack.com/docs/attachments
|
||||||
func (a *SlackAttachments) Process() {
|
func ProcessSlackAttachments(a []*SlackAttachment) []*SlackAttachment {
|
||||||
var nonNilAttachments []*SlackAttachment
|
var nonNilAttachments []*SlackAttachment
|
||||||
for _, attachment := range *a {
|
for _, attachment := range a {
|
||||||
if attachment == nil {
|
if attachment == nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -77,5 +75,5 @@ func (a *SlackAttachments) Process() {
|
|||||||
}
|
}
|
||||||
attachment.Fields = nonNilFields
|
attachment.Fields = nonNilFields
|
||||||
}
|
}
|
||||||
*a = nonNilAttachments
|
return nonNilAttachments
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,8 +10,8 @@ func TestExpandAnnouncement(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSlackAnnouncementProcess(t *testing.T) {
|
func TestProcessSlackAnnouncement(t *testing.T) {
|
||||||
attachments := SlackAttachments{
|
attachments := []*SlackAttachment{
|
||||||
{
|
{
|
||||||
Pretext: "<!channel> pretext",
|
Pretext: "<!channel> pretext",
|
||||||
Text: "<!channel> text",
|
Text: "<!channel> text",
|
||||||
@@ -25,7 +25,7 @@ func TestSlackAnnouncementProcess(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}, nil,
|
}, nil,
|
||||||
}
|
}
|
||||||
attachments.Process()
|
attachments = ProcessSlackAttachments(attachments)
|
||||||
if len(attachments) != 1 || len(attachments[0].Fields) != 1 {
|
if len(attachments) != 1 || len(attachments[0].Fields) != 1 {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user