mirror of
https://github.com/reiyua/github-profile-trophy-reiyua-mirror.git
synced 2026-11-17 06:02:09 +11:00
Merge branch 'ryo-ma:master' into master
This commit is contained in:
commit
eecfdd4c33
6 changed files with 75 additions and 48 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
import { ServiceError } from "../Types/index.ts";
|
import { ServiceError } from "../Types/index.ts";
|
||||||
import {
|
import {
|
||||||
GitHubUserActivity,
|
GitHubUserActivity,
|
||||||
|
GitHubUserAll,
|
||||||
GitHubUserIssue,
|
GitHubUserIssue,
|
||||||
GitHubUserPullRequest,
|
GitHubUserPullRequest,
|
||||||
GitHubUserRepository,
|
GitHubUserRepository,
|
||||||
|
|
@ -9,6 +10,9 @@ import {
|
||||||
|
|
||||||
export abstract class GithubRepository {
|
export abstract class GithubRepository {
|
||||||
abstract requestUserInfo(username: string): Promise<UserInfo | ServiceError>;
|
abstract requestUserInfo(username: string): Promise<UserInfo | ServiceError>;
|
||||||
|
abstract requestUserAll(
|
||||||
|
username: string,
|
||||||
|
): Promise<GitHubUserAll | ServiceError>;
|
||||||
abstract requestUserActivity(
|
abstract requestUserActivity(
|
||||||
username: string,
|
username: string,
|
||||||
): Promise<GitHubUserActivity | ServiceError>;
|
): Promise<GitHubUserActivity | ServiceError>;
|
||||||
|
|
|
||||||
|
|
@ -46,7 +46,49 @@ export const queryUserRepository = `
|
||||||
repositories(first: 50, ownerAffiliations: OWNER, orderBy: {direction: DESC, field: STARGAZERS}) {
|
repositories(first: 50, ownerAffiliations: OWNER, orderBy: {direction: DESC, field: STARGAZERS}) {
|
||||||
totalCount
|
totalCount
|
||||||
nodes {
|
nodes {
|
||||||
languages(first: 3, orderBy: {direction:DESC, field: SIZE}) {
|
languages(first: 2, orderBy: {direction:DESC, field: SIZE}) {
|
||||||
|
nodes {
|
||||||
|
name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
stargazers {
|
||||||
|
totalCount
|
||||||
|
}
|
||||||
|
createdAt
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
|
||||||
|
export const queryUserAll = `
|
||||||
|
query userInfo($username: String!) {
|
||||||
|
user(login: $username) {
|
||||||
|
createdAt
|
||||||
|
contributionsCollection {
|
||||||
|
totalCommitContributions
|
||||||
|
restrictedContributionsCount
|
||||||
|
totalPullRequestReviewContributions
|
||||||
|
}
|
||||||
|
organizations(first: 1) {
|
||||||
|
totalCount
|
||||||
|
}
|
||||||
|
followers(first: 1) {
|
||||||
|
totalCount
|
||||||
|
}
|
||||||
|
openIssues: issues(states: OPEN) {
|
||||||
|
totalCount
|
||||||
|
}
|
||||||
|
closedIssues: issues(states: CLOSED) {
|
||||||
|
totalCount
|
||||||
|
}
|
||||||
|
pullRequests(first: 1) {
|
||||||
|
totalCount
|
||||||
|
}
|
||||||
|
repositories(first: 50, ownerAffiliations: OWNER, orderBy: {direction: DESC, field: STARGAZERS}) {
|
||||||
|
totalCount
|
||||||
|
nodes {
|
||||||
|
languages(first: 2, orderBy: {direction:DESC, field: SIZE}) {
|
||||||
nodes {
|
nodes {
|
||||||
name
|
name
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
import { GithubRepository } from "../Repository/GithubRepository.ts";
|
import { GithubRepository } from "../Repository/GithubRepository.ts";
|
||||||
import {
|
import {
|
||||||
GitHubUserActivity,
|
GitHubUserActivity,
|
||||||
|
GitHubUserAll,
|
||||||
GitHubUserIssue,
|
GitHubUserIssue,
|
||||||
GitHubUserPullRequest,
|
GitHubUserPullRequest,
|
||||||
GitHubUserRepository,
|
GitHubUserRepository,
|
||||||
|
|
@ -8,6 +9,7 @@ import {
|
||||||
} from "../user_info.ts";
|
} from "../user_info.ts";
|
||||||
import {
|
import {
|
||||||
queryUserActivity,
|
queryUserActivity,
|
||||||
|
queryUserAll,
|
||||||
queryUserIssue,
|
queryUserIssue,
|
||||||
queryUserPullRequest,
|
queryUserPullRequest,
|
||||||
queryUserRepository,
|
queryUserRepository,
|
||||||
|
|
@ -25,6 +27,13 @@ export const TOKENS = [
|
||||||
];
|
];
|
||||||
|
|
||||||
export class GithubApiService extends GithubRepository {
|
export class GithubApiService extends GithubRepository {
|
||||||
|
async requestUserAll(
|
||||||
|
username: string,
|
||||||
|
): Promise<GitHubUserAll | ServiceError> {
|
||||||
|
return await this.executeQuery<GitHubUserAll>(queryUserAll, {
|
||||||
|
username,
|
||||||
|
});
|
||||||
|
}
|
||||||
async requestUserRepository(
|
async requestUserRepository(
|
||||||
username: string,
|
username: string,
|
||||||
): Promise<GitHubUserRepository | ServiceError> {
|
): Promise<GitHubUserRepository | ServiceError> {
|
||||||
|
|
@ -55,34 +64,13 @@ export class GithubApiService extends GithubRepository {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
async requestUserInfo(username: string): Promise<UserInfo | ServiceError> {
|
async requestUserInfo(username: string): Promise<UserInfo | ServiceError> {
|
||||||
// Avoid to call others if one of them is null
|
// Use single combined query instead of 4 separate queries to reduce Function Duration
|
||||||
|
|
||||||
const promises = Promise.allSettled([
|
|
||||||
this.requestUserRepository(username),
|
|
||||||
this.requestUserActivity(username),
|
|
||||||
this.requestUserIssue(username),
|
|
||||||
this.requestUserPullRequest(username),
|
|
||||||
]);
|
|
||||||
try {
|
try {
|
||||||
const [repository, activity, issue, pullRequest] = await promises;
|
const result = await this.requestUserAll(username);
|
||||||
const status = [
|
if (result instanceof ServiceError) {
|
||||||
repository.status,
|
return result;
|
||||||
activity.status,
|
|
||||||
issue.status,
|
|
||||||
pullRequest.status,
|
|
||||||
];
|
|
||||||
|
|
||||||
if (status.includes("rejected")) {
|
|
||||||
Logger.error(`Can not find a user with username:' ${username}'`);
|
|
||||||
return new ServiceError("Not found", EServiceKindError.NOT_FOUND);
|
|
||||||
}
|
}
|
||||||
|
return UserInfo.fromCombined(result);
|
||||||
return new UserInfo(
|
|
||||||
(activity as PromiseFulfilledResult<GitHubUserActivity>).value,
|
|
||||||
(issue as PromiseFulfilledResult<GitHubUserIssue>).value,
|
|
||||||
(pullRequest as PromiseFulfilledResult<GitHubUserPullRequest>).value,
|
|
||||||
(repository as PromiseFulfilledResult<GitHubUserRepository>).value,
|
|
||||||
);
|
|
||||||
} catch {
|
} catch {
|
||||||
Logger.error(`Error fetching user info for username: ${username}`);
|
Logger.error(`Error fetching user info for username: ${username}`);
|
||||||
return new ServiceError("Not found", EServiceKindError.NOT_FOUND);
|
return new ServiceError("Not found", EServiceKindError.NOT_FOUND);
|
||||||
|
|
|
||||||
|
|
@ -28,26 +28,8 @@ stub(
|
||||||
new Promise((resolve) => {
|
new Promise((resolve) => {
|
||||||
resolve(successGithubResponseMock.default);
|
resolve(successGithubResponseMock.default);
|
||||||
}),
|
}),
|
||||||
// Should throw NOT FOUND (requestUserInfo makes 4 API calls: repository, activity, issue, pullRequest)
|
// Should throw NOT FOUND (requestUserInfo makes 1 combined API call)
|
||||||
// Each call makes 2 attempts (one per token), so 8 promises total
|
// Each call makes 2 attempts (one per token), so 2 promises total
|
||||||
new Promise((resolve) => {
|
|
||||||
resolve(notFoundGithubResponseMock.default);
|
|
||||||
}),
|
|
||||||
new Promise((resolve) => {
|
|
||||||
resolve(notFoundGithubResponseMock.default);
|
|
||||||
}),
|
|
||||||
new Promise((resolve) => {
|
|
||||||
resolve(notFoundGithubResponseMock.default);
|
|
||||||
}),
|
|
||||||
new Promise((resolve) => {
|
|
||||||
resolve(notFoundGithubResponseMock.default);
|
|
||||||
}),
|
|
||||||
new Promise((resolve) => {
|
|
||||||
resolve(notFoundGithubResponseMock.default);
|
|
||||||
}),
|
|
||||||
new Promise((resolve) => {
|
|
||||||
resolve(notFoundGithubResponseMock.default);
|
|
||||||
}),
|
|
||||||
new Promise((resolve) => {
|
new Promise((resolve) => {
|
||||||
resolve(notFoundGithubResponseMock.default);
|
resolve(notFoundGithubResponseMock.default);
|
||||||
}),
|
}),
|
||||||
|
|
|
||||||
|
|
@ -41,6 +41,12 @@ export type GitHubUserActivity = {
|
||||||
totalCount: number;
|
totalCount: number;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type GitHubUserAll =
|
||||||
|
& GitHubUserActivity
|
||||||
|
& GitHubUserIssue
|
||||||
|
& GitHubUserPullRequest
|
||||||
|
& GitHubUserRepository;
|
||||||
export class UserInfo {
|
export class UserInfo {
|
||||||
public readonly totalCommits: number;
|
public readonly totalCommits: number;
|
||||||
public readonly totalFollowers: number;
|
public readonly totalFollowers: number;
|
||||||
|
|
@ -56,6 +62,11 @@ export class UserInfo {
|
||||||
public readonly ancientAccount: number;
|
public readonly ancientAccount: number;
|
||||||
public readonly joined2020: number;
|
public readonly joined2020: number;
|
||||||
public readonly ogAccount: number;
|
public readonly ogAccount: number;
|
||||||
|
|
||||||
|
static fromCombined(data: GitHubUserAll): UserInfo {
|
||||||
|
return new UserInfo(data, data, data, data);
|
||||||
|
}
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
userActivity: GitHubUserActivity,
|
userActivity: GitHubUserActivity,
|
||||||
userIssue: GitHubUserIssue,
|
userIssue: GitHubUserIssue,
|
||||||
|
|
|
||||||
|
|
@ -63,7 +63,7 @@ export const CONSTANTS = {
|
||||||
DEFAULT_NO_FRAME: false,
|
DEFAULT_NO_FRAME: false,
|
||||||
DEFAULT_GITHUB_API: "https://api.github.com/graphql",
|
DEFAULT_GITHUB_API: "https://api.github.com/graphql",
|
||||||
DEFAULT_GITHUB_RETRY_DELAY: 500,
|
DEFAULT_GITHUB_RETRY_DELAY: 500,
|
||||||
REVALIDATE_TIME: HOUR_IN_MILLISECONDS,
|
REVALIDATE_TIME: HOUR_IN_MILLISECONDS * 6,
|
||||||
REDIS_TTL: HOUR_IN_MILLISECONDS * 4,
|
REDIS_TTL: HOUR_IN_MILLISECONDS * 4,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue