forked from ryo-ma/github-profile-trophy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_info.ts
100 lines (96 loc) · 3 KB
/
user_info.ts
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
type Language = { name: string };
type Stargazers = { totalCount: number };
type Repository = {
languages: { nodes: Language[] };
stargazers: Stargazers;
};
export type GitHubUserRepository = {
repositories: {
totalCount: number;
nodes: Repository[];
};
};
export type GitHubUserIssue = {
openIssues: {
totalCount: number;
};
closedIssues: {
totalCount: number;
};
};
export type GitHubUserPullRequest = {
pullRequests: {
totalCount: number;
};
};
export type GitHubUserActivity = {
createdAt: string;
contributionsCollection: {
totalCommitContributions: number;
restrictedContributionsCount: number;
};
organizations: {
totalCount: number;
};
followers: {
totalCount: number;
};
};
export class UserInfo {
public readonly totalCommits: number;
public readonly totalFollowers: number;
public readonly totalIssues: number;
public readonly totalOrganizations: 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(
userActivity: GitHubUserActivity,
userIssue: GitHubUserIssue,
userPullRequest: GitHubUserPullRequest,
userRepository: GitHubUserRepository,
) {
const totalCommits =
userActivity.contributionsCollection.restrictedContributionsCount +
userActivity.contributionsCollection.totalCommitContributions;
const totalStargazers = userRepository.repositories.nodes.reduce(
(prev: number, node: Repository) => {
return prev + node.stargazers.totalCount;
},
0,
);
const languages = new Set<string>();
userRepository.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(userActivity.createdAt).getTime();
const durationYear = new Date(durationTime).getUTCFullYear() - 1970;
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 = userActivity.followers.totalCount;
this.totalIssues = userIssue.openIssues.totalCount + userIssue.closedIssues.totalCount;
this.totalOrganizations = userActivity.organizations.totalCount;
this.totalPullRequests = userPullRequest.pullRequests.totalCount;
this.totalStargazers = totalStargazers;
this.totalRepositories = userRepository.repositories.totalCount;
this.languageCount = languages.size;
this.durationYear = durationYear;
this.ancientAccount = ancientAccount;
this.joined2020 = joined2020;
}
}