mirror of
https://github.com/reiyua/github-profile-trophy-reiyua-mirror.git
synced 2026-11-17 06:02:09 +11:00
Refactor the UserInfo class
This commit is contained in:
parent
f8d8599d40
commit
4d299d4678
3 changed files with 88 additions and 68 deletions
|
|
@ -12,7 +12,7 @@ import {
|
||||||
Joined2020Trophy,
|
Joined2020Trophy,
|
||||||
AllSuperRankTrophy,
|
AllSuperRankTrophy,
|
||||||
} from "./trophies.ts";
|
} from "./trophies.ts";
|
||||||
import { UserInfo } from "./github_api_client.ts";
|
import { UserInfo } from "./user_info.ts";
|
||||||
import { RANK_ORDER, RANK } from "./utils.ts";
|
import { RANK_ORDER, RANK } from "./utils.ts";
|
||||||
import { Theme } from "./theme.ts";
|
import { Theme } from "./theme.ts";
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,26 +1,6 @@
|
||||||
import { soxa } from "../deps.ts";
|
import { soxa } from "../deps.ts";
|
||||||
export class UserInfo {
|
import { UserInfo } from "./user_info.ts"
|
||||||
constructor(
|
import type { GitHubUserData } from "./user_info.ts";
|
||||||
public totalCommits: number,
|
|
||||||
public totalFollowers: number,
|
|
||||||
public totalIssues: number,
|
|
||||||
public totalPullRequests: number,
|
|
||||||
public totalStargazers: number,
|
|
||||||
public totalRepositories: number,
|
|
||||||
public languageCount: number,
|
|
||||||
public durationYear: number,
|
|
||||||
public ancientAccount: number,
|
|
||||||
public joined2020: number,
|
|
||||||
) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
type Language = { name: string };
|
|
||||||
type Stargazers = { totalCount: number };
|
|
||||||
|
|
||||||
type Repository = {
|
|
||||||
languages: { nodes: Language[] };
|
|
||||||
stargazers: Stargazers;
|
|
||||||
};
|
|
||||||
|
|
||||||
export class GithubAPIClient {
|
export class GithubAPIClient {
|
||||||
constructor() {
|
constructor() {
|
||||||
|
|
@ -69,51 +49,10 @@ export class GithubAPIClient {
|
||||||
headers: { Authorization: `bearer ${token}` },
|
headers: { Authorization: `bearer ${token}` },
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
const userData = response.data.data.user;
|
if (response.status != 200) {
|
||||||
if (userData === null) {
|
throw new Error(response)
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
const totalCommits =
|
const userData: GitHubUserData = response.data.data.user;
|
||||||
userData.contributionsCollection.restrictedContributionsCount +
|
return new UserInfo(userData);
|
||||||
userData.contributionsCollection.totalCommitContributions;
|
|
||||||
const totalStargazers = userData.repositories.nodes.reduce(
|
|
||||||
(prev: number, node: Repository) => {
|
|
||||||
return prev + node.stargazers.totalCount;
|
|
||||||
},
|
|
||||||
0,
|
|
||||||
);
|
|
||||||
|
|
||||||
const languages = new Set<string>();
|
|
||||||
userData.repositories.nodes.forEach((node: Repository) => {
|
|
||||||
if (node.languages.nodes != undefined) {
|
|
||||||
node.languages.nodes.forEach((node: Language) => {
|
|
||||||
if (node != undefined) {
|
|
||||||
languages.add(node.name);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
const durationTime = new Date().getTime() -
|
|
||||||
new Date(userData.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
|
|
||||||
? 1
|
|
||||||
: 0;
|
|
||||||
const userInfo = new UserInfo(
|
|
||||||
totalCommits,
|
|
||||||
userData.followers.totalCount,
|
|
||||||
userData.issues.totalCount,
|
|
||||||
userData.pullRequests.totalCount,
|
|
||||||
totalStargazers,
|
|
||||||
userData.repositories.totalCount,
|
|
||||||
languages.size,
|
|
||||||
durationYear,
|
|
||||||
ancientAccount,
|
|
||||||
joined2020,
|
|
||||||
);
|
|
||||||
return userInfo;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
81
src/user_info.ts
Normal file
81
src/user_info.ts
Normal file
|
|
@ -0,0 +1,81 @@
|
||||||
|
type Language = { name: string };
|
||||||
|
type Stargazers = { totalCount: number };
|
||||||
|
type Repository = {
|
||||||
|
languages: { nodes: Language[] };
|
||||||
|
stargazers: Stargazers;
|
||||||
|
};
|
||||||
|
export type GitHubUserData = {
|
||||||
|
createdAt: string;
|
||||||
|
contributionsCollection: {
|
||||||
|
totalCommitContributions: number;
|
||||||
|
restrictedContributionsCount: number;
|
||||||
|
};
|
||||||
|
pullRequests: {
|
||||||
|
totalCount: number;
|
||||||
|
};
|
||||||
|
issues: {
|
||||||
|
totalCount: number;
|
||||||
|
};
|
||||||
|
followers: {
|
||||||
|
totalCount: number;
|
||||||
|
};
|
||||||
|
repositories: {
|
||||||
|
totalCount: number;
|
||||||
|
nodes: Repository[];
|
||||||
|
};
|
||||||
|
};
|
||||||
|
export class UserInfo {
|
||||||
|
public readonly totalCommits: number;
|
||||||
|
public readonly totalFollowers: number;
|
||||||
|
public readonly totalIssues: number;
|
||||||
|
public readonly totalPullRequests: number;
|
||||||
|
public readonly totalStargazers: number;
|
||||||
|
public readonly totalRepositories: number;
|
||||||
|
public readonly languageCount: number;
|
||||||
|
public readonly durationYear: number;
|
||||||
|
public readonly ancientAccount: number;
|
||||||
|
public readonly joined2020: number;
|
||||||
|
constructor(
|
||||||
|
userData: GitHubUserData,
|
||||||
|
) {
|
||||||
|
const totalCommits =
|
||||||
|
userData.contributionsCollection.restrictedContributionsCount +
|
||||||
|
userData.contributionsCollection.totalCommitContributions;
|
||||||
|
const totalStargazers = userData.repositories.nodes.reduce(
|
||||||
|
(prev: number, node: Repository) => {
|
||||||
|
return prev + node.stargazers.totalCount;
|
||||||
|
},
|
||||||
|
0,
|
||||||
|
);
|
||||||
|
|
||||||
|
const languages = new Set<string>();
|
||||||
|
userData.repositories.nodes.forEach((node: Repository) => {
|
||||||
|
if (node.languages.nodes != undefined) {
|
||||||
|
node.languages.nodes.forEach((node: Language) => {
|
||||||
|
if (node != undefined) {
|
||||||
|
languages.add(node.name);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
const durationTime = new Date().getTime() -
|
||||||
|
new Date(userData.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
|
||||||
|
? 1
|
||||||
|
: 0;
|
||||||
|
this.totalCommits = totalCommits;
|
||||||
|
this.totalFollowers = userData.followers.totalCount;
|
||||||
|
this.totalIssues = userData.issues.totalCount;
|
||||||
|
this.totalPullRequests = userData.pullRequests.totalCount;
|
||||||
|
this.totalStargazers = totalStargazers;
|
||||||
|
this.totalRepositories = userData.repositories.totalCount;
|
||||||
|
this.languageCount = languages.size;
|
||||||
|
this.durationYear = durationYear;
|
||||||
|
this.ancientAccount = ancientAccount;
|
||||||
|
this.joined2020 = joined2020;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue