-
Notifications
You must be signed in to change notification settings - Fork 3
/
oncall.go
142 lines (124 loc) · 3.02 KB
/
oncall.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
package main
import (
"fmt"
"log"
"net/url"
)
const SCHEME = "https"
const SQUADCAST_HOST = "api.squadcast.com"
const ACCESS_TOKEN_PATH = "/v3/oauth/access-token"
const SCHEDULES_PATH = "/v3/schedules"
const ONCALL_PATH = "/v3/schedules/%s/on-call"
type OnCall struct {
RefreshToken string
ScheduleName string
SlackWebhookURL string
AccessToken string
ScheduleID string
OnCallShiftType string
OnCallPeople []string
}
func NewOnCall(params Params) *OnCall {
return &OnCall{
RefreshToken: params.RefreshToken,
ScheduleName: params.ScheduleName,
SlackWebhookURL: params.SlackWebhookURL,
}
}
func (oc *OnCall) GetAccessToken() *OnCall {
var resp AccessTokenResponse
var sqerr SquadcastErrorResponse
err := NewRequest().
Get((&url.URL{
Scheme: SCHEME,
Host: SQUADCAST_HOST,
Path: ACCESS_TOKEN_PATH,
}).String()).
SetHeader("X-Refresh-Token", oc.RefreshToken).
With(&resp).
WithFail(&sqerr).
Do()
if err != nil {
log.Fatalf("%s : %s", err, sqerr.Meta.ErrorMessage)
}
oc.AccessToken = resp.Data.AccessToken
return oc
}
func (oc *OnCall) GetScheduleID() *OnCall {
var resp SchedulesResponse
var sqerr SquadcastErrorResponse
err := NewRequest().
Get((&url.URL{
Scheme: SCHEME,
Host: SQUADCAST_HOST,
Path: SCHEDULES_PATH,
}).String()).
SetHeader("Authorization", fmt.Sprintf("Bearer %s", oc.AccessToken)).
With(&resp).
WithFail(&sqerr).
Do()
if err != nil {
log.Fatalf("%s : %s", err, sqerr.Meta.ErrorMessage)
}
for _, sch := range resp.Data {
if sch.Name == oc.ScheduleName {
oc.ScheduleID = sch.ID
break
}
}
if oc.ScheduleID == "" {
log.Fatalf("Schedule of name: %s doesn't exist", oc.ScheduleName)
}
return oc
}
func (oc *OnCall) GetOnCallPeople() *OnCall {
var resp OnCallResponse
var sqerr SquadcastErrorResponse
err := NewRequest().
Get((&url.URL{
Scheme: SCHEME,
Host: SQUADCAST_HOST,
Path: fmt.Sprintf(ONCALL_PATH, oc.ScheduleID),
}).String()).
SetHeader("Authorization", fmt.Sprintf("Bearer %s", oc.AccessToken)).
With(&resp).
WithFail(&sqerr).
Do()
if err != nil {
log.Fatalf("%s : %s", err, sqerr.Meta.ErrorMessage)
}
oc.OnCallShiftType = resp.Data.ShiftType
for _, user := range resp.Data.Users {
oc.OnCallPeople = append(oc.OnCallPeople, fmt.Sprintf("%s %s", user.FirstName, user.LastName))
}
return oc
}
func (oc *OnCall) NotifySlack() {
var resp string
var slkerr string
fields := make([]SlackWebhookAttachmentFields, 0)
for _, user := range oc.OnCallPeople {
fields = append(fields, SlackWebhookAttachmentFields{
Title: user,
})
}
body := SlackWebhookRequest{
Attachments: []SlackWebhookAttachment{
{
Fallback: fmt.Sprintf("On-Call Update for Schedule: %s", oc.ScheduleName),
Pretext: fmt.Sprintf("People On-Call for Schedule: %s", oc.ScheduleName),
Color: "#00FF00",
Fields: fields,
},
},
}
err := NewRequest().
Post(oc.SlackWebhookURL).
Data(body).
With(&resp).
WithFail(&slkerr).
Do()
if err != nil {
log.Fatalf("%s : %s", err, slkerr)
}
}