-
Notifications
You must be signed in to change notification settings - Fork 787
/
slack.go
64 lines (57 loc) · 1.5 KB
/
slack.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package chats
import (
"fmt"
"strings"
"github.com/jenkins-x/jx/pkg/auth"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/nlopes/slack"
)
type SlackChatProvider struct {
SlackClient *slack.Client
Server *auth.AuthServer
UserAuth *auth.UserAuth
}
func CreateSlackChatProvider(server *auth.AuthServer, userAuth *auth.UserAuth, batchMode bool) (ChatProvider, error) {
u := server.URL
if u == "" {
return nil, fmt.Errorf("No base URL for server!")
}
if userAuth == nil || userAuth.IsInvalid() || userAuth.ApiToken == "" {
return nil, fmt.Errorf("No authentication found for Slack server %s", u)
}
slackClient := slack.New(userAuth.ApiToken)
return &SlackChatProvider{
SlackClient: slackClient,
Server: server,
UserAuth: userAuth,
}, nil
}
func (c *SlackChatProvider) GetChannelMetrics(name string) (*ChannelMetrics, error) {
metrics := &ChannelMetrics{
Name: name,
}
name = strings.TrimPrefix(name, "#")
id := name
channels, err := c.SlackClient.GetChannels(true)
if err != nil {
return metrics, err
}
for _, ch := range channels {
log.Infof("Found channel %s with id %s\n", ch.Name, ch.ID)
if ch.Name == name {
id = ch.ID
break
}
}
info, err := c.SlackClient.GetChannelInfo(id)
if err != nil {
return metrics, err
}
metrics.MemberCount = info.NumMembers
metrics.ID = info.ID
metrics.Name = info.Name
metrics.Members = info.Members
metrics.URL = util.UrlJoin(c.Server.URL, "messages", info.ID)
return metrics, nil
}