forked from ryo-ma/github-profile-trophy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_api_client.ts
More file actions
158 lines (153 loc) 路 4.64 KB
/
Copy pathgithub_api_client.ts
File metadata and controls
158 lines (153 loc) 路 4.64 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
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
import { soxa } from "../deps.ts";
import { UserInfo } from "./user_info.ts";
import type {
GitHubUserActivity,
GitHubUserIssue,
GitHubUserPullRequest,
GitHubUserRepository,
} from "./user_info.ts";
export class GithubAPIClient {
constructor() {
}
async requestUserInfo(
token: string | undefined,
username: string,
): Promise<UserInfo | null> {
// Avoid timeout for the Github API
const results = await Promise.all([
this.requestUserActivity(token, username),
this.requestUserIssue(token, username),
this.requestUserPullRequest(token, username),
this.requestUserRepository(token, username),
]);
return new UserInfo(results[0]!, results[1]!, results[2]!, results[3]!);
}
async requestOrganizationStargazer(username: string, token: string | undefined): Promise<number> {
let count = 0;
const response = await this.restAPIRequest("https://api.github.com/user/orgs", token);
console.log(response.data == undefined);
console.log(typeof response);
for (var data in response.data) {
const url = response.data[data].repos_url;
const repoInfo = await this.restAPIRequest(`${url}?per_page=100`, token);
for (var repo in repoInfo.data) {
const contributorUrl = repoInfo.data[repo].contributors_url;
const contributorInfo = await this.restAPIRequest(contributorUrl, token);
for (var contributor in contributorInfo.data) {
if (contributorInfo.data[contributor].login == username) {
count += repoInfo.data[repo].stargazers_count;
}
}
}
}
return count;
}
private async requestUserActivity(
token: string | undefined,
username: string,
): Promise<GitHubUserActivity | null> {
const query = `
query userInfo($username: String!) {
user(login: $username) {
createdAt
contributionsCollection {
totalCommitContributions
restrictedContributionsCount
}
organizations(first: 1) {
totalCount
}
followers(first: 1) {
totalCount
}
}
}
`;
return await this.request(query, token, username);
}
private async requestUserIssue(
token: string | undefined,
username: string,
): Promise<GitHubUserIssue | null> {
const query = `
query userInfo($username: String!) {
user(login: $username) {
openIssues: issues(states: OPEN) {
totalCount
}
closedIssues: issues(states: CLOSED) {
totalCount
}
}
}
`;
return await this.request(query, token, username);
}
private async requestUserPullRequest(
token: string | undefined,
username: string,
): Promise<GitHubUserPullRequest | null> {
const query = `
query userInfo($username: String!) {
user(login: $username) {
pullRequests(first: 1) {
totalCount
}
}
}
`;
return await this.request(query, token, username);
}
private async requestUserRepository(
token: string | undefined,
username: string,
): Promise<GitHubUserRepository | null> {
const query = `
query userInfo($username: String!) {
user(login: $username) {
repositories(first: 100, ownerAffiliations: OWNER, orderBy: {direction: DESC, field: STARGAZERS}) {
totalCount
nodes {
languages(first: 3, orderBy: {direction:DESC, field: SIZE}) {
nodes {
name
}
}
stargazers {
totalCount
}
}
}
}
}
`;
return await this.request(query, token, username);
}
private async restAPIRequest(url: string, token: string | undefined) {
return soxa.get(url, {
headers: { Authorization: `bearer ${token}`, Accept: `application/vnd.github.v3+json` }
})
}
private async request(
query: string,
token: string | undefined,
username: string,
) {
const variables = { username: username };
const response = await soxa.post(
"https://api.github.com/graphql",
{},
{
data: { query: query, variables },
headers: { Authorization: `bearer ${token}` },
},
).catch((error) => {
console.error(error.response.data.errors[0].message);
});
if (response.status != 200) {
console.error(`Status code: ${response.status}`);
console.error(response.data);
}
return response.data.data.user;
}
}