-
Notifications
You must be signed in to change notification settings - Fork 0
/
github-helper.js
164 lines (134 loc) · 4.73 KB
/
github-helper.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
const fetch = require('node-fetch');
const { Users } = require("./collections/users");
const githubUserMock = require('./mocks/user.json');
const githubEventsMock = require('./mocks/events.json');
function getGithubData(username) {
const result = {};
return getGithubApi(username)
.then(json => {
const languages = getLanguages(json);
const repos = getRepos(json);
result.languages = languages;
result.repos = repos;
})
.then(() => {
return getIsTabs(username);
})
.then(isTabs => {
result.isTabs = isTabs;
return result;
});
}
function getRepos(json) {
if (json.map) {
var data = json.map(item => item.name).filter(item => item != null);
var Repos = new Set(data);
return Array.from(Repos);
} else {
return [];
}
}
function getLanguages(json) {
if (json.map) {
var data = json.map(item => item.language).filter(item => item != null);
var languages = new Set(data);
return Array.from(languages);
} else {
return [];
}
}
function getGithubApi(username) {
return Promise.resolve(githubUserMock);
return fetch("https://api.github.com/users/" + username + "/repos")
.then(res => res.json());
}
function getIsTabs(username) {
// this code doesn't really work
return Promise.resolve(githubEventsMock)
// return fetch("https://api.github.com/users/" + username + "/events")
// .then(res => res.json())
.then(data => {
return JSON.stringify(data, null, "\t");
})
.then(events => {
const urls = [events.match(/"https:\/\/api\.github\.com\/repos\/.*?\/.*?\/commits\/.*?"/g)[0]];
return Promise.all(urls.map(url => fetch(url.replace(/"/g, ""))))
})
.then(results => Promise.all(results.map(result => result.json())))
.then(results => {
return results.map(result => result.files);
})
.then(results => {
return [].concat.apply([], results);
})
.then(results => {
return results.map(result => result.patch);
})
.then(patches => {
return patches.some(patch => !!patch.match(/\t/))
});
}
async function matchUsers(userId) {
const userRef = Users.getUser(userId);
const user = await userRef.get();
var userLang = user.data().languages || [];
var userRest = user.data().restaurants || [];
var userRepos = user.data().repos || [];
var response = [];
return await Users.getAll().get()
.then(users => {
users.docs.filter(user=> user.id != userId).forEach(user => {
var commonlanguages = userLang.filter(value => {
var languages = user.data().languages
if (languages && languages.includes(value)) {
return true
}
return false
});
var commonrestaurants = userRest.filter(value => {
var restaurants = user.data().restaurants
if(restaurants && restaurants.map(r=>r.id).includes(value.id)){
return true
}
return false
});
var commonrepos = userRepos.filter(value => {
var repos = user.data().repos
if(repos && repos.includes(value)){
return true
}
return false
});
sum = commonrestaurants.length + commonlanguages.length + commonrepos.length
var finalRank;
if(sum>40){
finalRank=100;
}
else{
finalRank= Math.floor(Math.random() * (100 - 60) + 60)
}
response.push({
usedId: user.id,
username:user.data().name,
profilePic:user.data().profilePic,
rank: finalRank,
resRank: commonrestaurants.length,
langRank: commonlanguages.length,
reposRank: commonrepos.length,
restaurants:commonrestaurants,
languages:commonlanguages,
repos: commonrepos
})
});
/*
response.sort(function(a, b) {
return b.rank - a.rank;
});
*/
return response;
});
}
module.exports = {
getGithubData,
matchUsers
}