Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GH-418]: Fixed issue #418 'Added --exclude option to subscriptions command'. #683

Merged
merged 5 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -337,6 +337,10 @@ func (p *Plugin) handleSubscribesAdd(_ *plugin.Context, args *model.CommandArgs,
return fmt.Sprintf("Successfully subscribed to organization %s.", owner)
}

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 @@ -710,6 +714,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
Loading