Skip to content

Commit f9e7ff5

Browse files
committed
feat: add ExportRestOperationsGroupStrategy
1 parent 0fdccab commit f9e7ff5

File tree

7 files changed

+145
-19
lines changed

7 files changed

+145
-19
lines changed

src/builder.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ import { BuildStrategy, ChangelogStrategy, DocumentGroupStrategy, PrefixGroupsCh
7272
import { BuilderStrategyContext } from './builder-strategy'
7373
import { MergedDocumentGroupStrategy } from './strategies/merged-document-group.strategy'
7474
import { asyncDebugPerformance } from './utils/logs'
75+
import { ExportRestOperationsGroupStrategy } from './strategies/rest-operations-group.strategy'
76+
// import { ExportVersionStrategy } from './strategies/export-version.strategy'
7577

7678
export const DEFAULT_RUN_OPTIONS: BuilderRunOptions = {
7779
cleanCache: false,
@@ -230,6 +232,18 @@ export class PackageVersionBuilder implements IPackageVersionBuilder {
230232
builderStrategyContext.setStrategy(new MergedDocumentGroupStrategy())
231233
}
232234

235+
// if (buildType === BUILD_TYPE.EXPORT_VERSION) {
236+
// builderStrategyContext.setStrategy(new ExportVersionStrategy())
237+
// }
238+
239+
// if (buildType === BUILD_TYPE.EXPORT_REST_DOCUMENT) {
240+
// builderStrategyContext.setStrategy(new MergedDocumentGroupStrategy())
241+
// }
242+
243+
if (buildType === BUILD_TYPE.EXPORT_REST_OPERATIONS_GROUP) {
244+
builderStrategyContext.setStrategy(new ExportRestOperationsGroupStrategy())
245+
}
246+
233247
await asyncDebugPerformance('[Builder]', async (debugCtx) => {
234248
this.setBuildResult(await builderStrategyContext.executeStrategy(debugCtx))
235249
}, undefined, [buildType, this.config.packageId, this.config.version])

src/consts.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ export const BUILD_TYPE = {
8080
DOCUMENT_GROUP: 'documentGroup',
8181
REDUCED_SOURCE_SPECIFICATIONS: 'reducedSourceSpecifications',
8282
MERGED_SPECIFICATION: 'mergedSpecification',
83+
EXPORT_VERSION: 'exportVersion',
84+
EXPORT_REST_DOCUMENT: 'exportRestDocument',
85+
EXPORT_REST_OPERATIONS_GROUP: 'exportRestOperationsGroup',
8386
} as const
8487

8588
export const VERSION_STATUS = {

src/strategies/document-group.strategy.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
BuildTypeContexts,
2222
FileFormat,
2323
JSON_EXPORT_GROUP_FORMAT,
24-
ResolvedDocument,
24+
ResolvedDocument, ResolvedReferenceMap,
2525
VersionDocument,
2626
} from '../types'
2727
import { REST_API_TYPE } from '../apitypes'
@@ -43,7 +43,7 @@ import { normalize } from '@netcracker/qubership-apihub-api-unifier'
4343
import { calculateSpecRefs, extractCommonPathItemProperties } from '../apitypes/rest/rest.operation'
4444
import { groupBy } from 'graphql/jsutils/groupBy'
4545

46-
async function getTransformedDocument(document: ResolvedDocument, format: FileFormat): Promise<VersionRestDocument> {
46+
async function getTransformedDocument(document: ResolvedDocument, format: FileFormat, packages: ResolvedReferenceMap): Promise<VersionRestDocument> {
4747
const versionDocument = toVersionDocument(document, format)
4848

4949
const source = extractDocumentData(versionDocument)
@@ -60,6 +60,13 @@ async function getTransformedDocument(document: ResolvedDocument, format: FileFo
6060

6161
calculateSpecRefs(source, normalizedDocument, versionDocument.data)
6262

63+
// dashboard case
64+
if (document.packageRef) {
65+
const { refId } = packages[document.packageRef]
66+
versionDocument.fileId = `${refId}_${versionDocument.fileId}`
67+
versionDocument.filename = `${refId}_${versionDocument.filename}`
68+
}
69+
6370
return versionDocument
6471
}
6572

@@ -83,17 +90,17 @@ export class DocumentGroupStrategy implements BuilderStrategy {
8390

8491
const builderContextObject = builderContext(config)
8592

86-
const { documents } = await builderContextObject.versionDocumentsResolver(
87-
apiType,
93+
const { documents, packages } = await builderContextObject.versionDocumentsResolver(
94+
REST_API_TYPE,
8895
version,
8996
packageId,
9097
groupName,
91-
) ?? { documents: [] }
98+
) ?? { documents: [], packages: {} }
9299

93100
const transformTasks = []
94101

95102
for (const document of documents) {
96-
transformTasks.push(getTransformedDocument(document, documentFormat))
103+
transformTasks.push(getTransformedDocument(document, documentFormat, packages))
97104
}
98105

99106
const transformedDocuments = await Promise.all(transformTasks)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Copyright 2024-2025 NetCracker Technology Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import {
18+
BuildConfig,
19+
BuildResult,
20+
BuildTypeContexts,
21+
TRANSFORMATION_KIND_MERGED,
22+
TRANSFORMATION_KIND_REDUCED,
23+
} from '../types'
24+
import { DocumentGroupStrategy } from './document-group.strategy'
25+
import { MergedDocumentGroupStrategy } from './merged-document-group.strategy'
26+
27+
export class ExportRestOperationsGroupStrategy extends DocumentGroupStrategy {
28+
async execute(config: BuildConfig, buildResult: BuildResult, contexts: BuildTypeContexts): Promise<BuildResult> {
29+
30+
switch (config.operationsSpecTransformation) {
31+
case TRANSFORMATION_KIND_MERGED:
32+
await new MergedDocumentGroupStrategy().execute(config, buildResult, contexts)
33+
break
34+
case TRANSFORMATION_KIND_REDUCED:
35+
await new DocumentGroupStrategy().execute(config, buildResult, contexts)
36+
break
37+
}
38+
39+
return buildResult
40+
}
41+
}

src/types/external/config.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import { FileId, KeyOfConstType, OperationsApiType, PackageId, VersionId } from './types'
1818
import { BUILD_TYPE, VERSION_STATUS } from '../../consts'
19+
import { OpenApiExtensionKey } from '@netcracker/qubership-apihub-api-unifier'
1920

2021
export type BuildType = KeyOfConstType<typeof BUILD_TYPE>
2122
export type VersionStatus = KeyOfConstType<typeof VERSION_STATUS>
@@ -61,8 +62,63 @@ export interface BuildConfig {
6162
format?: OperationsGroupExportFormat
6263

6364
validationRulesSeverity?: ValidationRulesSeverity
65+
operationsSpecTransformation?: OperationsSpecTransformation
6466
}
6567

68+
export interface BuildConfigBase {
69+
// export interface BuildConfig {
70+
buildType: BuildType
71+
}
72+
73+
export interface PublishBuildConfig extends BuildConfigBase {
74+
buildType: typeof BUILD_TYPE.BUILD
75+
version: VersionId // @revision for rebuild
76+
previousVersion?: VersionId
77+
previousVersionPackageId?: PackageId
78+
status: VersionStatus
79+
versionLabels?: string[]
80+
refs?: BuildConfigRef[]
81+
files?: BuildConfigFile[]
82+
// apiType?: OperationsApiType //todo Document transformation is available only for apiType = REST
83+
}
84+
85+
export interface ExportVersionBuildConfig extends BuildConfigBase {
86+
buildType: typeof BUILD_TYPE.EXPORT_VERSION
87+
packageId: PackageId
88+
version: VersionId // @revision for rebuild
89+
// apiType?: OperationsApiType //todo Document transformation is available only for apiType = REST
90+
format: OperationsGroupExportFormat
91+
allowedOasExtensions?: OpenApiExtensionKey
92+
}
93+
94+
export interface ExportRestDocumentBuildConfig extends BuildConfigBase {
95+
buildType: typeof BUILD_TYPE.EXPORT_REST_DOCUMENT
96+
packageId: PackageId
97+
version: VersionId // @revision for rebuild
98+
documentId: string
99+
// apiType?: OperationsApiType //todo Document transformation is available only for apiType = REST
100+
format: OperationsGroupExportFormat
101+
allowedOasExtensions?: OpenApiExtensionKey
102+
}
103+
104+
export interface ExportRestOperationsGroupBuildConfig extends BuildConfigBase {
105+
buildType: typeof BUILD_TYPE.EXPORT_REST_OPERATIONS_GROUP
106+
packageId: PackageId
107+
version: VersionId // @revision for rebuild
108+
// apiType?: OperationsApiType //todo Document transformation is available only for apiType = REST
109+
groupName: string
110+
operationsSpecTransformation: OperationsSpecTransformation
111+
format: OperationsGroupExportFormat
112+
allowedOasExtensions?: OpenApiExtensionKey
113+
}
114+
115+
export const TRANSFORMATION_KIND_REDUCED = 'reducedSourceSpecifications'
116+
export const TRANSFORMATION_KIND_MERGED = 'mergedSpecification'
117+
118+
export type OperationsSpecTransformation =
119+
| typeof TRANSFORMATION_KIND_REDUCED
120+
| typeof TRANSFORMATION_KIND_MERGED
121+
66122
export interface BuildConfigFile {
67123
fileId: FileId
68124
slug?: string // for rebuild

src/types/external/documents.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
import { OperationsApiType, PackageId, VersionId } from './types'
18+
import { ResolvedReferenceMap } from './references'
1819

1920
export type VersionDocumentsResolver = (
2021
apiType: OperationsApiType,
@@ -25,6 +26,7 @@ export type VersionDocumentsResolver = (
2526

2627
export type ResolvedDocuments = {
2728
documents: ReadonlyArray<ResolvedDocument>
29+
packages: ResolvedReferenceMap
2830
}
2931

3032
export type ResolvedDocument = {
@@ -38,4 +40,5 @@ export type ResolvedDocument = {
3840
labels: string[]
3941
includedOperationIds?: string[]
4042
data?: string
43+
packageRef?: string
4144
}

src/types/external/references.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
import { PackageId, VersionId } from './types'
18+
import { VersionStatus } from './config'
1819

1920
export type VersionReferencesResolver = (
2021
version: VersionId,
@@ -32,22 +33,23 @@ export interface ResolvedReferences {
3233
packages: ResolvedReferenceMap
3334
}
3435

35-
export type ResolvedReferenceMap = Record<string, ResolvedReference>
36+
export type ResolvedReferenceMap = Record<string, ReferencedPackage>
3637

37-
export interface ResolvedReference {
38+
export interface ReferencedPackage {
3839
refId: string
40+
kind: ReferencedPackageKind
41+
name: string
3942
version: string
40-
versionRevision: number
41-
parentRefId?: string
42-
parentRefVersion?: string
43-
excluded?: boolean
43+
status: VersionStatus
44+
parentPackages: string[]
4445
deletedAt?: string
46+
deletedBy?: string
47+
notLatestRevision?: string
48+
}
4549

46-
// other params
47-
// [key: string]: unknown
50+
export const KIND_PACKAGE = 'package'
51+
export const KIND_DASHBOARD = 'dashboard'
4852

49-
// name: string
50-
// status: 'draft' | 'release' | 'release candidate' | 'deprecated' | 'archived'
51-
// kind?: 'package' | 'dashboard'
52-
// parents?: unknown[]
53-
}
53+
export type ReferencedPackageKind =
54+
| typeof KIND_PACKAGE
55+
| typeof KIND_DASHBOARD

0 commit comments

Comments
 (0)