Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 6 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 75 additions & 11 deletions server/plugin/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,26 @@ 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"
"github.com/pkg/errors"
)

const (
featureIssueCreation = "issue_creations"
featureIssues = "issues"
featurePulls = "pulls"
featurePullsMerged = "pulls_merged"
featurePushes = "pushes"
featureCreates = "creates"
featureDeletes = "deletes"
featureIssueComments = "issue_comments"
featurePullReviews = "pull_reviews"
featureStars = "stars"
featureIssueCreation = "issue_creations"
featureIssues = "issues"
featurePulls = "pulls"
featurePullsMerged = "pulls_merged"
featurePushes = "pushes"
featureCreates = "creates"
featureDeletes = "deletes"
featureIssueComments = "issue_comments"
featurePullReviews = "pull_reviews"
featureStars = "stars"
NoWebhookFoundMsg = "\nNo webhook was found for this repository or organization. To create one, enter the following slash command `/github setup webhook`"
Kshitij-Katiyar marked this conversation as resolved.
Show resolved Hide resolved
ayusht2810 marked this conversation as resolved.
Show resolved Hide resolved
GithubListOptionsPerPageValue = 50
)

var validFeatures = map[string]bool{
Expand Down Expand Up @@ -265,6 +268,48 @@ func (p *Plugin) handleSubscriptionsList(_ *plugin.Context, args *model.CommandA
return txt
}

func (p *Plugin) getWebhookListForRepoOrOrg(githubClient *github.Client, repo, owner string, ctx context.Context) (bool, error) {
isWebhook := false

Kshitij-Katiyar marked this conversation as resolved.
Show resolved Hide resolved
opt := &github.ListOptions{
PerPage: GithubListOptionsPerPageValue,
}

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 {
Kshitij-Katiyar marked this conversation as resolved.
Show resolved Hide resolved
p.API.LogWarn("Not able to get the list of webhooks", "Owner", owner, "Repo", repo, "Error", err)
Kshitij-Katiyar marked this conversation as resolved.
Show resolved Hide resolved
// Breaking from the loop if the repo or org is not found
if strings.Contains(err.Error(), "404 Not Found") {
isWebhook = true
break
} else {
return isWebhook, err
}
}
for _, hook := range githubHooks {
Kshitij-Katiyar marked this conversation as resolved.
Show resolved Hide resolved
if strings.Contains(hook.Config["url"].(string), p.getSiteURL()) {
isWebhook = true
Kshitij-Katiyar marked this conversation as resolved.
Show resolved Hide resolved
}
}
if githubResponse.NextPage == 0 {
Kshitij-Katiyar marked this conversation as resolved.
Show resolved Hide resolved
break
}
opt.Page = githubResponse.NextPage
}

return isWebhook, nil

}

func (p *Plugin) handleSubscribesAdd(_ *plugin.Context, args *model.CommandArgs, parameters []string, userInfo *GitHubUserInfo) string {
if len(parameters) == 0 {
return "Please specify a repository."
Expand Down Expand Up @@ -325,7 +370,17 @@ func (p *Plugin) handleSubscribesAdd(_ *plugin.Context, args *model.CommandArgs,
return err.Error()
}

return fmt.Sprintf("Successfully subscribed to organization %s.", owner)
var subOrgMsg = fmt.Sprintf("Successfully subscribed to organization %s.", owner)
Kshitij-Katiyar marked this conversation as resolved.
Show resolved Hide resolved

isWebhook, err := p.getWebhookListForRepoOrOrg(githubClient, repo, owner, ctx)
if err != nil {
return errors.Wrap(err, "failed to get the list of webhooks").Error()
}

if !isWebhook {
subOrgMsg += NoWebhookFoundMsg
}
return subOrgMsg
}

if err := p.Subscribe(ctx, githubClient, args.UserId, owner, repo, args.ChannelId, features, flags); err != nil {
Expand All @@ -342,6 +397,15 @@ 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."
}

isWebhook, err := p.getWebhookListForRepoOrOrg(githubClient, repo, owner, ctx)
if err != nil {
return errors.Wrap(err, "failed to get the list of webhooks").Error()
}

if !isWebhook {
msg += NoWebhookFoundMsg
}

return msg
}

Expand Down
7 changes: 7 additions & 0 deletions server/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -990,3 +990,10 @@ func (p *Plugin) getUsername(mmUserID string) (string, error) {

return "@" + info.GitHubUsername, nil
}

func (p *Plugin) getSiteURL() string {
if p.API.GetConfig().ServiceSettings.SiteURL != nil {
return *p.API.GetConfig().ServiceSettings.SiteURL
}
return ""
}