Skip to content

Commit

Permalink
Install MessageFilter in app, notifier, and susbcribe command
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuixz committed Aug 15, 2024
1 parent 06138ee commit 43f7a90
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
18 changes: 9 additions & 9 deletions pkg/slack/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ type App struct {
}

type ChannelInfo struct {
channelID string
conclusions []string
channelID string
filter *MessageFilter
}

type exposedChannelInfo struct {
ChannelID string `json:"channelID"`
Conclusions []string `json:"messageFilters"`
ChannelID string `json:"channelID"`
Filter *MessageFilter `json:"messageFilters"`
}

func (f ChannelInfo) MarshalJSON() ([]byte, error) {
return json.Marshal(exposedChannelInfo{
ChannelID: f.channelID,
Conclusions: f.conclusions,
ChannelID: f.channelID,
Filter: f.filter,
})
}

Expand All @@ -49,7 +49,7 @@ func (f *ChannelInfo) UnmarshalJSON(data []byte) error {
return err
}
f.channelID = aux.ChannelID
f.conclusions = aux.Conclusions
f.filter = aux.Filter
return nil
}

Expand Down Expand Up @@ -226,8 +226,8 @@ func (a *App) messageLoop(ctx context.Context, client *socketmode.Client) {
result := a.cli.Execute(cliContext, subcommand, args[1:])
if result.printToChannel {
client.Ack(*e.Request, map[string]interface{}{
"response_type": "in_channel",
"text": result.message,
// "response_type": "in_channel",
"text": "in_channel " + result.message,
})
}
client.Ack(*e.Request, map[string]interface{}{
Expand Down
20 changes: 13 additions & 7 deletions pkg/slack/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package slack
import (
"context"
"fmt"
"strings"

"github.com/oursky/github-actions-manager/pkg/utils/array"
"github.com/slack-go/slack"
Expand Down Expand Up @@ -157,18 +156,25 @@ func DefaultCLI() *CLI {
return NewCLIResult(false, fmt.Sprintf("Invalid repo *%s*\n", repo))
}

conclusions := array.Unique(args[1:])
filterLayers := array.Unique(args[1:])
filter, err := NewFilter(filterLayers)
if err != nil {
env.logger.Warn("failed to subscribe", zap.Error(err))
return NewCLIResult(false, fmt.Sprintf("Failed to subscribe to *%s*: '%s'\n", repo, err))
}

channelInfo := ChannelInfo{
channelID: env.data.ChannelID,
conclusions: conclusions,
channelID: env.data.ChannelID,
filter: filter,
}
err := env.app.AddChannel(env.ctx, repo, channelInfo)
err = env.app.AddChannel(env.ctx, repo, channelInfo)
if err != nil {
env.logger.Warn("failed to subscribe", zap.Error(err))
return NewCLIResult(false, fmt.Sprintf("Failed to subscribe to *%s*: '%s'\n", repo, err))
}
if len(conclusions) > 0 {
return NewCLIResult(true, fmt.Sprintf("Subscribed to '%s' with conclusions: %s\n", repo, strings.Join(conclusions, ", ")))
if len(filterLayers) > 0 {
return NewCLIResult(true, fmt.Sprintf("Subscribed to *%s* with filter layers %s", repo, channelInfo.filter.filters))
// return NewCLIResult(true, fmt.Sprintf("Subscribed to *%s* with filter layers: %s\n", repo, strings.Join(filterLayers, ", ")))
} else {
return NewCLIResult(true, fmt.Sprintf("Subscribed to *%s*\n", repo))
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/slack/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/slack-go/slack/slackutilsx"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
"k8s.io/utils/strings/slices"
)

type JobsState interface {
Expand Down Expand Up @@ -159,7 +158,7 @@ func (n *Notifier) notify(ctx context.Context, run *jobs.WorkflowRun) {
}

for _, channel := range channels {
if len(channel.conclusions) > 0 && !slices.Contains(channel.conclusions, run.Conclusion) {
if !channel.filter.Any(run) {
return
}
err := n.app.SendMessage(ctx, channel.channelID, slack.MsgOptionAttachments(slackMsg))
Expand Down

0 comments on commit 43f7a90

Please sign in to comment.