Skip to content

Commit bd38e7d

Browse files
committed
refactor: cleanup
1 parent 4cdcddb commit bd38e7d

File tree

10 files changed

+59
-174
lines changed

10 files changed

+59
-174
lines changed

src/apitypes/graphql/graphql.changes.ts

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { VersionGraphQLOperation } from './graphql.types'
18-
import { isEmpty, removeComponents, removeFirstSlash, slugify, takeIf } from '../../utils'
19-
import {
20-
apiDiff,
21-
COMPARE_MODE_OPERATION,
22-
Diff,
23-
DIFF_META_KEY,
24-
DIFFS_AGGREGATED_META_KEY,
25-
} from '@netcracker/qubership-apihub-api-diff'
17+
import { isEmpty, removeFirstSlash, slugify, takeIf } from '../../utils'
18+
import { apiDiff, Diff, DIFF_META_KEY, DIFFS_AGGREGATED_META_KEY } from '@netcracker/qubership-apihub-api-diff'
2619
import { NORMALIZE_OPTIONS, ORIGINS_SYMBOL } from '../../consts'
2720
import { GraphApiOperation, GraphApiSchema } from '@netcracker/qubership-apihub-graphapi'
2821
import { buildSchema } from 'graphql/utilities'
@@ -134,33 +127,6 @@ export const compareDocuments = async (apiType: OperationsApiType, operationsMap
134127
return { operationChanges: changedOperations, tags: [...tags.values()] }
135128
}
136129

