From d70c438f7f5789bcc3d9e2b36fa3d3b868dc34ea Mon Sep 17 00:00:00 2001 From: Raghav Aggarwal Date: Fri, 22 Nov 2024 15:31:16 +0530 Subject: [PATCH] [MM-454] Fix Invalid message while creating subscription for a project with no access to create webhook (#512) * [MM-454] Fix Invalid message while creating subscription for a project with no access to create webhook * Review fixes * Fix test cases * [MM-454]: Fixed lint --------- Co-authored-by: kshitij katiyar --- server/command.go | 26 +++++++++++++------------- server/command_test.go | 6 +++++- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/server/command.go b/server/command.go index 5af8aac2..bcebb9ad 100644 --- a/server/command.go +++ b/server/command.go @@ -62,7 +62,6 @@ const commandHelp = `* |/gitlab connect| - Connect your Mattermost account to yo * |/gitlab about| - Display build information about the plugin ` const ( - webhookHowToURL = "/~https://github.com/mattermost/mattermost-plugin-gitlab#step-3-create-a-gitlab-webhook" inboundWebhookURL = "plugins/com.github.manland.mattermost-plugin-gitlab/webhook" specifyRepositoryMessage = "Please specify a repository." specifyRepositoryAndBranchMessage = "Please specify a repository and a branch." @@ -655,31 +654,32 @@ func (p *Plugin) subscriptionsAddCommand(ctx context.Context, info *gitlab.UserI ) return subscribeErr.Error() } + var hasHook bool + hasHookError := false if project != "" { hasHook, err = p.HasProjectHook(ctx, info, namespace, project) if err != nil { - return fmt.Sprintf( - "Unable to determine status of Webhook. See [setup instructions](%s) to validate.", - webhookHowToURL, - ) + p.client.Log.Debug("Unable to fetch project webhook data", "Error", err.Error()) + hasHookError = true } } else { hasHook, err = p.HasGroupHook(ctx, info, namespace) if err != nil { - return fmt.Sprintf( - "Unable to determine status of Webhook. See [setup instructions](%s) to validate.", - webhookHowToURL, - ) + p.client.Log.Debug("Unable to fetch group webhook data", "Error", err.Error()) + hasHookError = true } } + + hookErrorMessage := "" + if hasHookError { + hookErrorMessage = "\n**Note:** We are unable to determine the webhook status for this project. Please contact your project administrator" + } + var hookStatusMessage string if !hasHook { // no web hook found - hookStatusMessage = fmt.Sprintf( - "\nA Webhook is needed, run ```/gitlab webhook add %s``` to create one now.", - fullPath, - ) + hookStatusMessage = fmt.Sprintf("\nA Webhook is needed, run ```/gitlab webhook add %s``` to create one now.%s", fullPath, hookErrorMessage) } p.sendChannelSubscriptionsUpdated(updatedSubscriptions, channelID) diff --git a/server/command_test.go b/server/command_test.go index 145949f5..d2b1bf96 100644 --- a/server/command_test.go +++ b/server/command_test.go @@ -90,7 +90,7 @@ var subscribeCommandTests = []subscribeCommandTest{ mattermostURL: "example.com", mockGitlab: true, webhookInfo: []*gitlab.WebhookInfo{{}}, - want: "Unable to determine status of Webhook. See [setup instructions](/~https://github.com/mattermost/mattermost-plugin-gitlab#step-3-create-a-gitlab-webhook) to validate.", + want: "Successfully subscribed to group.\nA Webhook is needed, run ```/gitlab webhook add group``` to create one now.\n**Note:** We are unable to determine the webhook status for this project. Please contact your project administrator", projectHookErr: errors.New("unable to get project hooks"), }, } @@ -317,6 +317,10 @@ func getTestPlugin(t *testing.T, mockCtrl *gomock.Controller, hooks []*gitlab.We mock.AnythingOfType("string"), mock.AnythingOfType("string"), mock.AnythingOfType("string")) + api.On("LogDebug", + mock.AnythingOfType("string"), + mock.AnythingOfType("string"), + mock.AnythingOfType("string")) p.SetAPI(api) p.client = pluginapi.NewClient(api, p.Driver)