Created
April 18, 2009 04:52
-
-
Save paltman/97440 to your computer and use it in GitHub Desktop.
Greasemonkey Userscript to show commit detail in FogBugz tickets for GitHub commits
This file contains 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
// ==UserScript== | |
// @name githubugz | |
// @namespace paltman.com | |
// @description Show commit details in FogBugz Tickets | |
// @include https://*.fogbugz.com | |
// ==/UserScript== | |
// Set up configuration values by going to about:config in Firefox and changing | |
// the following parameters: | |
// | |
// greasemonkey.scriptvals.paltman.com/githubugz.user | |
// greasemonkey.scriptvals.paltman.com/githubugz.repo | |
// greasemonkey.scriptvals.paltman.com/githubugz.login | |
// greasemonkey.scriptvals.paltman.com/githubugz.token | |
// greasemonkey.scriptvals.paltman.com/githubugz.commit_regex -- defaults to 'Commit [a-f0-9]{40}' | |
(function() { | |
var gh_localVersion = "0.0.1"; | |
var gh_user = GM_getValue('user', null); | |
var gh_repo = GM_getValue('repo', null); | |
var gh_login = GM_getValue('login', null); | |
var gh_token = GM_getValue('token', null); | |
if (gh_user && gh_repo && gh_login && gh_token) { | |
var regex = GM_getValue('commit_regex', 'Commit: [a-f0-9]{40}'); | |
var gh_commit_sha = new RegExp(regex); | |
var gh_sc = null; | |
var gh_sc2 = null; | |
var gh_api_url = 'https://github.com/api/v2/json/commits/show/'; | |
var debugMode = true; | |
var jQuery = null; | |
function init() { | |
jQuery = this.jQuery; | |
if (jQuery == null) { | |
if (gh_sc == null) { | |
gh_sc = document.createElement('script'); | |
gh_sc.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js'; | |
document.getElementsByTagName('head')[0].appendChild(gh_sc); | |
} | |
setTimeout(init, 500); | |
} else { | |
showInfo(); | |
} | |
} | |
init(); | |
function findSha(string_to_parse) { | |
var m = gh_commit_sha.exec(string_to_parse); | |
if (m != null && m.length == 1) { | |
var u = m[0].replace('Commit: ', ''); | |
return u; | |
} | |
return null; | |
} | |
function extend(destination, source) { | |
for (var property in source) destination[property] = source[property]; | |
return destination; | |
} | |
function log(message) { | |
if (debugMode) { | |
for (var i = 1; i < arguments.length; i++); | |
message = message.replace('%s', arguments[i]); | |
if (typeof GM_log == "function") GM_log(message); | |
else if (window.console) console.log(message); | |
} | |
} | |
function ajax(params) { | |
var defaults = { | |
method: 'GET', | |
onerror: function(response) { | |
log('ERROR ' + response.status); | |
} | |
} | |
var defaultHeaders = { | |
'X-Requested-With': 'XMLHttpRequest', | |
'Accept': 'application/json, text/javascript, text/html, */*' | |
} | |
params = extend(defaults, params); | |
params.headers = extend(defaultHeaders, params.headers || {}); | |
if (typeof params.data == 'object') { | |
params.data = objectToQueryString(params.data); | |
params.headers['Content-type'] = 'application/x-www-form-urlencoded; charset=UTF-8'; | |
} | |
return GM_xmlhttpRequest(params); | |
} | |
function loadJSON(url, onload, params) { | |
params = extend({url: url, onload: onload}, params || {}); | |
var handler = params.onload; | |
params.onload = function(response) { | |
if (typeof response.getResponseHeader == 'function') { | |
// native XMLHttpRequest interface | |
var responseType = (response.getResponseHeader('Content-type') || '').split(';')[0]; | |
} else { | |
// GM_xmlhttpRequest interface | |
var responseType = (response.responseHeaders.match(/^Content-[Tt]ype:\s*([^\s;]+)/m) || [])[1]; | |
} | |
if (responseType == 'application/json' || responseType == 'text/javascript') { | |
var object = eval("(" + response.responseText + ")"); | |
if (object) handler(object, response); | |
} | |
} | |
return ajax(params); | |
} | |
function showInfo() { | |
sha = findSha(document.getElementsByTagName('body')[0].innerHTML); | |
if (sha != null) { | |
url = gh_api_url + gh_user + "/" + gh_repo + "/" + sha + "/?login=" + gh_login + "&token=" + gh_token; | |
loadJSON(url, function(updates) { | |
el = jQuery('div.bugevent div.body:contains(Commit: ' + sha + ')'); | |
html = '<div class="commit-details"><h1>Commit Details</h1><ul><li><b>Author:</b> '; | |
html += updates.commit.author.name + '</li><li><b>Message:</b> ' + updates.commit.message + "</li>"; | |
html += '<li><b>Commit: </b> <a href="'+ updates.commit.url +'">' + updates.commit.id + "</a></li></ul>"; | |
for (i = 0; i < updates.commit.modified.length; i++) { | |
html += '<div class="diff"><pre>' + updates.commit.modified[i].diff + '</pre>'; | |
html += '<span class="changed-file">' + updates.commit.modified[i].filename + '</span></div>'; | |
} | |
html += '</div>'; | |
el.append(html); | |
jQuery('div.commit-details ul').css('margin-top', '5px'); | |
jQuery('div.diff pre').css('background-color', '#000').css('color', '#EEE').css('padding', '8px'); | |
jQuery('div.commit-details h1').css('color', '#900'); | |
jQuery('span.changed-file').css('font-size', '10pt').css('font-style', 'italic'); | |
}); | |
} | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment