@@ -10,7 +10,7 @@ export interface GitHubOrgStats {
1010 totalRepositories : number ;
1111 totalContributors : number ;
1212 publicRepositories : number ;
13- discussionsCount : number ;
13+ discussionsCount : number | null ;
1414 lastUpdated : number ;
1515}
1616
@@ -82,6 +82,27 @@ class GitHubService {
8282 return typeof window === "undefined" ;
8383 }
8484
85+ private getGitHubToken ( ) : string | null {
86+ if ( typeof window !== "undefined" ) {
87+ return null ;
88+ }
89+
90+ return process . env . GITHUB_TOKEN ?. trim ( ) || null ;
91+ }
92+
93+ private getGraphQLHeaders ( ) : Record < string , string > {
94+ const token = this . getGitHubToken ( ) ;
95+
96+ if ( ! token ) {
97+ throw new Error ( this . DISCUSSIONS_UNAVAILABLE_MESSAGE ) ;
98+ }
99+
100+ return {
101+ ...this . getHeaders ( ) ,
102+ Authorization : `Bearer ${ token } ` ,
103+ } ;
104+ }
105+
85106 // === ADDED: setter to toggle anonymous contributors inclusion
86107 setIncludeAnonymousContributors ( value : boolean ) {
87108 this . includeAnonymousContributors = value ;
@@ -277,9 +298,9 @@ class GitHubService {
277298 private async getDiscussionsCount (
278299 signal ?: AbortSignal ,
279300 repoName : string = "Support" ,
280- ) : Promise < number > {
301+ ) : Promise < number | null > {
281302 if ( ! this . canUseGitHubGraphQL ( ) ) {
282- return 0 ;
303+ return null ;
283304 }
284305
285306 try {
@@ -295,7 +316,7 @@ class GitHubService {
295316 const resp = await fetch ( "https://api.github.com/graphql" , {
296317 method : "POST" ,
297318 headers : {
298- ...this . getHeaders ( ) ,
319+ ...this . getGraphQLHeaders ( ) ,
299320 "Content-Type" : "application/json" ,
300321 } ,
301322 body : JSON . stringify ( { query, variables } ) ,
@@ -304,20 +325,20 @@ class GitHubService {
304325
305326 if ( ! resp . ok ) {
306327 console . warn ( `GraphQL request for discussions failed: ${ resp . status } ` ) ;
307- return 0 ;
328+ return null ;
308329 }
309330
310331 const data = await resp . json ( ) ;
311332 if ( data . errors ) {
312333 console . warn ( "GraphQL errors while fetching discussions:" , data . errors ) ;
313- return 0 ;
334+ return null ;
314335 }
315336
316337 const count = data ?. data ?. repository ?. discussions ?. totalCount || 0 ;
317338 return Number ( count ) ;
318339 } catch ( error ) {
319340 console . warn ( "Error fetching discussions count via GraphQL:" , error ) ;
320- return 0 ;
341+ return null ;
321342 }
322343 }
323344
@@ -379,7 +400,7 @@ class GitHubService {
379400 totalRepositories : 0 ,
380401 publicRepositories : 0 ,
381402 totalContributors : 0 ,
382- discussionsCount : 0 ,
403+ discussionsCount : null ,
383404 lastUpdated : Date . now ( ) ,
384405 } ;
385406
@@ -415,6 +436,8 @@ class GitHubService {
415436 throw new Error ( this . DISCUSSIONS_UNAVAILABLE_MESSAGE ) ;
416437 }
417438
439+ const graphqlHeaders = this . getGraphQLHeaders ( ) ;
440+
418441 const query = `
419442 query GetDiscussions($owner: String!, $name: String!, $first: Int!) {
420443 repository(owner: $owner, name: $name) {
@@ -463,7 +486,7 @@ class GitHubService {
463486 const response = await fetch ( "https://api.github.com/graphql" , {
464487 method : "POST" ,
465488 headers : {
466- ...this . getHeaders ( ) ,
489+ ...graphqlHeaders ,
467490 "Content-Type" : "application/json" ,
468491 } ,
469492 body : JSON . stringify ( { query, variables } ) ,
0 commit comments