11import * as extract from '@acme/extract-schema' ;
22import * as transform from '@acme/transform-schema' ;
3- import { sql , eq , or , and , type ExtractTablesWithRelations } from "drizzle-orm" ;
3+ import { sql , eq , or , and , type ExtractTablesWithRelations , inArray } from "drizzle-orm" ;
44import type { LibSQLDatabase } from 'drizzle-orm/libsql' ;
55import { isCodeGen } from './is-codegen' ;
66import { parseHunks } from './parse-hunks' ;
@@ -77,7 +77,7 @@ function upsertMergeRequest(db: TransformDatabase, mergeRequest: transform.NewMe
7777 . returning ( ) ;
7878}
7979
80- function _upsertForgeUser ( db : TransformDatabase , forgeUser : transform . NewForgeUser ) {
80+ function upsertForgeUser ( db : TransformDatabase , forgeUser : transform . NewForgeUser ) {
8181 return db . insert ( transform . forgeUsers )
8282 . values ( forgeUser )
8383 . onConflictDoUpdate ( {
@@ -282,74 +282,112 @@ type MapUsersToJunksArgs = {
282282 reviewers : transform . ForgeUser [ 'id' ] [ ]
283283}
284284
285- function addUnique ( newElement : number , currentArray : number [ ] ) {
286- if ( ! currentArray . includes ( newElement ) ) {
287- currentArray . push ( newElement ) ;
288- }
285+ type MappedUsersTypesArgs = {
286+ author : number ,
287+ mergedBy : number | null ,
288+ approvers : number [ ] ,
289+ committers : string [ ] ,
290+ reviewers : number [ ]
289291}
290292
291- async function getId ( actorId : number , db : TransformDatabase ) {
292- return await db . select ( {
293- id : transform . forgeUsers . id ,
294- } ) . from ( transform . forgeUsers )
295- . where ( eq ( transform . forgeUsers . externalId , actorId ) ) . get ( ) ;
293+ type TransformUserArgs = {
294+ externalId : transform . ForgeUser [ 'externalId' ] ;
295+ name : transform . ForgeUser [ 'name' ] ;
296+ forgeType : transform . ForgeUser [ 'forgeType' ] ;
296297}
297298
298- async function getUserIds ( timelineEvents : TimelineEventData [ ] , extractDb : ExtractDatabase , transformDb : TransformDatabase ) {
299- const reviewers : number [ ] = [ ] ;
300- const approvers : number [ ] = [ ] ;
301- const committers : number [ ] = [ ] ;
302- let mergedBy ;
299+ function getUserData ( timelineEvents : TimelineEventData [ ] , authorExternalId : number ) {
300+ const reviewers = new Set < number > ( ) ;
301+ const approvers = new Set < number > ( ) ;
302+ const committers = new Set < string > ( ) ;
303+ let mergedBy : number | undefined ;
304+ const author : number = authorExternalId ;
303305
304306 for ( const timelineEvent of timelineEvents ) {
305307 switch ( timelineEvent . type ) {
306308 case 'reviewed' :
307309 if ( ! timelineEvent . actorId ) {
308310 break ;
309311 }
310- const reviewer = await getId ( timelineEvent . actorId , transformDb ) ;
311- if ( reviewer ) {
312- addUnique ( reviewer . id , reviewers ) ;
313- if ( timelineEvent . data && ( ( timelineEvent . data as extract . ReviewedEvent ) . state === 'approved' ) ) {
314- addUnique ( reviewer ?. id , approvers ) ;
315- }
312+ reviewers . add ( timelineEvent . actorId ) ;
313+ if ( timelineEvent . data && ( ( timelineEvent . data as extract . ReviewedEvent ) . state === 'approved' ) ) {
314+ approvers . add ( timelineEvent . actorId ) ;
316315 }
317316 break ;
318317 case 'committed' :
319- const data = timelineEvent . data as extract . CommittedEvent ;
320- const extractUserExternalId = await extractDb . select ( {
321- id : extract . members . externalId ,
322- } ) . from ( extract . members )
323- . where ( or (
324- eq ( extract . members . username , data . committerName ) ,
325- eq ( extract . members . name , data . committerName ) )
326- ) . get ( ) ;
327- if ( extractUserExternalId ) {
328- const committer = await getId ( extractUserExternalId . id , transformDb ) ;
329- if ( committer ) {
330- addUnique ( committer . id , committers ) ;
331- }
332- }
318+ committers . add ( ( timelineEvent . data as extract . CommittedEvent ) . committerName ) ;
333319 break ;
334- case 'merged' :
320+ case 'merged' :
335321 if ( ! timelineEvent . actorId ) {
336322 break ;
337323 }
338- mergedBy = await getId ( timelineEvent . actorId , transformDb ) ;
324+ mergedBy = timelineEvent . actorId ;
339325 break ;
340326 default :
341327 break ;
342328 }
343329 }
344-
330+
345331 return {
346- mergedBy : mergedBy ?. id ,
347- approvers,
348- committers,
349- reviewers,
332+ author,
333+ mergedBy : mergedBy ? mergedBy : null ,
334+ approvers : [ ...approvers . keys ( ) ] ,
335+ committers : [ ...committers . keys ( ) ] ,
336+ reviewers : [ ...reviewers . keys ( ) ] ,
350337 } ;
351338}
352339
340+ async function getTransformUserData ( extractDb : ExtractDatabase , transformDb : TransformDatabase , users : MappedUsersTypesArgs ) {
341+ const { author, mergedBy, approvers, committers, reviewers } = users ;
342+ const allUsers = new Set < number > ( ) ;
343+ const transformUsers : transform . ForgeUser [ ] = [ ] ;
344+ allUsers . add ( author ) ;
345+ if ( mergedBy ) {
346+ allUsers . add ( mergedBy ) ;
347+ }
348+ approvers . forEach ( ( approver ) => { { allUsers . add ( approver ) ; } } ) ;
349+ reviewers . forEach ( ( reverse ) => { { allUsers . add ( reverse ) ; } } ) ;
350+
351+
352+ const response = await extractDb
353+ . select ( {
354+ externalId : extract . members . externalId ,
355+ name : extract . members . name ,
356+ forgeType : extract . members . forgeType ,
357+ userName : extract . members . username ,
358+ } )
359+ . from ( extract . members )
360+ . where (
361+ or (
362+ allUsers . size > 0 ? inArray ( extract . members . externalId , [ ...allUsers . keys ( ) ] ) : undefined ,
363+ committers . length > 0 ? inArray ( extract . members . name , committers ) : undefined ,
364+ committers . length > 0 ? inArray ( extract . members . username , committers ) : undefined ,
365+ ) ,
366+ )
367+ . all ( ) ;
368+
369+ for ( const res of response ) {
370+ if ( res . name === null ) {
371+ res . name = res . userName ;
372+ }
373+ transformUsers . push ( await upsertForgeUser ( transformDb , res as TransformUserArgs ) . returning ( ) . get ( ) ) ;
374+ }
375+
376+ const transformAuthor = transformUsers . find ( ( { externalId } ) => externalId === author ) ?. id ;
377+ const transformMergedBy = transformUsers . find ( ( { externalId } ) => externalId === mergedBy ) ?. id ;
378+ const transformApprovers = approvers . map ( ( approver ) => transformUsers . find ( ( { externalId } ) => externalId === approver ) ?. id as number ) ;
379+ const transformCommitters = committers . map ( ( committer ) => transformUsers . find ( ( { name } ) => name === committer ) ?. id as number ) ;
380+ const transformReviewers = reviewers . map ( ( reviewer ) => transformUsers . find ( ( { externalId } ) => externalId === reviewer ) ?. id as number ) ;
381+
382+ return {
383+ author : transformAuthor ? transformAuthor : null ,
384+ mergedBy : transformMergedBy ? transformMergedBy : null ,
385+ approvers : transformApprovers ? transformApprovers : [ ] ,
386+ committers : transformCommitters ? transformCommitters : [ ] ,
387+ reviewers : transformReviewers ? transformReviewers : [ ] ,
388+ }
389+ }
390+
353391function mapUsersToJunk ( { author, mergedBy, approvers, committers, reviewers } : MapUsersToJunksArgs , nullForgeUserId : number ) {
354392 return {
355393 author : author || nullForgeUserId ,
@@ -730,7 +768,9 @@ export async function run(extractMergeRequestId: number, ctx: RunContext) {
730768
731769 const timeline = runTimeline ( extractData . mergeRequest , extractData . timelineEvents , extractData . notes ) ;
732770
733- const users = await getUserIds ( extractData . timelineEvents , ctx . extractDatabase , ctx . transformDatabase ) ;
771+ const timelineUsers = getUserData ( extractData . timelineEvents , extractData . mergeRequest . authorExternalId as number ) ;
772+
773+ const transformUsersIds = await getTransformUserData ( ctx . extractDatabase , ctx . transformDatabase , timelineUsers ) ;
734774
735775 const {
736776 dateId : nullDateId ,
@@ -757,14 +797,11 @@ export async function run(extractMergeRequestId: number, ctx: RunContext) {
757797 const reviewDuration = calculateDuration ( timeline . startedReviewAt , extractData . mergeRequest . closedAt ) ;
758798
759799 const usersJunk = mapUsersToJunk ( {
760- author : ( await ctx . transformDatabase . select ( ) . from ( transform . forgeUsers ) . where ( and (
761- eq ( transform . forgeUsers . externalId , extractData . mergeRequest . authorExternalId || 0 ) ,
762- eq ( transform . forgeUsers . forgeType , extractData . repository . forgeType ) ,
763- ) ) . get ( ) ) ?. id || null , // TODO: ???
764- mergedBy : users . mergedBy ,
765- approvers : users . approvers ,
766- committers : users . committers ,
767- reviewers : users . reviewers ,
800+ author : transformUsersIds . author ,
801+ mergedBy : transformUsersIds . mergedBy ,
802+ approvers : transformUsersIds . approvers ,
803+ committers : transformUsersIds . committers ,
804+ reviewers : transformUsersIds . reviewers ,
768805 } , nullUserId ) ;
769806
770807 const { id : transformRepositoryId } = await upsertRepository ( ctx . transformDatabase , extractData . repository ) . get ( ) ;
0 commit comments