@@ -67,7 +67,7 @@ const fetcher = (variables, token) => {
67
67
*
68
68
* @param {import('axios').AxiosRequestHeaders } variables Fetcher variables.
69
69
* @param {string } token Github token.
70
- * @returns {Promise<import('../common/types').StatsFetcherResponse > } Repositories fetcher response.
70
+ * @returns {Promise<import('../common/types').RepositoriesFetcherResponse > } Repositories fetcher response.
71
71
*/
72
72
const repositoriesFetcher = ( variables , token ) => {
73
73
return request (
@@ -98,46 +98,54 @@ const repositoriesFetcher = (variables, token) => {
98
98
) ;
99
99
} ;
100
100
101
+ /**
102
+ * Fetch all commits for a given username.
103
+ *
104
+ * @param {import('axios').AxiosRequestHeaders } variables Fetcher variables.
105
+ * @param {string } token Github token.
106
+ * @returns {Promise<import('../common/types').TotalCommitsFetcherResponse> } Total commits fetcher response.
107
+ *
108
+ * @see https://developer.github.com/v3/search/#search-commits.
109
+ */
110
+ const totalCommitsFetcher = ( variables , token ) => {
111
+ return axios ( {
112
+ method : "get" ,
113
+ url : `https://api.github.com/search/commits?q=author:${ variables . login } ` ,
114
+ headers : {
115
+ "Content-Type" : "application/json" ,
116
+ Accept : "application/vnd.github.cloak-preview" ,
117
+ Authorization : `token ${ token } ` ,
118
+ } ,
119
+ } ) ;
120
+ } ;
121
+
101
122
/**
102
123
* Fetch all the commits for all the repositories of a given username.
103
124
*
104
125
* @param {* } username Github username.
105
- * @returns {Promise<number > } Total commits.
126
+ * @returns {Promise<Object > } Object containing the total number of commits and a success boolean .
106
127
*
107
128
* @description Done like this because the Github API does not provide a way to fetch all the commits. See
108
129
* #92#issuecomment-661026467 and #211 for more information.
109
130
*/
110
- const totalCommitsFetcher = async ( username ) => {
131
+ const fetchTotalCommits = async ( username ) => {
111
132
if ( ! githubUsernameRegex . test ( username ) ) {
112
133
logger . log ( "Invalid username" ) ;
113
- return 0 ;
134
+ return { totalCommits : 0 , success : false } ;
114
135
}
115
136
116
- // https://developer.github.com/v3/search/#search-commits
117
- const fetchTotalCommits = ( variables , token ) => {
118
- return axios ( {
119
- method : "get" ,
120
- url : `https://api.github.com/search/commits?q=author:${ variables . login } ` ,
121
- headers : {
122
- "Content-Type" : "application/json" ,
123
- Accept : "application/vnd.github.cloak-preview" ,
124
- Authorization : `token ${ token } ` ,
125
- } ,
126
- } ) ;
127
- } ;
128
-
129
137
try {
130
- let res = await retryer ( fetchTotalCommits , { login : username } ) ;
138
+ let res = await retryer ( totalCommitsFetcher , { login : username } ) ;
131
139
let total_count = res . data . total_count ;
132
140
if ( ! ! total_count && ! isNaN ( total_count ) ) {
133
- return res . data . total_count ;
141
+ return { totalCommits : res . data . total_count , success : true } ;
134
142
}
135
143
} catch ( err ) {
136
144
logger . log ( err ) ;
137
145
}
138
146
// just return 0 if there is something wrong so that
139
147
// we don't break the whole app
140
- return 0 ;
148
+ return { totalCommits : 0 , success : false } ;
141
149
} ;
142
150
143
151
/**
@@ -186,7 +194,7 @@ const totalStarsFetcher = async (username, repoToHide) => {
186
194
* @param {string } username Github username.
187
195
* @param {boolean } count_private Include private contributions.
188
196
* @param {boolean } include_all_commits Include all commits.
189
- * @returns {Promise<import("./types").StatsData > } Stats data.
197
+ * @returns {Promise<Object > } Object containing the stats data a success boolean that can be used to handle the caching behavior .
190
198
*/
191
199
async function fetchStats (
192
200
username ,
@@ -247,9 +255,13 @@ async function fetchStats(
247
255
stats . totalCommits = user . contributionsCollection . totalCommitContributions ;
248
256
249
257
// if include_all_commits then just get that,
250
- // since totalCommitsFetcher already sends totalCommits no need to +=
258
+ // NOTE: Since fetchTotalCommits already sends totalCommits no need to +=.
259
+ let totalCommitsResp ;
260
+ let success = true ;
251
261
if ( include_all_commits ) {
252
- stats . totalCommits = await totalCommitsFetcher ( username ) ;
262
+ totalCommitsResp = await fetchTotalCommits ( username ) ;
263
+ stats . totalCommits = totalCommitsResp . totalCommits ;
264
+ success = totalCommitsResp . success ;
253
265
}
254
266
255
267
// if count_private then add private commits to totalCommits so far.
@@ -274,7 +286,7 @@ async function fetchStats(
274
286
issues : stats . totalIssues ,
275
287
} ) ;
276
288
277
- return stats ;
289
+ return { stats, success } ;
278
290
}
279
291
280
292
export { fetchStats } ;
0 commit comments