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) }