Skip to content

Commit

Permalink
[mattermostGH-418]: Fixed issue mattermost#418 'Added --exclude optio…
Browse files Browse the repository at this point in the history
…n to subscriptions command'. (mattermost#683)

Co-authored-by: Kshitij Katiyar <[email protected]>
Co-authored-by: Nityanand Rai <[email protected]>
  • Loading branch information
3 people authored Nov 24, 2023
1 parent 1a1f668 commit 573e4a3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ 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.
* __Setup GitHub integration__ - Use `/github setup` to configure the integration between GitHub and Mattermost. This command has the following subcommands:
Expand Down
6 changes: 6 additions & 0 deletions server/plugin/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,10 @@ func (p *Plugin) handleSubscribesAdd(_ *plugin.Context, args *model.CommandArgs,
return subOrgMsg
}

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 {
return err.Error()
}
Expand Down Expand Up @@ -781,6 +785,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
33 changes: 29 additions & 4 deletions server/plugin/subscriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,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 @@ -33,6 +35,12 @@ func (s *SubscriptionFlags) AddFlag(flag string, value string) error {
s.ExcludeOrgMembers = parsed
case flagRenderStyle:
s.RenderStyle = value
case flagExcludeRepository:
repos := strings.Split(value, ",")
for i := range repos {
repos[i] = strings.TrimSpace(repos[i])
}
s.ExcludeRepository = repos
}

return nil
Expand All @@ -51,6 +59,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 @@ -127,6 +140,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.GetFullName() {
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 @@ -307,6 +329,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 573e4a3

Please sign in to comment.