Last active
April 25, 2017 10:28
-
-
Save mgol/9c95818bc3fafc25af69 to your computer and use it in GitHub Desktop.
jQuery bug migration script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var tickets = [ | |
// List of tickets to migrate. | |
"http://bugs.jquery.com/ticket/XXXX", | |
"http://bugs.jquery.com/ticket/YYYY", | |
]; | |
var request = require('request'), | |
jsdom = require('jsdom'), | |
apiPrefix = 'https://api.github.com/repos/jquery/jquery/'; | |
function getConfig() { | |
return { | |
method: 'POST', | |
auth: { | |
user: GENERATED_API_TOKEN, | |
pass: 'x-oauth-basic', | |
}, | |
headers: { | |
'User-Agent': 'mzgol-migration-script', | |
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8', | |
}, | |
}; | |
} | |
function handleTicket() { | |
var $, title, description, owner, milestone, blocking, blockedby, | |
config, version, reporter, | |
labels = [], | |
ticket = tickets[0]; | |
console.log('\n\n\n *** Ticket ' + ticket + ' *** '); | |
request(ticket, function (error, response, body) { | |
if (error || response.statusCode !== 200) { | |
console.error(ticket, error || response.statusCode); | |
setTimeout(function () { | |
handleTicket(); | |
}, 1000); | |
return; | |
} | |
tickets.shift(); | |
jsdom.env(body, ['http://code.jquery.com/jquery-2.1.1.js'], function (errors, window) { | |
if (errors) { | |
console.error(errors); | |
throw errors; | |
} | |
$ = window.$; | |
$.fn.fixBugLinks = function () { | |
return this.each(function () { | |
$(this).find('a[href^="/"]').each(function () { | |
$(this).attr('href', 'http://bugs.jquery.com' + | |
$(this).attr('href')); | |
}); | |
}); | |
} | |
title = $('#ticket .summary').text().trim(); | |
reporter = $('#ticket .properties [headers=h_reporter]').text().trim(); | |
description = 'Originally reported by ' + reporter + ' at: ' + ticket + '\n' + | |
($('#ticket .description .searchable').fixBugLinks().html() || '').trim(); | |
owner = $('#ticket .properties [headers=h_owner]').text().trim(); | |
if (owner === 'gibson0421') { | |
owner = 'gibson042'; | |
} | |
else if (owner === 'm_gol') { | |
owner = 'mzgol'; | |
} else if (owner.indexOf('@') !== -1 || | |
['pferreir', 'sylvain courcoux', 'jlukic'].indexOf(owner) !== -1) { | |
owner = undefined; | |
} | |
milestone = $('#ticket .properties [headers=h_milestone]').text().trim().toLowerCase(); | |
if (milestone === '1.12/2.2') { | |
milestone = 2; //'3.0.0'; | |
} | |
else if (milestone === '1.12') { | |
milestone = 2; //'3.0.0'; | |
labels.push('compat-only'); | |
} | |
else if (milestone === '2.2') { | |
milestone = 2; //'3.0.0'; | |
labels.push('master-only'); | |
} | |
else if (milestone === '1.next/2.next') { | |
milestone = 3; //'future'; | |
} | |
else if (milestone === '1.next') { | |
milestone = 3; //'future'; | |
labels.push('compat-only'); | |
} | |
else if (milestone === '2.next') { | |
milestone = 3; //'future'; | |
labels.push('master-only'); | |
} else { | |
milestone = undefined; | |
} | |
version = $('#ticket .properties [headers=h_version]').text().trim(); | |
blocking = $('#ticket .properties [headers=h_blocking]').text().trim(); | |
blockedby = $('#ticket .properties [headers=h_blockedby]').text().trim(); | |
if (version) { | |
description += '<p>Issue reported for jQuery ' + version + '</p>' | |
} | |
if (blocking) { | |
description += '<p>Blocking: ' + blocking + '</p>' | |
} | |
if (blockedby) { | |
description += '<p>Blocked by: ' + blockedby + '</p>' | |
} | |
// Create an issue. | |
console.log('Creating an issue: ', title); | |
config = getConfig(); | |
config.url = apiPrefix + 'issues'; | |
config.body = JSON.stringify({ | |
title: title, | |
body: description, | |
assignee: owner || undefined, | |
milestone: milestone, | |
labels: labels, | |
}); | |
console.log(config); | |
request(config, function (error, response, body) { | |
if (error || response.statusCode !== 201) { | |
console.error(error || [response.statusCode, response.body]); | |
throw new Error('Creating an issue failed.\n', title); | |
} | |
body = JSON.parse(body); | |
var issueNumber = body.number; | |
// Create comments. | |
var changes = $('#changelog > .change').toArray(); | |
function addComment() { | |
if (!changes.length) { | |
if (tickets.length) { | |
setTimeout(function () { | |
handleTicket(); | |
}); | |
} | |
return; | |
} | |
var config; | |
var change = changes.shift(); | |
var comment = $(change).find('.comment').fixBugLinks().html().trim(); | |
if (comment) { | |
var author = $(change) | |
.find('h3.change').text().replace(/(?:.|\n)*ago by /, '').trim(); | |
comment = '**Comment author: ' + author + '**\n' + comment; | |
config = getConfig(); | |
config.url = apiPrefix + 'issues/' + issueNumber + '/comments'; | |
config.body = JSON.stringify({ | |
body: comment, | |
}); | |
console.log('Creating a comment...'); | |
request(config, function (error, response, body) { | |
if (error || response.statusCode !== 201) { | |
console.error(error || [response.statusCode, response.body]); | |
throw new Error('Creating a comment failed.\n', comment); | |
} | |
setTimeout(function () { | |
addComment(); | |
}); | |
}); | |
} else { | |
setTimeout(function () { | |
addComment(); | |
}); | |
} | |
} | |
addComment(); | |
}); | |
}); | |
}); | |
} | |
handleTicket(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment