forked from pivotal-cf/cred-alert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
72 lines (57 loc) · 1.62 KB
/
handler.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
65
66
67
68
69
70
71
72
package rolodex
import (
"code.cloudfoundry.org/lager"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"cred-alert/metrics"
"rolodex/rolodexpb"
)
type handler struct {
teamRepository TeamRepository
logger lager.Logger
successCounter metrics.Counter
failureCounter metrics.Counter
}
func NewHandler(logger lager.Logger, repo TeamRepository, emitter metrics.Emitter) rolodexpb.RolodexServer {
handlerLogger := logger.Session("handler")
return &handler{
teamRepository: repo,
logger: handlerLogger,
successCounter: emitter.Counter("rolodex.handler.success"),
failureCounter: emitter.Counter("rolodex.handler.failure"),
}
}
func (h *handler) GetOwners(ctx context.Context, request *rolodexpb.GetOwnersRequest) (*rolodexpb.GetOwnersResponse, error) {
owner := request.GetRepository().GetOwner()
name := request.GetRepository().GetName()
if owner == "" || name == "" {
h.failureCounter.Inc(h.logger)
return nil, grpc.Errorf(codes.InvalidArgument, "repository owner and/or name may not be empty")
}
teams, err := h.teamRepository.GetOwners(Repository{
Owner: owner,
Name: name,
})
if err != nil {
h.failureCounter.Inc(h.logger)
return nil, err
}
pbteams := []*rolodexpb.Team{}
for _, team := range teams {
pbteam := &rolodexpb.Team{
Name: team.Name,
}
if team.SlackChannel.Name != "" {
pbteam.SlackChannel = &rolodexpb.SlackChannel{
Team: team.SlackChannel.Team,
Name: team.SlackChannel.Name,
}
}
pbteams = append(pbteams, pbteam)
}
h.successCounter.Inc(h.logger)
return &rolodexpb.GetOwnersResponse{
Teams: pbteams,
}, nil
}