@stats-organization/github-readme-stats-core 2.1.5 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (169) hide show
  1. package/build/api/gist.d.ts +2 -7
  2. package/build/api/gist.d.ts.map +1 -1
  3. package/build/api/gist.js +9 -41
  4. package/build/api/index.d.ts +3 -8
  5. package/build/api/index.d.ts.map +1 -1
  6. package/build/api/index.js +23 -45
  7. package/build/api/pin.d.ts +2 -7
  8. package/build/api/pin.d.ts.map +1 -1
  9. package/build/api/pin.js +9 -41
  10. package/build/api/top-langs.d.ts +2 -7
  11. package/build/api/top-langs.d.ts.map +1 -1
  12. package/build/api/top-langs.js +11 -55
  13. package/build/api/wakatime.d.ts +2 -7
  14. package/build/api/wakatime.d.ts.map +1 -1
  15. package/build/api/wakatime.js +9 -41
  16. package/build/cards/common-options.d.ts +13 -0
  17. package/build/cards/common-options.d.ts.map +1 -0
  18. package/build/cards/common-options.js +1 -0
  19. package/build/cards/gist.d.ts +11 -16
  20. package/build/cards/gist.d.ts.map +1 -1
  21. package/build/cards/gist.js +26 -31
  22. package/build/cards/repo.d.ts +19 -16
  23. package/build/cards/repo.d.ts.map +1 -1
  24. package/build/cards/repo.js +58 -60
  25. package/build/cards/stats.d.ts +29 -46
  26. package/build/cards/stats.d.ts.map +1 -1
  27. package/build/cards/stats.js +113 -181
  28. package/build/cards/top-languages.d.ts +81 -77
  29. package/build/cards/top-languages.d.ts.map +1 -1
  30. package/build/cards/top-languages.js +174 -200
  31. package/build/cards/wakatime.d.ts +21 -11
  32. package/build/cards/wakatime.d.ts.map +1 -1
  33. package/build/cards/wakatime.js +73 -111
  34. package/build/common/Card.d.ts +34 -13
  35. package/build/common/Card.d.ts.map +1 -1
  36. package/build/common/Card.js +73 -40
  37. package/build/common/I18n.d.ts +4 -2
  38. package/build/common/I18n.d.ts.map +1 -1
  39. package/build/common/color.d.ts +60 -18
  40. package/build/common/color.d.ts.map +1 -1
  41. package/build/common/color.js +102 -5
  42. package/build/common/config.d.ts +2 -1
  43. package/build/common/config.d.ts.map +1 -1
  44. package/build/common/config.js +16 -26
  45. package/build/common/constants.d.ts +2 -1
  46. package/build/common/constants.d.ts.map +1 -1
  47. package/build/common/date.d.ts +37 -0
  48. package/build/common/date.d.ts.map +1 -0
  49. package/build/common/date.js +31 -0
  50. package/build/common/http.d.ts +25 -2
  51. package/build/common/http.d.ts.map +1 -1
  52. package/build/common/http.js +26 -3
  53. package/build/common/icons.d.ts +1 -0
  54. package/build/common/icons.d.ts.map +1 -1
  55. package/build/common/icons.js +1 -0
  56. package/build/common/languageColors.d.ts +11 -0
  57. package/build/common/languageColors.d.ts.map +1 -0
  58. package/build/common/languageColors.js +15 -0
  59. package/build/common/languageColors.json +1 -0
  60. package/build/common/ops.d.ts +3 -10
  61. package/build/common/ops.d.ts.map +1 -1
  62. package/build/common/ops.js +3 -15
  63. package/build/common/render.d.ts +43 -6
  64. package/build/common/render.d.ts.map +1 -1
  65. package/build/common/render.js +77 -11
  66. package/build/common/retryer.d.ts +3 -2
  67. package/build/common/retryer.d.ts.map +1 -1
  68. package/build/common/retryer.js +1 -0
  69. package/build/fetchers/gist.d.ts.map +1 -1
  70. package/build/fetchers/gist.js +8 -39
  71. package/build/fetchers/repo.d.ts +12 -11
  72. package/build/fetchers/repo.d.ts.map +1 -1
  73. package/build/fetchers/repo.js +26 -67
  74. package/build/fetchers/stats.d.ts +24 -17
  75. package/build/fetchers/stats.d.ts.map +1 -1
  76. package/build/fetchers/stats.js +302 -183
  77. package/build/fetchers/top-languages.d.ts +10 -15
  78. package/build/fetchers/top-languages.d.ts.map +1 -1
  79. package/build/fetchers/top-languages.js +52 -103
  80. package/build/fetchers/types.d.ts +24 -20
  81. package/build/fetchers/types.d.ts.map +1 -1
  82. package/build/fetchers/wakatime.d.ts +9 -5
  83. package/build/fetchers/wakatime.d.ts.map +1 -1
  84. package/build/fetchers/wakatime.js +8 -4
  85. package/build/graphql/contributionsDocument.d.ts +18 -0
  86. package/build/graphql/contributionsDocument.d.ts.map +1 -0
  87. package/build/graphql/contributionsDocument.js +30 -0
  88. package/build/graphql/generated/common.d.ts +42 -0
  89. package/build/graphql/generated/common.d.ts.map +1 -0
  90. package/build/graphql/generated/common.js +2 -0
  91. package/build/graphql/generated/gist.d.ts +44 -0
  92. package/build/graphql/generated/gist.d.ts.map +1 -0
  93. package/build/graphql/generated/gist.js +27 -0
  94. package/build/graphql/generated/repo.d.ts +67 -0
  95. package/build/graphql/generated/repo.d.ts.map +1 -0
  96. package/build/graphql/generated/repo.js +30 -0
  97. package/build/graphql/generated/stats.d.ts +150 -0
  98. package/build/graphql/generated/stats.d.ts.map +1 -0
  99. package/build/graphql/generated/stats.js +95 -0
  100. package/build/graphql/generated/top-languages.d.ts +54 -0
  101. package/build/graphql/generated/top-languages.d.ts.map +1 -0
  102. package/build/graphql/generated/top-languages.js +27 -0
  103. package/build/graphql/graphqlDocument.d.ts +22 -0
  104. package/build/graphql/graphqlDocument.d.ts.map +1 -0
  105. package/build/graphql/graphqlDocument.js +8 -0
  106. package/build/graphql/reposContributedToDocument.d.ts +27 -0
  107. package/build/graphql/reposContributedToDocument.d.ts.map +1 -0
  108. package/build/graphql/reposContributedToDocument.js +59 -0
  109. package/build/index.d.ts +3 -1
  110. package/build/index.d.ts.map +1 -1
  111. package/build/index.js +2 -1
  112. package/build/themes/index.d.ts +32 -0
  113. package/build/themes/index.d.ts.map +1 -1
  114. package/build/themes/index.js +28 -0
  115. package/build/translations.d.ts +17 -0
  116. package/build/translations.d.ts.map +1 -1
  117. package/build/translations.js +17 -0
  118. package/package.json +13 -7
  119. package/src/_github-username-regex.d.ts +4 -0
  120. package/src/api/gist.js +10 -46
  121. package/src/api/index.js +30 -51
  122. package/src/api/pin.js +10 -46
  123. package/src/api/top-langs.js +13 -60
  124. package/src/api/wakatime.js +10 -46
  125. package/src/cards/common-options.ts +13 -0
  126. package/src/cards/{gist.js → gist.ts} +37 -37
  127. package/src/cards/{repo.js → repo.ts} +86 -68
  128. package/src/cards/{stats.js → stats.ts} +168 -219
  129. package/src/cards/{top-languages.js → top-languages.ts} +309 -265
  130. package/src/cards/{wakatime.js → wakatime.ts} +136 -130
  131. package/src/common/Card.ts +94 -49
  132. package/src/common/I18n.ts +6 -2
  133. package/src/common/color.ts +163 -14
  134. package/src/common/config.ts +22 -29
  135. package/src/common/constants.ts +3 -1
  136. package/src/common/date.ts +45 -0
  137. package/src/common/http.ts +43 -3
  138. package/src/common/icons.ts +1 -0
  139. package/src/common/languageColors.json +1 -0
  140. package/src/common/languageColors.ts +19 -0
  141. package/src/common/ops.ts +8 -18
  142. package/src/common/render.ts +114 -14
  143. package/src/common/retryer.ts +6 -5
  144. package/src/fetchers/gist.ts +15 -72
  145. package/src/fetchers/repo.ts +114 -0
  146. package/src/fetchers/stats.ts +743 -0
  147. package/src/fetchers/top-languages.ts +135 -0
  148. package/src/fetchers/types.ts +28 -20
  149. package/src/fetchers/{wakatime.js → wakatime.ts} +19 -5
  150. package/src/graphql/contributionsDocument.ts +44 -0
  151. package/src/graphql/generated/common.ts +26 -0
  152. package/src/graphql/generated/gist.ts +59 -0
  153. package/src/graphql/generated/repo.ts +90 -0
  154. package/src/graphql/generated/stats.ts +194 -0
  155. package/src/graphql/generated/top-languages.ts +75 -0
  156. package/src/graphql/graphqlDocument.ts +25 -0
  157. package/src/graphql/queries/gist.graphql +25 -0
  158. package/src/graphql/queries/repo.graphql +28 -0
  159. package/src/graphql/queries/stats.graphql +120 -0
  160. package/src/graphql/queries/top-languages.graphql +34 -0
  161. package/src/graphql/reposContributedToDocument.ts +86 -0
  162. package/src/index.ts +3 -1
  163. package/src/themes/index.ts +33 -0
  164. package/src/translations.ts +17 -0
  165. package/src/cards/types.d.ts +0 -78
  166. package/src/fetchers/repo.js +0 -161
  167. package/src/fetchers/stats.js +0 -473
  168. package/src/fetchers/top-languages.js +0 -175
  169. package/src/themes/README.md +0 -229
