Skip to content

Commit

Permalink
Slack channel support added. (#24)
Browse files Browse the repository at this point in the history
Signed-off-by: viktor-kurchenko <[email protected]>
  • Loading branch information
viktor-kurchenko authored May 18, 2023
1 parent 8ef2d5a commit 45c8ebe
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/slack/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ func Notify(ctx context.Context, resourceFile, slackToken, slackDefaultChannel,
if err := slack.readUsers(ctx); err != nil {
return err
}
logger.Info("reading slack channels...")
if err := slack.readChannels(ctx); err != nil {
return err
}
logger.Info("preparing slack messages...")
channelMessages := prepareSlackMessage(groupSlackMessage(report.Accounts, slack))
logger.Info("sending slack notification...")
Expand Down
43 changes: 43 additions & 0 deletions pkg/slack/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

const (
slackUsersLimit = 10_000
slackChannelsLimit = 10_000
maxSlackMessageLength = 3_750
splitMessageThreshold = maxSlackMessageLength - maxSlackMessageLength/5
)
Expand All @@ -21,6 +22,8 @@ type slackProvider struct {
title string
defaultChannel string
slackIDs map[string]struct{}
slackChannelIDs map[string]struct{}
channelSlackID map[string]string
emailSlackID map[string]string
realNameSlackID map[string]string
normalRealNameSlackID map[string]string
Expand All @@ -35,6 +38,8 @@ func newSlackProvider(token, title, defaultChannel string) *slackProvider {
title: title,
defaultChannel: defaultChannel,
slackIDs: make(map[string]struct{}),
slackChannelIDs: make(map[string]struct{}),
channelSlackID: make(map[string]string),
emailSlackID: make(map[string]string),
realNameSlackID: make(map[string]string),
normalRealNameSlackID: make(map[string]string),
Expand All @@ -43,6 +48,7 @@ func newSlackProvider(token, title, defaultChannel string) *slackProvider {
lastNameSlackID: make(map[string]string),
}
}

func (s *slackProvider) readUsers(ctx context.Context) error {
users, err := s.client.GetUsersContext(ctx, slack.GetUsersOptionLimit(slackUsersLimit))
if err != nil {
Expand Down Expand Up @@ -71,14 +77,51 @@ func (s *slackProvider) readUsers(ctx context.Context) error {
return nil
}

func (s *slackProvider) readChannels(ctx context.Context) error {
var (
channels []slack.Channel
cursor string
err error
)
for {
params := &slack.GetConversationsParameters{
Cursor: cursor,
ExcludeArchived: true,
Limit: slackChannelsLimit,
Types: []string{"public_channel"},
}
channels, cursor, err = s.client.GetConversationsContext(ctx, params)
if err != nil {
return err
}
for _, c := range channels {
if !c.IsChannel {
continue
}
s.slackChannelIDs[c.Conversation.ID] = struct{}{}
s.channelSlackID[strings.ToLower(c.Conversation.NameNormalized)] = c.Conversation.ID
}
if cursor == "" {
break
}
}
return nil
}

func (s *slackProvider) getSlackIDByOwner(owner string) string {
if _, ok := s.slackIDs[strings.ToUpper(owner)]; ok {
return strings.ToUpper(owner)
}
if _, ok := s.slackChannelIDs[strings.ToUpper(owner)]; ok {
return strings.ToUpper(owner)
}
owner = strings.ToLower(owner)
if _, err := mail.ParseAddress(owner); err == nil {
return s.emailSlackID[owner]
}
if id, ok := s.channelSlackID[owner]; ok {
return id
}
if id, ok := s.displayNameSlackID[owner]; ok {
return id
}
Expand Down

0 comments on commit 45c8ebe

Please sign in to comment.