diff --git a/projects/openapi-utilities/src/index.ts b/projects/openapi-utilities/src/index.ts index db65d3212e..568c61fccc 100644 --- a/projects/openapi-utilities/src/index.ts +++ b/projects/openapi-utilities/src/index.ts @@ -54,7 +54,6 @@ import { } from './openapi3/implementations/openapi3/sourcemap-reader'; export { defaultEmptySpec } from './openapi3/constants'; -export * from './legacy-ci-types'; export * from './openapi3/implementations/openapi3/types'; export { SPEC_TAG_REGEXP, sanitizeGitTag } from './specs/tags'; @@ -75,11 +74,8 @@ export { groupChangesAndRules, } from './utilities/group-changes'; export { traverseSpec } from './utilities/traverse-spec'; -export { generateChangelogData } from './utilities/generate-changelog-data'; export { compareChangesByPath } from './utilities/compare-changes-by-path'; export { - getOperationsModifsLabel, - countOperationsModifications, getLabel, getOperationsChanged, getOperationsChangedLabel, diff --git a/projects/openapi-utilities/src/legacy-ci-types.ts b/projects/openapi-utilities/src/legacy-ci-types.ts deleted file mode 100644 index a115d5e0ad..0000000000 --- a/projects/openapi-utilities/src/legacy-ci-types.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { ResultWithSourcemap } from './types'; -import { IChange } from './openapi3/sdk/types'; - -export type CompareFileJson = { - results: ResultWithSourcemap[]; - changes: IChange[]; - projectRootDir?: string | false; - version?: string; -}; diff --git a/projects/openapi-utilities/src/utilities/__tests__/count-changed-operations.test.ts b/projects/openapi-utilities/src/utilities/__tests__/count-changed-operations.test.ts deleted file mode 100644 index 3365bf6b92..0000000000 --- a/projects/openapi-utilities/src/utilities/__tests__/count-changed-operations.test.ts +++ /dev/null @@ -1,97 +0,0 @@ -import { it, expect, describe } from '@jest/globals'; -import { countOperationsModifications } from '../count-changed-operations'; -import { - OpenApiKind, - ChangeType, - ChangeVariant, -} from '../../openapi3/sdk/types'; - -const postAddition: ChangeVariant = { - location: { - jsonPath: '/paths/~1user~1{username}/post', - conceptualPath: ['operations', '/user/{}', 'post'], - kind: OpenApiKind.Operation, - conceptualLocation: { method: 'post', path: '/user/{username}' }, - }, - added: { - tags: ['user'], - summary: 'post', - operationId: 'postUser', - method: 'post', - pathPattern: '/user/{username}', - }, - changeType: ChangeType.Added, -}; - -const putRemoval: ChangeVariant = { - location: { - jsonPath: '/paths/~1user~1{username}/put', - conceptualPath: ['operations', '/user/{}', 'put'], - kind: OpenApiKind.Operation, - conceptualLocation: { method: 'put', path: '/user/{username}' }, - }, - removed: { - before: { - tags: ['user'], - summary: 'put', - operationId: 'putUser', - method: 'put', - pathPattern: '/user/{username}', - }, - }, - changeType: ChangeType.Removed, -}; - -const patchChange: ChangeVariant = { - location: { - jsonPath: '/paths/~1user~1{username}/patch', - conceptualPath: ['operations', '/user/{}', 'patch'], - kind: OpenApiKind.Operation, - conceptualLocation: { method: 'patch', path: '/user/{username}' }, - }, - changed: { - before: { - summary: 'patch', - operationId: 'patchUser', - method: 'patch', - pathPattern: '/user/{username}', - }, - after: { - tags: ['user'], - summary: 'patch', - operationId: 'patchUser', - method: 'patch', - pathPattern: '/user/{username}', - }, - }, - changeType: ChangeType.Changed, -}; - -const nestedPatchChange: ChangeVariant = { - location: { - jsonPath: '/paths/~1user~1{username}/patch/requestBody', - conceptualPath: ['operations', '/user/{}', 'patch', 'requestBody'], - kind: OpenApiKind.Request, - conceptualLocation: { - method: 'patch', - path: '/user/{username}', - inRequest: {}, - }, - }, - added: { description: '' }, - changeType: ChangeType.Added, -}; - -describe('countOperationsModifications', () => { - it('counts an addition, a change and a removal correctly', () => { - const count = countOperationsModifications([ - postAddition, - patchChange, - nestedPatchChange, - putRemoval, - ]); - expect(count.added).toBe(1); - expect(count.changed).toBe(1); - expect(count.removed).toBe(1); - }); -}); diff --git a/projects/openapi-utilities/src/utilities/count-changed-operations.ts b/projects/openapi-utilities/src/utilities/count-changed-operations.ts index 25a657b119..5d8173f065 100644 --- a/projects/openapi-utilities/src/utilities/count-changed-operations.ts +++ b/projects/openapi-utilities/src/utilities/count-changed-operations.ts @@ -1,62 +1,12 @@ import { getEndpointDiffs, typeofV3Diffs } from '../openapi3/group-diff'; -import { isChangeVariant } from '../openapi3/sdk/isType'; -import { OpenApiKind, IChange, ChangeType } from '../openapi3/sdk/types'; +import { ChangeType } from '../openapi3/sdk/types'; import { GroupedDiffs } from '../openapi3/group-diff'; -const getChangeOperationId = (change: IChange) => { - const path = (change.location.conceptualLocation as any).path; - const method = (change.location.conceptualLocation as any).method; - if (!path || !method) return null; - return `${path}.${method}`; -}; - -export const countOperationsModifications = (changes: IChange[]) => { - const operationsChanges = changes.filter((c) => - isChangeVariant(c, OpenApiKind.Operation) - ); - - const operationsAdded = operationsChanges.filter( - (c) => c.changeType === ChangeType.Added - ); - const operationsRemoved = operationsChanges.filter( - (c) => c.changeType === ChangeType.Removed - ); - const operationsChanged = operationsChanges.filter( - (c) => c.changeType === ChangeType.Changed - ); - - const operationsByChange = { - [ChangeType.Added]: new Set( - operationsAdded.map(getChangeOperationId).filter((id) => !!id) - ), - [ChangeType.Changed]: new Set( - operationsChanged.map(getChangeOperationId).filter((id) => !!id) - ), - [ChangeType.Removed]: new Set( - operationsRemoved.map(getChangeOperationId).filter((id) => !!id) - ), - }; - - const operationWasAddedOrRemoved = (operationId: string) => - operationsByChange.added.has(operationId) || - operationsByChange.removed.has(operationId); - - for (const change of changes) { - const operationId = getChangeOperationId(change); - if (!operationId || operationWasAddedOrRemoved(operationId)) continue; - operationsByChange.changed.add(operationId); - } - - return { - [ChangeType.Added]: operationsByChange[ChangeType.Added].size, - [ChangeType.Changed]: operationsByChange[ChangeType.Changed].size, - [ChangeType.Removed]: operationsByChange[ChangeType.Removed].size, - }; -}; - -export const getLabel = ( - operationsModifsCount: ReturnType -) => +export const getLabel = (operationsModifsCount: { + [ChangeType.Added]: number; + [ChangeType.Changed]: number; + [ChangeType.Removed]: number; +}) => Object.keys(operationsModifsCount) .filter((k) => (operationsModifsCount as any)[k]) .map( @@ -71,9 +21,6 @@ export const getLabel = ( ) .join(', '); -export const getOperationsModifsLabel = (changes: IChange[]) => - getLabel(countOperationsModifications(changes)); - export const getOperationsChanged = ( groupedDiffs: GroupedDiffs ): { diff --git a/projects/openapi-utilities/src/utilities/generate-changelog-data.ts b/projects/openapi-utilities/src/utilities/generate-changelog-data.ts deleted file mode 100644 index 542657f8fd..0000000000 --- a/projects/openapi-utilities/src/utilities/generate-changelog-data.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { groupChangesAndRules } from './group-changes'; -import { OpenAPIV3 } from 'openapi-types'; -import { traverseSpec } from './traverse-spec'; -import { IChange } from '../openapi3/sdk/types'; -import { ResultWithSourcemap } from '../types'; - -/** - * Takes raw outputs form a run and generates the data required to display a changelog. - * - * @example - * generateChangelogData({ changes: compareOutput.changes, toFile: parsedTo.jsonLike}); - */ -export const generateChangelogData = ({ - changes, - toFile, - rules, -}: { - changes: IChange[]; - toFile: OpenAPIV3.Document; - rules: ResultWithSourcemap[]; -}) => { - const toFacts = traverseSpec(toFile); - return groupChangesAndRules({ toFacts, changes, rules }); -};