@@ -18,35 +18,28 @@ import {
1818 CompareContext ,
1919 CompareOperationsPairContext ,
2020 OperationChanges ,
21- OperationId ,
2221 OperationsApiType ,
2322 OperationType ,
24- ResolvedVersionDocument ,
2523 VersionCache ,
2624 VersionParams ,
2725 VersionsComparison ,
2826} from '../../types'
2927import {
28+ calculatePairedDocs ,
3029 calculateTotalImpactedSummary ,
30+ comparePairedDocs ,
3131 createPairOperationsMap ,
32- dedupePairs ,
3332 getUniqueApiTypesFromVersions ,
34- normalizeOperationIds ,
3533} from './compare.utils'
3634import {
3735 calculateApiAudienceTransitions ,
3836 calculateChangeSummary ,
3937 calculateDiffId ,
4038 convertToSlug ,
41- difference ,
42- executeInBatches ,
4339 getSplittedVersionKey ,
44- intersection ,
45- removeFirstSlash ,
4640 removeObjectDuplicates ,
47- slugify ,
4841} from '../../utils'
49- import { asyncDebugPerformance , DebugPerformanceContext } from '../../utils/logs'
42+ import { asyncDebugPerformance , DebugPerformanceContext , syncDebugPerformance } from '../../utils/logs'
5043
5144export async function compareVersionsOperations (
5245 prev : VersionParams ,
@@ -109,125 +102,59 @@ async function compareCurrentApiType(
109102 debugCtx ?: DebugPerformanceContext ,
110103) : Promise < [ OperationType , OperationChanges [ ] ] | null > {
111104 const {
112- batchSize,
113105 versionOperationsResolver,
114- versionDocumentsResolver,
115106 rawDocumentResolver,
116107 config : { currentGroup = '' , previousGroup = '' } ,
117108 } = ctx
118109 const apiBuilder = ctx . apiBuilders . find ( ( builder ) => apiType === builder . apiType )
119110 if ( ! apiBuilder ) { return null }
120111
121- const { operations : prevOperations = [ ] } = await versionOperationsResolver ( apiType , prev ?. version ?? '' , prev ?. packageId ?? '' , undefined , false ) || { }
122- const { operations : currOperations = [ ] } = await versionOperationsResolver ( apiType , curr ?. version ?? '' , curr ?. packageId ?? '' , undefined , false ) || { }
123-
124- const previousGroupSlug = convertToSlug ( previousGroup )
125- const prevOperationsWithPrefix = previousGroupSlug ? prevOperations . filter ( operation => operation . operationId . startsWith ( previousGroupSlug ) ) : prevOperations
126-
127- const currentGroupSlug = convertToSlug ( currentGroup )
128- const currOperationsWithPrefix = currentGroupSlug ? currOperations . filter ( operation => operation . operationId . startsWith ( currentGroupSlug ) ) : currOperations
129-
130- const [ prevNormalizedOperationIds , prevNormalizedOperationIdToOperation ] = normalizeOperationIds ( prevOperationsWithPrefix , apiBuilder , previousGroup )
131- const [ currNormalizedOperationIds , currNormalizedOperationIdToOperation ] = normalizeOperationIds ( currOperationsWithPrefix , apiBuilder , currentGroup )
132-
133- const added = difference ( currNormalizedOperationIds , prevNormalizedOperationIds )
134- const removed = difference ( prevNormalizedOperationIds , currNormalizedOperationIds )
135- const potentiallyChanged = intersection ( prevNormalizedOperationIds , currNormalizedOperationIds )
136-
137112 const { version : prevVersion , packageId : prevPackageId } = prev ?? { version : '' , packageId : '' }
138113 const { version : currVersion , packageId : currPackageId } = curr ?? { version : '' , packageId : '' }
139114
140- const { documents : prevDocuments } = await versionDocumentsResolver ( prev ?. version ?? '' , prev ?. packageId ?? '' , apiType ) ?? { documents : [ ] }
141- const { documents : unfilteredCurrDocuments } = await versionDocumentsResolver ( curr ?. version ?? '' , curr ?. packageId ?? '' , apiType ) ?? { documents : [ ] }
142- // todo this filters out files that are not in build config, but config.files is stored between tests and that breaks the '[Response] Should be non-breaking if response code changed in case' test
143- // const currDocuments = ctx.config.files ? unfilteredCurrDocuments.filter(({fileId}) => ctx.config.files?.find((file) => file.fileId === fileId)) : unfilteredCurrDocuments
144- const currDocuments = unfilteredCurrDocuments
145-
146- const pairedRestDocs : [ ResolvedVersionDocument , ResolvedVersionDocument ] [ ] = [ ]
147- for ( const normalizedOperationId of potentiallyChanged ) {
148- const prevDocumentId = prevNormalizedOperationIdToOperation [ normalizedOperationId ] ?. documentId
149- const currDocumentId = currNormalizedOperationIdToOperation [ normalizedOperationId ] ?. documentId
150- const prevDoc = prevDocuments . find ( document => document . slug === prevDocumentId )
151- const currDoc = currDocuments . find ( document => document . slug === currDocumentId )
152-
153- if ( ! prevDoc || ! currDoc ) {
154- throw new Error ( 'should not happen' )
155- }
156-
157- pairedRestDocs . push ( [ prevDoc , currDoc ] )
158- }
159-
160- const pairedDocs : [ ResolvedVersionDocument | undefined , ResolvedVersionDocument | undefined ] [ ] = dedupePairs ( pairedRestDocs )
161- const uniqueCurrentDocuments = [ ...new Set ( added . map ( ( normalizedOperationId ) => {
162- const currDocumentId = currNormalizedOperationIdToOperation [ normalizedOperationId ] ?. documentId
163- return currDocuments . find ( document => document . slug === currDocumentId )
164- } ) ) ]
165- for ( const currDoc of uniqueCurrentDocuments ) {
166- pairedDocs . push ( [ undefined , currDoc ] )
167- }
168-
169- const asd = [ ...new Set ( removed . map ( ( normalizedOperationId ) => {
170- const prevDocumentId = prevNormalizedOperationIdToOperation [ normalizedOperationId ] ?. documentId
171- return prevDocuments . find ( document => document . slug === prevDocumentId )
172- } ) ) ]
173- for ( const prevDoc of asd ) {
174- pairedDocs . push ( [ prevDoc , undefined ] )
175- }
176-
177- const currGroupSlug = slugify ( removeFirstSlash ( currentGroup || '' ) )
178- const prevGroupSlug = slugify ( removeFirstSlash ( previousGroup || '' ) )
115+ const { operations : prevOperations = [ ] } = await versionOperationsResolver ( apiType , prevVersion , prevPackageId , undefined , false ) || { }
116+ const { operations : currOperations = [ ] } = await versionOperationsResolver ( apiType , currVersion , currPackageId , undefined , false ) || { }
179117
180- const operationsMap = createPairOperationsMap ( prevGroupSlug , currGroupSlug , prevOperationsWithPrefix , currOperationsWithPrefix , apiBuilder )
118+ const previousGroupSlug = convertToSlug ( previousGroup )
119+ const currentGroupSlug = convertToSlug ( currentGroup )
181120
182- const operationChanges : OperationChanges [ ] = [ ]
183- const tags : string [ ] = [ ]
121+ const prevOperationsWithPrefix = previousGroupSlug ? prevOperations . filter ( operation => operation . operationId . startsWith ( ` ${ previousGroupSlug } -` ) ) : prevOperations
122+ const currOperationsWithPrefix = currentGroupSlug ? currOperations . filter ( operation => operation . operationId . startsWith ( ` ${ currentGroupSlug } -` ) ) : currOperations
184123
185124 const pairContext : CompareOperationsPairContext = {
125+ apiType : apiType ,
186126 notifications : ctx . notifications ,
187127 rawDocumentResolver,
188128 versionDeprecatedResolver : ctx . versionDeprecatedResolver ,
129+ versionDocumentsResolver : ctx . versionDocumentsResolver ,
189130 previousVersion : prevVersion ,
190131 currentVersion : currVersion ,
191- previousPackageId : prevPackageId ,
192132 currentPackageId : currPackageId ,
193- currentGroup : currentGroup ,
133+ previousPackageId : prevPackageId ,
194134 previousGroup : previousGroup ,
135+ currentGroup : currentGroup ,
136+ previousGroupSlug : previousGroupSlug ,
137+ currentGroupSlug : currentGroupSlug ,
195138 }
196139
197- for ( const [ prevDoc , currDoc ] of pairedDocs ) {
198- const {
199- operationChanges : docsPairOperationChanges ,
200- tags : docsPairTags ,
201- } = await apiBuilder . compareDocuments ! ( apiType , operationsMap , prevDoc , currDoc , pairContext )
202-
203- operationChanges . push ( ...docsPairOperationChanges )
204- tags . push ( ...docsPairTags )
205- }
140+ const operationsMap = createPairOperationsMap ( previousGroupSlug , currentGroupSlug , prevOperationsWithPrefix , currOperationsWithPrefix , apiBuilder )
141+ const operationPairs = Object . values ( operationsMap )
142+ const pairedDocs = await calculatePairedDocs ( operationPairs , pairContext )
143+ const [ operationChanges , tags ] = await comparePairedDocs ( operationsMap , pairedDocs , apiBuilder , pairContext )
206144
207145 const dedupedChanges = removeObjectDuplicates ( operationChanges . flatMap ( ( { diffs } ) => diffs ) , calculateDiffId )
208146 const changesSummary = calculateChangeSummary ( dedupedChanges )
209147 const numberOfImpactedOperations = calculateTotalImpactedSummary (
210148 operationChanges . map ( ( { impactedSummary } ) => impactedSummary ) ,
211149 )
212150
213- const pairedOperationIds =
214- Object . entries ( operationsMap )
215- . filter ( ( [ , { previous, current } ] ) => previous && current )
216- . map ( ( [ , { previous, current } ] ) => [ previous ?. operationId , current ?. operationId ] as [ OperationId , OperationId ] )
217-
218151 const apiAudienceTransitions : ApiAudienceTransition [ ] = [ ]
219152 // todo: convert from objects analysis to apihub-diff result analysis after the "info" section participates in the comparison of operations
220- await asyncDebugPerformance ( '[ApiAudience]' , async ( ) => await executeInBatches ( pairedOperationIds , async ( operationsBatch ) => {
221- const previousBatch = operationsBatch . map ( ( [ prevOperationId ] ) => prevOperationId )
222- const currentBatch = operationsBatch . map ( ( [ , currOperationId ] ) => currOperationId )
223- const { operations : currOperationsWithoutData = [ ] } = await versionOperationsResolver ( apiType , currVersion , currPackageId , previousBatch , false ) || { }
224- const { operations : prevOperationsWithoutData = [ ] } = await versionOperationsResolver ( apiType , prevVersion , prevPackageId , currentBatch , false ) || { }
225-
226- const pairOperationsMap = createPairOperationsMap ( prevGroupSlug , currGroupSlug , prevOperationsWithoutData , currOperationsWithoutData , apiBuilder )
227- Object . values ( pairOperationsMap ) . forEach ( ( pair ) => {
228- calculateApiAudienceTransitions ( pair . current , pair . previous , apiAudienceTransitions )
153+ syncDebugPerformance ( '[ApiAudience]' , ( ) => {
154+ operationPairs . forEach ( ( { current, previous} ) => {
155+ calculateApiAudienceTransitions ( current , previous , apiAudienceTransitions )
229156 } )
230- } , batchSize ) , debugCtx )
157+ } , debugCtx )
231158
232159 return [
233160 {
0 commit comments