@@ -1,161 +0,0 @@
1
- import { MissingParamError } from "../common/error.js";
2
- import { request } from "../common/http.js";
3
- import { retryer } from "../common/retryer.js";
4
-
5
- import { fetchRepoUserStats } from "./stats.js";
6
-
7
- /**
8
- * Repo data fetcher.
9
- *
10
- * @param {object} variables Fetcher variables.
11
- * @param {string} token GitHub token.
12
- * @returns {Promise<import('axios').AxiosResponse>} The response.
13
- */
14
- const fetcher = (variables, token) => {
15
- return request(
16
- {
17
- query: `
18
- fragment RepoInfo on Repository {
19
- name
20
- nameWithOwner
21
- isPrivate
22
- isArchived
23
- isTemplate
24
- stargazerCount
25
- description
26
- primaryLanguage {
27
- color
28
- id
29
- name
30
- }
31
- forkCount
32
- }
33
- query getRepo($login: String!, $repo: String!) {
34
- user(login: $login) {
35
- repository(name: $repo) {
36
- ...RepoInfo
37
- }
38
- }
39
- organization(login: $login) {
40
- repository(name: $repo) {
41
- ...RepoInfo
42
- }
43
- }
44
- }
45
- `,
46
- variables,
47
- },
48
- {
49
- Authorization: `token ${token}`,
50
- },
51
- );
52
- };
53
-
54
- const urlExample = "/api/pin?username=USERNAME&repo=REPO_NAME";
55
-
56
- /**
57
- * @typedef {import("./types").RepositoryData} RepositoryData Repository data.
58
- */
59
-
60
- /**
61
- * Fetch repository data.
62
- *
63
- * @param {string} username GitHub username.
64
- * @param {string} reponame GitHub repository name.
65
- * @returns {Promise<RepositoryData>} Repository data.
66
- */
67
- const fetchRepo = async (
68
- username,
69
- reponame,
70
- include_prs_authored = false,
71
- include_prs_commented = false,
72
- include_prs_reviewed = false,
73
- include_issues_authored = false,
74
- include_issues_commented = false,
75
- pat = null,
76
- ) => {
77
- let owner = username;
78
- if (reponame && reponame.includes("/")) {
79
- const [parsed_owner, parsed_repo] = reponame.split("/");
80
- owner = parsed_owner;
81
- reponame = parsed_repo;
82
- }
83
-
84
- if (owner && !username) {
85
- username = owner;
86
- }
87
- if (username && !owner) {
88
- owner = username;
89
- }
90
- if (!username && !reponame) {
91
- throw new MissingParamError(["username", "repo"], urlExample);
92
- }
93
- if (!username) {
94
- throw new MissingParamError(["username"], urlExample);
95
- }
96
- if (!reponame) {
97
- throw new MissingParamError(["repo"], urlExample);
98
- }
99
-
100
- let res = await retryer(fetcher, { login: owner, repo: reponame }, pat);
101
-
102
- const data = res.data.data;
103
-
104
- if (!data.user && !data.organization) {
105
- throw new Error("Not found");
106
- }
107
-
108
- const isUser = data.organization === null && data.user;
109
- const isOrg = data.user === null && data.organization;
110
-
111
- if (isUser) {
112
- if (!data.user.repository || data.user.repository.isPrivate) {
113
- throw new Error("User Repository Not found");
114
- }
115
- let repoUserStats = await fetchRepoUserStats(
116
- username,
117
- [owner + "/" + reponame],
118
- [],
119
- include_prs_authored,
120
- include_prs_commented,
121
- include_prs_reviewed,
122
- include_issues_authored,
123
- include_issues_commented,
124
- pat,
125
- );
126
- return {
127
- ...repoUserStats,
128
- ...data.user.repository,
129
- starCount: data.user.repository.stargazerCount,
130
- };
131
- }
132
-
133
- if (isOrg) {
134
- if (
135
- !data.organization.repository ||
136
- data.organization.repository.isPrivate
137
- ) {
138
- throw new Error("Organization Repository Not found");
139
- }
140
- let repoUserStats = await fetchRepoUserStats(
141
- username,
142
- [owner + "/" + reponame],
143
- [],
144
- include_prs_authored,
145
- include_prs_commented,
146
- include_prs_reviewed,
147
- include_issues_authored,
148
- include_issues_commented,
149
- pat,
150
- );
151
- return {
152
- ...repoUserStats,
153
- ...data.organization.repository,
154
- starCount: data.organization.repository.stargazerCount,
155
- };
156
- }
157
-
158
- throw new Error("Unexpected behavior");
159
- };
160
-
161
- export { fetchRepo };
@@ -1,473 +0,0 @@
1
- import axios from "axios";
2
- import githubUsernameRegex from "github-username-regex";
3
-
4
- import { calculateRank } from "../calculateRank.js";
5
- import { getConfig } from "../common/config.js";
6
- import { CustomError, MissingParamError } from "../common/error.js";
7
- import { wrapTextMultiline } from "../common/fmt.js";
8
- import { request } from "../common/http.js";
9
- import { logger } from "../common/log.js";
10
- import { buildSearchFilter, parseOwnerAffiliations } from "../common/ops.js";
11
- import { retryer } from "../common/retryer.js";
12
-
13
- // GraphQL queries.
14
- const GRAPHQL_REPOS_FIELD = `
15
- repositories(first: 100, after: $after, ownerAffiliations: $ownerAffiliations, orderBy: {direction: DESC, field: STARGAZERS}) {
16
- totalCount
17
- nodes {
18
- name
19
- stargazerCount
20
- }
21
- pageInfo {
22
- hasNextPage
23
- endCursor
24
- }
25
- }
26
- `;
27
-
28
- const GRAPHQL_REPOS_QUERY = `
29
- query userInfo($login: String!, $after: String, $ownerAffiliations: [RepositoryAffiliation]) {
30
- user(login: $login) {
31
- ${GRAPHQL_REPOS_FIELD}
32
- }
33
- }
34
- `;
35
-
36
- const GRAPHQL_STATS_QUERY = `
37
- query userInfo($login: String!, $after: String, $includeMergedPullRequests: Boolean!, $includeDiscussions: Boolean!, $includeDiscussionsAnswers: Boolean!, $startTime: DateTime = null, $ownerAffiliations: [RepositoryAffiliation]) {
38
- user(login: $login) {
39
- name
40
- login
41
- commits: contributionsCollection (from: $startTime) {
42
- totalCommitContributions,
43
- }
44
- reviews: contributionsCollection {
45
- totalPullRequestReviewContributions
46
- }
47
- repositoriesContributedTo(first: 1, contributionTypes: [COMMIT, ISSUE, PULL_REQUEST, REPOSITORY]) {
48
- totalCount
49
- }
50
- pullRequests(first: 1) {
51
- totalCount
52
- }
53
- mergedPullRequests: pullRequests(states: MERGED) @include(if: $includeMergedPullRequests) {
54
- totalCount
55
- }
56
- openIssues: issues(states: OPEN) {
57
- totalCount
58
- }
59
- closedIssues: issues(states: CLOSED) {
60
- totalCount
61
- }
62
- followers {
63
- totalCount
64
- }
65
- repositoryDiscussions @include(if: $includeDiscussions) {
66
- totalCount
67
- }
68
- repositoryDiscussionComments(onlyAnswers: true) @include(if: $includeDiscussionsAnswers) {
69
- totalCount
70
- }
71
- ${GRAPHQL_REPOS_FIELD}
72
- }
73
- }
74
- `;
75
-
76
- /**
77
- * Stats fetcher object.
78
- *
79
- * @param {object & { after: string | null }} variables Fetcher variables.
80
- * @param {string} token GitHub token.
81
- * @returns {Promise<import('axios').AxiosResponse>} Axios response.
82
- */
83
- const fetcher = (variables, token) => {
84
- const query = variables.after ? GRAPHQL_REPOS_QUERY : GRAPHQL_STATS_QUERY;
85
- return request(
86
- {
87
- query,
88
- variables,
89
- },
90
- {
91
- Authorization: `bearer ${token}`,
92
- },
93
- );
94
- };
95
-
96
- /**
97
- * Fetch stats information for a given username.
98
- *
99
- * @param {object} variables Fetcher variables.
100
- * @param {string} variables.username GitHub username.
101
- * @param {boolean} variables.includeMergedPullRequests Include merged pull requests.
102
- * @param {boolean} variables.includeDiscussions Include discussions.
103
- * @param {boolean} variables.includeDiscussionsAnswers Include discussions answers.
104
- * @param {string|undefined} variables.startTime Time to start the count of total commits.
105
- * @param {string[]} variables.ownerAffiliations The owner affiliations to filter by. Default: OWNER.
106
- * @param {string | null} variables.pat PAT override or null.
107
- * @returns {Promise<import('axios').AxiosResponse>} Axios response.
108
- *
109
- * @description This function supports multi-page fetching if the 'FETCH_MULTI_PAGE_STARS' environment variable is set to true or a limit of fetches.
110
- */
111
- const statsFetcher = async ({
112
- username,
113
- includeMergedPullRequests,
114
- includeDiscussions,
115
- includeDiscussionsAnswers,
116
- startTime,
117
- ownerAffiliations,
118
- pat,
119
- }) => {
120
- let stats;
121
- let hasNextPage = true;
122
- let endCursor = null;
123
- let fetchedPages = 0;
124
- while (hasNextPage) {
125
- const variables = {
126
- login: username,
127
- first: 100,
128
- after: endCursor,
129
- includeMergedPullRequests,
130
- includeDiscussions,
131
- includeDiscussionsAnswers,
132
- startTime,
133
- ownerAffiliations,
134
- };
135
- let res = await retryer(fetcher, variables, pat);
136
- if (res.data.errors) {
137
- return res;
138
- }
139
-
140
- // Store stats data.
141
- const repoNodes = res.data.data.user.repositories.nodes;
142
- if (stats) {
143
- if (fetchedPages === 1) {
144
- // make deep copy of relevant stats fields to avoid altering the cached response object in frontend
145
- stats = structuredClone({
146
- data: stats.data,
147
- statusText: stats.statusText,
148
- });
149
- }
150
- stats.data.data.user.repositories.nodes.push(...repoNodes);
151
- } else {
152
- stats = res;
153
- }
154
-
155
- fetchedPages++;
156
- const repoNodesWithStars = repoNodes.filter(
157
- (node) => node.stargazerCount !== 0,
158
- );
159
-
160
- hasNextPage =
161
- (getConfig().fetchMultiPageStars === "true" ||
162
- getConfig().fetchMultiPageStars > fetchedPages) &&
163
- repoNodes.length === repoNodesWithStars.length &&
164
- res.data.data.user.repositories.pageInfo.hasNextPage;
165
-
166
- endCursor = res.data.data.user.repositories.pageInfo.endCursor;
167
- }
168
-
169
- return stats;
170
- };
171
-
172
- /**
173
- * Fetch total commits using the REST API.
174
- *
175
- * @param {object} variables Fetcher variables.
176
- * @param {string} token GitHub token.
177
- * @returns {Promise<import('axios').AxiosResponse>} Axios response.
178
- *
179
- * @see https://developer.github.com/v3/search/#search-commits
180
- */
181
- const fetchTotalItems = (variables, token) => {
182
- return axios({
183
- method: "get",
184
- url:
185
- `https://api.github.com/search/` +
186
- variables.type +
187
- `?per_page=1&q=` +
188
- buildSearchFilter(variables.repo, variables.owner).replaceAll(" ", "+") +
189
- variables.filter,
190
- headers: {
191
- "Content-Type": "application/json",
192
- Accept: "application/vnd.github.cloak-preview",
193
- Authorization: `token ${token}`,
194
- },
195
- });
196
- };
197
-
198
- /**
199
- * Fetch all the commits for all the repositories of a given username.
200
- *
201
- * @param {string} username GitHub username.
202
- * @returns {Promise<number>} Total commits.
203
- *
204
- * @description Done like this because the GitHub API does not provide a way to fetch all the commits. See
205
- * #92#issuecomment-661026467 and #211 for more information.
206
- */
207
- const totalItemsFetcher = async (username, repo, owner, type, filter, pat) => {
208
- if (!githubUsernameRegex.test(username)) {
209
- logger.log("Invalid username provided.");
210
- throw new Error("Invalid username provided.");
211
- }
212
-
213
- let res;
214
- try {
215
- res = await retryer(
216
- fetchTotalItems,
217
- {
218
- login: username,
219
- repo,
220
- owner,
221
- type,
222
- filter,
223
- },
224
- pat,
225
- );
226
- } catch (err) {
227
- logger.log(err);
228
- throw err;
229
- }
230
-
231
- const totalCount = res.data.total_count;
232
- if (isNaN(totalCount)) {
233
- logger.error("GitHub error: " + JSON.stringify(res.data));
234
- throw new CustomError(
235
- "Could not fetch data from GitHub REST API.",
236
- CustomError.GITHUB_REST_API_ERROR,
237
- );
238
- }
239
- return totalCount;
240
- };
241
-
242
- const fetchRepoUserStats = async (
243
- username,
244
- repo,
245
- owner,
246
- include_prs_authored,
247
- include_prs_commented,
248
- include_prs_reviewed,
249
- include_issues_authored,
250
- include_issues_commented,
251
- pat,
252
- ) => {
253
- let stats = {};
254
- if (include_prs_authored) {
255
- stats.totalPRsAuthored = await totalItemsFetcher(
256
- username,
257
- repo,
258
- owner,
259
- "issues",
260
- `author:${username}+type:pr`,
261
- pat,
262
- );
263
- }
264
- if (include_prs_commented) {
265
- stats.totalPRsCommented = await totalItemsFetcher(
266
- username,
267
- repo,
268
- owner,
269
- "issues",
270
- `commenter:${username}+-author:${username}+type:pr`,
271
- pat,
272
- );
273
- }
274
- if (include_prs_reviewed) {
275
- stats.totalPRsReviewed = await totalItemsFetcher(
276
- username,
277
- repo,
278
- owner,
279
- "issues",
280
- `reviewed-by:${username}+-author:${username}+type:pr`,
281
- pat,
282
- );
283
- }
284
- if (include_issues_authored) {
285
- stats.totalIssuesAuthored = await totalItemsFetcher(
286
- username,
287
- repo,
288
- owner,
289
- "issues",
290
- `author:${username}+type:issue`,
291
- pat,
292
- );
293
- }
294
- if (include_issues_commented) {
295
- stats.totalIssuesCommented = await totalItemsFetcher(
296
- username,
297
- repo,
298
- owner,
299
- "issues",
300
- `commenter:${username}+-author:${username}+type:issue`,
301
- pat,
302
- );
303
- }
304
- return stats;
305
- };
306
-
307
- /**
308
- * Fetch stats for a given username.
309
- *
310
- * @param {string} username GitHub username.
311
- * @param {boolean} include_all_commits Include all commits.
312
- * @param {string[]} exclude_repo Repositories to exclude.
313
- * @param {boolean} include_merged_pull_requests Include merged pull requests.
314
- * @param {boolean} include_discussions Include discussions.
315
- * @param {boolean} include_discussions_answers Include discussions answers.
316
- * @param {number|undefined} commits_year Year to count total commits
317
- * @param {string[]} ownerAffiliations Owner affiliations. Default: OWNER.
318
- * @returns {Promise<import("./types").StatsData>} Stats data.
319
- */
320
- const fetchStats = async (
321
- username,
322
- include_all_commits = false,
323
- exclude_repo = [],
324
- include_merged_pull_requests = false,
325
- include_discussions = false,
326
- include_discussions_answers = false,
327
- commits_year,
328
- repo = [],
329
- owner = [],
330
- include_prs_authored = false,
331
- include_prs_commented = false,
332
- include_prs_reviewed = false,
333
- include_issues_authored = false,
334
- include_issues_commented = false,
335
- ownerAffiliations = [],
336
- pat = null,
337
- ) => {
338
- if (!username) {
339
- throw new MissingParamError(["username"]);
340
- }
341
-
342
- const stats = {
343
- name: "",
344
- totalPRs: 0,
345
- totalPRsMerged: 0,
346
- mergedPRsPercentage: 0,
347
- totalReviews: 0,
348
- totalCommits: 0,
349
- totalIssues: 0,
350
- totalStars: 0,
351
- totalDiscussionsStarted: 0,
352
- totalDiscussionsAnswered: 0,
353
- contributedTo: 0,
354
- totalPRsAuthored: 0,
355
- totalPRsCommented: 0,
356
- totalPRsReviewed: 0,
357
- totalIssuesAuthored: 0,
358
- totalIssuesCommented: 0,
359
- rank: { level: "C", percentile: 100 },
360
- };
361
- ownerAffiliations = parseOwnerAffiliations(ownerAffiliations);
362
-
363
- let res = await statsFetcher(
364
- {
365
- username,
366
- includeMergedPullRequests: include_merged_pull_requests,
367
- includeDiscussions: include_discussions,
368
- includeDiscussionsAnswers: include_discussions_answers,
369
- startTime: commits_year ? `${commits_year}-01-01T00:00:00Z` : undefined,
370
- ownerAffiliations,
371
- },
372
- pat,
373
- );
374
-
375
- // Catch GraphQL errors.
376
- if (res.data.errors) {
377
- logger.error(res.data.errors);
378
- if (res.data.errors[0].type === "NOT_FOUND") {
379
- throw new CustomError(
380
- res.data.errors[0].message || "Could not fetch user.",
381
- CustomError.USER_NOT_FOUND,
382
- );
383
- }
384
- if (res.data.errors[0].message) {
385
- throw new CustomError(
386
- wrapTextMultiline(res.data.errors[0].message, 525, 12)[0],
387
- res.statusText,
388
- );
389
- }
390
- throw new CustomError(
391
- "Something went wrong while trying to retrieve the stats data using the GraphQL API.",
392
- CustomError.GRAPHQL_ERROR,
393
- );
394
- }
395
-
396
- const user = res.data.data.user;
397
-
398
- stats.name = user.name || user.login;
399
-
400
- // if include_all_commits, fetch all commits using the REST API.
401
- if (include_all_commits) {
402
- stats.totalCommits = await totalItemsFetcher(
403
- username,
404
- repo,
405
- owner,
406
- "commits",
407
- `author:${username}`,
408
- pat,
409
- );
410
- } else {
411
- stats.totalCommits = user.commits.totalCommitContributions;
412
- }
413
- let repoUserStats = await fetchRepoUserStats(
414
- username,
415
- repo,
416
- owner,
417
- include_prs_authored,
418
- include_prs_commented,
419
- include_prs_reviewed,
420
- include_issues_authored,
421
- include_issues_commented,
422
- pat,
423
- );
424
- Object.assign(stats, repoUserStats);
425
-
426
- stats.totalPRs = user.pullRequests.totalCount;
427
- if (include_merged_pull_requests) {
428
- stats.totalPRsMerged = user.mergedPullRequests.totalCount;
429
- stats.mergedPRsPercentage =
430
- (user.mergedPullRequests.totalCount / user.pullRequests.totalCount) *
431
- 100 || 0;
432
- }
433
- stats.totalReviews = user.reviews.totalPullRequestReviewContributions;
434
- stats.totalIssues = user.openIssues.totalCount + user.closedIssues.totalCount;
435
- if (include_discussions) {
436
- stats.totalDiscussionsStarted = user.repositoryDiscussions.totalCount;
437
- }
438
- if (include_discussions_answers) {
439
- stats.totalDiscussionsAnswered =
440
- user.repositoryDiscussionComments.totalCount;
441
- }
442
- stats.contributedTo = user.repositoriesContributedTo.totalCount;
443
-
444
- // Retrieve stars while filtering out repositories to be hidden.
445
- const allExcludedRepos = [
446
- ...exclude_repo,
447
- ...getConfig().excludeRepositories,
448
- ];
449
- let repoToHide = new Set(allExcludedRepos);
450
-
451
- stats.totalStars = user.repositories.nodes
452
- .filter((data) => {
453
- return !repoToHide.has(data.name);
454
- })
455
- .reduce((prev, curr) => {
456
- return prev + curr.stargazerCount;
457
- }, 0);
458
-
459
- stats.rank = calculateRank({
460
- all_commits: include_all_commits,
461
- commits: stats.totalCommits,
462
- prs: stats.totalPRs,
463
- reviews: stats.totalReviews,
464
- issues: stats.totalIssues,
465
- repos: user.repositories.totalCount,
466
- stars: stats.totalStars,
467
- followers: user.followers.totalCount,
468
- });
469
-
470
- return stats;
471
- };
472
-
473
- export { fetchStats, fetchRepoUserStats };