@@ -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
130146func (s * Slack ) buildDeploymentLink (r * ent.Repo , d * ent.Deployment ) string {
0 commit comments