Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.

Commit 3b499a2

Browse files
author
Noah Hanjun Lee
authored
fix: migrate selecting the recipient in Slack pkg (#106)
1 parent 18a6cf4 commit 3b499a2

File tree

4 files changed

+101
-128
lines changed

4 files changed

+101
-128
lines changed

internal/interactor/event.go

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ package interactor
22

33
import (
44
"context"
5-
"fmt"
65
"time"
76

87
"github.com/gitploy-io/gitploy/ent"
9-
"github.com/gitploy-io/gitploy/ent/event"
108
"go.uber.org/zap"
119
)
1210

@@ -52,41 +50,3 @@ func (i *Interactor) SubscribeEvent(fn func(e *ent.Event)) error {
5250
func (i *Interactor) UnsubscribeEvent(fn func(e *ent.Event)) error {
5351
return i.events.Unsubscribe(gitployEvent, fn)
5452
}
55-
56-
func (i *Interactor) ListUsersOfEvent(ctx context.Context, e *ent.Event) ([]*ent.User, error) {
57-
if e.Kind == event.KindDeployment {
58-
d, err := i.Store.FindDeploymentByID(ctx, e.DeploymentID)
59-
if err != nil {
60-
return nil, fmt.Errorf("It has failed to find the deployment: %w", err)
61-
}
62-
63-
return []*ent.User{d.Edges.User}, nil
64-
}
65-
66-
// Notify to who has the request of approval.
67-
if e.Kind == event.KindApproval && e.Type == event.TypeCreated {
68-
a, err := i.Store.FindApprovalByID(ctx, e.ApprovalID)
69-
if err != nil {
70-
return nil, fmt.Errorf("It has failed to find the approval: %w", err)
71-
}
72-
73-
return []*ent.User{a.Edges.User}, nil
74-
}
75-
76-
// Notify to who has requested the approval.
77-
if e.Kind == event.KindApproval && e.Type == event.TypeUpdated {
78-
a, err := i.Store.FindApprovalByID(ctx, e.ApprovalID)
79-
if err != nil {
80-
return nil, fmt.Errorf("It has failed to find the approval: %w", err)
81-
}
82-
83-
d, err := i.Store.FindDeploymentByID(ctx, a.DeploymentID)
84-
if err != nil {
85-
return nil, fmt.Errorf("It has failed to find the deployment of the approval: %w", err)
86-
}
87-
88-
return []*ent.User{d.Edges.User}, nil
89-
}
90-
91-
return nil, fmt.Errorf("It is out of use-cases.")
92-
}

internal/server/slack/interface.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ type (
3838
SubscribeEvent(fn func(e *ent.Event)) error
3939
UnsubscribeEvent(fn func(e *ent.Event)) error
4040

41-
ListUsersOfEvent(ctx context.Context, e *ent.Event) ([]*ent.User, error)
4241
CheckNotificationRecordOfEvent(ctx context.Context, e *ent.Event) bool
4342
CreateEvent(ctx context.Context, e *ent.Event) (*ent.Event, error)
4443

internal/server/slack/notification.go

Lines changed: 100 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -20,111 +20,127 @@ const (
2020
colorRed = "#f5222d"
2121
)
2222

23-
func (s *Slack) Notify(ctx context.Context, e *ent.Event) error {
24-
if s.i.CheckNotificationRecordOfEvent(ctx, e) {
25-
return nil
23+
func (s *Slack) Notify(ctx context.Context, e *ent.Event) {
24+
if e.Kind == event.KindDeployment {
25+
s.notifyDeploymentEvent(ctx, e)
2626
}
2727

28-
var (
29-
users []*ent.User
30-
err error
31-
)
32-
33-
if users, err = s.i.ListUsersOfEvent(ctx, e); err != nil {
34-
return err
28+
if e.Kind == event.KindApproval {
29+
s.notifyApprovalEvent(ctx, e)
3530
}
31+
}
3632

37-
for _, u := range users {
38-
// Eager loading for chat user.
39-
if u, err = s.i.FindUserByID(ctx, u.ID); err != nil {
40-
s.log.Error("It has failed to eager load.", zap.Error(err))
41-
continue
42-
}
43-
if u.Edges.ChatUser == nil {
44-
continue
45-
}
33+
func (s *Slack) notifyDeploymentEvent(ctx context.Context, e *ent.Event) {
34+
if err := e.CheckEagerLoading(); err != nil {
35+
s.log.Error("The eager loading of event has failed.")
36+
return
37+
}
4638

47-
if err := s.notify(ctx, u.Edges.ChatUser, e); err != nil {
48-
s.log.Error("It has failed to notify the event.", zap.Error(err))
49-
}
39+
d := e.Edges.Deployment
40+
if err := d.CheckEagerLoading(); err != nil {
41+
s.log.Error("The eager loading of deployment has failed.")
42+
return
5043
}
5144

52-
return nil
53-
}
45+
owner, err := s.i.FindUserByID(ctx, d.Edges.User.ID)
46+
if err != nil {
47+
s.log.Error("It has failed to find the owner of the deployment.", zap.Error(err))
48+
return
49+
}
50+
if owner.Edges.ChatUser == nil {
51+
s.log.Debug("Skip the notification. The owner is not connected with Slack.")
52+
return
53+
}
5454

55-
func (s *Slack) notify(ctx context.Context, cu *ent.ChatUser, e *ent.Event) error {
55+
// Build the message and post it.
5656
var option slack.MsgOption
5757

58-
// Check the event has processed eager loading.
58+
if e.Type == event.TypeCreated {
59+
option = slack.MsgOptionAttachments(slack.Attachment{
60+
Color: mapDeploymentStatusToColor(d.Status),
61+
Pretext: fmt.Sprintf("*New Deployment #%d*", d.Number),
62+
Text: fmt.Sprintf("*%s* deploys `%s` to the `%s` environment of `%s`. <%s|• View Details> ", owner.Login, d.GetShortRef(), d.Env, d.Edges.Repo.GetFullName(), s.buildDeploymentLink(d.Edges.Repo, d)),
63+
})
64+
} else if e.Type == event.TypeUpdated {
65+
option = slack.MsgOptionAttachments(slack.Attachment{
66+
Color: mapDeploymentStatusToColor(d.Status),
67+
Pretext: fmt.Sprintf("*Deployment Updated #%d*", d.Number),
68+
Text: fmt.Sprintf("The deployment <%s|#%d> of `%s` is updated `%s`.", s.buildDeploymentLink(d.Edges.Repo, d), d.Number, d.Edges.Repo.GetFullName(), d.Status),
69+
})
70+
}
71+
72+
if _, _, err := slack.
73+
New(owner.Edges.ChatUser.BotToken).
74+
PostMessageContext(ctx, owner.Edges.ChatUser.ID, option); err != nil {
75+
s.log.Error("It has failed to post the message.", zap.Error(err))
76+
}
77+
}
78+
79+
func (s *Slack) notifyApprovalEvent(ctx context.Context, e *ent.Event) {
5980
if err := e.CheckEagerLoading(); err != nil {
60-
return err
81+
s.log.Error("The eager loading of event has failed.")
82+
return
6183
}
6284

63-
if e.Kind == event.KindDeployment {
64-
d := e.Edges.Deployment
65-
if err := d.CheckEagerLoading(); err != nil {
66-
return err
67-
}
85+
a := e.Edges.Approval
86+
if err := a.CheckEagerLoading(); err != nil {
87+
s.log.Error("The eager loading of approval has failed.")
88+
return
89+
}
6890

69-
var (
70-
r = d.Edges.Repo
71-
u = d.Edges.User
72-
)
73-
74-
if e.Type == event.TypeCreated {
75-
option = slack.MsgOptionAttachments(
76-
slack.Attachment{
77-
Color: mapDeploymentStatusToColor(d.Status),
78-
Pretext: fmt.Sprintf("*New Deployment #%d*", d.Number),
79-
Text: fmt.Sprintf("*%s* deploys `%s` to the `%s` environment of `%s`. <%s|• View Details> ", u.Login, d.GetShortRef(), d.Env, r.GetFullName(), s.buildDeploymentLink(r, d)),
80-
},
81-
)
82-
} else if e.Type == event.TypeUpdated {
83-
option = slack.MsgOptionAttachments(
84-
slack.Attachment{
85-
Color: mapDeploymentStatusToColor(d.Status),
86-
Pretext: fmt.Sprintf("*Deployment Updated #%d*", d.Number),
87-
Text: fmt.Sprintf("The deployment <%s|#%d> of `%s` is updated `%s`.", s.buildDeploymentLink(r, d), d.Number, r.GetFullName(), d.Status),
88-
},
89-
)
91+
d := e.Edges.Deployment
92+
if err := d.CheckEagerLoading(); err != nil {
93+
s.log.Error("The eager loading of deployment has failed.")
94+
return
95+
}
96+
97+
if e.Type == event.TypeCreated {
98+
option := slack.MsgOptionAttachments(slack.Attachment{
99+
Color: colorPurple,
100+
Pretext: "*Approval Requested*",
101+
Text: fmt.Sprintf("%s has requested the approval for the deployment <%s|#%d> of `%s`.", d.Edges.User.Login, s.buildDeploymentLink(d.Edges.Repo, d), d.Number, d.Edges.Repo.GetFullName()),
102+
})
103+
104+
recipient, err := s.i.FindUserByID(ctx, a.Edges.User.ID)
105+
if err != nil {
106+
s.log.Error("It has failed to find the recipient of the approval.", zap.Error(err))
107+
return
90108
}
91-
} else if e.Kind == event.KindApproval {
92-
a := e.Edges.Approval
93-
if err := a.CheckEagerLoading(); err != nil {
94-
return err
109+
if recipient.Edges.ChatUser == nil {
110+
s.log.Debug("Skip the notification. The recipient is not connected with Slack.")
111+
return
95112
}
96113

97-
var (
98-
u *ent.User = a.Edges.User
99-
d *ent.Deployment = a.Edges.Deployment
100-
r *ent.Repo
101-
)
114+
if _, _, err := slack.
115+
New(recipient.Edges.ChatUser.BotToken).
116+
PostMessageContext(ctx, recipient.Edges.ChatUser.ID, option); err != nil {
117+
s.log.Error("It has failed to post the message.", zap.Error(err))
118+
}
119+
}
102120

103-
// Approval have to process eager loading for the deployment, too.
104-
if err := d.CheckEagerLoading(); err != nil {
105-
return err
121+
if e.Type == event.TypeUpdated {
122+
option := slack.MsgOptionAttachments(slack.Attachment{
123+
Color: mapApprovalStatusToColor(a.Status),
124+
Pretext: "*Approval Responded*",
125+
Text: fmt.Sprintf("%s has *%s* for the deployment <%s|#%d> of `%s`.", a.Edges.User.Login, a.Status, s.buildDeploymentLink(d.Edges.Repo, d), d.Number, d.Edges.Repo.GetFullName()),
126+
})
127+
128+
requester, err := s.i.FindUserByID(ctx, d.Edges.User.ID)
129+
if err != nil {
130+
s.log.Error("It has failed to find the requester of the approval.", zap.Error(err))
131+
return
106132
}
107-
r = d.Edges.Repo
108-
109-
if e.Type == event.TypeCreated {
110-
option = slack.MsgOptionAttachments(slack.Attachment{
111-
Color: colorPurple,
112-
Pretext: "*Approval Requested*",
113-
Text: fmt.Sprintf("%s has requested the approval for the deployment <%s|#%d> of `%s`.", u.Login, s.buildDeploymentLink(r, d), d.Number, r.GetFullName()),
114-
})
115-
} else if e.Type == event.TypeUpdated {
116-
option = slack.MsgOptionAttachments(slack.Attachment{
117-
Color: mapApprovalStatusToColor(a.Status),
118-
Pretext: "*Approval Responded*",
119-
Text: fmt.Sprintf("%s has *%s* for the deployment <%s|#%d> of `%s`.", u.Login, a.Status, s.buildDeploymentLink(r, d), d.Number, r.GetFullName()),
120-
})
133+
if requester.Edges.ChatUser == nil {
134+
s.log.Debug("Skip the notification. The requester is not connected with Slack.")
135+
return
121136
}
122-
}
123137

124-
_, _, err := slack.
125-
New(cu.BotToken).
126-
PostMessageContext(ctx, cu.ID, option)
127-
return err
138+
if _, _, err := slack.
139+
New(requester.Edges.ChatUser.BotToken).
140+
PostMessageContext(ctx, requester.Edges.ChatUser.ID, option); err != nil {
141+
s.log.Error("It has failed to post the message.", zap.Error(err))
142+
}
143+
}
128144
}
129145

130146
func (s *Slack) buildDeploymentLink(r *ent.Repo, d *ent.Deployment) string {

internal/server/slack/slack.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ func NewSlack(c *SlackConfig) *Slack {
4242
}
4343

4444
s.i.SubscribeEvent(func(e *ent.Event) {
45-
if err := s.Notify(context.Background(), e); err != nil {
46-
s.log.Error("It has failed to push notificaitons.", zap.Error(err))
47-
}
45+
s.Notify(context.Background(), e)
4846
})
4947

5048
return s

0 commit comments

Comments
 (0)