Skip to content

Instantly share code, notes, and snippets.

@theY4Kman
Last active August 29, 2015 13:55
Show Gist options
  • Select an option

  • Save theY4Kman/8725285 to your computer and use it in GitHub Desktop.

Select an option

Save theY4Kman/8725285 to your computer and use it in GitHub Desktop.
Shows clickable assignees list to filter issues by
// ==UserScript==
// @name GitHub Issues list assignees
// @version 0.1
// @description enter something useful
// @match https://github.com/*/*/issues?*
// @match https://github.com/*/*/issues/*
// @match https://github.com/*/*/issues
// @grant GM_unsafeWindow
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_log
// @require https://raw.github.com/sizzlemctwizzle/GM_config/master/gm_config.js
// ==/UserScript==
function create_assignee_links() {
var GITHUB_TOKEN = GM_config.get('github_token');
if (GITHUB_TOKEN.length == 0) {
GM_config.open();
return;
}
var repo = document.querySelector('.js-current-repository').getAttribute('href');
var base_qs = (function(a) {
if (a == "") return {};
var b = {};
for (var i = 0; i < a.length; ++i)
{
var p=a[i].split('=');
if (p.length != 2) continue;
b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
}
return b;
})(window.location.search.substr(1).split('&'));
var currently_assigned = base_qs.assignee;
function get_assignee_url(username) {
base_qs['assignee'] = username;
var params = [];
for (key in base_qs)
params.push(key + '=' + encodeURIComponent(base_qs[key]));
return repo + '/issues?' + params.join('&');
}
var collaborator_list = document.getElementById('github-assignees-list');
if (collaborator_list == null) {
var filter_ul = unsafeWindow.document.querySelector('.issues-list-sidebar .filter-list');
if (filter_ul == null) {
// Most likely not on an issues list page
return;
}
var new_li = unsafeWindow.document.createElement('li');
filter_ul.appendChild(new_li);
var collaborator_list = unsafeWindow.document.createElement('ul');
collaborator_list.id = 'github-assignees-list';
collaborator_list.style.listStyle = 'none';
new_li.appendChild(collaborator_list);
}
collaborator_list.innerHTML = '';
var error = function() {
alert('There was an error retrieving the repo\'s assignees. Please ensure your GitHub API token is correct.');
GM_config.open();
};
var xhr = new XMLHttpRequest;
xhr.addEventListener('load', function() {
if (this.status == 401)
return error();
var collaborators = JSON.parse(this.response);
Array.prototype.forEach.call(collaborators, function(collaborator) {
var img = unsafeWindow.document.createElement('img');
img.src = collaborator.avatar_url;
img.alt = collaborator.login;
img.height = img.width = 32;
img.style.boxSizing = 'content-box';
img.style.borderRadius = '4px';
img.style.border = '3px solid transparent';
if (collaborator.login === currently_assigned) {
img.style.borderColor = '#4183c4';
}
var a = unsafeWindow.document.createElement('a');
a.href = get_assignee_url(collaborator.login);
a.appendChild(img);
var li = unsafeWindow.document.createElement('li');
li.style.display = 'inline-block';
li.className = 'assignee tooltipped rightwards';
li.setAttribute('original-title', 'Show issues assigned to ' + collaborator.login);
li.appendChild(a);
collaborator_list.appendChild(li);
});
});
xhr.addEventListener('abort', error);
xhr.open('GET', 'https://api.github.com/repos' + repo + '/assignees?access_token=' + GITHUB_TOKEN, true)
xhr.send();
}
GM_config.init({
'id': 'github_issues_list_assignees',
'title': 'Please enter a GitHub token (<a href="https://github.com/settings/tokens/new" target="_blank">generate one here</a>)',
'fields': {
'github_token': {
'label': 'GitHub API Token',
'type': 'text',
}
},
'css': '#github_issues_list_assignees_closeBtn { display: none; }' +
'.config_var { text-align:center; }'
});
GM_config.onSave = function() {
GM_config.close();
}
GM_config.onClose = function() {
if (GM_config.get('github_token').length == 0)
alert('Please enter a GitHub API token!');
setTimeout(create_assignee_links, 0);
}
create_assignee_links();
// When a link is clicked, it's loaded through AJAX, so we've gotta re-add the assignees list afterward
unsafeWindow.jQuery.ajaxSettings.xhr = function() {
var xhr = new unsafeWindow.XMLHttpRequest;
xhr.addEventListener('load', function() {
if (this.status !== 200)
return;
if (this.response.indexOf('pjax') === -1)
return;
// Because we're the first load listener to be called, wait till processing is complete
setTimeout(function() {
create_assignee_links();
}, 0);
});
return xhr;
};
@theY4Kman
Copy link
Copy Markdown
Author

Hahahaha, always good to commit API keys publicly!

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