Skip to content

Instantly share code, notes, and snippets.

@jlongster
Created October 14, 2011 20:00
Show Gist options
  • Save jlongster/1288142 to your computer and use it in GitHub Desktop.
Save jlongster/1288142 to your computer and use it in GitHub Desktop.
Bugzilla jsonrpc
function jsonrpc(user, method, params, http_method, cont) {
if(_.isFunction(http_method)) {
cont = http_method;
http_method = 'GET';
}
var request;
var opts = {
host: 'bugzilla.mozilla.org',
path: '/jsonrpc.cgi',
method: http_method,
};
if(user) {
opts['headers'] = {
'Cookie': 'Bugzilla_login=' + user.login + '; ' +
'Bugzilla_logincookie=' + user.logincookie
}
}
var query = {
method: method,
params: params
};
if(http_method == 'GET') {
query.params = JSON.stringify(query.params);
opts.path += '?' + qs.stringify(query);
// For some reason, I have to use get explicitly. Otherwise I
// get a socket hangup.
request = https.get;
}
else {
request = https.request;
}
var content = '';
var req = request(opts, function(res) {
res.on('data', function(chunk) {
content += chunk;
});
res.on('end', function() {
try {
if(content.trim().length > 0) {
content = JSON.parse(content);
}
cont(null, res, content);
}
catch (e) {
console.log(e.message);
cont('JSON parse error', res, null);
}
});
});
req.on('error', function(e) {
cont('Authentication or server error');
});
if(http_method == 'POST') {
req.write(JSON.stringify(query));
req.end();
}
}
function login(user, pass, cont) {
jsonrpc(null, 'User.login', [{"login": user, "password": pass}], 'POST', function(err, res) {
var cookies = {};
if('set-cookie' in res.headers) {
var raw = res.headers['set-cookie'];
for(var i=0; i<raw.length; i++) {
v = connect.utils.parseCookie(raw[i]);
for(var key in v) {
cookies[key] = v[key];
}
}
cont(null,
{name: user,
login: cookies['bugzilla_login'],
logincookie: cookies['bugzilla_logincookie']});
}
else {
cont('Authentication error');
}
});
}
bz.login('jlong@mozilla.com', '******', function(err, user) {
bz.jsonrpc(user, 'Bug.get', [{ids:['475810']}], 'POST', function(err, res, data) {
console.log(err, data);
});
});
@jlongster
Copy link
Author

Response headers:

{ server: 'Apache',
'x-backend-server': 'pp-app-bugs02',
'content-type': 'application/json; charset=UTF-8',
'strict-transport-security': 'max-age=2629744; includeSubDomains',
date: 'Fri, 14 Oct 2011 21:19:43 GMT',
'keep-alive': 'timeout=300, max=1000',
connection: 'close',
'x-frame-options': 'SAMEORIGIN',
'content-length': '0' }

null ''
james:~/projects/sites/omgbugs(master)%

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment