diff --git a/server/plugin/command.go b/server/plugin/command.go index 9c99dae72..2361f674c 100644 --- a/server/plugin/command.go +++ b/server/plugin/command.go @@ -262,14 +262,6 @@ func (p *Plugin) handleSubscriptionsList(_ *plugin.Context, args *model.CommandA txt += "\n" } - excludeRepos, err := p.GetExcludedNotificationRepos() - if err != nil { - return err.Error() - } - for _, repo := range excludeRepos { - txt += fmt.Sprintf("* `%s` - %s", strings.Trim(repo, "/"), "notification : disabled") - txt += "\n" - } return txt } @@ -281,20 +273,17 @@ func (p *Plugin) handleSubscribesAdd(_ *plugin.Context, args *model.CommandArgs, features := "pulls,issues,creates,deletes" flags := SubscriptionFlags{} - var excludeRepo string if len(parameters) > 1 { var optionList []string for _, element := range parameters[1:] { - switch { - case isFlag(element): + if isFlag(element) { flags.AddFlag(parseFlag(element)) - case flags.ExcludeOrgRepos && excludeRepo == "": - excludeRepo = element - default: + } else { optionList = append(optionList, element) } } + if len(optionList) > 1 { return "Just one list of features is allowed" } else if len(optionList) == 1 { @@ -325,31 +314,8 @@ func (p *Plugin) handleSubscribesAdd(_ *plugin.Context, args *model.CommandArgs, if err := p.SubscribeOrg(ctx, githubClient, args.UserId, owner, args.ChannelId, features, flags); err != nil { return err.Error() } - orgLink := p.getBaseURL() + owner - var subOrgMsg = fmt.Sprintf("Successfully subscribed to organization [%s](%s).", owner, orgLink) - if flags.ExcludeOrgRepos { - var excludeMsg string - for _, value := range strings.Split(excludeRepo, ",") { - val := strings.TrimSpace(value) - notificationOffRepoOwner, NotificationOffRepo := parseOwnerAndRepo(val, p.getBaseURL()) - if notificationOffRepoOwner != owner { - return fmt.Sprintf("--exclude repository %s is not of subscribed organization .", NotificationOffRepo) - } - if err := p.StoreExcludedNotificationRepo(val); err != nil { - return err.Error() - } - if excludeMsg != "" { - excludeMsg += fmt.Sprintf(" and [%s](%s)", NotificationOffRepo, orgLink+"/"+NotificationOffRepo) - continue - } - excludeMsg += fmt.Sprintf("[%s](%s)", NotificationOffRepo, orgLink+"/"+NotificationOffRepo) - } - subOrgMsg += "\n\n" + fmt.Sprintf("Notifications are disabled for %s", excludeMsg) - } - return subOrgMsg - } - if flags.ExcludeOrgRepos { - return "--exclude feature currently support on organization level." + + return fmt.Sprintf("Successfully subscribed to organization %s.", owner) } if err := p.Subscribe(ctx, githubClient, args.UserId, owner, repo, args.ChannelId, features, flags); err != nil { @@ -376,10 +342,6 @@ func (p *Plugin) handleUnsubscribe(_ *plugin.Context, args *model.CommandArgs, p repo := parameters[0] - if err := p.EnableNotificationTurnedOffRepo(repo); err != nil { - p.API.LogWarn("Failed to unsubscribe while removing repo from disable notification list", "repo", repo, "error", err.Error()) - return "Encountered an error trying to remove from notify disabled list. Please try again." - } if err := p.Unsubscribe(args.ChannelId, repo); err != nil { p.API.LogWarn("Failed to unsubscribe", "repo", repo, "error", err.Error()) return "Encountered an error trying to unsubscribe. Please try again." @@ -638,23 +600,12 @@ func getAutocompleteData(config *Configuration) *model.AutocompleteData { subscriptionsAdd.AddTextArgument("Owner/repo to subscribe to", "[owner/repo]", "") subscriptionsAdd.AddTextArgument("Comma-delimited list of one or more of: issues, pulls, pulls_merged, pushes, creates, deletes, issue_creations, issue_comments, pull_reviews, label:\"\". Defaults to pulls,issues,creates,deletes", "[features] (optional)", `/[^,-\s]+(,[^,-\s]+)*/`) if config.GitHubOrg != "" { - exclude := []model.AutocompleteListItem{ - { - HelpText: "notifications for these repos will be turned off", - Hint: "(optional)", - Item: "--exclude", - }, - } - subscriptionsAdd.AddStaticListArgument("Currently supports --exclude", true, exclude) - subscriptionsAdd.AddTextArgument("Owner/repo to subscribe to", "[owner/repo]", "") - flags := []model.AutocompleteListItem{ - { - HelpText: "Events triggered by organization members will not be delivered (the organization config should be set, otherwise this flag has no effect)", - Hint: "(optional)", - Item: "--exclude-org-member", - }, - } - subscriptionsAdd.AddStaticListArgument("Currently supports --exclude-org-member ", false, flags) + flags := []model.AutocompleteListItem{{ + HelpText: "Events triggered by organization members will not be delivered (the organization config should be set, otherwise this flag has not effect)", + Hint: "(optional)", + Item: "--exclude-org-member", + }} + subscriptionsAdd.AddStaticListArgument("Currently supports --exclude-org-member", false, flags) } subscriptions.AddCommand(subscriptionsAdd) diff --git a/server/plugin/subscriptions.go b/server/plugin/subscriptions.go index 100b2b038..d544d777e 100644 --- a/server/plugin/subscriptions.go +++ b/server/plugin/subscriptions.go @@ -13,23 +13,18 @@ import ( ) const ( - SubscriptionsKey = "subscriptions" - excludeOrgMemberFlag = "exclude-org-member" - excludeOrgReposFlag = "exclude" - SubscribedRepoNotificationOff = "subscribed-turned-off-notifications" + SubscriptionsKey = "subscriptions" + excludeOrgMemberFlag = "exclude-org-member" ) type SubscriptionFlags struct { ExcludeOrgMembers bool - ExcludeOrgRepos bool } func (s *SubscriptionFlags) AddFlag(flag string) { switch flag { // nolint:gocritic // It's expected that more flags get added. case excludeOrgMemberFlag: s.ExcludeOrgMembers = true - case excludeOrgReposFlag: - s.ExcludeOrgRepos = true } } @@ -178,19 +173,6 @@ func (p *Plugin) SubscribeOrg(ctx context.Context, githubClient *github.Client, return p.Subscribe(ctx, githubClient, userID, org, "", channelID, features, flags) } -func (p *Plugin) IsNotificationOff(repoName string) bool { - repos, err := p.GetExcludedNotificationRepos() - if err != nil { - p.API.LogWarn("Failed to check the disabled notification repo list.", "error", err.Error()) - return false - } - if len(repos) == 0 { - return false - } - exist, _ := ItemExists(repos, repoName) - - return exist -} func (p *Plugin) GetSubscriptionsByChannel(channelID string) ([]*Subscription, error) { var filteredSubs []*Subscription subs, err := p.GetSubscriptions() @@ -284,66 +266,6 @@ func (p *Plugin) StoreSubscriptions(s *Subscriptions) error { return nil } -func (p *Plugin) GetExcludedNotificationRepos() ([]string, error) { - var subscriptions []string - value, appErr := p.API.KVGet(SubscribedRepoNotificationOff) - if appErr != nil { - return nil, errors.Wrap(appErr, "could not get subscriptions from KVStore") - } - if value == nil { - return []string{}, nil - } - err := json.NewDecoder(bytes.NewReader(value)).Decode(&subscriptions) - if err != nil { - return nil, errors.Wrap(err, "could not properly decode subscriptions key") - } - return subscriptions, nil -} - -func (p *Plugin) StoreExcludedNotificationRepo(s string) error { - var repoNames, err = p.GetExcludedNotificationRepos() - if err != nil { - return errors.Wrap(err, "error while getting previous value of key") - } - isDer, _ := ItemExists(repoNames, s) - if len(repoNames) > 0 && !isDer { - repoNames = append(repoNames, s) - } else if len(repoNames) == 0 { - repoNames = append(repoNames, s) - } - b, err := json.Marshal(repoNames) - if err != nil { - return errors.Wrap(err, "error while converting subscriptions map to json") - } - - if appErr := p.API.KVSet(SubscribedRepoNotificationOff, b); appErr != nil { - return errors.Wrap(appErr, "could not store subscriptions in KV store") - } - - return nil -} -func (p *Plugin) EnableNotificationTurnedOffRepo(s string) error { - var repoNames, err = p.GetExcludedNotificationRepos() - if err != nil { - return errors.Wrap(err, "error while getting previous value of key") - } - if len(repoNames) > 0 { - exists, index := ItemExists(repoNames, s) - if exists { - repoNames = append(repoNames[:index], repoNames[index+1:]...) - b, err := json.Marshal(repoNames) - if err != nil { - return errors.Wrap(err, "error while converting subscriptions map to json") - } - - if appErr := p.API.KVSet(SubscribedRepoNotificationOff, b); appErr != nil { - return errors.Wrap(appErr, "could not store subscriptions in KV store") - } - } - } - - return nil -} func (p *Plugin) GetSubscribedChannelsForRepository(repo *github.Repository) []*Subscription { name := repo.GetFullName() name = strings.ToLower(name) diff --git a/server/plugin/utils.go b/server/plugin/utils.go index a44eef334..5ed0b4754 100644 --- a/server/plugin/utils.go +++ b/server/plugin/utils.go @@ -332,13 +332,3 @@ func getToDoDisplayText(baseURL, title, url, notifType string) string { return fmt.Sprintf("* %s %s %s\n", repoPart, notifType, titlePart) } - -// ItemExists check if the element exist in the passed array -func ItemExists(array []string, item string) (bool, int) { - for i := 0; i < len(array); i++ { - if array[i] == item { - return true, i - } - } - return false, -1 -} diff --git a/server/plugin/webhook.go b/server/plugin/webhook.go index 0b71766c4..a16e25793 100644 --- a/server/plugin/webhook.go +++ b/server/plugin/webhook.go @@ -115,9 +115,6 @@ func (p *Plugin) handleWebhook(w http.ResponseWriter, r *http.Request) { switch event := event.(type) { case *github.PullRequestEvent: repo = event.GetRepo() - if p.IsNotificationOff(*repo.FullName) { - return - } handler = func() { p.postPullRequestEvent(event) p.handlePullRequestNotification(event) @@ -125,18 +122,12 @@ func (p *Plugin) handleWebhook(w http.ResponseWriter, r *http.Request) { } case *github.IssuesEvent: repo = event.GetRepo() - if p.IsNotificationOff(*repo.FullName) { - return - } handler = func() { p.postIssueEvent(event) p.handleIssueNotification(event) } case *github.IssueCommentEvent: repo = event.GetRepo() - if p.IsNotificationOff(*repo.FullName) { - return - } handler = func() { p.postIssueCommentEvent(event) p.handleCommentMentionNotification(event) @@ -145,42 +136,27 @@ func (p *Plugin) handleWebhook(w http.ResponseWriter, r *http.Request) { } case *github.PullRequestReviewEvent: repo = event.GetRepo() - if p.IsNotificationOff(*repo.FullName) { - return - } handler = func() { p.postPullRequestReviewEvent(event) p.handlePullRequestReviewNotification(event) } case *github.PullRequestReviewCommentEvent: repo = event.GetRepo() - if p.IsNotificationOff(*repo.FullName) { - return - } handler = func() { p.postPullRequestReviewCommentEvent(event) } case *github.PushEvent: repo = ConvertPushEventRepositoryToRepository(event.GetRepo()) - if p.IsNotificationOff(*repo.FullName) { - return - } handler = func() { p.postPushEvent(event) } case *github.CreateEvent: repo = event.GetRepo() - if p.IsNotificationOff(*repo.FullName) { - return - } handler = func() { p.postCreateEvent(event) } case *github.DeleteEvent: repo = event.GetRepo() - if p.IsNotificationOff(*repo.FullName) { - return - } handler = func() { p.postDeleteEvent(event) }