-
Notifications
You must be signed in to change notification settings - Fork 4
/
standup.go
259 lines (232 loc) · 7.34 KB
/
standup.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package main
import (
"context"
"fmt"
"log"
"strings"
"time"
"github.com/nlopes/slack"
"github.com/ryanfaerman/fsm"
)
const (
AreYouReady = "Hey :) are you ready for standup? If you are say Yes. If you aren't taking part today say No."
NotUnderstoodYesOrNo = "Sorry, I didn't understand that, please say yes or no."
Yesterday = "Let's get started, what did you get done since last standup?"
Today = "Awesome, what are you working on today?"
FinishedWhen = "Great, when do you think you'll be finished with that?"
Blockers = "Is there anything blocking you?"
Done = "Thanks, have a great day!"
)
var (
rules = fsm.Ruleset{}
)
func init() {
rules.AddTransition(fsm.T{O: "ready?", E: "yesterday?"})
rules.AddTransition(fsm.T{O: "ready?", E: "complete"})
rules.AddTransition(fsm.T{O: "yesterday?", E: "today?"})
rules.AddTransition(fsm.T{O: "today?", E: "finishedWhen?"})
rules.AddTransition(fsm.T{O: "finishedWhen?", E: "blockers?"})
rules.AddTransition(fsm.T{O: "blockers?", E: "complete"})
}
type StandupQuestionnaire struct {
State fsm.State
Machine *fsm.Machine
Member *slack.User
yesterday string
today string
blockers string
finishedWhen string
}
func (s *StandupQuestionnaire) CurrentState() fsm.State {
return s.State
}
func (s *StandupQuestionnaire) SetState(state fsm.State) {
s.State = state
}
func (s *StandupQuestionnaire) SetYesterdaysUpdate(update string) {
s.yesterday = update
}
func (s *StandupQuestionnaire) SetTodaysUpdate(update string) {
s.today = update
}
func (s *StandupQuestionnaire) SetBlockers(update string) {
s.blockers = update
}
func (s *StandupQuestionnaire) SetFinishedWhen(update string) {
s.finishedWhen = update
}
func (s *StandupQuestionnaire) GetYesterdaysUpdate() string {
return s.yesterday
}
func (s *StandupQuestionnaire) GetTodaysUpdate() string {
return s.today
}
func (s *StandupQuestionnaire) GetBlockers() string {
return s.blockers
}
func (s *StandupQuestionnaire) GetFinishedWhen() string {
return s.finishedWhen
}
type Standup struct {
Slack *Slack
MemberStandupQuestionnaires map[string]*StandupQuestionnaire
Context context.Context
CancelFunc context.CancelFunc
}
func NewStandup(slack *Slack, finishTime time.Time, members map[string]*slack.User) Standup {
membersQuestionnaires := make(map[string]*StandupQuestionnaire)
for memberId, memberInfo := range members {
if memberInfo.IsBot || memberInfo.Deleted {
continue
}
questions := &StandupQuestionnaire{Member: memberInfo, State: "ready?"}
machine := fsm.New(fsm.WithRules(rules), fsm.WithSubject(questions))
questions.Machine = &machine
membersQuestionnaires[memberId] = questions
}
ctx, cancelFunc := context.WithDeadline(context.Background(), finishTime)
return Standup{
MemberStandupQuestionnaires: membersQuestionnaires,
Context: ctx,
CancelFunc: cancelFunc,
Slack: slack,
}
}
func (s Standup) Start() map[string]*StandupQuestionnaire {
go s.Slack.StartRealTimeMessagingListener(s.Context)
for member, questions := range s.MemberStandupQuestionnaires {
go func(member string, questions *StandupQuestionnaire) {
for {
if s.Context.Err() != nil {
log.Printf("Context completed before member %s finished: %v", member, s.Context.Err())
return
}
switch questions.CurrentState() {
case "ready?":
s.readyState(member, questions)
case "yesterday?":
s.yesterdayState(member, questions)
case "today?":
s.todayState(member, questions)
case "finishedWhen?":
s.finishedWhenState(member, questions)
case "blockers?":
s.blockersState(member, questions)
case "complete":
s.completeState(member)
return
}
}
}(member, questions)
}
s.waitForCompletion()
return s.MemberStandupQuestionnaires
}
func (s Standup) waitForCompletion() {
for {
select {
case <-s.Context.Done():
return
case <-time.After(1 * time.Minute):
complete := 0
for _, questionnaires := range s.MemberStandupQuestionnaires {
if questionnaires.CurrentState() == "complete" {
complete++
}
}
if complete == len(s.MemberStandupQuestionnaires) {
s.CancelFunc()
return
}
}
}
}
func (s Standup) completeState(member string) {
if _, err := s.Slack.SendMessage(member, Done); err != nil {
log.Printf("Error telling member %s standup is complete: %v", member, err)
}
}
func (s Standup) blockersState(member string, standup *StandupQuestionnaire) {
ctx, _ := context.WithCancel(s.Context)
resp := s.Slack.AskQuestion(member, Blockers, ctx)
if resp.err != nil {
log.Printf("Error asking member %s about blockers: %v", member, resp.err)
return
}
standup.SetBlockers(resp.msg.Text)
if err := standup.Machine.Transition("complete"); err != nil {
log.Printf("Error transitioning to complete: %v", err)
}
}
func (s Standup) finishedWhenState(member string, standup *StandupQuestionnaire) {
ctx, _ := context.WithCancel(s.Context)
resp := s.Slack.AskQuestion(member, FinishedWhen, ctx)
if resp.err != nil {
log.Printf("Error asking member %s when they'll be finished: %v", member, resp.err)
return
}
standup.SetFinishedWhen(resp.msg.Text)
if err := standup.Machine.Transition("blockers?"); err != nil {
log.Printf("Error transitioning to blockers?: %v", err)
}
}
func (s Standup) todayState(member string, standup *StandupQuestionnaire) {
ctx, _ := context.WithCancel(s.Context)
resp := s.Slack.AskQuestion(member, Today, ctx)
if resp.err != nil {
log.Printf("Error asking member %s about today: %v", member, resp.err)
return
}
standup.SetTodaysUpdate(resp.msg.Text)
if err := standup.Machine.Transition("finishedWhen?"); err != nil {
log.Printf("Error transitioning to finishedWhen?: %v", err)
}
}
func (s Standup) yesterdayState(member string, standup *StandupQuestionnaire) {
ctx, _ := context.WithCancel(s.Context)
resp := s.Slack.AskQuestion(member, Yesterday, ctx)
if resp.err != nil {
log.Printf("Error asking member %s about yesterday: %v", member, resp.err)
return
}
standup.SetYesterdaysUpdate(resp.msg.Text)
if err := standup.Machine.Transition("today?"); err != nil {
log.Printf("Error transitioning to today?: %v", err)
}
}
func (s Standup) readyState(member string, standup *StandupQuestionnaire) {
ready, err := s.AskIfMemberReady(member)
if err != nil {
log.Printf("Error asking member %s if they are ready: %v", member, err)
return
}
if ready == false {
if err := standup.Machine.Transition("complete"); err != nil {
log.Printf("Error transitioning to complete?: %v", err)
}
return
}
if err := standup.Machine.Transition("yesterday?"); err != nil {
log.Printf("Error transitioning to yesterday?: %v", err)
}
}
func (s Standup) AskIfMemberReady(member string) (bool, error) {
ctx, _ := context.WithCancel(s.Context)
resp := s.Slack.AskQuestion(member, AreYouReady, ctx)
if resp.err != nil {
return false, fmt.Errorf("Error asking question: %v", resp.err)
}
msg := strings.ToLower(resp.msg.Text)
for msg != "yes" && msg != "no" {
ctx, _ := context.WithCancel(ctx)
resp = s.Slack.AskQuestion(member, NotUnderstoodYesOrNo, ctx)
if resp.err != nil {
return false, fmt.Errorf("Error asking question: %v", resp.err)
}
msg = resp.msg.Text
}
if msg == "yes" {
return true, nil
}
return false, nil
}