@@ -123,19 +123,110 @@ export async function fetchGitHubRepositories(options?: {
123123 type ?: 'all' | 'owner' | 'public' | 'private' | 'member'
124124} ) : Promise < { repositories : GitHubRepository [ ] ; total_count : number ; has_more : boolean } | null > {
125125 try {
126- const params = new URLSearchParams ( )
127- if ( options ?. page ) params . append ( 'page' , options . page . toString ( ) )
128- if ( options ?. per_page ) params . append ( 'per_page' , options . per_page . toString ( ) )
129- if ( options ?. sort ) params . append ( 'sort' , options . sort )
130- if ( options ?. type ) params . append ( 'type' , options . type )
126+ // First get the current user to know whose repos to fetch
127+ const userResponse = await fetch ( '/api/github/user' )
128+ if ( ! userResponse . ok ) {
129+ throw new Error ( 'Failed to fetch GitHub user' )
130+ }
131+ const userData = await userResponse . json ( )
131132
132- const response = await fetch ( `/api/integrations/github/repos? ${ params . toString ( ) } ` )
133-
134- if ( ! response . ok ) {
133+ // Get user's repositories
134+ const reposResponse = await fetch ( `/api/github/repos?owner= ${ userData . login } ` )
135+ if ( ! reposResponse . ok ) {
135136 throw new Error ( 'Failed to fetch repositories' )
136137 }
138+ const userRepos = await reposResponse . json ( )
139+
140+ // Get user's organizations and their repos if type allows
141+ let orgRepos : any [ ] = [ ]
142+ if ( ! options ?. type || options . type === 'all' || options . type === 'member' ) {
143+ const orgsResponse = await fetch ( '/api/github/orgs' )
144+ if ( orgsResponse . ok ) {
145+ const orgs = await orgsResponse . json ( )
146+
147+ for ( const org of orgs ) {
148+ try {
149+ const orgReposResponse = await fetch ( `/api/github/repos?owner=${ org . login } ` )
150+ if ( orgReposResponse . ok ) {
151+ const repos = await orgReposResponse . json ( )
152+ orgRepos . push ( ...repos )
153+ }
154+ } catch ( error ) {
155+ console . warn ( `Failed to fetch repos for org ${ org . login } :` , error )
156+ }
157+ }
158+ }
159+ }
160+
161+ // Filter repositories based on type
162+ let allRepos = [ ...userRepos , ...orgRepos ]
163+ if ( options ?. type === 'owner' ) {
164+ allRepos = userRepos // Only user's own repos
165+ }
137166
138- return await response . json ( )
167+ // Convert to expected format
168+ const repositories : GitHubRepository [ ] = allRepos . map ( ( repo : any ) => ( {
169+ id : repo . id || Math . random ( ) ,
170+ name : repo . name ,
171+ full_name : repo . full_name ,
172+ description : repo . description ,
173+ html_url : `https://github.com/${ repo . full_name } ` ,
174+ clone_url : repo . clone_url ,
175+ ssh_url :
`[email protected] :${ repo . full_name } .git` , 176+ private : repo . private ,
177+ fork : false ,
178+ archived : false ,
179+ disabled : false ,
180+ owner : {
181+ login : repo . full_name . split ( '/' ) [ 0 ] ,
182+ avatar_url : userData . avatar_url ,
183+ type : 'User'
184+ } ,
185+ created_at : new Date ( ) . toISOString ( ) ,
186+ updated_at : repo . updated_at ,
187+ pushed_at : repo . updated_at ,
188+ language : repo . language ,
189+ stargazers_count : 0 ,
190+ watchers_count : 0 ,
191+ forks_count : 0 ,
192+ open_issues_count : 0 ,
193+ size : 0 ,
194+ default_branch : 'main' ,
195+ topics : [ ] ,
196+ has_issues : true ,
197+ has_projects : true ,
198+ has_wiki : true ,
199+ has_pages : false ,
200+ has_downloads : true ,
201+ license : null
202+ } ) )
203+
204+ // Apply sorting if specified
205+ if ( options ?. sort ) {
206+ repositories . sort ( ( a , b ) => {
207+ switch ( options . sort ) {
208+ case 'full_name' :
209+ return a . full_name . localeCompare ( b . full_name )
210+ case 'updated' :
211+ return new Date ( b . updated_at ) . getTime ( ) - new Date ( a . updated_at ) . getTime ( )
212+ default :
213+ return 0
214+ }
215+ } )
216+ }
217+
218+ // Apply pagination
219+ const page = options ?. page || 1
220+ const perPage = options ?. per_page || 30
221+ const startIndex = ( page - 1 ) * perPage
222+ const endIndex = startIndex + perPage
223+ const paginatedRepos = repositories . slice ( startIndex , endIndex )
224+
225+ return {
226+ repositories : paginatedRepos ,
227+ total_count : repositories . length ,
228+ has_more : endIndex < repositories . length
229+ }
139230 } catch ( error ) {
140231 console . error ( 'Error fetching GitHub repositories:' , error )
141232 return null
0 commit comments