Page MenuHomePhabricator
Paste P5409

add to gcal rt round 2. keep it going!
ActivePublic

Authored by ArielGlenn on May 9 2017, 6:32 PM.
Tags
None
Referenced Files
F8115825: add to gcal rt round 2. keep it going!
May 18 2017, 12:56 PM
F8010453: add to gcal rt round 2. keep it going!
May 10 2017, 5:51 AM
F8000359: add to gcal rt round 2. keep it going!
May 9 2017, 6:32 PM
Subscribers
None
// ==UserScript==
// @name Maintenance AddToCal
// @namespace apergosmods
// @description Add maintenance event to calendar
// @include https://rt.wikimedia.org/Ticket/Display.html?*
// @version 1
// @grant none
// @run-at document-idle
// ==/UserScript==
// TODO: we hardcode Daylightsavings (for America) in here, see below.
// Find a workaround!
var daylight_savings = true;
if (daylight_savings) {
var centralzone = 'CDT';
var easternzone = 'EDT';
}
else {
centralzone = 'CST';
easternzone = 'EST';
}
function gcal_link_element(start_date, end_date, src, text, details, location) {
var start_date_gcal = start_date.toISOString().replace(/-|:|\.\d\d\d/g,'');
var end_date_gcal = end_date.toISOString().replace(/-|:|\.\d\d\d/g,'');
var link = document.createElement('a');
link.href = 'https://www.google.com/calendar/event?action=TEMPLATE';
link.href += '&src=' + encodeURIComponent(src);
link.href += '&text=' + encodeURIComponent(text);
link.href += '&details=' + encodeURIComponent(details);
link.href += '&location=' + encodeURIComponent(location);
link.href += '&dates=' + encodeURIComponent(start_date_gcal) + '/' + encodeURIComponent(end_date_gcal);
link.target = '_blank';
link.innerHTML = 'Gcal';
return link;
}
function append_action(actions, element) {
actions.appendChild(document.createTextNode('['));
actions.appendChild(element);
actions.appendChild(document.createTextNode(']'));
}
function hour_cleanup(hour) {
if (hour.match(/a.m./i)) {
hour = hour.replace(/a.m./i, ' AM');
}
else if (hour.match(/(pm|p.m.)/i)) {
hour = hour.replace(/p.m./i, ' PM');
}
// Now if there's no colon we better add some minutes in there
if (!hour.match(/:/)) {
hour = hour.replace(/([0-9]*)/, '$1:00');
}
return(hour);
}
var tm_interval_id = '';
function wait_for_message_transactions() {
var ticket_messages = document.getElementsByClassName('message transaction');
if (ticket_messages && ticket_messages.length > 0) {
clearInterval(tm_interval_id);
do_main();
}
}
function add_gcal_action() {
// wait for the message transactions to be generated by js. ugh.
tm_interval_id = setInterval(wait_for_message_transactions, 300);
}
function do_main() {
// now go pick up the ticket messages, they are ready
var ticket_messages = document.getElementsByClassName('message transaction');
var ticket_title = document.getElementById('header').innerText;
ticket_title = ticket_title.replace(/^(#[0-9]*:\s*)/, '');
var l3_date_re = /^Scheduled: (.*) to (.*)$/m;
var equinix_date_re = /^UTC:(.*)-(.*)$/m;
var equinix_spandate_re = /^SPAN: *([0-9]*-[A-Z]*-[0-9]*)\s*-\s*([0-9]*-[A-Z]*-[0-9]*)$/m;
var telia_date_re = /^Start Date and Time:(.*)$/m;
var telia_date2_re = /^End Date and Time:(.*)$/m;
var cyrusone_date_re = /^Activity Date: (.*)-(.*)$/m;
var cyrusone_date2_re = /^Activity Time: (.*)-(.*?)\s*([A-Z]*)$/m;
var zayo_date_re = /^Primary Date:\s*(.*)$/m;
var zayo_date2_re = /^Maintenance Window:\s*([0-9]+:[0-9]+)\s*-\s*([0-9]+:[0-9]+)\s*([a-zA-Z]+)$/m;
var ntt_date_re = /Start Date\/Time:\s+([0-9\- :]+UTC)/;
var ntt_date2_re = /End Date\/Time:\s+([0-9\- :]+UTC)/;
var l3_location_re = /^'MAINTENANCE LOCATIONS:\s*(.*)$/m;
var equinix_location_re = /^(IBX.*)$/m;
var telia_location_re = /^Location of Maintenance:\s*(.*)$/m;
var telia_location2_re = /^Location of work:\s*(.*)$/m;
var cyrusone_location_re = /^Activity Location:\s*(.*)$/m;
var zayo_location_re = /^Location of Maintenance:\s*(.*)$/m;
var details = window.location.href;
for (var i=0; i < ticket_messages.length; i++) {
var message = ticket_messages[i];
var actions = message.getElementsByClassName('actions')[0];
var bodies = message.getElementsByClassName('messagebody');
for (var j=0; j < bodies.length; j++) {
var body = bodies[j];
var match = l3_date_re.exec(body.innerText);
if (match) {
var start_date = new Date(match[1]);
var end_date = new Date(match[2]);
var location_match = l3_location_re.exec(body.innerText);
if (location_match) var location = location_match[1];
else location = '';
append_action(actions, gcal_link_element(start_date, end_date, calendar_id, ticket_title, details, location));
}
match = equinix_date_re.exec(body.innerText);
if (match) {
// Forcefully insert year and timezone for Date() to pick up
// span_date = /DATE: *([0-9]+-[A-Z]+-[0-9]+)/.exec(body.innerText);
var span_date = equinix_spandate_re.exec(body.innerText);
if (span_date) {
var sanitized = span_date[1].replace(/-/g, ' ');
var span_year_start = new Date(sanitized).getFullYear();
sanitized = span_date[2].replace(/-/g, ' ');
var span_year_end = new Date(sanitized).getFullYear();
}
else {
// No SPAN element, guess:
span_year_start = new Date().getFullYear();
span_year_end = span_year_start;
}
start_date = new Date(match[1] + ' ' + span_year_start + ' UTC');
end_date = new Date(match[2] + ' ' + span_year_end + ' UTC');
location_match = equinix_location_re.exec(body.innerText);
if (location_match) location = location_match[1];
else location = '';
append_action(actions, gcal_link_element(start_date, end_date, calendar_id, ticket_title, details, location));
}
match = telia_date_re.exec(body.innerText);
if (match) {
var end_date_match = telia_date2_re.exec(body.innerText);
start_date = new Date(match[1].replace(/-/g, ' '));
end_date = new Date(end_date_match[1].replace(/-/g, ' '));
location_match = telia_location_re.exec(body.innerText);
if (location_match) location = location_match[1];
else {
location_match = telia_location2_re.exec(body.innerText);
if (location_match) location = location_match[1];
else location = '';
}
append_action(actions, gcal_link_element(start_date, end_date, calendar_id, ticket_title, details, location));
}
match = cyrusone_date_re.exec(body.innerText);
if (match) {
// may or may not have the year. Here are two formats: ddMMM-ddMMM dd/mm/yyyy - dd/mm/yyyy
if (match[1].match(/\//)) {
start_date = match[1];
end_date = match[2];
}
else {
// ewww but what can you do.
var current_year = new Date().getFullYear();
start_date = match[1] + ' ' + current_year;
end_date = match[2] + ' ' + current_year;
}
// now get the times. two formats: '8am-5pm', '07:00 A.M. - 5:00 P.M. CDT' Grand isn't it?
match = cyrusone_date2_re.exec(body.innerText);
var start_hour = match[1];
var end_hour = match[2];
if (match[3]) {
// oh joy, we have a tz tacked on to the end
start_date += ' ' + match[3];
end_date += ' ' + match[3];
}
else {
start_date += ' ' + centralzone;
end_date += ' ' + centralzone;
}
start_hour = hour_cleanup(match[1]);
end_hour = hour_cleanup(match[2]);
start_date += ' ' + start_hour;
end_date += ' ' + end_hour;
start_date = new Date(start_date);
end_date = new Date(end_date);
location_match = cyrusone_location_re.exec(body.innerText);
if (location_match) location = location_match[1];
else location = '';
append_action(actions, gcal_link_element(start_date, end_date, calendar_id, ticket_title, details, location));
}
match = zayo_date_re.exec(body.innerText);
if (match) {
var sanitized_date = match[1].replace(/-/g, ' ');
match = zayo_date2_re.exec(body.innerText);
start_hour = match[1];
end_hour = match[2];
if (match[3] == 'Central') var timezone = centralzone;
else if (match[3] == 'Eastern') timezone = easternzone;
else timezone = '';
start_date = sanitized_date + ' ' + timezone + ' ' + start_hour;
end_date = sanitized_date + ' ' + timezone + ' ' + end_hour;
start_date = new Date(start_date);
end_date = new Date(end_date);
location_match = zayo_location_re.exec(body.innerText);
if (location_match) location = location_match[1];
else location = '';
append_action(actions, gcal_link_element(start_date, end_date, calendar_id, ticket_title, details, location));
}
match = ntt_date_re.exec(body.innerText);
if (match) {
end_date_match = ntt_date2_re.exec(body.innerText);
start_date = new Date(match[1].replace(/-/g, ' '));
end_date = new Date(end_date_match[1].replace(/-/g, ' '));
location = '';
append_action(actions, gcal_link_element(start_date, end_date, calendar_id, ticket_title, details, location));
}
}
}
}
var calendar_id = 'd2lraW1lZGlhLm9yZ181OXJwOTczY243NmV2YWdzcmlxanMwZXV0OEBncm91cC5jYWxlbmRhci5nb29nbGUuY29t';
add_gcal_action();