Skip to content

Commit

Permalink
[MI-2874]: Added --exclude option to subscriptions command (#24)
Browse files Browse the repository at this point in the history
* [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 <kshitij.katiyar@brightscout.com>
  • Loading branch information
Nityanand13 and Kshitij-Katiyar authored Jun 12, 2023
1 parent 39a2a70 commit f744885
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions server/plugin/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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]", "")
Expand Down
29 changes: 25 additions & 4 deletions server/plugin/subscriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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, ",")
}

Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit f744885

Please sign in to comment.