Skip to content

Commit

Permalink
[GH-378]:Fixed issue #378 'Check for webhook while subscribing' (#664)
Browse files Browse the repository at this point in the history
* [MI-2935]:Fixed issue #378 'Check for webhook while subscribing' (#28)

* [MI-2935]:Fixed issue #378 'Check for webhook while subscribing'

* [MI-2935]:Fixed self review comments

* [MI-2935]:Updated the message

* [MI-2935]:Fixed review comments

* [MI-2935]:Fixed lint errors

* [MI-2935]:Fixed review comments

* [MI-2998]:Fixed review comments of PR #664 on github plugin (#29)

* [MI-2998]:Fixed review comments of PR #664 on github plugin

* [MI-2998]:Fixed self review comments

* [MM-378]:Updated function name

* [MM-378]:Fixed review comments

* [MM-378]:Fixed review comments
  • Loading branch information
Kshitij-Katiyar authored Nov 22, 2023
1 parent af69820 commit 1a1f668
Showing 1 changed file with 72 additions and 1 deletion.
73 changes: 72 additions & 1 deletion server/plugin/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"
"unicode"

"github.com/google/go-github/v41/github"
"github.com/mattermost/mattermost-plugin-api/experimental/command"
"github.com/mattermost/mattermost-server/v6/model"
"github.com/mattermost/mattermost-server/v6/plugin"
Expand All @@ -25,6 +26,10 @@ const (
featureStars = "stars"
)

const (
PerPageValue = 50
)

var validFeatures = map[string]bool{
featureIssueCreation: true,
featureIssues: true,
Expand Down Expand Up @@ -274,7 +279,48 @@ func (p *Plugin) handleSubscriptionsList(_ *plugin.Context, args *model.CommandA
return txt
}

func (p *Plugin) checkIfConfiguredWebhookExists(ctx context.Context, githubClient *github.Client, repo, owner string) (bool, error) {
found := false
opt := &github.ListOptions{
PerPage: PerPageValue,
}
siteURL := *p.client.Configuration.GetConfig().ServiceSettings.SiteURL

for {
var githubHooks []*github.Hook
var githubResponse *github.Response
var err error

if repo == "" {
githubHooks, githubResponse, err = githubClient.Organizations.ListHooks(ctx, owner, opt)
} else {
githubHooks, githubResponse, err = githubClient.Repositories.ListHooks(ctx, owner, repo, opt)
}

if err != nil {
p.API.LogWarn("Not able to get the list of webhooks", "Owner", owner, "Repo", repo, "Error", err.Error())
return found, err
}

for _, hook := range githubHooks {
if strings.Contains(hook.Config["url"].(string), siteURL) {
found = true
break
}
}

if githubResponse.NextPage == 0 {
break
}
opt.Page = githubResponse.NextPage
}

return found, nil
}

func (p *Plugin) handleSubscribesAdd(_ *plugin.Context, args *model.CommandArgs, parameters []string, userInfo *GitHubUserInfo) string {
const errorNoWebhookFound = "\nNo webhook was found for this repository or organization. To create one, enter the following slash command `/github setup webhook`"
const errorWebhookToUser = "\nNot able to get the list of webhooks. This feature is not available for subscription to a user."
if len(parameters) == 0 {
return "Please specify a repository."
}
Expand Down Expand Up @@ -334,7 +380,20 @@ func (p *Plugin) handleSubscribesAdd(_ *plugin.Context, args *model.CommandArgs,
return err.Error()
}

return fmt.Sprintf("Successfully subscribed to organization %s.", owner)
subOrgMsg := fmt.Sprintf("Successfully subscribed to organization %s.", owner)

found, err := p.checkIfConfiguredWebhookExists(ctx, githubClient, repo, owner)
if err != nil {
if strings.Contains(err.Error(), "404 Not Found") {
return errorWebhookToUser
}
return errors.Wrap(err, "failed to get the list of webhooks").Error()
}

if !found {
subOrgMsg += errorNoWebhookFound
}
return subOrgMsg
}

if err := p.Subscribe(ctx, githubClient, args.UserId, owner, repo, args.ChannelId, features, flags); err != nil {
Expand All @@ -351,6 +410,18 @@ func (p *Plugin) handleSubscribesAdd(_ *plugin.Context, args *model.CommandArgs,
msg += "\n\n**Warning:** You subscribed to a private repository. Anyone with access to this channel will be able to read the events getting posted here."
}

found, err := p.checkIfConfiguredWebhookExists(ctx, githubClient, repo, owner)
if err != nil {
if strings.Contains(err.Error(), "404 Not Found") {
return errorWebhookToUser
}
return errors.Wrap(err, "failed to get the list of webhooks").Error()
}

if !found {
msg += errorNoWebhookFound
}

return msg
}

Expand Down

0 comments on commit 1a1f668

Please sign in to comment.