-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
208 lines (186 loc) · 5.57 KB
/
main.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
package main
import (
"fmt"
"log"
"math"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/jawher/mow.cli"
"github.com/nlopes/slack"
)
const (
NAME = "standup-slackbot"
DESC = "A slackbot for standups"
)
func main() {
app := cli.App(NAME, DESC)
slackToken := app.String(cli.StringOpt{
Name: "slack-token",
Desc: "Slack API token",
EnvVar: "SLACK_TOKEN",
})
standupChannelName := app.String(cli.StringOpt{
Name: "standup-channel",
Desc: "The Slack channel to use for standups",
EnvVar: "STANDUP_CHANNEL",
})
standupTime := app.String(cli.StringOpt{
Name: "standup-time",
Desc: "The time standup should start in 24hr 00:00 format",
EnvVar: "STANDUP_TIME",
})
standupLengthMins := app.Int(cli.IntOpt{
Name: "standup-length-mins",
Desc: "The standup length time in minutes",
EnvVar: "STANDUP_LENGTH_MINS",
Value: 60,
})
timeZone := app.String(cli.StringOpt{
Name: "time-zone",
Desc: "The timezone IANA format e.g. Europe/London",
EnvVar: "TIME_ZONE",
Value: "Europe/London",
})
doImmediately := app.Bool(cli.BoolOpt{
Name: "do-standup-immediately",
Value: false,
EnvVar: "DO_STANDUP_IMMEDIATELY",
Desc: "Should we do a standup immediately at launch?",
})
app.Action = func() {
var lastStandupDay *int = nil
tz, err := time.LoadLocation(*timeZone)
if err != nil {
log.Fatalf("Error getting location for timezone: %v", err)
}
for {
now := time.Now().In(tz)
day := now.Day()
if *doImmediately && lastStandupDay == nil {
if err := DoStandup(*slackToken, *standupChannelName, *standupLengthMins); err != nil {
log.Fatalf("Error doing standup: %v", err)
}
lastStandupDay = &day
continue
}
// prevents busy waiting
<-time.After(1 * time.Minute)
isWeekend := now.Weekday() < 1 || now.Weekday() > 5
if isWeekend {
continue
}
standupAlreadyDone := lastStandupDay != nil && *lastStandupDay == day
if standupAlreadyDone {
continue
}
hour, mins, err := parseStandupStartTime(standupTime)
if err != nil {
log.Fatalf("Error parsing standup start time: %v", err)
}
notTimeYet := now.Hour() < *hour || now.Minute() < *mins
if notTimeYet {
continue
}
standupStartTime := time.Date(now.Year(), now.Month(), now.Day(), *hour, *mins, 0, 0, tz)
standupDuration := time.Minute * time.Duration(*standupLengthMins)
// this prevents us doing standup in the case where we have no prior state (due to a restart)
// but have already done standup for the day
standupEndTimePassed := lastStandupDay == nil && standupStartTime.Add(standupDuration).Before(now)
if standupEndTimePassed {
continue
}
if err := DoStandup(*slackToken, *standupChannelName, *standupLengthMins); err != nil {
log.Fatalf("Error doing standup: %v", err)
}
lastStandupDay = &day
}
}
app.Run(os.Args)
}
func parseStandupStartTime(standupTime *string) (*int, *int, error) {
hoursAndMins := strings.Split(*standupTime, ":")
hour, err := strconv.ParseInt(hoursAndMins[0], 10, 8)
if err != nil {
return nil, nil, fmt.Errorf("Could not parse hours from standup start time: %v", err)
}
mins, err := strconv.ParseInt(hoursAndMins[1], 10, 8)
if err != nil {
return nil, nil, fmt.Errorf("Could not parse mins from standup start time: %v", err)
}
hourInt := int(hour)
minsInt := int(mins)
return &hourInt, &minsInt, nil
}
func DoStandup(slackToken string, standupChannelName string, standupLengthMins int) error {
baseParams := slack.NewPostMessageParameters()
baseParams.Username = "Standup Bot"
slackClient := &Slack{
slack.New(slackToken),
make(map[string]string),
make(map[string]func(event *slack.MessageEvent)),
sync.Mutex{},
baseParams,
}
channelId, err := slackClient.GetChannelIdForChannel(standupChannelName)
if err != nil {
return fmt.Errorf("Could not get channel ID for channel %s: %v", standupChannelName, err)
}
members, err := slackClient.GetChannelMembers(*channelId)
if err != nil {
return fmt.Errorf("Error getting standup channel members: %v", err)
}
standup := NewStandup(slackClient, time.Now().Add(time.Minute*time.Duration(standupLengthMins)), members)
results := standup.Start()
for i := 0; i < 5; i++ {
_, _, err = slackClient.apiClient.PostMessage(
*channelId,
"Standup is finished, keep up the good work team!",
BuildSlackReport(baseParams, results),
)
if err == nil {
break
}
log.Printf("Error posting standup result to Slack: %v", err)
<-time.After(time.Duration(3*math.Pow(2, float64(i+1))) * time.Second)
}
return nil
}
func BuildSlackReport(baseParams slack.PostMessageParameters, questionnaires map[string]*StandupQuestionnaire) slack.PostMessageParameters {
postParams := baseParams
attachments := make([]slack.Attachment, len(questionnaires))
for _, questionnaire := range questionnaires {
attachment := slack.Attachment{
Fallback: "There is no fallback text...",
AuthorIcon: questionnaire.Member.Profile.Image48,
AuthorName: questionnaire.Member.RealName,
Fields: []slack.AttachmentField{
{
Title: "What did you get done since last standup?",
Short: false,
Value: questionnaire.yesterday,
},
{
Title: "What are you working on today?",
Short: false,
Value: questionnaire.today,
},
{
Title: "When do you think you'll be finished?",
Short: false,
Value: questionnaire.finishedWhen,
},
{
Title: "Is there anything blocking you?",
Short: false,
Value: questionnaire.blockers,
},
},
}
attachments = append(attachments, attachment)
}
postParams.Attachments = attachments
return postParams
}