@@ -11,6 +11,11 @@ function encRepo(repo: string): string {
1111}
1212
1313async function gh < T > ( path : string ) : Promise < T > {
14+ return ( await ghRes < T > ( path ) ) . data ;
15+ }
16+
17+ /** Igual que gh() pero devuelve también los headers (para leer el `Link` de paginación). */
18+ async function ghRes < T > ( path : string ) : Promise < { data : T ; headers : Headers } > {
1419 const res = await fetch ( `${ API } ${ path } ` , {
1520 headers : {
1621 authorization : `Bearer ${ config . github . token } ` ,
@@ -19,7 +24,37 @@ async function gh<T>(path: string): Promise<T> {
1924 } ,
2025 } ) ;
2126 if ( ! res . ok ) throw new Error ( `GitHub API error ${ res . status } : ${ await res . text ( ) } ` ) ;
22- return ( await res . json ( ) ) as T ;
27+ return { data : ( await res . json ( ) ) as T , headers : res . headers } ;
28+ }
29+
30+ /** Nº de la última página del header `Link` (rel="last"), o null si no hay más páginas. */
31+ function parseLastPage ( link : string | null ) : number | null {
32+ if ( ! link ) return null ;
33+ const m = link . match ( / [ ? & ] p a g e = ( \d + ) [ ^ > ] * > ; \s * r e l = " l a s t " / ) ;
34+ return m ? Number ( m [ 1 ] ) : null ;
35+ }
36+
37+ // Rutas de "trabajo no-real": dependencias, artefactos generados y lockfiles. Sus líneas NO cuentan
38+ // como contribución (un commit que versiona node_modules metía cientos de miles de líneas basura).
39+ // Se filtran por commit recalculando additions/deletions solo sobre los archivos que SÍ son código.
40+ const GENERATED_DIRS = [
41+ 'node_modules/' , 'vendor/' , 'dist/' , 'build/' , '.next/' , 'out/' , 'coverage/' , '.venv/' ,
42+ 'venv/' , '__pycache__/' , 'bin/' , 'obj/' , '.turbo/' , 'target/' ,
43+ ] ;
44+ const GENERATED_FILES = new Set ( [
45+ 'package-lock.json' , 'yarn.lock' , 'pnpm-lock.yaml' , 'composer.lock' , 'gemfile.lock' ,
46+ 'poetry.lock' , 'cargo.lock' , 'go.sum' , 'podfile.lock' , 'flake.lock' ,
47+ ] ) ;
48+ const GENERATED_SUFFIXES = [ '.lock' , '.min.js' , '.min.css' , '.map' , '.snap' ] ;
49+
50+ /** ¿La ruta es de trabajo generado/dependencias (no cuenta como líneas)? Case-insensitive. */
51+ export function isGeneratedPath ( filename : string ) : boolean {
52+ const f = filename . toLowerCase ( ) ;
53+ const base = f . split ( '/' ) . pop ( ) ?? f ;
54+ if ( GENERATED_FILES . has ( base ) ) return true ;
55+ if ( GENERATED_SUFFIXES . some ( ( s ) => base . endsWith ( s ) ) ) return true ;
56+ // Directorio generado en cualquier nivel del path (p.ej. "apps/web/node_modules/...").
57+ return GENERATED_DIRS . some ( ( d ) => f . startsWith ( d ) || f . includes ( `/${ d } ` ) ) ;
2358}
2459
2560/** Llama a la GraphQL API v4 (la REST v3 no expone el calendario de contribuciones). */
@@ -119,23 +154,55 @@ export interface CommitMeta {
119154}
120155
121156export async function getCommit ( repo : string , sha : string ) : Promise < CommitMeta > {
122- const data = await gh < {
157+ type FileEntry = { filename : string ; additions ?: number ; deletions ?: number } ;
158+ type CommitResp = {
123159 sha : string ;
124160 html_url : string ;
125161 commit : { message : string ; author ?: { email ?: string ; date ?: string } } ;
126162 author : { login : string } | null ;
127163 stats ?: { additions ?: number ; deletions ?: number } ;
128164 parents ?: unknown [ ] ;
129- } > ( `/repos/${ encRepo ( repo ) } /commits/${ sha } ` ) ;
165+ files ?: FileEntry [ ] ;
166+ } ;
167+
168+ // El endpoint devuelve el desglose por archivo (`files`), paginado a 100. Un commit que versiona
169+ // node_modules trae miles de archivos: paginamos hasta un tope defensivo para poder filtrarlos.
170+ const enc = `/repos/${ encRepo ( repo ) } /commits/${ encodeURIComponent ( sha ) } ` ;
171+ const data = await gh < CommitResp > ( `${ enc } ?per_page=100&page=1` ) ;
172+ const files : FileEntry [ ] = [ ...( data . files ?? [ ] ) ] ;
173+ const MAX_FILE_PAGES = 30 ; // 30×100 = 3000 archivos: de sobra para código real; acota los dumps.
174+ let batchLen = data . files ?. length ?? 0 ;
175+ for ( let page = 2 ; batchLen === 100 && page <= MAX_FILE_PAGES ; page ++ ) {
176+ const next = await gh < CommitResp > ( `${ enc } ?per_page=100&page=${ page } ` ) ;
177+ const batch = next . files ?? [ ] ;
178+ files . push ( ...batch ) ;
179+ batchLen = batch . length ;
180+ }
181+
182+ // Recalcular líneas contando SOLO archivos que no son generados/dependencias. Si el desglose no
183+ // vino (caso raro), se cae a los stats totales del commit para no quedarnos sin dato.
184+ let additions = 0 ;
185+ let deletions = 0 ;
186+ if ( data . files != null ) {
187+ for ( const f of files ) {
188+ if ( isGeneratedPath ( f . filename ) ) continue ;
189+ additions += f . additions ?? 0 ;
190+ deletions += f . deletions ?? 0 ;
191+ }
192+ } else {
193+ additions = data . stats ?. additions ?? 0 ;
194+ deletions = data . stats ?. deletions ?? 0 ;
195+ }
196+
130197 return {
131198 sha : data . sha ,
132199 message : data . commit . message ,
133200 url : data . html_url ,
134201 author : data . author ?. login ?? null ,
135202 authorEmail : data . commit . author ?. email ?? null ,
136203 committedAt : data . commit . author ?. date ?? null ,
137- additions : data . stats ?. additions ?? null ,
138- deletions : data . stats ?. deletions ?? null ,
204+ additions,
205+ deletions,
139206 isMerge : ( data . parents ?. length ?? 0 ) >= 2 ,
140207 } ;
141208}
@@ -156,20 +223,29 @@ export interface RepoCommitListItem {
156223 * sí trae `parents` para descartar merges sin una llamada extra. La lista omite ramas no mergeadas
157224 * (default branch), igual que el conteo en vivo. Devuelve [] en 404 (repo sin acceso).
158225 */
159- export async function listRepoCommits ( repo : string , sinceISO : string , page = 1 ) : Promise < RepoCommitListItem [ ] > {
160- const data = await gh <
161- {
162- sha : string ;
163- html_url : string ;
164- commit : { message : string ; author ?: { email ?: string ; date ?: string } } ;
165- author : { login ?: string } | null ;
166- parents ?: unknown [ ] ;
167- } [ ]
168- > ( `/repos/${ encRepo ( repo ) } /commits?since=${ encodeURIComponent ( sinceISO ) } &per_page=100&page=${ page } ` ) . catch ( ( e ) => {
169- if ( String ( e ) . includes ( '404' ) ) return [ ] as never [ ] ;
226+ export interface RepoCommitsPage {
227+ items : RepoCommitListItem [ ] ;
228+ lastPage : number ; // total de páginas (del header Link); = page actual si es la única/última.
229+ }
230+
231+ export async function listRepoCommits ( repo : string , sinceISO : string , page = 1 ) : Promise < RepoCommitsPage > {
232+ type Row = {
233+ sha : string ;
234+ html_url : string ;
235+ commit : { message : string ; author ?: { email ?: string ; date ?: string } } ;
236+ author : { login ?: string } | null ;
237+ parents ?: unknown [ ] ;
238+ } ;
239+ let res : { data : Row [ ] ; headers : Headers } ;
240+ try {
241+ res = await ghRes < Row [ ] > (
242+ `/repos/${ encRepo ( repo ) } /commits?since=${ encodeURIComponent ( sinceISO ) } &per_page=100&page=${ page } ` ,
243+ ) ;
244+ } catch ( e ) {
245+ if ( String ( e ) . includes ( '404' ) ) return { items : [ ] , lastPage : page } ; // repo sin acceso
170246 throw e ;
171- } ) ;
172- return data . map ( ( d ) => ( {
247+ }
248+ const items = res . data . map ( ( d ) => ( {
173249 sha : d . sha ,
174250 message : d . commit . message ,
175251 url : d . html_url ,
@@ -178,6 +254,23 @@ export async function listRepoCommits(repo: string, sinceISO: string, page = 1):
178254 committedAt : d . commit . author ?. date ?? null ,
179255 isMerge : ( d . parents ?. length ?? 0 ) >= 2 ,
180256 } ) ) ;
257+ return { items, lastPage : parseLastPage ( res . headers . get ( 'link' ) ) ?? page } ;
258+ }
259+
260+ /**
261+ * Todos los repos de la organización (full_name en minúsculas), para el autocomplete al vincular.
262+ * Ordenados por push reciente. Best-effort: si una página falla, corta y devuelve lo acumulado.
263+ */
264+ export async function listOrgRepos ( org = 'hyperlabs-ai' ) : Promise < string [ ] > {
265+ const out : string [ ] = [ ] ;
266+ for ( let page = 1 ; page <= 20 ; page ++ ) {
267+ const batch = await gh < { full_name : string } [ ] > (
268+ `/orgs/${ encodeURIComponent ( org ) } /repos?per_page=100&sort=pushed&page=${ page } ` ,
269+ ) . catch ( ( ) => [ ] as { full_name : string } [ ] ) ;
270+ for ( const r of batch ) out . push ( r . full_name . toLowerCase ( ) ) ;
271+ if ( batch . length < 100 ) break ;
272+ }
273+ return out ;
181274}
182275
183276export interface RepoMeta {
0 commit comments