-
Notifications
You must be signed in to change notification settings - Fork 2
/
gh.js
177 lines (165 loc) · 6.37 KB
/
gh.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
(function() {
"use strict";
function rateMessage(){
console.log("rate limit fail");
document.getElementById("results").style.display = 'none';
document.getElementById("instructions").style.display = 'block';
var message = "<p id=\"warn\">It looks like you've exceeded the GitHub API's rate "+
"limit, which is 60 requests per hour. Try again later, or from a "+
'different IP address. Alternately, enter a token (as found <a href='+
'"https://github.com/settings/tokens">here</a>)</p>';
document.getElementById("instructions").innerHTML = message;
}
// yay https://developer.github.com/v3/#cross-origin-resource-sharing
function handleRepoList() {
var data = JSON.parse(this.responseText);
console.log(data);
if (data.message){
if (data.message.match(/Not Found/)){
alert("Invalid user. Please try with a valid GitHub username.");
}
else if (data.message.match(/API rate limit/i)){
rateMessage();
}
else {
// make sure an unknown error doesn't get lost in the aether
var row = document.createElement("li");
row.appendChild(document.createTextNode(data.message));
document.getElementById("messages").appendChild(row);
}
// TODO, handle 'Bad credentials' more gracefully
}
else{
data.forEach(learnAboutRepo);
}
}
function learnAboutRepo(repoObj){
var token = document.getElementById("ghtoken").value;
var name = repoObj.name;
console.log(name);
var url = repoObj.url + '/contents';
var html_url = repoObj.html_url;
var isfork = repoObj.fork;
console.log(url);
var repoReq = new XMLHttpRequest();
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind
repoReq.onload = digInFiles.bind(repoReq, name, isfork, html_url);
repoReq.open("get", url, true);
if(token.length > 0) {
repoReq.setRequestHeader("Authorization", "token " + token);
}
repoReq.send();
}
function digInFiles(name, isfork, link){
console.log(name);
var repo = JSON.parse(this.responseText);
var licensefound = false;
var readmefound = false;
var contribfound = false;
repo.forEach(function(repo){
licensefound = !!(licensefound || repo.name.match(/license/i) || repo.name.match(/copying/i));
readmefound = !!(readmefound || repo.name.match(/readme/i));
contribfound = !!(contribfound || repo.name.match(/contributing/i));
});
console.log(repo);
// Wrap row creation in a function so that we get two different elements; can't append the same element twice
// cloneNode(deep) isn't supported on some old browsers I think, so avoid that
var createRow = function() {
var row = document.createElement("li");
var rowLink = document.createElement("a");
rowLink.href = link;
rowLink.appendChild(document.createTextNode(name));
row.appendChild(rowLink);
if (isfork) {
var isForkNode = document.createElement("i");
isForkNode.appendChild(document.createTextNode("(fork)"));
row.appendChild(isForkNode);
var forksHidden = document.querySelector("#hideforks").checked;
row.classList.add('fork');
if(forksHidden) {
row.classList.add('hide-fork');
}
}
return row;
};
if (repo.message){
// If we got back a message, that means we hit an error of some kind. Add it to the 'messages' thingy just so the user sees *something*
if (repo.message.match(/API rate limit/i)){
rateMessage();
}
else{
var row = createRow();
row.appendChild(document.createTextNode(repo.message));
document.getElementById("messages").appendChild(row);
}
}
else{
// try to do analytics with gaq
var _gaq = _gaq || [];
if (licensefound){
_gaq.push(['users._trackEvent', 'licenseFound', link]);
document.getElementById("haslicense").appendChild(createRow());
}
else{
_gaq.push(['users._trackEvent', 'licenseMissing', link]);
document.getElementById("lackslicense").appendChild(createRow());
}
if (readmefound){
_gaq.push(['users._trackEvent', 'readmeFound', link]);
document.getElementById("hasreadme").appendChild(createRow());
}
else{
_gaq.push(['users._trackEvent', 'readmeMissing', link]);
document.getElementById("lacksreadme").appendChild(createRow());
}
if (contribfound){
_gaq.push(['users._trackEvent', 'contribFound', link]);
document.getElementById("hascontrib").appendChild(createRow());
}
else{
_gaq.push(['users._trackEvent', 'contribMissing', link]);
document.getElementById("lackscontrib").appendChild(createRow());
}
}
}
function getUser(){
//this function called by clicking the stalk repos button
//first, clear any old results
document.getElementById("haslicense").innerHTML = "";
document.getElementById("lackslicense").innerHTML = "";
var user = document.getElementById('ghuser').value;
var token = document.getElementById("ghtoken").value;
// be stalkey, because why not
var _gaq = _gaq || [];
_gaq.push(['users._setAccount', 'UA-58732341-2']);
_gaq.push(['users._trackEvent', 'userChecked', user]);
var oReq = new XMLHttpRequest();
oReq.onload = handleRepoList;
var url = "https://api.github.com/users/" + user + "/repos";
oReq.open("get", url, true);
if(token.length > 0) {
oReq.setRequestHeader("Authorization", "token " + token);
}
oReq.send();
console.log("username " + user);
//make output visible and hide intsructions
document.getElementById("results").style.display = 'block';
document.getElementById("instructions").style.display = 'none';
}
document.querySelector('#ghform').addEventListener('submit', function(ev){
ev.preventDefault();
getUser();
return false;
});
document.querySelector("#hideforks").addEventListener('change', function(ev) {
var forkNodes = document.querySelectorAll(".fork");
for(var i = 0; i < forkNodes.length; i++) {
if (ev.target.checked) {
forkNodes[i].classList.add("hide-fork");
}
else {
forkNodes[i].classList.remove("hide-fork");
}
}
});
})();