@@ -35,7 +35,6 @@ import {
3535 getOperationTags ,
3636 getOperationTypesFromTwoVersions ,
3737 getUniqueApiTypesFromVersions ,
38- OperationIdentityMap ,
3938 takeSubstringIf ,
4039 totalChanges ,
4140} from './compare.utils'
@@ -50,9 +49,9 @@ import {
5049 removeObjectDuplicates ,
5150 slugify ,
5251} from '../../utils'
53- import { asyncFunction } from '../../utils/async'
54- import { asyncDebugPerformance , DebugPerformanceContext , logLongBuild , syncDebugPerformance } from '../../utils/logs'
52+ import { asyncDebugPerformance , DebugPerformanceContext } from '../../utils/logs'
5553import { validateBwcBreakingChanges } from './bwc.validation'
54+ import { REST_API_TYPE } from '../../apitypes'
5655
5756export async function compareVersionsOperations (
5857 prev : VersionParams ,
@@ -107,6 +106,18 @@ export async function compareVersionsOperations(
107106 }
108107}
109108
109+ const HANDLE_TYPE_FULLY_ADDED = 'added'
110+ const HANDLE_TYPE_FULLY_REMOVED = 'removed'
111+ const HANDLE_TYPE_FULLY_CHANGED = 'changed'
112+
113+ type HandleType = typeof HANDLE_TYPE_FULLY_ADDED | typeof HANDLE_TYPE_FULLY_REMOVED | typeof HANDLE_TYPE_FULLY_CHANGED
114+
115+ export interface MappingResult < T extends PropertyKey > {
116+ [ HANDLE_TYPE_FULLY_ADDED ] : T [ ]
117+ [ HANDLE_TYPE_FULLY_REMOVED ] : T [ ]
118+ [ HANDLE_TYPE_FULLY_CHANGED ] : Record < T , T >
119+ }
120+
110121async function compareCurrentApiType (
111122 apiType : OperationsApiType ,
112123 prev : VersionCache | null ,
@@ -124,30 +135,37 @@ async function compareCurrentApiType(
124135
125136 const [ prevOperationTypesData , currOperationTypesData ] = getOperationTypesFromTwoVersions ( prev , curr )
126137
127- const changedIdToOriginal : OperationIdentityMap = { }
128- const prevOperationHashMap = getOperationsHashMapByApiType ( apiType , prevOperationTypesData , changedIdToOriginal , ctx )
129- const currOperationHashMap = getOperationsHashMapByApiType ( apiType , currOperationTypesData , changedIdToOriginal , ctx , true )
138+ const prevOperationsCount = prevOperationTypesData . find ( data => apiType === data . apiType ) ?. operationsCount
139+ const currOperationsCount = currOperationTypesData . find ( data => apiType === data . apiType ) ?. operationsCount
130140
131- const operationIds = new Set ( [ ...Object . keys ( prevOperationHashMap ) , ...Object . keys ( currOperationHashMap ) ] )
132- const operationsMapping : Record < string , Array < string > > = { added : [ ] , removed : [ ] , changed : [ ] }
133- const apiAudienceTransitions : ApiAudienceTransition [ ] = [ ]
134- const pairedOperationIds : Array < string > = [ ]
141+ const { operations : prevOperations = [ ] } = await versionOperationsResolver ( apiType , prev ?. version ?? '' , prev ?. packageId ?? '' , undefined , false , prevOperationsCount ) || { }
142+ const { operations : currOperations = [ ] } = await versionOperationsResolver ( apiType , curr ?. version ?? '' , curr ?. packageId ?? '' , undefined , false , currOperationsCount ) || { }
135143
136- for ( const operationId of operationIds ) {
137- const v1OperationHash = prevOperationHashMap [ operationId ]
138- const v2OperationHash = currOperationHashMap [ operationId ]
139- if ( v1OperationHash && v2OperationHash ) {
140- pairedOperationIds . push ( changedIdToOriginal [ operationId ] || operationId )
144+ const [ prevReducedOperationIdToHashMap , prevReducedOperationIdToOriginal ] = getOperationsHashMapByApiType ( apiType , prevOperations , ctx )
145+ const [ currReducedOperationIdToHashMap , currReducedOperationIdToOriginal ] = getOperationsHashMapByApiType ( apiType , currOperations , ctx , true )
146+
147+ const reducedOperationIds = new Set ( [ ...Object . keys ( prevReducedOperationIdToHashMap ) , ...Object . keys ( currReducedOperationIdToHashMap ) ] )
148+ const operationsMapping : MappingResult < OperationId > = { [ HANDLE_TYPE_FULLY_ADDED ] : [ ] , [ HANDLE_TYPE_FULLY_REMOVED ] : [ ] , [ HANDLE_TYPE_FULLY_CHANGED ] : { } }
149+ const apiAudienceTransitions : ApiAudienceTransition [ ] = [ ]
150+ const pairedOperationIds : Record < OperationId , OperationId > = { }
151+
152+ for ( const reducedOperationId of reducedOperationIds ) {
153+ const prevOperationHash = prevReducedOperationIdToHashMap [ reducedOperationId ]
154+ const currOperationHash = currReducedOperationIdToHashMap [ reducedOperationId ]
155+ const prevOperationId = prevReducedOperationIdToOriginal [ reducedOperationId ]
156+ const currOperationId = currReducedOperationIdToOriginal [ reducedOperationId ]
157+ if ( prevOperationHash && currOperationHash ) {
158+ pairedOperationIds [ prevOperationId ] = currOperationId
141159 // operation not changed
142- if ( v1OperationHash === v2OperationHash ) { continue }
160+ if ( prevOperationHash === currOperationHash ) { continue }
143161 // operation changed
144- operationsMapping . changed . push ( changedIdToOriginal [ operationId ] || operationId )
145- } else if ( v1OperationHash ) {
162+ operationsMapping [ HANDLE_TYPE_FULLY_CHANGED ] [ prevOperationId ] = currOperationId
163+ } else if ( prevOperationHash ) {
146164 // operation removed
147- operationsMapping . removed . push ( changedIdToOriginal [ operationId ] || operationId )
148- } else if ( v2OperationHash ) {
165+ operationsMapping [ HANDLE_TYPE_FULLY_REMOVED ] . push ( prevOperationId )
166+ } else if ( currOperationHash ) {
149167 // operation added
150- operationsMapping . added . push ( changedIdToOriginal [ operationId ] || operationId )
168+ operationsMapping [ HANDLE_TYPE_FULLY_ADDED ] . push ( currOperationId )
151169 }
152170 }
153171
@@ -229,9 +247,11 @@ async function compareCurrentApiType(
229247 }
230248
231249 // todo: convert from objects analysis to apihub-diff result analysis after the "info" section participates in the comparison of operations
232- await asyncDebugPerformance ( '[ApiAudience]' , async ( ) => await executeInBatches ( pairedOperationIds , async ( operationsBatch ) => {
233- const { operations : prevOperationsWithoutData = [ ] } = await versionOperationsResolver ( apiType , prevVersion , prevPackageId , operationsBatch , false ) || { }
234- const { operations : currOperationsWithoutData = [ ] } = await versionOperationsResolver ( apiType , currVersion , currPackageId , operationsBatch , false ) || { }
250+ await asyncDebugPerformance ( '[ApiAudience]' , async ( ) => await executeInBatches ( Object . entries ( pairedOperationIds ) , async ( operationsBatch ) => {
251+ const previousBatch = operationsBatch . map ( ( [ prevOperationId ] ) => prevOperationId )
252+ const currentBatch = operationsBatch . map ( ( [ , currOperationId ] ) => currOperationId )
253+ const { operations : currOperationsWithoutData = [ ] } = await versionOperationsResolver ( apiType , currVersion , currPackageId , previousBatch , false ) || { }
254+ const { operations : prevOperationsWithoutData = [ ] } = await versionOperationsResolver ( apiType , prevVersion , prevPackageId , currentBatch , false ) || { }
235255
236256 const pairOperationsMap = createPairOperationsMap ( currGroupSlug , prevGroupSlug , currOperationsWithoutData , prevOperationsWithoutData )
237257 Object . values ( pairOperationsMap ) . forEach ( ( pair ) => {
@@ -262,12 +282,11 @@ async function compareCurrentApiType(
262282 if ( ! apiBuilder . compareOperationsData ) { return null }
263283
264284 await asyncDebugPerformance ( '[Changed]' , async ( innerDebugCtx ) =>
265- await executeInBatches ( operationsMapping . changed , async ( operationsBatch ) => {
266- const currentBatch = currentGroup ? operationsBatch . map ( operationId => currGroupSlug + operationId . substring ( currGroupSlug . length ) ) : operationsBatch
267- const previousBatch = previousGroup ? operationsBatch . map ( operationId => prevGroupSlug + operationId . substring ( prevGroupSlug . length ) ) : operationsBatch
268-
269- const { operations : prevOperationsWithData = [ ] } = await versionOperationsResolver ( apiType , prevVersion ! , prevPackageId ! , previousBatch ) || { }
270- const { operations : currOperationsWithData = [ ] } = await versionOperationsResolver ( apiType , currVersion ! , currPackageId ! , currentBatch ) || { }
285+ await executeInBatches ( Object . entries ( operationsMapping . changed ) , async ( operationsBatch ) => {
286+ const previousBatch = operationsBatch . map ( ( [ prevOperationId ] ) => prevOperationId )
287+ const currentBatch = operationsBatch . map ( ( [ , currOperationId ] ) => currOperationId )
288+ const { operations : prevOperationsWithData = [ ] } = await versionOperationsResolver ( apiType , prevVersion , prevPackageId , previousBatch ) || { }
289+ const { operations : currOperationsWithData = [ ] } = await versionOperationsResolver ( apiType , currVersion , currPackageId , currentBatch ) || { }
271290
272291 const operationsMap = createPairOperationsMap ( currGroupSlug , prevGroupSlug , currOperationsWithData , prevOperationsWithData )
273292
@@ -343,21 +362,20 @@ async function compareCurrentApiType(
343362 ]
344363}
345364
346- const HANDLE_TYPE_FULLY_ADDED = 'added'
347- const HANDLE_TYPE_FULLY_REMOVED = 'removed'
348-
349- type HandleType = typeof HANDLE_TYPE_FULLY_ADDED | typeof HANDLE_TYPE_FULLY_REMOVED
350-
351365const createPairOperationsMap = ( currGroupSlug : string , prevGroupSlug : string , currentOperations : ResolvedOperation [ ] , previousOperations : ResolvedOperation [ ] ) : Record < string , { previous ?: ResolvedOperation ; current : ResolvedOperation } > => {
352366
353367 const operationsMap : Record < string , { previous ?: ResolvedOperation ; current : ResolvedOperation } > = { }
354368
355369 for ( const currentOperation of currentOperations ) {
356- operationsMap [ takeSubstringIf ( ! ! currGroupSlug , currentOperation . operationId , currGroupSlug . length ) ] = { current : currentOperation }
370+ // todo
371+ const normalizedOperationId = currentOperation . apiType === REST_API_TYPE ? slugify ( `${ removeFirstSlash ( `${ currentOperation . metadata . path } ` ) } -${ currentOperation . metadata . method } ` ) : currentOperation . operationId
372+ operationsMap [ takeSubstringIf ( ! ! currGroupSlug , normalizedOperationId , currGroupSlug . length ) ] = { current : currentOperation }
357373 }
358374
359375 for ( const previousOperation of previousOperations ) {
360- const prevOperationId = takeSubstringIf ( ! ! prevGroupSlug , previousOperation . operationId , prevGroupSlug . length )
376+ // todo
377+ const normalizedOperationId = previousOperation . apiType === REST_API_TYPE ? slugify ( `${ removeFirstSlash ( `${ previousOperation . metadata . path } ` ) } -${ previousOperation . metadata . method } ` ) : previousOperation . operationId
378+ const prevOperationId = takeSubstringIf ( ! ! prevGroupSlug , normalizedOperationId , prevGroupSlug . length )
361379 const operationsMappingElement = operationsMap [ prevOperationId ]
362380 if ( operationsMappingElement ) {
363381 operationsMap [ prevOperationId ] = {
0 commit comments