MM-56781: Use correct error in doInviteRemote (#26304)

* Use correct error in doInviteRemote

A Sentry crash report identified this nil pointer dereference:
https://mattermost-mr.sentry.io/issues/4930233067/events/eae438652c7b4335be2bbe19c977f680

Interestingly, the stack trace shows the actual crash happening in the
function deferred above this line, which makes sense, given that it's
accessing the returned value, which is now nil as well. However, the
origin of the crash is here, since it's using the previous appErr, which
at this point is ensured to be nil, instead of the error returned by
InviteRemoteToChannel. This makes the returned value that should have
been generated by responsef to no longer exist.

* Test inviting a remote to a channel shared with us

* Improve error when InviteRemoteToChannel fails

* Clarify that channel has remoteID

Co-authored-by: Doug Lauder <wiggin77@warpmail.net>

* gofmt

---------

Co-authored-by: Doug Lauder <wiggin77@warpmail.net>
Этот коммит содержится в:
Alejandro García Montoro
2024-02-28 15:00:41 +01:00
коммит произвёл GitHub
родитель 6dbdfbbcb4
Коммит 344c5a7528
3 изменённых файлов: 43 добавлений и 1 удалений

Просмотреть файл

@@ -254,7 +254,7 @@ func (sp *ShareProvider) doInviteRemote(a *app.App, c request.CTX, args *model.C
}
if err = a.InviteRemoteToChannel(args.ChannelId, remoteID, args.UserId, true); err != nil {
return responsef(appErr.Error())
return responsef(args.T("api.command_share.invite_remote_to_channel.error", map[string]any{"Error": err.Error()}))
}
return responsef("##### " + args.T("api.command_share.invitation_sent", map[string]any{"Name": rc.DisplayName, "SiteURL": rc.SiteURL}))

Просмотреть файл

@@ -90,4 +90,42 @@ func TestShareProviderDoCommand(t *testing.T) {
})
require.Len(t, channelConvertedMessages, 2) // one msg for share creation, one for unshare.
})
t.Run("invite remote to channel shared with us", func(t *testing.T) {
th := setupForSharedChannels(t).initBasic()
defer th.tearDown()
th.addPermissionToRole(model.PermissionManageSharedChannels.Id, th.BasicUser.Roles)
mockSyncService := app.NewMockSharedChannelService(th.Server.GetSharedChannelSyncService())
th.Server.SetSharedChannelSyncService(mockSyncService)
testCluster := &testlib.FakeClusterInterface{}
th.Server.Platform().SetCluster(testCluster)
remoteCluster, err := th.App.AddRemoteCluster(&model.RemoteCluster{
RemoteId: model.NewId(),
Name: "remote",
SiteURL: "example.com",
Token: model.NewId(),
Topics: "topic",
CreateAt: model.GetMillis(),
LastPingAt: model.GetMillis(),
CreatorId: model.NewId(),
})
require.Nil(t, err)
commandProvider := ShareProvider{}
channel := th.CreateChannel(th.BasicTeam, WithShared(true)) // will create with generated remoteID
args := &model.CommandArgs{
T: func(s string, args ...any) string { return s },
ChannelId: channel.Id,
UserId: th.BasicUser.Id,
TeamId: th.BasicTeam.Id,
Command: "/share-channel invite --connectionID " + remoteCluster.RemoteId,
}
response := commandProvider.DoCommand(th.App, th.Context, args, "")
require.Contains(t, response.Text, args.T("api.command_share.invite_remote_to_channel.error"))
})
}

Просмотреть файл

@@ -1449,6 +1449,10 @@
"id": "api.command_share.invite_remote.help",
"translation": "Invites an external Mattermost instance to the current shared channel"
},
{
"id": "api.command_share.invite_remote_to_channel.error",
"translation": "Cannot invite remote to channel: {{.Error}}"
},
{
"id": "api.command_share.missing_action",
"translation": "Missing action. Available actions: {{.Actions}}"