Avoid timeout for the GitHub API

This commit is contained in:
ryo-ma 2021-10-31 14:08:15 +09:00
parent 6da9763bff
commit b2522282b3
4 changed files with 120 additions and 43 deletions

View file

@ -1,2 +1,2 @@
export { ServerRequest } from "https://deno.land/std@0.66.0/http/server.ts";
export { soxa } from "https://deno.land/x/soxa@v1.3/mod.ts";
export { soxa } from "https://deno.land/x/soxa@1.4/mod.ts";

View file

@ -50,7 +50,8 @@ export default async (req: ServerRequest) => {
);
return;
}
const userInfo = await client.requestUserInfo(username);
const token = Deno.env.get("GITHUB_TOKEN");
const userInfo = await client.requestUserInfo(token, username);
if (userInfo === null) {
req.respond(
{

View file

@ -1,13 +1,32 @@
import { soxa } from "../deps.ts";
import { UserInfo } from "./user_info.ts"
import type { GitHubUserData } from "./user_info.ts";
import { UserInfo } from "./user_info.ts";
import type {
GitHubUserActivity,
GitHubUserIssue,
GitHubUserPullRequest,
GitHubUserRepository,
} from "./user_info.ts";
export class GithubAPIClient {
constructor() {
}
async requestUserInfo(username: string): Promise<UserInfo | null> {
const token = Deno.env.get("GITHUB_TOKEN");
const variables = { username: username };
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]!);
}
private async requestUserActivity(
token: string | undefined,
username: string,
): Promise<GitHubUserActivity | null> {
const query = `
query userInfo($username: String!) {
user(login: $username) {
@ -16,18 +35,54 @@ export class GithubAPIClient {
totalCommitContributions
restrictedContributionsCount
}
pullRequests(first: 1) {
totalCount
}
issues(first: 1) {
totalCount
}
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) {
issues(first: 1) {
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, isFork: false, orderBy: {direction: DESC, field: STARGAZERS}) {
totalCount
nodes {
@ -44,6 +99,14 @@ export class GithubAPIClient {
}
}
`;
return await this.request(query, token, username);
}
private async request(
query: string,
token: string | undefined,
username: string,
) {
const variables = { username: username };
const response = await soxa.post(
"https://api.github.com/graphql",
{},
@ -51,11 +114,13 @@ export class GithubAPIClient {
data: { query: query, variables },
headers: { Authorization: `bearer ${token}` },
},
);
).catch((error) => {
console.error(error.response.data.errors[0].message);
});
if (response.status != 200) {
throw new Error(response)
console.error(`Status code: ${response.status}`);
console.error(response.data);
}
const userData: GitHubUserData = response.data.data.user;
return new UserInfo(userData);
return response.data.data.user;
}
}

View file

@ -4,28 +4,37 @@ type Repository = {
languages: { nodes: Language[] };
stargazers: Stargazers;
};
export type GitHubUserData = {
export type GitHubUserRepository = {
repositories: {
totalCount: number;
nodes: Repository[];
};
};
export type GitHubUserIssue = {
issues: {
totalCount: number;
};
};
export type GitHubUserPullRequest = {
pullRequests: {
totalCount: number;
};
};
export type GitHubUserActivity = {
createdAt: string;
contributionsCollection: {
totalCommitContributions: number;
restrictedContributionsCount: number;
};
pullRequests: {
totalCount: number;
};
issues: {
totalCount: number;
};
organizations: {
totalCount: number;
};
followers: {
totalCount: number;
};
repositories: {
totalCount: number;
nodes: Repository[];
};
};
export class UserInfo {
public readonly totalCommits: number;
@ -40,12 +49,15 @@ export class UserInfo {
public readonly ancientAccount: number;
public readonly joined2020: number;
constructor(
userData: GitHubUserData,
userActivity: GitHubUserActivity,
userIssue: GitHubUserIssue,
userPullRequest: GitHubUserPullRequest,
userRepository: GitHubUserRepository,
) {
const totalCommits =
userData.contributionsCollection.restrictedContributionsCount +
userData.contributionsCollection.totalCommitContributions;
const totalStargazers = userData.repositories.nodes.reduce(
userActivity.contributionsCollection.restrictedContributionsCount +
userActivity.contributionsCollection.totalCommitContributions;
const totalStargazers = userRepository.repositories.nodes.reduce(
(prev: number, node: Repository) => {
return prev + node.stargazers.totalCount;
},
@ -53,7 +65,7 @@ export class UserInfo {
);
const languages = new Set<string>();
userData.repositories.nodes.forEach((node: Repository) => {
userRepository.repositories.nodes.forEach((node: Repository) => {
if (node.languages.nodes != undefined) {
node.languages.nodes.forEach((node: Language) => {
if (node != undefined) {
@ -63,21 +75,20 @@ export class UserInfo {
}
});
const durationTime = new Date().getTime() -
new Date(userData.createdAt).getTime();
new Date(userActivity.createdAt).getTime();
const durationYear = new Date(durationTime).getUTCFullYear() - 1970;
const ancientAccount = new Date(userData.createdAt).getFullYear() <= 2010
? 1
: 0;
const joined2020 = new Date(userData.createdAt).getFullYear() == 2020
const ancientAccount =
new Date(userActivity.createdAt).getFullYear() <= 2010 ? 1 : 0;
const joined2020 = new Date(userActivity.createdAt).getFullYear() == 2020
? 1
: 0;
this.totalCommits = totalCommits;
this.totalFollowers = userData.followers.totalCount;
this.totalIssues = userData.issues.totalCount;
this.totalOrganizations = userData.organizations.totalCount;
this.totalPullRequests = userData.pullRequests.totalCount;
this.totalFollowers = userActivity.followers.totalCount;
this.totalIssues = userIssue.issues.totalCount;
this.totalOrganizations = userActivity.organizations.totalCount;
this.totalPullRequests = userPullRequest.pullRequests.totalCount;
this.totalStargazers = totalStargazers;
this.totalRepositories = userData.repositories.totalCount;
this.totalRepositories = userRepository.repositories.totalCount;
this.languageCount = languages.size;
this.durationYear = durationYear;
this.ancientAccount = ancientAccount;