-
Notifications
You must be signed in to change notification settings - Fork 0
/
standup.go
179 lines (146 loc) · 4.68 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
package standup
import (
"fmt"
"math/rand"
"strings"
"time"
"github.com/chrispruitt/go-slackbot/lib/bot"
"github.com/justmiles/standup-bot/lib/types"
"github.com/lucasb-eyer/go-colorful"
logger "github.com/sirupsen/logrus"
"github.com/slack-go/slack"
)
var header = "Asynchronous Standups! Less time in meetings means more time getting things done. Keep the channel clean by using threads! To manage times, participants, etc, just type the `/standup` command."
func RegisterStandup(settings types.StandupSettings) error {
logger.Infof("\n\nRegistering Standup \n %v\n\n", settings)
err := bot.RegisterPeriodicScript(bot.PeriodicScript{
Name: fmt.Sprintf("standup-solicit-%s", settings.ChannelID),
CronSpec: settings.SolicitCronSpec,
Function: getSolicitStandupFunc(settings),
})
if err != nil {
return err
}
bot.RegisterPeriodicScript(bot.PeriodicScript{
Name: fmt.Sprintf("standup-share-%s", settings.ChannelID),
CronSpec: settings.ShareCronSpec,
Function: getShareStandupFunc(settings),
})
brain.Standups[settings.ChannelID] = settings
return nil
}
func getSolicitStandupFunc(settings types.StandupSettings) func() {
return func() {
// declare settings within function scope
settings := settings
for _, userId := range settings.Participants {
bot.PostMessage(userId, settings.SolicitMsg)
}
}
}
func getShareStandupFunc(settings types.StandupSettings) func() {
return func() {
// declare settings within function scope
settings := settings
bot.PostMessage(settings.ChannelID, header)
users, err := bot.SlackClient.GetUsersInfo(settings.Participants...)
if err != nil {
logger.Error("Error getting users: ", err)
}
conversations, err := getConversations()
if err != nil {
logger.Error("Error getting conversations: ", err)
}
// seed our random colors and randomize share order
rand.Seed(time.Now().UTC().UnixNano())
rand.Shuffle(len(*users), func(i, j int) { (*users)[i], (*users)[j] = (*users)[j], (*users)[i] })
for _, user := range *users {
if channel, ok := conversations[user.ID]; ok {
notes, err := getStandupNote(channel.ID, settings.SolicitMsg, user.ID)
if err != nil {
logger.Error(err)
continue
}
if len(notes) == 0 {
if settings.Shame {
notes = append(notes, fmt.Sprintf(":poop: %s has no standup notes", strings.Split(user.RealName, " ")[0]))
} else {
continue
}
}
attachments := []slack.Attachment{
{
Title: fmt.Sprintf("%s standup notes:", user.RealName),
Text: fmt.Sprintf(strings.Join(reverse(notes), "\n")),
Color: colorful.FastHappyColor().Hex(),
},
}
_, _, err = bot.SlackClient.PostMessage(
settings.ChannelID,
slack.MsgOptionText("", false),
slack.MsgOptionEnableLinkUnfurl(),
slack.MsgOptionAttachments(attachments...))
if err != nil {
logger.Errorf("Error posting standup report: %s\n", err)
}
} else {
logger.Errorf("channel %s is orphaned", channel.ID)
}
}
}
}
func getConversations() (map[string]slack.Channel, error) {
conversations := make(map[string]slack.Channel)
r, _, err := bot.SlackClient.GetConversations(&slack.GetConversationsParameters{
ExcludeArchived: true,
Types: []string{"im"},
})
if err != nil {
return nil, err
}
for _, channel := range r {
conversations[channel.User] = channel
}
return conversations, nil
}
func getStandupNote(channelID string, solicitStandupMessage string, userID string) ([]string, error) {
conversationHistory, err := bot.SlackClient.GetConversationHistory(&slack.GetConversationHistoryParameters{
ChannelID: channelID,
// Oldest: fmt.Sprintf("%d.000001", (now.Add(time.Hour * time.Duration(-1))).Unix()),
})
if err != nil {
return nil, fmt.Errorf("error getting standup info from channel %s: %s", channelID, err)
}
var txt []string
for _, m := range conversationHistory.Messages {
if m.Text == solicitStandupMessage {
threadReplies, _, _, _ := bot.SlackClient.GetConversationReplies(&slack.GetConversationRepliesParameters{
ChannelID: channelID,
Timestamp: m.ThreadTimestamp,
})
userThreadReplies := []string{}
for _, t := range threadReplies {
if t.User == userID {
txt = append(userThreadReplies, t.Text)
}
}
// If user replied in thread, then use only thread message for standup notes
if len(userThreadReplies) > 0 {
txt = userThreadReplies
}
break
}
// filter out any other standup solicitations for users in multiple standups
if m.User == userID {
txt = append(txt, m.Text)
}
}
return txt, nil
}
func reverse(ss []string) []string {
last := len(ss) - 1
for i := 0; i < len(ss)/2; i++ {
ss[i], ss[last-i] = ss[last-i], ss[i]
}
return ss
}