-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
101 lines (75 loc) · 2.44 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
const dotenv = require('dotenv').config();
const path = require('path');
const express = require('express');
const apollo = require('apollo-fetch');
const uri = 'https://api.github.com/graphql';
const app = express();
const apolloFetch = apollo.createApolloFetch({ uri });
const query = require('./query/participantsOfIssues');
app.set('view engine', 'ejs');
app.set('port', process.env.PORT || 9000);
app.listen(app.get('port'), () => {
console.log('Server running at http://localhost:9000');
});
apolloFetch.use(({ options }, next) => {
if (!options.headers) {
options.headers = {}; // Create the headers object if needed.
}
options.headers.Authorization = `bearer ${process.env.GITHUB_AUTH_TOKEN}`;
next();
});
const getParticipantsByIssues = (cursor = "") => {
let issuesParams = 'first:100';
if (cursor != '') {
issuesParams += `, after:"${cursor}"`;
}
return apolloFetch({
query: query(issuesParams),
});
};
const USERS = [];
const startCounting = (cursor, callback) => {
console.log('starting count', cursor);
getParticipantsByIssues(cursor).then((resp) => {
console.log('getParticipantsByIssues', cursor);
const repository = resp.data.repository;
const edgesLength = repository.issues.edges.length;
if (edgesLength >= 100) {
repository.issues.nodes.forEach((node) => {
if (node.author) {
const userPosition = USERS[node.author.login];
if (userPosition === undefined) {
USERS[node.author.login] = 0;
}
USERS[node.author.login] += 1;
node.participants.edges.forEach((edge) => {
const userCount = USERS[edge.node.login];
if (userCount === undefined) {
USERS[edge.node.login] = 0;
}
USERS[edge.node.login] += 1;
});
}
});
}
if (edgesLength === 0) {
return callback(USERS);
}
return startCounting(repository.issues.edges[edgesLength - 1].cursor, callback);
}).catch(error => console.error(error));
};
app.get('/', (req, res) => {
console.log('starting...');
startCounting('', function (users) {
var sortedUsers = [];
Object.keys(users).forEach(function(key, i) {
console.log(key, users[key]);
sortedUsers.push([key, users[key]]);
});
sortedUsers.sort(function (a, b) {
return b[1] - a[1];
})
console.log(sortedUsers);
res.render('./users.ejs', { users: sortedUsers });
})
});