Skip to content

Instantly share code, notes, and snippets.

@olls
Created May 11, 2014 10:16
Show Gist options
  • Save olls/f25e94c7aabe71fdb310 to your computer and use it in GitHub Desktop.
Save olls/f25e94c7aabe71fdb310 to your computer and use it in GitHub Desktop.
A simple Github widget
function github_req(user, cb) {
makeRequest(
'https://api.github.com/users/' + user + '/events',
function (text) {
var data = JSON.parse(text);
cb(data);
}
);
}
var github_events = {
CommitCommentEvent: function (event, user, repo) {
return user+' commented on a commit in '+repo;
},
CreateEvent: function (event, user, repo) {
return user+' created '+repo;
},
DeleteEvent: function (event, user, repo) {
return user+' deleted '+repo;
},
DeploymentEvent: function (event, user, repo) {
return user+' deployed '+repo;
},
ForkEvent: function (event, user, repo) {
return user+' forked '+repo;
},
GollumEvent: function (event, user, repo) {
return user+' updated the wiki in '+repo;
},
IssueCommentEvent: function (event, user, repo) {
return user+' commented on an issue in '+repo;
},
IssuesEvent: function (event, user, repo) {
return user+' created an issue in '+repo;
},
MemberEvent: function (event, user, repo) {
return user+' was added to '+repo;
},
PageBuildEvent: function (event, user, repo) {
return null;
},
PublicEvent: function (event, user, repo) {
return user+' made '+repo+' open source';
},
PullRequestEvent: function (event, user, repo) {
return user+' created pull request on '+repo;
},
PullRequestReviewCommentEvent: function (event, user, repo) {
return user+' commented on a pull request on '+repo;
},
PushEvent: function (event, user, repo) {
return user+' pushed to '+repo;
},
ReleaseEvent: function (event, user, repo) {
return user+' released a new version of '+repo;
},
WatchEvent: function (event, user, repo) {
return user+' watched '+repo;
}
}
function github_widget(user, element) {
github_req(user, function (data) {
var github_list = document.createElement('ul');
data = data.slice(0, 10);
data.forEach(function (item) {
var user = item.actor.login.charAt(0).toUpperCase() + item.actor.login.slice(1); // Capitalize
var repo = '<a href="http://github.com/'+item.repo.name+'" target="_blank">'+item.repo.name+'</a>';
if (github_events[item.type]) {
var event_str = github_events[item.type](item, user, repo);
} else {
var event_str = item.type;
}
if (event_str) {
var li = document.createElement('li');
github_list.appendChild(li);
li.innerHTML = event_str;
li.setAttribute('data-type', item.type);
}
});
element.appendChild(github_list);
var more = document.createElement('a');
more.href = 'http://github.com/' + user;
more.innerText = 'More...'
more.target = '_blank';
element.appendChild(more);
});
}
function addEvent(elem, type, eventHandle) {
if (elem == null || typeof(elem) == 'undefined') return
if (elem.addEventListener) {
elem.addEventListener(type, eventHandle, false)
} else if (elem.attachEvent) {
elem.attachEvent("on" + type, eventHandle)
} else {
elem["on" + type] = eventHandle
}
}
function addAfter(elem, add) {
if (elem.nextSibling) {
elem.parentNode.insertBefore(add, elem.nextSibling)
} else {
elem.parentNode.appendChild(add)
}
}
function makeRequest(url, cb) {
var httpRequest
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
httpRequest = new XMLHttpRequest()
} else if (window.ActiveXObject) { // IE
try {
httpRequest = new ActiveXObject("Msxml2.XMLHTTP")
}
catch (e) {
try {
httpRequest = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e) {}
}
}
if (!httpRequest) {
console.log('Giving up :( Cannot create an XMLHTTP instance')
return false
}
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState === 4) {
if (httpRequest.status === 200) {
cb(httpRequest.responseText)
} else {
console.log('There was a problem with the request.')
}
}
}
httpRequest.open('GET', url)
httpRequest.send()
}
function sortByKey(array, key) {
return array.sort(function(a, b) {
var x = a[key]; var y = b[key];
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
});
}
@olls
Copy link
Author

olls commented May 13, 2014

Issues:

  • IssuesEvent is not just for creating issues, there are probably a few other events with the same issue.

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