137-
/** @deprecated */
138-
export const graphqlOperationsCompare = async (current: VersionGraphQLOperation | undefined, previous: VersionGraphQLOperation | undefined): Promise<Diff[]> => {
139-
let previousOperation = removeComponents(previous?.data)
140-
let currentOperation = removeComponents(current?.data)
141-
if (!previousOperation && currentOperation) {
142-
previousOperation = getCopyWithEmptyOperations(currentOperation as GraphApiSchema)
143-
}
144-
145-
if (previousOperation && !currentOperation) {
146-
currentOperation = getCopyWithEmptyOperations(previousOperation as GraphApiSchema)
147-
}
148-
149-
//todo think about normalize options
150-
const { diffs } = apiDiff(
151-
previousOperation,
152-
currentOperation,
153-
{
154-
...NORMALIZE_OPTIONS,
155-
mode: COMPARE_MODE_OPERATION,
156-
normalizedResult: true,
157-
beforeSource: previous?.data,
158-
afterSource: current?.data,
159-
},
160-
)
161-
return diffs
162-
}
163-
164130
function getCopyWithEmptyOperations(template: GraphApiSchema): GraphApiSchema {
165131
// eslint-disable-next-line @typescript-eslint/no-unused-vars
166132
const { queries, mutations, subscriptions, ...rest } = template

src/apitypes/graphql/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { buildGraphQLOperations } from './graphql.operations'
2121
import { GRAPHQL_API_TYPE, GRAPHQL_DOCUMENT_TYPE } from './graphql.consts'
2222
import { parseGraphQLFile } from './graphql.parser'
2323
import { ApiBuilder } from '../../types'
24-
import { compareDocuments, graphqlOperationsCompare } from './graphql.changes'
24+
import { compareDocuments } from './graphql.changes'
2525

2626
export * from './graphql.consts'
2727

@@ -32,6 +32,5 @@ export const graphqlApiBuilder: ApiBuilder<GraphApiSchema> = {
3232
buildDocument: buildGraphQLDocument,
3333
buildOperations: buildGraphQLOperations,
3434
dumpDocument: dumpGraphQLDocument,
35-
compareOperationsData: graphqlOperationsCompare,
3635
compareDocuments: compareDocuments,
3736
}

src/apitypes/rest/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { OpenAPIV3 } from 'openapi-types'
1818

1919
import { buildRestDocument, createRestExportDocument, dumpRestDocument } from './rest.document'
2020
import { REST_API_TYPE, REST_DOCUMENT_TYPE } from './rest.consts'
21-
import { compareDocuments, compareRestOperationsData } from './rest.changes'
21+
import { compareDocuments } from './rest.changes'
2222
import { buildRestOperations, createNormalizedOperationId } from './rest.operations'
2323
import { parseRestFile } from './rest.parser'
2424

@@ -33,7 +33,6 @@ export const restApiBuilder: ApiBuilder<OpenAPIV3.Document> = {
3333
buildDocument: buildRestDocument,
3434
buildOperations: buildRestOperations,
3535
dumpDocument: dumpRestDocument,
36-
compareOperationsData: compareRestOperationsData,
3736
compareDocuments: compareDocuments,
3837
createNormalizedOperationId: createNormalizedOperationId,
3938
createExportDocument: createRestExportDocument,

src/apitypes/rest/rest.changes.ts

Lines changed: 20 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,20 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { RestOperationData, VersionRestOperation } from './rest.types'
17+
import { RestOperationData } from './rest.types'
1818
import {
1919
areDeprecatedOriginsNotEmpty,
2020
IGNORE_PATH_PARAM_UNIFIED_PLACEHOLDER,
2121
isEmpty,
2222
isOperationRemove,
2323
isPathParamRenameDiff,
2424
normalizePath,
25-
removeComponents,
2625
removeFirstSlash,
2726
slugify,
2827
} from '../../utils'
2928
import {
3029
apiDiff,
3130
breaking,
32-
COMPARE_MODE_OPERATION,
3331
Diff,
3432
DIFF_META_KEY,
3533
DiffAction,
@@ -79,12 +77,10 @@ export const compareDocuments = async (apiType: OperationsApiType, operationsMap
7977
const isChangedOperations = prevDoc && currDoc
8078

8179
if (prevDocData && previousGroup) {
82-
// todo what's with components? why don't remove?
8380
prevDocData = createCopyWithCurrentGroupOperationsOnly(prevDocData, previousGroup)
8481
}
8582

8683
if (currDocData && currentGroup) {
87-
// todo what's with components? why don't remove?
8884
currDocData = createCopyWithCurrentGroupOperationsOnly(currDocData, currentGroup)
8985
}
9086

@@ -158,21 +154,14 @@ export const compareDocuments = async (apiType: OperationsApiType, operationsMap
158154
// ignore removed and added operations, they'll be handled in a separate docs comparison
159155
continue
160156
}
161-
// const deprecatedInVersionsCount = previousVersionDeprecations?.operations.find((operation) => operation.operationId === operationId)?.deprecatedInPreviousVersions?.length ?? 0
162-
// if (isOperationRemove(operationDiff) && deprecatedInVersionsCount > 1) {
163-
// operationDiff.type = risky
164-
// }
165157
operationDiffs.push(operationDiff)
166158
}
167159

168160
if (isEmpty(operationDiffs)) {
169161
continue
170162
}
171163

172-
const onlyBreaking = operationDiffs.filter((diff) => diff.type === breaking)
173-
if (onlyBreaking.length > 0 && previous?.operationId) {
174-
await reclassifyBreakingChanges(previous.operationId, merged, onlyBreaking, ctx)
175-
}
164+
await reclassifyBreakingChanges(previous?.operationId, merged, operationDiffs, ctx)
176165

177166
changedOperations.push(createOperationChange(apiType, operationDiffs, previous, current, currGroupSlug, prevGroupSlug, currentGroup, previousGroup))
178167
getOperationTags(current ?? previous).forEach(tag => tags.add(tag))
@@ -182,57 +171,31 @@ export const compareDocuments = async (apiType: OperationsApiType, operationsMap
182171
return { operationChanges: changedOperations, tags: [...tags.values()] }
183172
}
184173

185-
/** @deprecated */
186-
export const compareRestOperationsData = async (current: VersionRestOperation | undefined, previous: VersionRestOperation | undefined, ctx: CompareOperationsPairContext): Promise<Diff[]> => {
187-
188-
let previousOperation = removeComponents(previous?.data)
189-
let currentOperation = removeComponents(current?.data)
190-
if (!previousOperation && currentOperation) {
191-
previousOperation = createCopyWithEmptyPathItems(currentOperation as RestOperationData)
192-
}
193-
194-
if (previousOperation && !currentOperation) {
195-
currentOperation = createCopyWithEmptyPathItems(previousOperation as RestOperationData)
196-
}
197-
198-
const diffResult = apiDiff(
199-
previousOperation,
200-
currentOperation,
201-
{
202-
...NORMALIZE_OPTIONS,
203-
originsFlag: ORIGINS_SYMBOL,
204-
mode: COMPARE_MODE_OPERATION,
205-
normalizedResult: true,
206-
beforeSource: previous?.data,
207-
afterSource: current?.data,
208-
},
209-
)
210-
const olnyBreaking = diffResult.diffs.filter((diff) => diff.type === breaking)
211-
if (olnyBreaking.length > 0 && previous?.operationId) {
212-
await reclassifyBreakingChanges(previous.operationId, diffResult.merged, olnyBreaking, ctx)
213-
}
214-
return diffResult.diffs
215-
}
216-
217174
async function reclassifyBreakingChanges(
218-
operationId: string,
175+
previousOperationId: string | undefined,
219176
mergedJso: unknown,
220177
diffs: Diff[],
221178
ctx: CompareOperationsPairContext,
222179
): Promise<void> {
223-
if (!ctx.previousVersion || !ctx.previousPackageId) {
180+
if (!previousOperationId || !ctx.previousVersion || !ctx.previousPackageId) {
181+
return
182+
}
183+
184+
const onlyBreaking = diffs.filter((diff) => diff.type === breaking)
185+
if (isEmpty(onlyBreaking)) {
224186
return
225187
}
226-
const previosVersionDeprecations = await ctx.versionDeprecatedResolver(REST_API_TYPE, ctx.previousVersion, ctx.previousPackageId, [operationId])
227-
if (!previosVersionDeprecations) {
188+
189+
const previousVersionDeprecations = await ctx.versionDeprecatedResolver(REST_API_TYPE, ctx.previousVersion, ctx.previousPackageId, [previousOperationId])
190+
if (!previousVersionDeprecations) {
228191
return
229192
}
230193

231-
const previousOperation = previosVersionDeprecations.operations[0]
194+
const [previousOperation] = previousVersionDeprecations.operations
232195

233196
if (!previousOperation?.deprecatedItems) { return }
234197

235-
for (const diff of diffs) {
198+
for (const diff of onlyBreaking) {
236199
if (diff.type !== breaking) {
237200
continue
238201
}
@@ -286,15 +249,13 @@ async function reclassifyBreakingChanges(
286249
}
287250
}
288251
// mark removed required status of the property as risky
289-
if (diffs.length) {
290-
const requiredProperties = findRequiredRemovedProperties(mergedJso, diffs)
252+
const requiredProperties = findRequiredRemovedProperties(mergedJso, onlyBreaking)
291253

292-
requiredProperties?.forEach(prop => {
293-
if (prop.propDiff.type === RISKY_CHANGE_TYPE && prop.requiredDiff?.type === BREAKING_CHANGE_TYPE) {
294-
prop.requiredDiff.type = risky
295-
}
296-
})
297-
}
254+
requiredProperties?.forEach(prop => {
255+
if (prop.propDiff.type === RISKY_CHANGE_TYPE && prop.requiredDiff?.type === BREAKING_CHANGE_TYPE) {
256+
prop.requiredDiff.type = risky
257+
}
258+
})
298259
}
299260

300261
export function createCopyWithEmptyPathItems(template: RestOperationData): RestOperationData {

src/builder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ export class PackageVersionBuilder implements IPackageVersionBuilder {
476476
): Promise<ResolvedVersionDocuments | null> {
477477
packageId = packageId ?? this.config.packageId
478478
// this is a case when the version has been built just now, and there's nothing to fetch yet, so
479-
// the only way possible is to get the docs from buildResult, but the referenced packages map will be empty
479+
// the only way to get the docs is to get them from buildResult, but the referenced packages map will be empty (packages: {})
480480
if (this.canBeResolvedLocally(version, packageId)) {
481481
const apiBuilder = this.apiBuilders.find(apiBuilder => apiBuilder.apiType === apiType)
482482
const currentApiTypeDocuments = this.documentList.filter(({ type }) => apiBuilder?.types.includes(type))

src/components/compare/bwc.validation.ts

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,22 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { BREAKING_CHANGE_TYPE, OperationChanges, RISKY_CHANGE_TYPE } from '../../types'
17+
import { BREAKING_CHANGE_TYPE, ResolvedOperation, RISKY_CHANGE_TYPE } from '../../types'
1818
import { API_KIND } from '../../consts'
19+
import { Diff } from '@netcracker/qubership-apihub-api-diff'
1920

20-
export function validateBwcBreakingChanges(
21-
operationChanges: OperationChanges,
22-
): void {
23-
if (!operationChanges.changeSummary?.breaking) {
24-
return
21+
export function reclassifyNoBwcBreakingChanges(
22+
operationDiffs: Diff[],
23+
previous?: ResolvedOperation,
24+
current?: ResolvedOperation,
25+
): Diff[] {
26+
if (current?.apiKind === API_KIND.NO_BWC || previous?.apiKind === API_KIND.NO_BWC) {
27+
return operationDiffs?.map((diff) => {
28+
if (diff.type !== BREAKING_CHANGE_TYPE) {
29+
return diff
30+
}
31+
return { ...diff, type: RISKY_CHANGE_TYPE }
32+
})
2533
}
26-
if (operationChanges.apiKind !== API_KIND.NO_BWC && operationChanges.previousApiKind !== API_KIND.NO_BWC) {
27-
return
28-
}
29-
30-
operationChanges.changeSummary[RISKY_CHANGE_TYPE] = operationChanges.changeSummary[BREAKING_CHANGE_TYPE]
31-
operationChanges.changeSummary[BREAKING_CHANGE_TYPE] = 0
32-
33-
operationChanges.impactedSummary[BREAKING_CHANGE_TYPE] = false
34-
operationChanges.impactedSummary[RISKY_CHANGE_TYPE] = true
35-
36-
operationChanges.diffs = operationChanges.diffs?.map((diff) => {
37-
if (diff.type !== BREAKING_CHANGE_TYPE) {
38-
return { ...diff }
39-
}
40-
return { ...diff, type: RISKY_CHANGE_TYPE }
41-
})
34+
return operationDiffs
4235
}

src/components/compare/compare.utils.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import {
1818
ApiBuilder,
1919
ChangeSummary,
20-
CompareContext,
2120
DIFF_TYPES,
2221
ImpactedOperationSummary,
2322
NormalizedOperationId,
@@ -27,25 +26,18 @@ import {
2726
OperationsApiType,
2827
OperationTypes,
2928
ResolvedOperation,
30-
ResolvedVersionOperationsHashMap,
3129
VersionCache,
3230
} from '../../types'
33-
import { BUILD_TYPE, EMPTY_CHANGE_SUMMARY, MESSAGE_SEVERITY } from '../../consts'
31+
import { EMPTY_CHANGE_SUMMARY } from '../../consts'
3432
import {
3533
calculateChangeSummary,
3634
calculateImpactedSummary,
3735
convertToSlug,
3836
removeFirstSlash,
3937
takeIfDefined,
4038
} from '../../utils'
41-
import { validateBwcBreakingChanges } from './bwc.validation'
4239
import { Diff } from '@netcracker/qubership-apihub-api-diff'
43-
44-
export const totalChanges = (changeSummary?: ChangeSummary): number => {
45-
return changeSummary
46-
? Object.values(changeSummary).reduce((acc, current) => acc + current, 0)
47-
: 0
48-
}
40+
import { reclassifyNoBwcBreakingChanges } from './bwc.validation'
4941

5042
export function calculateTotalChangeSummary(
5143
summaries: ChangeSummary[],
@@ -171,6 +163,7 @@ export const createPairOperationsMap = (
171163
for (const previousOperation of previousOperations) {
172164
const normalizedOperationId = apiBuilder.createNormalizedOperationId?.(previousOperation) ?? previousOperation.operationId
173165
const prevOperationId = takeSubstringIf(!!prevGroupSlug, normalizedOperationId, prevGroupSlug.length + '-'.length)
166+
// todo fix
174167
// todo it won't work if groups have different length (add test with /api/v1/ and /api/abc/)
175168
const operationsMappingElement = operationsMap[prevOperationId]
176169
operationsMap[prevOperationId] = {
@@ -182,11 +175,22 @@ export const createPairOperationsMap = (
182175
return operationsMap
183176
}
184177

185-
export function createOperationChange(apiType: OperationsApiType, operationDiffs: Diff[], previous?: ResolvedOperation, current?: ResolvedOperation, currGroupSlug?: string, prevGroupSlug?: string, currentGroup?: string, previousGroup?: string): OperationChanges {
186-
const changeSummary = calculateChangeSummary(operationDiffs)
178+
export function createOperationChange(
179+
apiType: OperationsApiType,
180+
operationDiffs: Diff[],
181+
previous?: ResolvedOperation,
182+
current?: ResolvedOperation,
183+
currGroupSlug?: string,
184+
prevGroupSlug?: string,
185+
currentGroup?: string,
186+
previousGroup?: string,
187+
): OperationChanges {
188+
const reclassifiedDiffs = reclassifyNoBwcBreakingChanges(operationDiffs, previous, current)
189+
const changeSummary = calculateChangeSummary(reclassifiedDiffs)
187190
const impactedSummary = calculateImpactedSummary([changeSummary])
188191

189192
const currentOperationFields = current && {
193+
// todo remove one that is excessive: slug or group
190194
operationId: takeSubstringIf(!!currGroupSlug, current.operationId, removeFirstSlash(currentGroup ?? '').length),
191195
apiKind: current.apiKind,
192196
metadata: getOperationMetadata(current),
@@ -198,15 +202,12 @@ export function createOperationChange(apiType: OperationsApiType, operationDiffs
198202
previousMetadata: getOperationMetadata(previous),
199203
}
200204

201-
const operationChange = {
205+
return {
202206
apiType,
203-
diffs: operationDiffs,
207+
diffs: reclassifiedDiffs,
204208
changeSummary: changeSummary,
205209
impactedSummary: impactedSummary,
206210
...currentOperationFields,
207211
...previousOperationFields,
208212
}
209-
validateBwcBreakingChanges(operationChange)
210-
211-
return operationChange
212213
}

0 commit comments

Comments
 (0)