1+ import { GitHubAPIClient } from "@api/githubClient" ;
12import { PostHogAPIClient } from "@api/posthogClient" ;
23import { identifyUser , resetUser , track } from "@renderer/lib/analytics" ;
34import { electronStorage } from "@renderer/lib/electronStorage" ;
45import { logger } from "@renderer/lib/logger" ;
56import { queryClient } from "@renderer/lib/queryClient" ;
7+ import { parseRepository } from "@renderer/utils/repository" ;
68import type { CloudRegion } from "@shared/types/oauth" ;
79import { useNavigationStore } from "@stores/navigationStore" ;
10+ import { repositoryWorkspaceStore } from "@stores/repositoryWorkspaceStore" ;
811import { create } from "zustand" ;
912import { persist } from "zustand/middleware" ;
1013import {
@@ -36,6 +39,9 @@ interface AuthState {
3639 client : PostHogAPIClient | null ;
3740 projectId : number | null ; // Current team/project ID
3841
42+ // GitHub client
43+ githubClient : GitHubAPIClient | null ;
44+
3945 // OpenAI API key (separate concern, kept for now)
4046 openaiApiKey : string | null ;
4147 encryptedOpenaiKey : string | null ;
@@ -50,6 +56,9 @@ interface AuthState {
5056 // Other methods
5157 setOpenAIKey : ( apiKey : string ) => Promise < void > ;
5258 setDefaultWorkspace : ( workspace : string ) => void ;
59+ createGitHubClient : ( accessToken : string , repository : string ) => boolean ;
60+ initializeGitHubClient : ( accessToken : string ) => boolean ;
61+ initializeGitHubClientFromIntegration : ( ) => Promise < boolean > ;
5362 logout : ( ) => void ;
5463}
5564
@@ -70,6 +79,9 @@ export const useAuthStore = create<AuthState>()(
7079 client : null ,
7180 projectId : null ,
7281
82+ // GitHub client
83+ githubClient : null ,
84+
7385 // OpenAI key
7486 openaiApiKey : null ,
7587 encryptedOpenaiKey : null ,
@@ -143,6 +155,13 @@ export const useAuthStore = create<AuthState>()(
143155 project_id : projectId . toString ( ) ,
144156 region,
145157 } ) ;
158+
159+ // Initialize GitHub client if available
160+ try {
161+ await get ( ) . initializeGitHubClientFromIntegration ( ) ;
162+ } catch ( error ) {
163+ log . warn ( "Failed to initialize GitHub client during login:" , error ) ;
164+ }
146165 } catch {
147166 throw new Error ( "Failed to authenticate with PostHog" ) ;
148167 }
@@ -316,6 +335,16 @@ export const useAuthStore = create<AuthState>()(
316335 region : tokens . cloudRegion ,
317336 } ) ;
318337
338+ // Initialize GitHub client if available
339+ try {
340+ await get ( ) . initializeGitHubClientFromIntegration ( ) ;
341+ } catch ( error ) {
342+ log . warn (
343+ "Failed to initialize GitHub client during OAuth init:" ,
344+ error ,
345+ ) ;
346+ }
347+
319348 if ( state . encryptedOpenaiKey ) {
320349 const decryptedOpenaiKey =
321350 await window . electronAPI . retrieveApiKey (
@@ -359,6 +388,73 @@ export const useAuthStore = create<AuthState>()(
359388 setDefaultWorkspace : ( workspace : string ) => {
360389 set ( { defaultWorkspace : workspace } ) ;
361390 } ,
391+
392+ createGitHubClient : ( accessToken : string , repository : string ) => {
393+ const parsed = parseRepository ( repository ) ;
394+ if ( ! parsed ) {
395+ log . error ( "Invalid repository format:" , repository ) ;
396+ return false ;
397+ }
398+
399+ const githubClient = new GitHubAPIClient (
400+ accessToken ,
401+ parsed . organization ,
402+ parsed . repoName ,
403+ ) ;
404+ set ( { githubClient } ) ;
405+ return true ;
406+ } ,
407+
408+ initializeGitHubClient : ( accessToken : string ) => {
409+ const { selectedRepository } = repositoryWorkspaceStore . getState ( ) ;
410+
411+ if ( ! selectedRepository ) {
412+ log . warn ( "No repository selected, cannot create GitHub client" ) ;
413+ return false ;
414+ }
415+
416+ return get ( ) . createGitHubClient ( accessToken , selectedRepository ) ;
417+ } ,
418+
419+ initializeGitHubClientFromIntegration : async ( ) => {
420+ const { client } = get ( ) ;
421+
422+ if ( ! client ) {
423+ log . warn (
424+ "No PostHog client available, cannot fetch GitHub integration" ,
425+ ) ;
426+ return false ;
427+ }
428+
429+ try {
430+ const integrations = await client . getIntegrations ( ) ;
431+ const githubIntegration = integrations . find (
432+ ( i : any ) => i . kind === "github" ,
433+ ) ;
434+
435+ if ( ! githubIntegration ) {
436+ log . warn ( "No GitHub integration found" ) ;
437+ return false ;
438+ }
439+
440+ const accessToken = await client . getGitHubAccessToken (
441+ githubIntegration . id ,
442+ ) ;
443+
444+ if ( ! accessToken ) {
445+ log . warn ( "No access token found for GitHub integration" ) ;
446+ return false ;
447+ }
448+
449+ return get ( ) . initializeGitHubClient ( accessToken ) ;
450+ } catch ( error ) {
451+ log . error (
452+ "Failed to initialize GitHub client from integration:" ,
453+ error ,
454+ ) ;
455+ return false ;
456+ }
457+ } ,
362458 logout : ( ) => {
363459 track ( ANALYTICS_EVENTS . USER_LOGGED_OUT ) ;
364460 resetUser ( ) ;
@@ -381,6 +477,7 @@ export const useAuthStore = create<AuthState>()(
381477 isAuthenticated : false ,
382478 client : null ,
383479 projectId : null ,
480+ githubClient : null ,
384481 openaiApiKey : null ,
385482 encryptedOpenaiKey : null ,
386483 } ) ;
0 commit comments