Skip to content

Commit

Permalink
[MM-454] Fix Invalid message while creating subscription for a projec…
Browse files Browse the repository at this point in the history
…t 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 <[email protected]>
  • Loading branch information
raghavaggarwal2308 and Kshitij-Katiyar authored Nov 22, 2024
1 parent a99bfb1 commit d70c438
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
26 changes: 13 additions & 13 deletions server/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion server/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
},
}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit d70c438

Please sign in to comment.