-
Notifications
You must be signed in to change notification settings - Fork 30
/
fetchContributorStats.ts
66 lines (61 loc) · 1.71 KB
/
fetchContributorStats.ts
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
/*
* This file is part of the Github Contributor Stats.
*
* (c) TaehyunHwang <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import * as dotenv from 'dotenv';
import axios from 'axios';
/**
* The Fetch Contributor Stats Function.
*
* This function holds the request for the github graphql APIs.
*
* @param {String} username The target github username for contribution stats.
*
* @return {*}
*/
const fetchContributorStats = async (username) => {
try {
const response = await axios({
url: 'https://api.github.com/graphql',
method: 'POST',
headers: {
Authorization: `token ${process.env.GITHUB_PERSONAL_ACCESS_TOKEN}`,
},
data: {
query: `query {
user(login: "${username}") {
id
repositoriesContributedTo(first :100, contributionTypes: COMMIT) {
totalCount
nodes {
owner {
id
avatarUrl
}
isInOrganization
url
homepageUrl
name
nameWithOwner
stargazerCount
openGraphImageUrl
}
}
}
}`,
},
});
if (response.status === 200) {
return response.data.data.user.repositoriesContributedTo.nodes;
}
return {};
} catch (error) {
console.error(error);
return error;
}
};
export { fetchContributorStats };