This repository was archived by the owner on Dec 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathfetch.js
More file actions
108 lines (98 loc) · 3.1 KB
/
fetch.js
File metadata and controls
108 lines (98 loc) · 3.1 KB
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
const axios = require("axios");
require("dotenv").config();
const { multiTokenizer } = require("./multitokenizer");
// Github Search REST API currently does not support sorting repositories by number of stars
const fetchTotalStars = (parameters, token) => {
return axios({
url: "https://api.github.com/graphql",
method: "post",
headers: {
Authorization: `bearer ${token}`,
},
data: {
query: `
query userInfo($login: String!) {
user(login: $login) {
repositories(first: 100, ownerAffiliations: OWNER, orderBy: {direction: DESC, field: STARGAZERS}) {
totalCount
nodes {
stargazers {
totalCount
}
}
}
}
}
`,
variables: parameters,
},
}).then((e) =>
e.data.data.user.repositories.nodes.reduce((prev, curr) => {
return prev + curr.stargazers.totalCount;
}, 0)
);
};
// https://docs.github.com/en/free-pro-team@latest/rest/reference/search#search-commits
const fetchTotalCommits = (variables, token) => {
return axios({
method: "get",
url: `https://api.github.com/search/commits?q=author:${variables.login}+committer-date:>2021-01-01`,
headers: {
"Content-Type": "application/json",
Accept: "application/vnd.github.cloak-preview",
Authorization: `bearer ${token}`,
},
}).then((e) => e);
};
//https://docs.github.com/en/free-pro-team@latest/rest/reference/search#search-issues-and-pull-requests
const fetchTotalIssues = (params, token) => {
return axios({
method: "get",
url: `https://api.github.com/search/issues?q=author:${params.login}+is:issue+created:2021-01-01..2021-12-31`,
headers: {
"Content-Type": "application/json",
Accept: "application/vnd.github.cloak-preview",
Authorization: `bearer ${token}`,
},
}).then((e) => e);
};
//https://docs.github.com/en/free-pro-team@latest/rest/reference/search#search-issues-and-pull-requests
const fetchTotalPRs = (params, token) => {
return axios({
method: "get",
url: `https://api.github.com/search/issues?q=author:${params.login}+is:pr+created:2021-01-01..2021-12-31`,
headers: {
"Content-Type": "application/json",
Accept: "application/vnd.github.cloak-preview",
Authorization: `bearer ${token}`,
},
}).then((e) => e);
};
const statsFetch = async (parameters) => {
const _stats = {
pr: 0,
stars: 0,
issues: 0,
commits: 0,
};
_stats.commits = await fetcher(fetchTotalCommits, parameters);
_stats.issues = await fetcher(fetchTotalIssues, parameters);
_stats.pr = await fetcher(fetchTotalPRs, parameters);
_stats.stars = await fetchTotalStars(parameters, process.env.TOKEN_1);
return _stats;
};
const fetcher = async (func, params) => {
try {
// let res = await func(params, process.env.TOKEN_1);
// return res;
let res = await multiTokenizer(func, params, 0);
return res;
} catch (e) {
console.log(e);
return 0;
}
};
export async function fetchStats(username) {
let res = await statsFetch({ login: username });
return res;
}