From f7448854e9c903d63abaf26a869f7f05d06b5711 Mon Sep 17 00:00:00 2001 From: Nityanand Rai <107465508+Nityanand13@users.noreply.github.com> Date: Mon, 12 Jun 2023 17:34:23 +0530 Subject: [PATCH 1/3] [MI-2874]: Added --exclude option to subscriptions command (#24) * [MI-2874]: Added --exclude option to subscriptions commandx * [MI-2874]:Updated the read me files * [MI-2874]:Fixed review comments * [MI-2874]:Fixed review comments --------- Co-authored-by: Kshitij Katiyar --- README.md | 1 + server/plugin/command.go | 4 ++++ server/plugin/subscriptions.go | 29 +++++++++++++++++++++++++---- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index d54819d18..201c55d5e 100644 --- a/README.md +++ b/README.md @@ -152,6 +152,7 @@ When you’ve tested the plugin and confirmed it’s working, notify your team s - `--exclude-org-member`: events triggered by organization members will not be delivered. It will be locked to the organization provided in the plugin configuration and it will only work for users whose membership is public. Note that organization members and collaborators are not the same. - `--render-style`: notifications will be delivered in the specified style (for example, the body of a pull request will not be displayed). Supported values are `collapsed`, `skip-body` or `default` (same as omitting the flag). + - `--exclude`: comma-separated list of the repositories to exclude from getting the subscription notifications like `mattermost/mattermost-server`. Only supported for subscriptions to an organization. * __Get to do items__ - Use `/github todo` to get an ephemeral message with items to do in GitHub, including a list of unread messages and pull requests awaiting your review. * __Update settings__ - Use `/github settings` to update your settings for notifications and daily reminders. diff --git a/server/plugin/command.go b/server/plugin/command.go index bb1549725..cf746e74b 100644 --- a/server/plugin/command.go +++ b/server/plugin/command.go @@ -326,6 +326,8 @@ func (p *Plugin) handleSubscribesAdd(_ *plugin.Context, args *model.CommandArgs, } return fmt.Sprintf("Successfully subscribed to organization %s.", owner) + } else if len(flags.ExcludeRepository) > 0 { + return "Exclude repository feature is only available to subscriptions of an organization." } if err := p.Subscribe(ctx, githubClient, args.UserId, owner, repo, args.ChannelId, features, flags); err != nil { @@ -701,6 +703,8 @@ func getAutocompleteData(config *Configuration) *model.AutocompleteData { }, }) + subscriptionsAdd.AddNamedTextArgument("exclude", "Comma separated list of the repositories to exclude getting the notifications. Only supported for subscriptions to an organization", "", `/[^,-\s]+(,[^,-\s]+)*/`, false) + subscriptions.AddCommand(subscriptionsAdd) subscriptionsDelete := model.NewAutocompleteData("delete", "[owner/repo]", "Unsubscribe the current channel from an organization or repository") subscriptionsDelete.AddTextArgument("Owner/repo to unsubscribe from", "[owner/repo]", "") diff --git a/server/plugin/subscriptions.go b/server/plugin/subscriptions.go index d5067fe65..90a356957 100644 --- a/server/plugin/subscriptions.go +++ b/server/plugin/subscriptions.go @@ -14,15 +14,17 @@ import ( ) const ( - SubscriptionsKey = "subscriptions" - flagExcludeOrgMember = "exclude-org-member" - flagRenderStyle = "render-style" - flagFeatures = "features" + SubscriptionsKey = "subscriptions" + flagExcludeOrgMember = "exclude-org-member" + flagRenderStyle = "render-style" + flagFeatures = "features" + flagExcludeRepository = "exclude" ) type SubscriptionFlags struct { ExcludeOrgMembers bool RenderStyle string + ExcludeRepository []string } func (s *SubscriptionFlags) AddFlag(flag string, value string) error { @@ -35,6 +37,8 @@ func (s *SubscriptionFlags) AddFlag(flag string, value string) error { s.ExcludeOrgMembers = parsed case flagRenderStyle: s.RenderStyle = value + case flagExcludeRepository: + s.ExcludeRepository = strings.Split(value, ",") } return nil @@ -53,6 +57,11 @@ func (s SubscriptionFlags) String() string { flags = append(flags, flag) } + if len(s.ExcludeRepository) > 0 { + flag := "--" + flagExcludeRepository + " " + strings.Join(s.ExcludeRepository, ",") + flags = append(flags, flag) + } + return strings.Join(flags, ",") } @@ -129,6 +138,15 @@ func (s *Subscription) RenderStyle() string { return s.Flags.RenderStyle } +func (s *Subscription) excludedRepoForSub(repo *github.Repository) bool { + for _, repository := range s.Flags.ExcludeRepository { + if repository == *repo.FullName { + return true + } + } + return false +} + func (p *Plugin) Subscribe(ctx context.Context, githubClient *github.Client, userID, owner, repo, channelID, features string, flags SubscriptionFlags) error { if owner == "" { return errors.Errorf("invalid repository") @@ -318,6 +336,9 @@ func (p *Plugin) GetSubscribedChannelsForRepository(repo *github.Repository) []* if repo.GetPrivate() && !p.permissionToRepo(sub.CreatorID, name) { continue } + if sub.excludedRepoForSub(repo) { + continue + } subsToReturn = append(subsToReturn, sub) } From ff2da306ece38b3ca01fae8f854cee93fd7befde Mon Sep 17 00:00:00 2001 From: Kshitij Katiyar Date: Wed, 21 Jun 2023 13:40:28 +0530 Subject: [PATCH 2/3] [MM-418]:Fixed review comments --- server/plugin/command.go | 4 +++- server/plugin/subscriptions.go | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/server/plugin/command.go b/server/plugin/command.go index 589c4066b..45f3b48bf 100644 --- a/server/plugin/command.go +++ b/server/plugin/command.go @@ -335,7 +335,9 @@ func (p *Plugin) handleSubscribesAdd(_ *plugin.Context, args *model.CommandArgs, } return fmt.Sprintf("Successfully subscribed to organization %s.", owner) - } else if len(flags.ExcludeRepository) > 0 { + } + + if len(flags.ExcludeRepository) > 0 { return "Exclude repository feature is only available to subscriptions of an organization." } diff --git a/server/plugin/subscriptions.go b/server/plugin/subscriptions.go index da6f75e8a..e527a707e 100644 --- a/server/plugin/subscriptions.go +++ b/server/plugin/subscriptions.go @@ -138,7 +138,7 @@ func (s *Subscription) RenderStyle() string { func (s *Subscription) excludedRepoForSub(repo *github.Repository) bool { for _, repository := range s.Flags.ExcludeRepository { - if repository == *repo.FullName { + if repository == repo.GetFullName() { return true } } From c1ce0fef20dd8343e9f0239422da88777770da34 Mon Sep 17 00:00:00 2001 From: kshitij katiyar <90389917+Kshitij-Katiyar@users.noreply.github.com> Date: Wed, 21 Jun 2023 15:52:09 +0530 Subject: [PATCH 3/3] [MI-3073]:Fixed review comments (#33) --- server/plugin/subscriptions.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/server/plugin/subscriptions.go b/server/plugin/subscriptions.go index e527a707e..2f76d4a28 100644 --- a/server/plugin/subscriptions.go +++ b/server/plugin/subscriptions.go @@ -36,7 +36,11 @@ func (s *SubscriptionFlags) AddFlag(flag string, value string) error { case flagRenderStyle: s.RenderStyle = value case flagExcludeRepository: - s.ExcludeRepository = strings.Split(value, ",") + repos := strings.Split(value, ",") + for i := range repos { + repos[i] = strings.TrimSpace(repos[i]) + } + s.ExcludeRepository = repos } return nil