-
Notifications
You must be signed in to change notification settings - Fork 42
/
index.js
221 lines (169 loc) · 5.96 KB
/
index.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
var validator = require('validator'),
https = require('https');
require('handlebars/runtime');
if (window.location.href.indexOf('?') > -1) {
// Form submitted
// Render the mini search bar
var search = Handlebars.templates.search(),
body = document.getElementsByTagName("body")[0];
body.innerHTML = search + "\n\n" + body.innerHTML;
// Render the results
render();
} else {
// Index page
var html = "<h2>High Scores</h2>\n" +
"<p>Please enter a Github repository URL</p>\n";
document.getElementsByClassName('wrapper')[0].innerHTML += html;
show_form();
}
// Process the search form
// Handles the mini search forms, as well as the one on the home page
document.getElementById('form').onsubmit = function(e) {
// Handle form using process_form()
e.preventDefault();
process_form( document.getElementById('form').url.value );
return false;
};
function show_form() {
var form = "<form action='' id='form'>\n" +
"\t<input type='text' size='24' name='url' />\n" +
"\t<input type='submit' value='1 UP'/>\n" +
"</form>\n";
// Display the form
document.getElementsByClassName('wrapper')[0].innerHTML += form;
}
function process_form(input) {
// check whether the input is a valid URL
var url = sanitize_url(input)
.then(function(url) {
var isURL = validator.isURL(url, {
protocols: ['http','https'],
allow_underscores: true
});
if(isURL) {
// if it's a valid url, redirect to ?user/repo
var user = get_user_from_github_url(url),
repo = get_repo_from_github_url(url);
window.location = window.location.origin + '/github-high-scores/?' + user + '/' + repo;
} else if(url !== '') {
// if the user entered an invalid url, display error
process_error();
}
}, function(err) {
// couldn't sanitize url, display error
process_error();
});
}
function process_error() {
var error = "<h2>404</h2>\n" +
"<p>Sorry, but this cat is in another castle!</p>\n" +
"<div class='align-center'>\n" +
"\t<img src='/github-high-scores/img/octocat.png'>\n" +
"</div>";
document.getElementsByClassName('wrapper')[0].innerHTML = error;
show_form();
}
function render() {
var hash = window.location.search.replace('?','').split('/'),
user = hash[0],
repo = hash[1];
get_high_scores(user,repo)
.then(function(scores) {
// get the handlebars template
var template = Handlebars.templates.results;
var html = "<h2><a href='https://github.com/" + user + "'>" + user + "</a> >> <a href='/github-high-scores/?" + user + "/" + repo + "'>" + repo + "</a> >> <span class='white'>High Scores</span></h2>\n" +
template({scores,user,repo});
document.getElementsByClassName('wrapper')[0].innerHTML = html;
});
}
function get_user_from_github_url(sanitized_github_url) {
return sanitized_github_url.split('/')[3];
}
function get_repo_from_github_url(sanitized_github_url) {
return sanitized_github_url.split('/')[4];
}
function get_high_scores(user, repo) {
return new Promise(function(resolve, reject) {
get_contributors(user,repo)
.then(function(response) {
// empty array to contain results
var results = [];
// score = contributions * 100
// iterate through each user
Promise.all( response.map( function(user) {
// add current user to the results array
return {
username : user.login,
score : user.contributions * 100,
avatar : user.avatar_url
};
})).then( function(results) {
// once the users have been processed, return the results
resolve(results);
});
})
.catch(function(error) {
// if there's an error, reject the promise
if(typeof(error) === 'undefined') {
reject('Repository not found');
return;
}
reject(error);
return;
});
});
}
function get_contributors(user, repo) {
return new Promise(function(resolve, reject) {
try {
var options = {
host: 'api.github.com',
path: '/repos/' + user + '/' + repo + '/contributors'
};
var callback = function(response) {
var str = '';
//another chunk of data has been recieved, so append it to `str`
response.on('data', function (chunk) {
str += chunk;
});
//the whole response has been recieved, so we just print it out here
response.on('end', function () {
resolve(JSON.parse(str));
});
};
https.request(options, callback).end();
} catch(e) {
reject('test:' + e);
}
});
}
function sanitize_url(unsanitized_url) {
return new Promise(function(resolve, reject) {
if( typeof(unsanitized_url) === 'undefined' || '' === unsanitized_url) {
reject('No query');
return;
}
var url = unsanitized_url.toLowerCase();
// Check the start of the URL
if (url.substring(0, 10) === 'github.com') {
// Special rules for Github URLs starting with 'github.com'
url = 'https://www.github.com' + url.substring(10);
} else if (url.substring(0, 14) === 'www.github.com') {
// Special rules for Github URLs starting with 'www.github.com'
url = 'https://www.github.com' + url.substring(14);
}
// Check the end of the URL
if (url.substring(url.length - 4) === '.git') {
// Special rules for Github URLs ending in 'git'
url = url.substring(0, url.length - 4);
}
url = url.replace("http://", "https://").replace("git@github.com:", "https://www.github.com/").replace("git://", "https://www.");
// Check if someone just passes in user/repo e.g. leereilly/leereilly.net
// Trim leading/trailing slash
tokens = url.replace(/^\/|\/$/g, '').split('/');
if (tokens.length === 2) {
url = "https://www.github.com/" + tokens[0] + "/" + tokens[1];
}
resolve(url);
});
}