@@ -168,86 +168,91 @@ export async function getGitHubStats(
168
168
}
169
169
}
170
170
`
171
- const lastMonth = new Date ( )
172
- lastMonth . setMonth ( lastMonth . getMonth ( ) - 3 )
173
- const response = await fetch ( "https://api.github.com/graphql" , {
174
- method : "POST" ,
175
- body : JSON . stringify ( {
176
- query,
177
- variables : { owner, repoName, since : lastMonth } ,
178
- } ) ,
179
- headers : {
180
- Authorization : `Bearer ${ accessToken } ` ,
181
- "Content-Type" : "application/json" ,
182
- } ,
183
- } )
184
- if ( ! response . ok ) {
185
- console . warn (
186
- `Get invalid response from GitHub for ${ owner } /${ repoName } . Status: ${ response . status } `
187
- )
188
- }
189
- const responseJson : GitHubStatsFetchResponse = await response . json ( )
171
+ const lastThreeMonths = new Date ( )
172
+ lastThreeMonths . setMonth ( lastThreeMonths . getMonth ( ) - 3 )
173
+
174
+ try {
175
+ const response = await fetch ( "https://api.github.com/graphql" , {
176
+ method : "POST" ,
177
+ body : JSON . stringify ( {
178
+ query,
179
+ variables : { owner, repoName, since : lastThreeMonths . toISOString ( ) } ,
180
+ } ) ,
181
+ headers : {
182
+ Authorization : `Bearer ${ accessToken } ` ,
183
+ "Content-Type" : "application/json" ,
184
+ } ,
185
+ } )
186
+
187
+ if ( ! response . ok ) {
188
+ console . warn (
189
+ `Error fetching GitHub stats for ${ owner } /${ repoName } . Status: ${ response . status } `
190
+ )
191
+ return undefined
192
+ }
193
+
194
+ const responseJson : GitHubStatsFetchResponse = await response . json ( )
190
195
191
- if ( responseJson && "data" in responseJson ) {
192
- const repositoryOwner = responseJson . data . repositoryOwner
193
- if ( ! repositoryOwner ) {
194
- throw `No GitHub user found for ${ owner } /${ repoName } `
196
+ if ( "errors" in responseJson ) {
197
+ console . warn (
198
+ `GitHub GraphQL errors for ${ owner } /${ repoName } :` ,
199
+ responseJson . errors
200
+ )
201
+ return undefined
195
202
}
196
- const { repository : repo } = repositoryOwner
197
- console . log ( "repo:" , repo . tags )
203
+
204
+ const repo = responseJson . data . repositoryOwner ?. repository
198
205
if ( ! repo ) {
199
- throw `No GitHub repo found ${ owner } /${ repoName } `
206
+ console . warn ( `No GitHub repository found for ${ owner } /${ repoName } ` )
207
+ return undefined
200
208
}
201
- const stars = repo . stargazers . totalCount
202
- const commitHistory = repo . defaultBranchRef . target . history . edges
203
209
204
- let hasCommitsInLast3Months = false
205
- commitHistory . forEach ( commit => {
206
- if ( ! commit . node . author . name . match ( / b o t / i) ) {
207
- hasCommitsInLast3Months = true
208
- }
209
- } )
210
- const formattedStars = numbro ( stars ) . format ( {
210
+ const hasCommitsInLast3Months =
211
+ repo . defaultBranchRef . target . history . edges . some (
212
+ edge => new Date ( edge . node . pushedDate ) > lastThreeMonths
213
+ )
214
+ const formattedStars = numbro ( repo . stargazers . totalCount ) . format ( {
211
215
average : true ,
212
216
} )
213
- const releases : Release [ ] = [ ]
214
- if (
215
- repo . tags &&
216
- repo . tags . nodes &&
217
- repo . tags . nodes . length &&
218
- repo . tags . nodes [ 0 ] . target . target &&
219
- repo . tags . nodes [ 0 ] . target . target . pushedDate
220
- ) {
217
+
218
+ const lastRelease = getLastRelease ( repo )
219
+
220
+ return {
221
+ hasCommitsInLast3Months,
222
+ stars : repo . stargazers . totalCount ,
223
+ formattedStars,
224
+ license : repo . licenseInfo ?. name ?? "Unknown" ,
225
+ lastRelease : lastRelease ?. date ?? "" ,
226
+ formattedLastRelease : lastRelease ?. formattedDate ?? "" ,
227
+ }
228
+ } catch ( error ) {
229
+ console . error ( `Exception fetching GitHub stats for ${ githubRepo } :` , error )
230
+ return undefined
231
+ }
232
+ }
233
+
234
+ function getLastRelease ( repo : any ) : Release | undefined {
235
+ const releases : Release [ ] = [ ]
236
+
237
+ repo . tags . nodes . forEach ( ( node : any ) => {
238
+ if ( node . target . target ?. pushedDate ) {
221
239
releases . push ( {
222
- date : repo . tags . nodes [ 0 ] . target . target . pushedDate ,
223
- formattedDate : timeago ( repo . tags . nodes [ 0 ] . target . target . pushedDate ) ,
240
+ date : node . target . target . pushedDate ,
241
+ formattedDate : timeago ( node . target . target . pushedDate ) ,
224
242
} )
225
243
}
226
- if ( repo . releases && repo . releases . nodes && repo . releases . nodes . length ) {
244
+ } )
245
+
246
+ repo . releases . nodes . forEach ( ( node : any ) => {
247
+ if ( node . publishedAt ) {
227
248
releases . push ( {
228
- date : repo . releases . nodes [ 0 ] . publishedAt ,
229
- formattedDate : timeago ( repo . releases . nodes [ 0 ] . publishedAt ) ,
249
+ date : node . publishedAt ,
250
+ formattedDate : timeago ( node . publishedAt ) ,
230
251
} )
231
252
}
232
- if ( owner . includes ( "graphql" ) ) {
233
- console . log ( { releases, repoName } )
234
- }
235
- console . log ( "releases" , releases )
253
+ } )
236
254
237
- const lastRelease = releases . filter ( Boolean ) . sort ( ) . reverse ( ) [ 0 ]
238
- return {
239
- hasCommitsInLast3Months,
240
- stars,
241
- formattedStars,
242
- license : repo . licenseInfo && repo . licenseInfo . name ,
243
- lastRelease : lastRelease ? lastRelease . date : "" ,
244
- formattedLastRelease : lastRelease ? lastRelease . formattedDate : "" ,
245
- }
246
- } else {
247
- console . warn (
248
- `Get invalid response from GitHub for ${ owner } /${ repoName } . Response: ${ JSON . stringify (
249
- responseJson
250
- ) } `
251
- )
252
- }
255
+ return releases . sort (
256
+ ( a , b ) => new Date ( b . date ) . getTime ( ) - new Date ( a . date ) . getTime ( )
257
+ ) [ 0 ]
253
258
}
0 commit comments