Created
June 26, 2013 01:42
-
-
Save jbowes/5864101 to your computer and use it in GitHub Desktop.
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 My Fancy New Userscript | |
// @namespace http://use.i.E.your.homepage/ | |
// @version 0.1 | |
// @description enter something useful | |
// @match https://github.com/*/pulls | |
// @copyright 2012+, James Bowes | |
// ==/UserScript== | |
// TODO: | |
// - Better flow for getting auth token | |
// - not modified checks | |
// - remove status icon if no status set at all | |
var TOKEN = "FILL ME IN"; | |
function main() { | |
GM_addStyle("li.build-status {float: right}"); | |
var parts = document.URL.split('/'); | |
var owner = parts[parts.length - 3]; | |
var repo = parts[parts.length - 2]; | |
var pullList = document.getElementsByClassName('pulls-list')[0]; | |
var pulls = pullList.getElementsByClassName('list-group-item'); | |
for (var i = 0; i < pulls.length; i++) { | |
annotatePull(pulls[i], {owner: owner, repo: repo}); | |
} | |
} | |
var baseStatus = 'build-status octicon status '; | |
function annotatePull(pull, info) { | |
var pullId = pull.getElementsByClassName('list-group-item-number')[0].innerHTML.substring(1); | |
var metaList = pull.getElementsByClassName('list-group-item-meta')[0]; | |
var statusItem = document.createElement('li'); | |
statusItem.setAttribute('class', baseStatus + 'octicon-clock'); | |
metaList.insertBefore(statusItem); | |
getStatus(pullId, statusItem, info); | |
} | |
function statusLoaded(responseText, pullId, statusItem) { | |
// only care about the most recent status | |
var status = JSON.parse(responseText)[0]; | |
var statusStr; | |
switch (status.state) { | |
case 'success': | |
statusStr = baseStatus + 'octicon-check status-success'; | |
break; | |
case 'failure': | |
statusStr = baseStatus + 'octicon-x status-failure'; | |
break; | |
case 'pending': | |
statusStr = baseStatus + 'octicon-primitive-dot status-pending'; | |
break; | |
default: | |
// unknown? problem! | |
statusStr = baseStatus + 'octicon-stop'; | |
break; | |
} | |
statusItem.setAttribute('class', statusStr); | |
} | |
function pullLoaded(responseText, pullId, statusItem, info) { | |
var pull = JSON.parse(responseText); | |
var ref = pull.head.sha; | |
var req = new XMLHttpRequest(); | |
req.onload = function() { | |
statusLoaded(this.responseText, pullId, statusItem); | |
} | |
req.open('get', 'https://api.github.com/repos/' + info.owner + '/' + info.repo + '/statuses/' + ref); | |
req.setRequestHeader('Authorization', 'token ' + TOKEN); | |
req.send(); | |
} | |
function getStatus(pullId, statusItem, info) { | |
var req = new XMLHttpRequest(); | |
req.onload = function() { | |
pullLoaded(this.responseText, pullId, statusItem, info); | |
} | |
req.open('get', 'https://api.github.com/repos/' + info.owner + '/' + info.repo + '/pulls/' + pullId); | |
req.setRequestHeader('Authorization', 'token ' + TOKEN); | |
req.send(); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment