Skip to content

Commit

Permalink
feat: add support for formatted verification results
Browse files Browse the repository at this point in the history
  • Loading branch information
YOU54F committed Oct 8, 2024
1 parent f29caf3 commit acbfc75
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 47 deletions.
106 changes: 68 additions & 38 deletions lib/swagger-mock-validator/pact-broker.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import _ from 'lodash';
import {PactBrokerClient} from './clients/pact-broker-client';
import {SwaggerMockValidatorErrorImpl} from './swagger-mock-validator-error-impl';
import { PactBrokerClient } from './clients/pact-broker-client';
import { SwaggerMockValidatorErrorImpl } from './swagger-mock-validator-error-impl';
import {
PactBrokerProviderPacts,
PactBrokerProviderPactsLinksPact,
PactBrokerRootResponse,
PactBrokerPacticipantResponse,
PactBrokerUserOptions,
PactBrokerUserOptionsWithTag,
ParsedSwaggerMockValidatorOptions,
SerializedMock
SerializedMock,
} from './types';
import {ValidationOutcome} from '../api-types';
import {ParsedMock} from './mock-parser/parsed-mock';
import { ValidationOutcome } from '../api-types';
import { ParsedMock } from './mock-parser/parsed-mock';

export class PactBroker {
private static getProviderTemplateUrl(pactBrokerRootResponse: PactBrokerRootResponse, template: string): string {
Expand All @@ -39,7 +38,7 @@ export class PactBroker {
providerName,
}: ParsedSwaggerMockValidatorOptions,
{ verificationUrl }: ParsedMock,
{ success }: ValidationOutcome,
{ success, errors, warnings }: ValidationOutcome,
): Promise<void> {
// if (mockSource !== 'pactBroker') {
// throw new SwaggerMockValidatorErrorImpl(
Expand All @@ -63,48 +62,79 @@ export class PactBroker {
let branchVersionUrl;
let versionTagUrl;
if ((providerBranch || providerTags) && providerName) {
const pactBrokerRootResponse = await this.pactBrokerClient.loadAsObject<PactBrokerRootResponse>(
mockPathOrUrl
);
const pactBrokerPacticipantUrl = pactBrokerRootResponse._links['pb:pacticipant'].href;
const pactBrokerPacticipantResponse = await this.pactBrokerClient.loadAsObject<PactBrokerPacticipantResponse>(
this.getSpecificUrlFromTemplate(pactBrokerPacticipantUrl,{pacticipant: providerName}),
);
branchVersionUrl = pactBrokerPacticipantResponse._links['pb:branch-version'].href;
versionTagUrl = pactBrokerPacticipantResponse._links['pb:version-tag'].href;
}

if (providerBranch && providerName && branchVersionUrl) {
await this.pactBrokerClient.put(
this.getSpecificUrlFromTemplate(branchVersionUrl, {
provider: providerName,
branch: providerBranch,
version: providerApplicationVersion,
}),
{
version: providerApplicationVersion,
branch: providerBranch,
},
);
const pactBrokerRootResponse =
await this.pactBrokerClient.loadAsObject<PactBrokerRootResponse>(mockPathOrUrl);
branchVersionUrl = pactBrokerRootResponse._links['pb:pacticipant-branch-version'].href;
versionTagUrl = pactBrokerRootResponse._links['pb:pacticipant-version-tag'].href;
}

if (providerTags && providerName && versionTagUrl) {
const tags = providerTags.split(',');
for (const tag of tags) {
try {
if (providerBranch && providerName && branchVersionUrl) {
await this.pactBrokerClient.put(
this.getSpecificUrlFromTemplate(versionTagUrl, { tag: tag, version: providerApplicationVersion }),
{
this.getSpecificUrlFromTemplate(branchVersionUrl, {
pacticipant: providerName,
branch: providerBranch,
version: providerApplicationVersion,
tag: tag,
},
}),
{},
);
}
} catch (e) {
console.error('Failed to create provider branch for verification result', e.message);
}

try {
if (providerTags && providerName && versionTagUrl) {
const tags = providerTags.split(',');
for (const tag of tags) {
await this.pactBrokerClient.put(
this.getSpecificUrlFromTemplate(versionTagUrl, {
pacticipant: providerName,
tag: tag,
version: providerApplicationVersion,
}),
{},
);
}
}
} catch (e) {
console.error('Failed to create provider tag for verification result', e.message);
}

const testResults = [
...errors.map((error) => ({
interactionDescription: error.mockDetails?.interactionDescription,
interactionId: null,
success: false,
mismatches: [
{
attribute: error.code,
description: error.message,
identifier: error.mockDetails?.value,
},
],
})),
...warnings.map((warning) => ({
interactionDescription: warning.mockDetails?.interactionDescription,
interactionId: null,
success: true,
mismatches: [
{
attribute: warning.code,
description: warning.message,
identifier: warning.mockDetails?.value,
},
],
})),
];
console.log(testResults);
return this.pactBrokerClient.post(verificationUrl, {
success,
providerApplicationVersion,
buildUrl,
testResults,
verifiedBy: {
implementation: 'swagger-mock-validator',
},
});
}

Expand Down
9 changes: 2 additions & 7 deletions lib/swagger-mock-validator/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,13 @@ import {SwaggerMockValidatorOptionsMockType, SwaggerMockValidatorOptionsSpecType
export interface PactBrokerRootResponse {
_links: PactBrokerLinks;
}
export interface PactBrokerPacticipantResponse {
_links: PactBrokerPacticipantResponseLinks;
}

interface PactBrokerLinks {
'pb:pacticipant': PactBrokerLinksPacticipant;
'pb:latest-provider-pacts': PactBrokerLinksLatestProviderPacts;
'pb:latest-provider-pacts-with-tag': PactBrokerLinksLatestProviderPacts;
}
interface PactBrokerPacticipantResponseLinks {
'pb:version-tag': PactBrokerLinksPacticipantVersionTag;
'pb:branch-version': PactBrokerLinksPacticipantBranchVersion;
'pb:pacticipant-version-tag': PactBrokerLinksPacticipantVersionTag;
'pb:pacticipant-branch-version': PactBrokerLinksPacticipantBranchVersion;
}

interface PactBrokerLinksPacticipant {
Expand Down
21 changes: 19 additions & 2 deletions test/e2e/cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,13 +400,30 @@ describe('swagger-mock-validator/cli', () => {
it('should publish verification status to pact broker', async () => {
const expectCall = {
providerApplicationVersion: '1.2.3',
buildUrl: 'https://pipeline/1'
buildUrl: 'https://pipeline/1',
testResults: [
{
interactionDescription: 'unknown header as a warning test',
interactionId: null,
success: true,
mismatches: [
{
attribute: 'request.header.unknown',
description: 'Request header is not defined in the spec file: x-unknown-header',
identifier: '1,2',
},
],
},
],
verifiedBy: {
implementation: "swagger-mock-validator"
}
};
await invokeCommand({
mock: urlTo('test/e2e/fixtures/pact-working-consumer.json'),
swagger: urlTo('test/e2e/fixtures/swagger-provider.json'),
publish: true,
...expectCall
...expectCall,
});

expect(mockPactBroker.post).toHaveBeenCalledWith({
Expand Down

0 comments on commit acbfc75

Please sign in to comment.