forked from martindstone/PDcal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.js
144 lines (124 loc) · 3.39 KB
/
builder.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
var token, anyUserID;
var schedules;
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? null : decodeURIComponent(results[1].replace(/\+/g, " "));
}
function PDRequest(endpoint, method, options) {
console.log(token);
var merged = $.extend(true, {}, {
type: method,
dataType: "json",
url: "https://api.pagerduty.com/" + endpoint,
headers: {
"Authorization": "Token token=" + token,
"Accept": "application/vnd.pagerduty+json;version=2"
},
error: function(err) {
var alertStr = "Error '" + err.status + " - " + err.statusText + "' while attempting " + method + " request to '" + endpoint + "'";
try {
alertStr += ": " + err.responseJSON.error.message;
} catch (e) {
alertStr += ".";
}
try {
alertStr += "\n\n" + err.responseJSON.error.errors.join("\n");
} catch (e) {}
alert(alertStr);
}
},
options);
$.ajax(merged);
}
function getAllSchedules(callback) {
schedules = [];
var options = {
success: function(data) { processSchedules(callback, data); }
};
PDRequest("schedules", "GET", options);
}
function processSchedules(callback, data) {
data.schedules.forEach(function(schedule) {
schedules.push(schedule);
});
if ( data.more == true ) {
var offset = data.offset + data.limit;
var options = {
data: {
"offset": offset
},
success: function(data) { processSchedules(callback, data); }
}
PDRequest("schedules", "GET", options);
} else {
callback(null, "schedules");
}
}
function getAllScheduleURLs(callback) {
var infoFns = [];
schedules.forEach(function(schedule) {
infoFns.push(function(callback) {
var options = {
data: {
requester_id: anyUserID
},
success: function(data) {
console.log(data);
callback(null, data.schedule);
}
}
PDRequest("schedules/" + schedule.id, "GET", options);
});
});
async.parallel(infoFns,
function(err, results) {
schedules = results;
callback(null, 'schedules');
}
);
}
function main() {
token = getParameterByName('token');
$('.busy').show();
async.series([
function(callback) {
var options = {
success: function(data) {
anyUserID = data.users[0].id;
callback(null, "anyUserID");
}
}
PDRequest("/users", "GET", options);
},
function(callback) {
getAllSchedules(callback);
},
function(callback) {
getAllScheduleURLs(callback);
},
function(callback) {
var htmlStr = '';
schedules.forEach(function(schedule) {
htmlStr += '<div><input type="checkbox" id="' + schedule.id + '" value="' + schedule.http_cal_url + '">';
htmlStr += '<label for="' + schedule.id + '">' + schedule.summary + '</label></div>';
});
$('#calendar-list').html(htmlStr);
$(':checkbox').change(function() {
if ( $(':checked').length > 0 ) {
var urls = [];
$(':checked').each(function() {
urls.push($(this).val());
});
var PDcalURL = 'http://martindstone.github.io/PDcal/index.html?iCalURL=' + urls.join('&iCalURL=');
$('#calendar-url').html('<h1>PDcal URL for ' + urls.length + ' schedules:</h1><pre><a target="_blank" href="' + PDcalURL + '">' + PDcalURL + '</a></pre>');
}
});
$('.busy').hide();
console.log(schedules);
callback(null, "log");
}
]);
}
$(document).ready(main);