Skip to content

Commit

Permalink
feat: fetch latest pacts for provider by branch
Browse files Browse the repository at this point in the history
uses latest-provider-pacts-with-branch

relies on pact-foundation/pact_broker#728
  • Loading branch information
YOU54F committed Oct 4, 2024
1 parent c5b801a commit d1df45d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
2 changes: 2 additions & 0 deletions lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ program
.arguments('<swagger> <mock>')
.option('-p, --provider [string]', 'The name of the provider in the pact broker')
.option('-t, --tag [string]', 'The tag to filter pacts retrieved from the pact broker')
.option('-b, --branch [string]', 'The branch to filter pacts retrieved from the pact broker')
.option('-u, --user [USERNAME:PASSWORD]', 'The basic auth username and password to access the pact broker')
.option('-a, --analyticsUrl [string]', 'The url to send analytics events to as a http post')
.option('-o, --outputDepth [integer]', 'Specifies the number of times to recurse ' +
Expand Down Expand Up @@ -84,6 +85,7 @@ If the pact broker has basic auth enabled, pass a --user option with username an
providerName: options.provider,
specPathOrUrl: swagger,
tag: options.tag,
branch: options.branch,
additionalPropertiesInResponse: options.additionalPropertiesInResponse,
requiredPropertiesInResponse: options.requiredPropertiesInResponse
});
Expand Down
3 changes: 2 additions & 1 deletion lib/swagger-mock-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ export class SwaggerMockValidator {
? this.pactBroker.loadPacts({
pactBrokerUrl: options.mockPathOrUrl,
providerName: options.providerName,
tag: options.tag
tag: options.tag,
branch: options.branch
})
: this.getPactFromFileOrUrl(options.mockPathOrUrl);

Expand Down
35 changes: 32 additions & 3 deletions lib/swagger-mock-validator/pact-broker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
PactBrokerRootResponse,
PactBrokerUserOptions,
PactBrokerUserOptionsWithTag,
PactBrokerUserOptionsWithBranch,
SerializedMock
} from './types';

Expand All @@ -29,12 +30,21 @@ export class PactBroker {
const pactBrokerRootResponse =
await this.pactBrokerClient.loadAsObject<PactBrokerRootResponse>(options.pactBrokerUrl);

return options.tag
? this.getUrlForProviderPactsByTag(pactBrokerRootResponse, {
if (options.tag){
return this.getUrlForProviderPactsByTag(pactBrokerRootResponse, {
pactBrokerUrl: options.pactBrokerUrl,
providerName: options.providerName,
tag: options.tag
}) : this.getUrlForAllProviderPacts(pactBrokerRootResponse, options);
})
} else if (options.branch){
return this.getUrlForProviderPactsByBranch(pactBrokerRootResponse, {
pactBrokerUrl: options.pactBrokerUrl,
providerName: options.providerName,
branch: options.branch
})
} else {
return this.getUrlForAllProviderPacts(pactBrokerRootResponse, options);
}
}

private getUrlForProviderPactsByTag(pactBrokerRootResponse: PactBrokerRootResponse,
Expand All @@ -56,6 +66,25 @@ export class PactBroker {
);
}

private getUrlForProviderPactsByBranch(pactBrokerRootResponse: PactBrokerRootResponse,
options: PactBrokerUserOptionsWithBranch): string {
const providerTemplateUrl = PactBroker.getProviderTemplateUrl(
pactBrokerRootResponse,
'_links.pb:latest-provider-pacts-with-branch.href'
);

if (!providerTemplateUrl) {
throw new SwaggerMockValidatorErrorImpl(
'SWAGGER_MOCK_VALIDATOR_READ_ERROR',
`Unable to read "${options.pactBrokerUrl}": No latest pact file url found for tag`
);
}

return this.getSpecificUrlFromTemplate(
providerTemplateUrl, {provider: options.providerName, branch: options.branch}
);
}

private getUrlForAllProviderPacts(
pactBrokerRootResponse: PactBrokerRootResponse,
options: PactBrokerUserOptions
Expand Down
6 changes: 6 additions & 0 deletions lib/swagger-mock-validator/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface SwaggerMockValidatorUserOptions {
providerName?: string;
specPathOrUrl: string;
tag?: string;
branch?: string;
additionalPropertiesInResponse?: string;
requiredPropertiesInResponse?: string;
}
Expand All @@ -39,11 +40,15 @@ export interface PactBrokerUserOptions {
pactBrokerUrl: string;
providerName: string;
tag?: string;
branch?: string;
}

export type PactBrokerUserOptionsWithTag = PactBrokerUserOptions & {
tag: string;
};
export type PactBrokerUserOptionsWithBranch = PactBrokerUserOptions & {
branch: string;
};

export type AutoDetectFormat = 'auto-detect';

Expand Down Expand Up @@ -74,6 +79,7 @@ interface ParsedSwaggerMockValidatorOptions {
specPathOrUrl: string;
specSource: SpecSource;
tag?: string;
branch?: string;
additionalPropertiesInResponse: boolean;
requiredPropertiesInResponse: boolean;
}
Expand Down

0 comments on commit d1df45d

Please sign in to comment.