-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
290 lines (263 loc) · 9.89 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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package main
import (
"context"
"errors"
"fmt"
"log"
"strings"
"time"
"github.com/PagerDuty/go-pagerduty"
"github.com/jaceklubzinski/pd-tools-bot/pkg/client"
"github.com/jaceklubzinski/pd-tools-bot/pkg/extensions"
"github.com/jaceklubzinski/pd-tools-bot/pkg/incident"
"github.com/jaceklubzinski/pd-tools-bot/pkg/maintenance"
"github.com/jaceklubzinski/pd-tools-bot/pkg/oncall"
"github.com/jaceklubzinski/pd-tools-bot/pkg/schedule"
"github.com/jaceklubzinski/pd-tools-bot/pkg/services"
team "github.com/jaceklubzinski/pd-tools-bot/pkg/teams"
"github.com/kelseyhightower/envconfig"
"github.com/shomali11/slacker"
)
type envConfig struct {
PagerDutyAuthToken string `required:"true" split_words:"true"`
SlackAuthToken string `required:"true" split_words:"true"`
DutyPay map[string]int `required:"true" split_words:"true"`
PagerDutyTeamID map[string]string `split_words:"true"`
PagerDutyScheduleID map[string]string `split_words:"true"`
SlackAuthorizedChannels string `split_words:"true"`
}
func main() {
var env envConfig
if err := envconfig.Process("pdbot", &env); err != nil {
log.Fatal(err.Error())
}
fmt.Println(env.PagerDutyScheduleID["LTS"])
fmt.Println(env.PagerDutyTeamID["LTS"])
pdclient := pagerduty.NewClient(env.PagerDutyAuthToken)
conn := client.NewAPIClient(pdclient)
bot := slacker.NewClient(env.SlackAuthToken)
bot.Init(func() {
log.Println("Connected!")
})
bot.Err(func(err string) {
log.Println(err)
})
bot.DefaultCommand(func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
response.Reply("Say what? try `help` command")
})
bot.DefaultEvent(func(event interface{}) {
fmt.Println(event)
})
authorizedChannels := []string{env.SlackAuthorizedChannels}
oncallDuty := &slacker.CommandDefinition{
Description: "PagerDuty today oncall user",
Example: "oncall today",
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
onCall := &oncall.AdminOnDutyList{OnCall: conn}
schedule := &schedule.NewSchedule{Schedule: conn}
today := time.Now()
for _, pdschedule := range schedule.GetAll() {
onCall.UsersOnCallOptions(today.String(), today.String(), pdschedule.APIObject.ID)
if err := onCall.UsersOnCall(today, today); err != nil {
response.ReportError(errors.New("something went wrong during processing bot command"))
}
oncall := onCall.PrintTodayDuty(pdschedule.Name)
response.Typing()
time.Sleep(time.Second)
response.Reply(oncall)
}
},
}
oncallMonth := &slacker.CommandDefinition{
Description: "PagerDuty oncall current month summary with profit",
Example: "oncall month lts current|next|prev",
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
var period, today time.Time
onCall := &oncall.AdminOnDutyList{OnCall: conn}
pdschedule := request.StringParam("pdschedule", "lts")
userPeriod := request.StringParam("period", "current")
envPDSchedule, ok := env.PagerDutyScheduleID[strings.ToUpper(pdschedule)]
if ok {
pdschedule = envPDSchedule
}
today = time.Now()
if userPeriod == "current" {
period = today
}
if userPeriod == "prev" {
period = today.AddDate(0, -1, 0)
}
if userPeriod == "next" {
period = today.AddDate(0, 1, 0)
}
start := extensions.BeginningOfMonth(period)
end := extensions.EndOfMonth(period)
onCall.UsersOnCallOptions(start.String(), end.String(), pdschedule)
if err := onCall.UsersOnCall(start, end); err != nil {
response.ReportError(errors.New("something went wrong during processing bot command"))
}
onCall.DutyUsersProfits(env.DutyPay)
oncall := onCall.PrintDutySummary(true)
response.Typing()
time.Sleep(time.Second)
response.Reply(oncall)
},
}
incidentListTeam := &slacker.CommandDefinition{
Description: "PagerDuty list of triggered and acknowledged incident for specific team",
Example: "incident list lts",
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
incident := incident.Incidents{Incident: conn}
pdTeam := request.StringParam("pdteam", "lts")
envPDTeam, ok := env.PagerDutyTeamID[strings.ToUpper(pdTeam)]
if ok {
pdTeam = envPDTeam
}
pdTeamList := []string{}
pdTeamList = append(pdTeamList, pdTeam)
incidentOutls, err := incident.GetTeam(pdTeamList)
if err != nil {
response.ReportError(errors.New("something went wrong during processing bot command"))
}
response.Typing()
time.Sleep(time.Second)
response.Reply(incidentOutls)
},
}
incidentListTeamDuty := &slacker.CommandDefinition{
Description: "PagerDuty list all incident incident for specific team and since defined hours",
Example: "incident duty lts 24h",
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
incident := incident.Incidents{Incident: conn}
toHour := request.StringParam("pdhour", "24h")
pdTeam := request.StringParam("pdteam", "lts")
envPDTeam, ok := env.PagerDutyTeamID[strings.ToUpper(pdTeam)]
if ok {
pdTeam = envPDTeam
}
pdTeamList := []string{}
pdTeamList = append(pdTeamList, pdTeam)
incidentOutls, err := incident.GetTeamDuty(pdTeamList, toHour)
if err != nil {
response.ReportError(errors.New("something went wrong during processing bot command"))
}
response.Typing()
time.Sleep(time.Second)
response.Reply(incidentOutls)
},
}
serviceListTeam := &slacker.CommandDefinition{
Description: "PagerDuty list of services assigned to specific team",
Example: "service list lts",
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
service := &services.Services{Service: conn}
pdTeam := request.StringParam("pdteam", "lts")
envPDTeam, ok := env.PagerDutyTeamID[strings.ToUpper(pdTeam)]
if ok {
pdTeam = envPDTeam
}
pdTeamList := []string{}
pdTeamList = append(pdTeamList, pdTeam)
serviceOutls, err := service.GetTeam(pdTeamList)
if err != nil {
response.ReportError(errors.New("something went wrong during processing bot command"))
}
response.Typing()
time.Sleep(time.Second)
response.Reply(serviceOutls)
},
}
maintenanceListTeam := &slacker.CommandDefinition{
Description: "PagerDuty list of service maintenance to specific team",
Example: "maintenace list lts",
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
maintenance := &maintenance.Maintenances{Maintenance: conn}
pdTeam := request.StringParam("pdteam", "lts")
envPDTeam, ok := env.PagerDutyTeamID[strings.ToUpper(pdTeam)]
if ok {
pdTeam = envPDTeam
}
pdTeamList := []string{}
pdTeamList = append(pdTeamList, pdTeam)
maintenanceOutls, err := maintenance.Get(pdTeamList)
if err != nil {
response.ReportError(errors.New("something went wrong during processing bot command"))
}
response.Typing()
time.Sleep(time.Second)
response.Reply(maintenanceOutls)
},
}
maintenanceCreateTeam := &slacker.CommandDefinition{
Description: "PagerDuty create maintenance window for specific service from current time + given duration",
Example: "maintenace create PDSERVICEID 4h",
AuthorizationFunc: func(botCtx slacker.BotContext, request slacker.Request) bool {
return contains(authorizedChannels, botCtx.Event().Channel)
},
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
maintenance := &maintenance.Maintenances{Maintenance: conn}
pdServiceID := request.StringParam("pdservice", "PDSERVICEID")
toHour := request.StringParam("pdhour", "4h")
maintenanceCreateOutls, err := maintenance.Create(pdServiceID, toHour)
if err != nil {
response.ReportError(errors.New("something went wrong during processing bot command"))
}
response.Reply(maintenanceCreateOutls)
},
}
scheduleList := &slacker.CommandDefinition{
Description: "PagerDuty schedule list",
Example: "schedule list",
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
schedule := &schedule.NewSchedule{Schedule: conn}
scheduleOutls := schedule.PrintSchedules()
response.Typing()
time.Sleep(time.Second)
response.Reply(scheduleOutls)
},
}
teamList := &slacker.CommandDefinition{
Description: "PagerDuty team list",
Example: "team list",
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
team := &team.Team{Team: conn}
teamOutls := team.PrintTeams()
response.Typing()
time.Sleep(time.Second)
response.Reply(teamOutls)
},
}
bot.Command("oncall today", oncallDuty)
bot.Command("oncall month <pdschedule> <period>", oncallMonth)
bot.Command("incident list <pdteam>", incidentListTeam)
bot.Command("incident duty <pdteam> <pdhour>", incidentListTeamDuty)
bot.Command("schedule list", scheduleList)
bot.Command("team list", teamList)
bot.Command("service list <pdteam>", serviceListTeam)
bot.Command("maintenance list <pdteam>", maintenanceListTeam)
bot.Command("maintenance create <pdservice> <pdhour>", maintenanceCreateTeam)
authorizedDefinition := &slacker.CommandDefinition{
Description: "Very secret stuff",
AuthorizationFunc: func(botCtx slacker.BotContext, request slacker.Request) bool {
return contains(authorizedChannels, botCtx.Event().Channel)
},
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
response.Reply("You are authorized!")
},
}
bot.Command("secret", authorizedDefinition)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
err := bot.Listen(ctx)
if err != nil {
log.Fatal(err)
}
}
func contains(list []string, element string) bool {
for _, value := range list {
if value == element {
return true
}
}
return false
}