-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added publishing swagger files
- Loading branch information
Showing
16 changed files
with
340 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,37 @@ | ||
import { generateContractsForm } from '../utils/generate-contracts-form'; | ||
import { generateExpectations } from '../utils/generate-expectations'; | ||
import { CliPublishArguments } from '../utils/define-args'; | ||
import { readPacts } from '../utils/read-pacts'; | ||
import { postServiceContractsForm } from '../api/post-service-contracts-form'; | ||
import { URL } from 'url'; | ||
import { readSwagger } from '../utils/read-swagger'; | ||
import { generateRestContract } from '../utils/generate-rest-contract'; | ||
import { ServiceContractsForm } from '../types'; | ||
|
||
export async function publish(argv: CliPublishArguments) { | ||
const { pactsDir, serviceName, url, serviceVersion } = argv; | ||
const { pactsDir, serviceName, url, serviceVersion, swaggerFile } = argv; | ||
|
||
const pacts = readPacts(pactsDir); | ||
const contractsForm = generateContractsForm(pacts); | ||
const judgeDUrl = new URL( | ||
const serviceContractsForm: ServiceContractsForm = { | ||
capabilities: {}, | ||
expectations: {}, | ||
}; | ||
|
||
if (pactsDir) { | ||
const pacts = readPacts(pactsDir); | ||
serviceContractsForm.expectations = generateExpectations(pacts); | ||
} | ||
|
||
if (swaggerFile) { | ||
const swagger = readSwagger(swaggerFile); | ||
serviceContractsForm.capabilities = generateRestContract(swagger); | ||
} | ||
|
||
const postJudgeDServiceContractsFormUrl = new URL( | ||
`./contracts/services/${serviceName}/versions/${serviceVersion}`, | ||
url | ||
).toString(); | ||
|
||
await postServiceContractsForm(judgeDUrl, contractsForm); | ||
await postServiceContractsForm( | ||
postJudgeDServiceContractsFormUrl, | ||
serviceContractsForm | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export interface ServiceContractsForm { | ||
capabilities: Partial<RestContract>; | ||
expectations: Record<string, RestContract>; | ||
} | ||
|
||
export interface RestContract { | ||
rest: { | ||
value: string; | ||
mimeType: 'application/json'; | ||
}; | ||
} | ||
|
||
export type SwaggerDefinition = object; | ||
|
||
export interface Pact { | ||
provider?: { name?: string }; | ||
interactions: Array<unknown>; | ||
[x: string]: any; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Pact, ServiceContractsForm } from '../types'; | ||
import { generateRestContract } from './generate-rest-contract'; | ||
|
||
export function generateExpectations( | ||
pactFiles: Pact[] | ||
): ServiceContractsForm['expectations'] { | ||
const expectations: ServiceContractsForm['expectations'] = {}; | ||
|
||
for (const pactFile of pactFiles) { | ||
if (pactFile.provider?.name) { | ||
expectations[pactFile.provider.name] = generateRestContract( | ||
pactFile | ||
); | ||
} | ||
} | ||
|
||
return expectations; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { RestContract } from '../types'; | ||
|
||
export function generateRestContract(value: object): RestContract { | ||
return { | ||
rest: { | ||
value: JSON.stringify(value), | ||
mimeType: 'application/json', | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import * as fs from 'fs'; | ||
import { SwaggerDefinition } from '../types'; | ||
|
||
export function readSwagger(swaggerFilePath: string): SwaggerDefinition { | ||
if (!swaggerFilePath.endsWith('.json')) { | ||
throw new Error(`Only json swagger files are supported.`); | ||
} | ||
|
||
return JSON.parse(fs.readFileSync(swaggerFilePath, 'utf8')); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,45 @@ | ||
import { defineArgs } from '../src/utils/define-args'; | ||
|
||
describe('defineArgs', () => { | ||
const commonRequiredArgs = [ | ||
'publish', | ||
'--url', | ||
'judge-d.instance.com', | ||
'--serviceName', | ||
'example-service', | ||
'--serviceVersion', | ||
'1.0.0', | ||
]; | ||
|
||
test('returns correctly parsed process arguments', () => { | ||
const argv = defineArgs([ | ||
'publish', | ||
'--url', | ||
'judge-d.instance.com', | ||
...commonRequiredArgs, | ||
'--pactsDir', | ||
'/pacts', | ||
'--serviceName', | ||
'example-service', | ||
'--serviceVersion', | ||
'1.0.0', | ||
'--swaggerFile', | ||
'/swagger/swagger.json', | ||
]); | ||
|
||
expect(argv).toMatchObject({ | ||
_: ['publish'], | ||
url: 'judge-d.instance.com', | ||
pactsDir: '/pacts', | ||
swaggerFile: '/swagger/swagger.json', | ||
serviceName: 'example-service', | ||
serviceVersion: '1.0.0', | ||
}); | ||
}); | ||
|
||
test('throws error and exit when both swaggerFile and pactsDir arguments are not passed', () => { | ||
jest.spyOn(console, 'error').mockImplementation(jest.fn()); | ||
jest.spyOn(process, 'exit').mockImplementation(); | ||
|
||
defineArgs(commonRequiredArgs); | ||
|
||
expect(console.error).toHaveBeenNthCalledWith( | ||
3, | ||
'Either pactsDir or swaggerFile argument must be passed.' | ||
); | ||
expect(process.exit).toHaveBeenCalledWith(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,9 @@ | ||
import { makeFactory } from 'factory.ts'; | ||
import { ServiceContractsForm } from '../../src/utils/generate-contracts-form'; | ||
import { pactMockFactory } from './pact.mock'; | ||
import { ServiceContractsForm } from '../../src/types'; | ||
|
||
export const serviceContractsFormMockFactory = makeFactory<ServiceContractsForm>( | ||
{ | ||
capabilities: {}, | ||
expectations: { | ||
'provider-service-1': { | ||
rest: { | ||
value: JSON.stringify(pactMockFactory.build()), | ||
mimeType: 'application/json', | ||
}, | ||
}, | ||
}, | ||
expectations: {}, | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
export const swaggerFileMock = { | ||
swagger: '2.0', | ||
info: { | ||
description: 'Api Documentation', | ||
version: '1.0', | ||
title: 'Api Documentation', | ||
termsOfService: 'urn:tos', | ||
contact: {}, | ||
license: { | ||
name: 'Apache 2.0', | ||
url: 'http://www.apache.org/licenses/LICENSE-2.0', | ||
}, | ||
}, | ||
host: 'localhost', | ||
basePath: '/', | ||
tags: [ | ||
{ | ||
name: 'environment-controller', | ||
description: 'Environment Controller', | ||
}, | ||
], | ||
paths: { | ||
'/environments/{name}': { | ||
put: { | ||
tags: ['environment-controller'], | ||
summary: 'Update the environment', | ||
operationId: 'update environment', | ||
consumes: ['application/json'], | ||
produces: ['*/*'], | ||
parameters: [ | ||
{ | ||
name: 'name', | ||
in: 'path', | ||
description: 'name', | ||
required: true, | ||
type: 'string', | ||
}, | ||
{ | ||
in: 'body', | ||
name: 'services', | ||
description: 'services', | ||
required: true, | ||
schema: { | ||
type: 'array', | ||
items: { | ||
$ref: '#/definitions/ServiceForm', | ||
}, | ||
}, | ||
}, | ||
], | ||
responses: { | ||
'200': { | ||
description: 'Success', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
definitions: { | ||
ServiceForm: { | ||
type: 'object', | ||
properties: { | ||
name: { | ||
type: 'string', | ||
}, | ||
version: { | ||
type: 'string', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; |
Oops, something went wrong.