Skip to content

Commit f24c253

Browse files
committed
Refactor slack service
1 parent ec99f13 commit f24c253

File tree

1 file changed

+37
-33
lines changed

1 file changed

+37
-33
lines changed

pkg/slack/service.go

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,51 @@ import (
55
"github.com/slack-go/slack"
66
)
77

8-
// Service structure
9-
type Service struct {
10-
Log logr.Logger
11-
APIToken string
8+
// Service interface
9+
type Service interface {
10+
CreateChannel(string, bool) (*string, error)
11+
SetDescription(string, string) error
12+
SetTopic(string, string) error
13+
RenameChannel(string, string) error
14+
InviteUsers(string, []string) error
1215
}
1316

14-
var api *slack.Client
17+
// SlackService structure
18+
type SlackService struct {
19+
log logr.Logger
20+
api *slack.Client
21+
}
1522

16-
func (s *Service) init() {
17-
if api == nil {
18-
api = slack.New(s.APIToken)
23+
// New creates a new SlackService
24+
func New(APIToken string, logger logr.Logger) *SlackService {
25+
return &SlackService{
26+
api: slack.New(APIToken),
27+
log: logger,
1928
}
2029
}
2130

2231
// CreateChannel creates a public or private channel on slack with the given name
23-
func (s *Service) CreateChannel(name string, isPrivate bool) (*string, error) {
24-
s.Log.Info("Creating Slack Channel", "name", name, "isPrivate", isPrivate)
25-
s.init()
32+
func (s *SlackService) CreateChannel(name string, isPrivate bool) (*string, error) {
33+
s.log.Info("Creating Slack Channel", "name", name, "isPrivate", isPrivate)
2634

27-
channel, err := api.CreateConversation(name, isPrivate)
35+
channel, err := s.api.CreateConversation(name, isPrivate)
2836
if err != nil {
29-
s.Log.Error(err, "Error Creating channel", "name", name)
37+
s.log.Error(err, "Error Creating channel", "name", name)
3038
return nil, err
3139
}
3240

33-
s.Log.V(1).Info("Created Slack Channel", "channel", channel)
41+
s.log.V(1).Info("Created Slack Channel", "channel", channel)
3442

3543
return &channel.ID, nil
3644
}
3745

3846
// SetDescription sets description/"purpose" of the slack channel
39-
func (s *Service) SetDescription(channelID string, description string) error {
40-
log := s.Log.WithValues("channelID", channelID)
41-
s.init()
47+
func (s *SlackService) SetDescription(channelID string, description string) error {
48+
log := s.log.WithValues("channelID", channelID)
4249

4350
log.V(1).Info("Setting Description of the Slack Channel")
4451

45-
_, err := api.SetPurposeOfConversation(channelID, description)
52+
_, err := s.api.SetPurposeOfConversation(channelID, description)
4653

4754
if err != nil {
4855
log.Error(err, "Error setting description of the channel")
@@ -52,13 +59,12 @@ func (s *Service) SetDescription(channelID string, description string) error {
5259
}
5360

5461
// SetTopic sets "topic" of the slack channel
55-
func (s *Service) SetTopic(channelID string, topic string) error {
56-
log := s.Log.WithValues("channelID", channelID)
57-
s.init()
62+
func (s *SlackService) SetTopic(channelID string, topic string) error {
63+
log := s.log.WithValues("channelID", channelID)
5864

5965
log.V(1).Info("Setting Topic of the Slack Channel")
6066

61-
_, err := api.SetTopicOfConversation(channelID, topic)
67+
_, err := s.api.SetTopicOfConversation(channelID, topic)
6268

6369
if err != nil {
6470
log.Error(err, "Error setting topic of the channel")
@@ -68,13 +74,12 @@ func (s *Service) SetTopic(channelID string, topic string) error {
6874
}
6975

7076
// RenameChannel renames the slack channel
71-
func (s *Service) RenameChannel(channelID string, name string) error {
72-
log := s.Log.WithValues("channelID", channelID)
73-
s.init()
77+
func (s *SlackService) RenameChannel(channelID string, newName string) error {
78+
log := s.log.WithValues("channelID", channelID)
7479

75-
log.V(1).Info("Renaming Slack Channel", "name", name)
80+
log.V(1).Info("Renaming Slack Channel", "newName", newName)
7681

77-
_, err := api.RenameConversation(channelID, name)
82+
_, err := s.api.RenameConversation(channelID, newName)
7883

7984
if err != nil {
8085
log.Error(err, "Error renaming channel")
@@ -84,20 +89,19 @@ func (s *Service) RenameChannel(channelID string, name string) error {
8489
}
8590

8691
// InviteUsers invites users to the slack channel
87-
func (s *Service) InviteUsers(channelID string, userEmails []string) error {
88-
log := s.Log.WithValues("channelID", channelID)
89-
s.init()
92+
func (s *SlackService) InviteUsers(channelID string, userEmails []string) error {
93+
log := s.log.WithValues("channelID", channelID)
9094

9195
for _, email := range userEmails {
92-
user, err := api.GetUserByEmail(email)
96+
user, err := s.api.GetUserByEmail(email)
9397

9498
if err != nil {
95-
s.Log.Error(err, "Error getting user by email")
99+
log.Error(err, "Error getting user by email")
96100
return err
97101
}
98102

99103
log.V(1).Info("Inviting user to Slack Channel", "userID", user.ID)
100-
_, err = api.InviteUsersToConversation(channelID, user.ID)
104+
_, err = s.api.InviteUsersToConversation(channelID, user.ID)
101105

102106
if err != nil {
103107
log.Error(err, "Error Inviting user to channel", "userID", user.ID)

0 commit comments

Comments
 (0)