This repository has been archived by the owner on Jul 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgoogleapi_helper.js
238 lines (220 loc) · 6.31 KB
/
googleapi_helper.js
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
var exports = module.exports = {};
const {google} = require('googleapis');
const moment = require('moment');
function getoAuth2Client(){
if(!process.env.GOOGLEAPI_CLIENT_ID || !process.env.GOOGLEAPI_CLIENT_SECRET){
console.log('GOOGLEAPI_CLIENT_ID or GOOGLEAPI_CLIENT_SECRET not provided. Calendar/Conference details wont be provided');
return;
}
var client_secret = process.env.GOOGLEAPI_CLIENT_SECRET;
var client_id = process.env.GOOGLEAPI_CLIENT_ID;
const oAuth2Client = new google.auth.OAuth2(client_id, client_secret, 'urn:ietf:wg:oauth:2.0:oob');
var token = JSON.parse(process.env.GOOGLE_AUTHORIZATION_TOKEN);
oAuth2Client.setCredentials(token);
return oAuth2Client;
}
exports.createIncidentsLogFile = function createIncidentsLogFile(fileName, folder, incidentTitle, reportedBy, onSuccess) {
const oAuth2Client = getoAuth2Client();
if(!oAuth2Client){
return;
}
const drive = google.drive({version: 'v3', auth: oAuth2Client});
var metadata = {
"mimeType": "application/vnd.google-apps.document",
"name":fileName
};
if(folder){
metadata['parents'] = [folder];
}
drive.files.create({
resource:metadata,
"fields": 'id',
}, (err, res) => {
if (err) return console.log('The API returned an error: ' + err);
var documentUrl = 'https://docs.google.com/document/d/'+res.data.id;
//Call on success callback
onSuccess(documentUrl);
const docs = google.docs({version: 'v1', auth: oAuth2Client});
var now = moment.utc();
var inicidentTitleLength = incidentTitle.length;
//console.log(res);
//docs.documents.move()
var texts = [
{
text: incidentTitle + "\n",
style: {
"updateParagraphStyle":{
paragraphStyle:{
namedStyleType:"TITLE"
},
fields:'namedStyleType'
}
}
},
{
text: "Quick description of the problem\n",
style: {
"updateParagraphStyle":{
paragraphStyle:{
namedStyleType:"HEADING_1"
},
fields:'namedStyleType'
}
},
},
{
text: "\n",
},
{
text:"Timeline\n",
style: {
"updateParagraphStyle":{
paragraphStyle:{
namedStyleType:"HEADING_1"
},
fields:'namedStyleType'
}
},
},
{
text: "Times in UTC\n\n",
style: {
"updateTextStyle":{
textStyle:{
italic:true
},
fields:'italic'
}
}
},
{
text: now.format("YYYY-MM-DD HH:mm Z") + ": Incident started by " + reportedBy + "\n\n"
},
{
text:"[Copy & paste data]\n",
style: {
"updateParagraphStyle":{
paragraphStyle:{
namedStyleType:"HEADING_1"
},
fields:'namedStyleType'
}
},
},
{
text: "\n\n"
}
];
var requests = [];
var index = 1;
for(var i=0;i<texts.length;i++){
var text = texts[i].text;
requests.push(
{
"insertText":{
"text": text,
location:{
"index":index
}
}
}
);
if(texts[i].style){
var style = texts[i].style;
style[Object.keys(style)[0]]['range'] = {
startIndex:index,
endIndex: index + text.length
};
requests.push(texts[i].style);
}
index = index + text.length;
}
setTimeout(
function(){docs.documents.batchUpdate({
documentId: res.data.id,
resource:{
"requests": requests
}
},(err, res) =>{
if (err){
console.log("Error writing to file: " + err);
}
}
)},1000);;
});
}
/**
* Create an OAuth2 client with the given credentials
* @param {Object} credentials The authorization client credentials.
*/
exports.registerIncidentEvent = function registerIncidentEvent(incidentId, incidentName, reportedBy, slackChannel, onSuccess) {
const oAuth2Client = getoAuth2Client();
if(!oAuth2Client){
return;
}
var now = moment();
var eventDescription = "<b>"+incidentName+"</b>\n"+
"<small>" +
"Incident response triggered on " + now.format("DD/MM/YYYY HH:mm") + "\n" +
"Reported by " + reportedBy + "\n" +
(slackChannel?"<a href='https://slack.com/app_redirect?channel=" + slackChannel+ "'>Incident Slack Channel</a>\n":'')+
"</small>";
createEvent(oAuth2Client, incidentName, incidentId, eventDescription, onSuccess);
}
function createEvent(auth, incidentName, incidentId, incidentDescription, onSuccess){
const calendar = google.calendar({version: 'v3', auth});
var calendarId = process.env.GOOGLE_CALENDAR_ID;
if(!calendarId){
calendarId = 'primary';
}
var calendarTimezone = process.env.GOOGLE_CALENDAR_TIMEZONE;
if(!calendarTimezone){
calendarTimezone = 'Europe/Amsterdam';
}
var start = new Date ();
var end = new Date ( start );
end.setMinutes ( start.getMinutes() + 5 );
var event = {
'summary': incidentName,
'description': incidentDescription,
'start': {
'dateTime': start.toISOString(),
'timeZone': calendarTimezone,
},
'end': {
'dateTime': end.toISOString(),
'timeZone': calendarTimezone,
},
};
var eventCreated;
calendar.events.insert({
auth: auth,
calendarId: calendarId,
resource: event,
}, function(err, event) {
if (err) {
console.log('There was an error contacting the Calendar service: ' + err);
return;
}
var eventPatch = {
conferenceData: {
createRequest: {requestId: incidentId},
},
};
calendar.events.patch({
calendarId: calendarId,
eventId: event.data.id,
resource: eventPatch,
sendNotifications: true,
conferenceDataVersion: 1
}, function(err, event) {
if(err){
console.log('There was an error adding the conference details');
}
else{
onSuccess(event);
}
}
);
});
}