-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
153 lines (131 loc) · 3.38 KB
/
server.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
var express = require('express');
var app = express();
var morgan = require('morgan');
var methodOverride = require('method-override');
var GitHubApi = require('github');
var fs = require('fs');
var Promise = require('promise');
/* Configuration */
var github = new GitHubApi({
// required
version: "3.0.0",
// optional
debug: true,
protocol: "https",
host: "api.github.com",
timeout: 5000
});
/* Including your github credentials makes
* the api queries faster
github.authenticate({
type: "basic",
username: <USERNAME>,
password: <PASSWORD>
});
*/
/* Spins up the web app server */
app.use(express.static(__dirname + '/public'));
app.use(morgan('dev'));
app.use(methodOverride());
app.listen(8080);
console.log("App listening on port 8080");
/* Routing API */
app.post('/api/githubmap/:gh_user', function(req, res) {
/* Calls the function call to gather and send
* the github map in JSON format */
getFromUser(req.params.gh_user).then( function(data) {
res.send(string);
});
});
/* Github Backend */
/* Global vars */
var num_repos = 0; /* number of repos */
var watch_count = 0; /* number of watchers */
var string = ''; /* initially empty JSON resultant */
/**
* @name getWatchers
*
* @desc
* This function takes a given repo name and
* gets all the watchers of the repo and fills
* the global string variable accordingly.
*
* @param user_name
* github user name
*
* @param repo_name
* github user's repository name
*/
function getWatchers(user_name, repo_name) {
return new Promise( function(fulfill, reject) {
github.repos.getWatchers({
user: user_name,
repo: repo_name
}, function(err, res) {
var watchers = JSON.parse(JSON.stringify(res));
string = string.concat('{"name": "' +
repo_name + '","children": [');
while (watchers.length > 0) {
var watchers_login = JSON.parse(
JSON.stringify(watchers.pop().login));
string = string.concat('{"name": "' +
watchers_login + '", "size": 3000}');
if (watchers.length > 0) {
string = string.concat(',');
}
}
string = string.concat(']}');
/* Hacky approach */
/* Uses the watchers count against the
* number of repos present to determine
* when to no longer expect anymore responses
* from this api call, if the watchers count
* exceeds the number of repos something went
* wrong. */
watch_count++;
if (watch_count == num_repos) {
fulfill(res);
} else if (watch_count > num_repos) {
reject(err);
} else {
string = string.concat(',');
}
});
});
}
/**
* @name getFromUser
*
* @desc
* This function takes a github username and
* determines all of his repos, then calls
* getWatchers().
*
* @param user_name
* Github username
*/
function getFromUser(user_name) {
/* Reset the watch count
* and JSON result
*/
watch_count = 0;
string = '';
string = string.concat('{"name": "' +
user_name + '","children": [');
return new Promise( function(fulfill, reject) {
github.repos.getFromUser({
user: user_name
}, function(err, res) {
var repos = JSON.parse(JSON.stringify(res));
num_repos = repos.length;
while (repos.length > 0) {
var repo_name = JSON.parse(
JSON.stringify(repos.pop().name));
getWatchers(user_name, repo_name).done( function(res) {
string = string.concat(']}');
fulfill(res);
}, reject);
}
});
});
}