mirror of
https://github.com/reiyua/github-profile-trophy-reiyua-mirror.git
synced 2026-11-17 06:02:09 +11:00
Avoid timeout for the GitHub API
This commit is contained in:
parent
6da9763bff
commit
b2522282b3
4 changed files with 120 additions and 43 deletions
2
deps.ts
2
deps.ts
|
|
@ -1,2 +1,2 @@
|
||||||
export { ServerRequest } from "https://deno.land/std@0.66.0/http/server.ts";
|
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";
|
||||||
|
|
|
||||||
3
index.ts
3
index.ts
|
|
@ -50,7 +50,8 @@ export default async (req: ServerRequest) => {
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const userInfo = await client.requestUserInfo(username);
|
const token = Deno.env.get("GITHUB_TOKEN");
|
||||||
|
const userInfo = await client.requestUserInfo(token, username);
|
||||||
if (userInfo === null) {
|
if (userInfo === null) {
|
||||||
req.respond(
|
req.respond(
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,32 @@
|
||||||
import { soxa } from "../deps.ts";
|
import { soxa } from "../deps.ts";
|
||||||
import { UserInfo } from "./user_info.ts"
|
import { UserInfo } from "./user_info.ts";
|
||||||
import type { GitHubUserData } from "./user_info.ts";
|
import type {
|
||||||
|
GitHubUserActivity,
|
||||||
|
GitHubUserIssue,
|
||||||
|
GitHubUserPullRequest,
|
||||||
|
GitHubUserRepository,
|
||||||
|
} from "./user_info.ts";
|
||||||
|
|
||||||
export class GithubAPIClient {
|
export class GithubAPIClient {
|
||||||
constructor() {
|
constructor() {
|
||||||
}
|
}
|
||||||
async requestUserInfo(username: string): Promise<UserInfo | null> {
|
async requestUserInfo(
|
||||||
const token = Deno.env.get("GITHUB_TOKEN");
|
token: string | undefined,
|
||||||
const variables = { username: username };
|
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 = `
|
const query = `
|
||||||
query userInfo($username: String!) {
|
query userInfo($username: String!) {
|
||||||
user(login: $username) {
|
user(login: $username) {
|
||||||
|
|
@ -16,18 +35,54 @@ export class GithubAPIClient {
|
||||||
totalCommitContributions
|
totalCommitContributions
|
||||||
restrictedContributionsCount
|
restrictedContributionsCount
|
||||||
}
|
}
|
||||||
pullRequests(first: 1) {
|
|
||||||
totalCount
|
|
||||||
}
|
|
||||||
issues(first: 1) {
|
|
||||||
totalCount
|
|
||||||
}
|
|
||||||
organizations(first: 1) {
|
organizations(first: 1) {
|
||||||
totalCount
|
totalCount
|
||||||
}
|
}
|
||||||
followers(first: 1) {
|
followers(first: 1) {
|
||||||
totalCount
|
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}) {
|
repositories(first: 100, ownerAffiliations: OWNER, isFork: false, orderBy: {direction: DESC, field: STARGAZERS}) {
|
||||||
totalCount
|
totalCount
|
||||||
nodes {
|
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(
|
const response = await soxa.post(
|
||||||
"https://api.github.com/graphql",
|
"https://api.github.com/graphql",
|
||||||
{},
|
{},
|
||||||
|
|
@ -51,11 +114,13 @@ export class GithubAPIClient {
|
||||||
data: { query: query, variables },
|
data: { query: query, variables },
|
||||||
headers: { Authorization: `bearer ${token}` },
|
headers: { Authorization: `bearer ${token}` },
|
||||||
},
|
},
|
||||||
);
|
).catch((error) => {
|
||||||
|
console.error(error.response.data.errors[0].message);
|
||||||
|
});
|
||||||
if (response.status != 200) {
|
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 response.data.data.user;
|
||||||
return new UserInfo(userData);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,28 +4,37 @@ type Repository = {
|
||||||
languages: { nodes: Language[] };
|
languages: { nodes: Language[] };
|
||||||
stargazers: Stargazers;
|
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;
|
createdAt: string;
|
||||||
contributionsCollection: {
|
contributionsCollection: {
|
||||||
totalCommitContributions: number;
|
totalCommitContributions: number;
|
||||||
restrictedContributionsCount: number;
|
restrictedContributionsCount: number;
|
||||||
};
|
};
|
||||||
pullRequests: {
|
|
||||||
totalCount: number;
|
|
||||||
};
|
|
||||||
issues: {
|
|
||||||
totalCount: number;
|
|
||||||
};
|
|
||||||
organizations: {
|
organizations: {
|
||||||
totalCount: number;
|
totalCount: number;
|
||||||
};
|
};
|
||||||
followers: {
|
followers: {
|
||||||
totalCount: number;
|
totalCount: number;
|
||||||
};
|
};
|
||||||
repositories: {
|
|
||||||
totalCount: number;
|
|
||||||
nodes: Repository[];
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
export class UserInfo {
|
export class UserInfo {
|
||||||
public readonly totalCommits: number;
|
public readonly totalCommits: number;
|
||||||
|
|
@ -40,12 +49,15 @@ export class UserInfo {
|
||||||
public readonly ancientAccount: number;
|
public readonly ancientAccount: number;
|
||||||
public readonly joined2020: number;
|
public readonly joined2020: number;
|
||||||
constructor(
|
constructor(
|
||||||
userData: GitHubUserData,
|
userActivity: GitHubUserActivity,
|
||||||
|
userIssue: GitHubUserIssue,
|
||||||
|
userPullRequest: GitHubUserPullRequest,
|
||||||
|
userRepository: GitHubUserRepository,
|
||||||
) {
|
) {
|
||||||
const totalCommits =
|
const totalCommits =
|
||||||
userData.contributionsCollection.restrictedContributionsCount +
|
userActivity.contributionsCollection.restrictedContributionsCount +
|
||||||
userData.contributionsCollection.totalCommitContributions;
|
userActivity.contributionsCollection.totalCommitContributions;
|
||||||
const totalStargazers = userData.repositories.nodes.reduce(
|
const totalStargazers = userRepository.repositories.nodes.reduce(
|
||||||
(prev: number, node: Repository) => {
|
(prev: number, node: Repository) => {
|
||||||
return prev + node.stargazers.totalCount;
|
return prev + node.stargazers.totalCount;
|
||||||
},
|
},
|
||||||
|
|
@ -53,7 +65,7 @@ export class UserInfo {
|
||||||
);
|
);
|
||||||
|
|
||||||
const languages = new Set<string>();
|
const languages = new Set<string>();
|
||||||
userData.repositories.nodes.forEach((node: Repository) => {
|
userRepository.repositories.nodes.forEach((node: Repository) => {
|
||||||
if (node.languages.nodes != undefined) {
|
if (node.languages.nodes != undefined) {
|
||||||
node.languages.nodes.forEach((node: Language) => {
|
node.languages.nodes.forEach((node: Language) => {
|
||||||
if (node != undefined) {
|
if (node != undefined) {
|
||||||
|
|
@ -63,21 +75,20 @@ export class UserInfo {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
const durationTime = new Date().getTime() -
|
const durationTime = new Date().getTime() -
|
||||||
new Date(userData.createdAt).getTime();
|
new Date(userActivity.createdAt).getTime();
|
||||||
const durationYear = new Date(durationTime).getUTCFullYear() - 1970;
|
const durationYear = new Date(durationTime).getUTCFullYear() - 1970;
|
||||||
const ancientAccount = new Date(userData.createdAt).getFullYear() <= 2010
|
const ancientAccount =
|
||||||
? 1
|
new Date(userActivity.createdAt).getFullYear() <= 2010 ? 1 : 0;
|
||||||
: 0;
|
const joined2020 = new Date(userActivity.createdAt).getFullYear() == 2020
|
||||||
const joined2020 = new Date(userData.createdAt).getFullYear() == 2020
|
|
||||||
? 1
|
? 1
|
||||||
: 0;
|
: 0;
|
||||||
this.totalCommits = totalCommits;
|
this.totalCommits = totalCommits;
|
||||||
this.totalFollowers = userData.followers.totalCount;
|
this.totalFollowers = userActivity.followers.totalCount;
|
||||||
this.totalIssues = userData.issues.totalCount;
|
this.totalIssues = userIssue.issues.totalCount;
|
||||||
this.totalOrganizations = userData.organizations.totalCount;
|
this.totalOrganizations = userActivity.organizations.totalCount;
|
||||||
this.totalPullRequests = userData.pullRequests.totalCount;
|
this.totalPullRequests = userPullRequest.pullRequests.totalCount;
|
||||||
this.totalStargazers = totalStargazers;
|
this.totalStargazers = totalStargazers;
|
||||||
this.totalRepositories = userData.repositories.totalCount;
|
this.totalRepositories = userRepository.repositories.totalCount;
|
||||||
this.languageCount = languages.size;
|
this.languageCount = languages.size;
|
||||||
this.durationYear = durationYear;
|
this.durationYear = durationYear;
|
||||||
this.ancientAccount = ancientAccount;
|
this.ancientAccount = ancientAccount;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue