-
Notifications
You must be signed in to change notification settings - Fork 0
/
pd.go
165 lines (132 loc) · 4.05 KB
/
pd.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
package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/jasonlvhit/gocron"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
)
type PdUser struct {
Id string `json:"id"`
Summary string `json:"summary"`
HtmlUrl string `json:"html_url"`
}
type PdSchedule struct {
Id string `json:"id"`
Summary string `json:"summary"`
Name string `json:"name"`
Users []PdUser `json:"users"`
}
type PdOncall struct {
Schedule PdSchedule `json:"schedule"`
User PdUser `json:"user"`
}
type PdSchedulesResponse struct {
Schedules []PdSchedule `json:"schedules"`
}
type PdOncallsResponse struct {
Oncalls []PdOncall `json:"oncalls"`
}
type SlackChannel struct {
Id string `json:"id"`
Name string `json:"name"`
Topic SlackTopic `json:"topic"`
}
type SlackTopic struct {
Value string `json:"value"`
UnixTimeSet int64 `json:"last_set"`
Creator string `json:"creator"`
}
type SlackResponse struct {
Channel SlackChannel `json:"channel"`
Ok bool `json:"ok"`
}
func getChannelTopic(slackToken string, channelId string) string {
var infoUrl = "https://slack.com/api/channels.info"
request, _ := http.NewRequest("GET", infoUrl, nil)
var query = request.URL.Query()
query.Add("token", slackToken)
query.Add("channel", channelId)
request.URL.RawQuery = query.Encode()
resp, err := http.DefaultClient.Do(request)
if err != nil {
log.Fatal(err)
}
body, _ := ioutil.ReadAll(resp.Body)
var dat SlackResponse
if err := json.Unmarshal(body, &dat); err != nil {
panic(err)
}
fmt.Println("Current topic for channel " + channelId + ": " + dat.Channel.Topic.Value)
return dat.Channel.Topic.Value
}
func updateChannelTopic(slackToken string, topic string, channelId string) {
var updateUrl = "https://slack.com/api/channels.setTopic"
request, _ := http.NewRequest("GET", updateUrl, nil)
var query = request.URL.Query()
query.Add("token", slackToken)
query.Add("channel", channelId)
query.Add("topic", topic)
request.URL.RawQuery = query.Encode()
_, err := http.DefaultClient.Do(request)
if err != nil {
log.Fatal(err)
}
}
func getOncallName(pdScheduleId string) (string, error) {
var PD_TOKEN = os.Getenv("PD_TOKEN")
// https://api.pagerduty.com/oncalls?time_zone=UTC&schedule_ids%5B%5D=P7CMRA9%2CTESTETS
request, _ := http.NewRequest("GET", "https://api.pagerduty.com/oncalls", nil)
var query = request.URL.Query()
query.Add("schedule_ids[]", pdScheduleId)
request.URL.RawQuery = query.Encode()
request.Header.Set("Accept", "application/vnd.pagerduty+json;version=2")
request.Header.Set("Authorization", "Token token="+PD_TOKEN)
resp, err := http.DefaultClient.Do(request)
if err != nil {
return "", err
}
body, _ := ioutil.ReadAll(resp.Body)
var dat PdOncallsResponse
if err := json.Unmarshal(body, &dat); err != nil {
return "", err
}
for _, oncall := range dat.Oncalls {
if oncall.Schedule.Id == pdScheduleId {
return oncall.User.Summary, nil
}
}
return "", errors.New("couldn't get the on call name")
}
func getOncallAndUpdateSlackChannel(slackChannelId string, pdScheduleId string) {
fmt.Println("Checking for slack channel with id " + slackChannelId)
var slackToken = os.Getenv("SLACK_TOKEN")
var prefix = "Engineer on call: "
var currentTopic = getChannelTopic(slackToken, slackChannelId)
var oncallName, err = getOncallName(pdScheduleId)
if err != nil {
fmt.Println(err)
return
}
var topic = strings.TrimSpace(prefix + oncallName)
if currentTopic != "" && currentTopic != topic {
fmt.Println("Setting the new topic: " + topic)
updateChannelTopic(slackToken, prefix+oncallName, slackChannelId)
}
}
func main() {
// put these into an array
var PLATFORM_CHANNEL_ID = "C11L5HUJY"
var PLATFORM_SCHEDULE_ID = "P7CMRA9"
var GATEWAY_CHANNEL_ID = "C148EBE76"
var GATEWAY_SCHEDULE_ID = "PDV0B84"
fmt.Println("Starting the bot...")
// "G2K8LQ3SA"
gocron.Every(30).Minutes().Do(getOncallAndUpdateSlackChannel, PLATFORM_CHANNEL_ID, PLATFORM_SCHEDULE_ID)
gocron.Every(30).Minutes().Do(getOncallAndUpdateSlackChannel, GATEWAY_CHANNEL_ID, GATEWAY_SCHEDULE_ID)
<-gocron.Start()
}