Skip to content

Commit

Permalink
Revert "MM 418 - Add --exclude option to subscriptions command #418 (#…
Browse files Browse the repository at this point in the history
…457)"

This reverts commit 943ee2f.
  • Loading branch information
mickmister committed Feb 1, 2022
1 parent 40e7fcf commit d2149e4
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 174 deletions.
71 changes: 11 additions & 60 deletions server/plugin/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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."
Expand Down Expand Up @@ -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:\"<labelname>\". 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)

Expand Down
82 changes: 2 additions & 80 deletions server/plugin/subscriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 0 additions & 10 deletions server/plugin/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
24 changes: 0 additions & 24 deletions server/plugin/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,28 +115,19 @@ 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)
p.handlePRDescriptionMentionNotification(event)
}
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)
Expand All @@ -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)
}
Expand Down

0 comments on commit d2149e4

Please sign in to comment.