Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fetch latest pacts for provider by branch #67

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
? this.pactBroker.loadPacts({
pactBrokerUrl: options.mockPathOrUrl,
providerName: options.providerName,
tag: options.tag
tag: options.tag,
branch: options.branch
})
: this.getPactFromFileOrUrl(options.mockPathOrUrl);

Expand Down Expand Up @@ -250,7 +251,7 @@
specSource: options.specSource,
validationOutcome
});
} catch (error) {

Check warning on line 254 in lib/swagger-mock-validator.ts

View workflow job for this annotation

GitHub Actions / build-and-test-osx (18.x)

'error' is defined but never used

Check warning on line 254 in lib/swagger-mock-validator.ts

View workflow job for this annotation

GitHub Actions / build-and-test-osx (22.x)

'error' is defined but never used

Check warning on line 254 in lib/swagger-mock-validator.ts

View workflow job for this annotation

GitHub Actions / build-and-test-ubuntu (20.x)

'error' is defined but never used

Check warning on line 254 in lib/swagger-mock-validator.ts

View workflow job for this annotation

GitHub Actions / build-and-test-osx (20.x)

'error' is defined but never used

Check warning on line 254 in lib/swagger-mock-validator.ts

View workflow job for this annotation

GitHub Actions / build-and-test-ubuntu (22.x)

'error' is defined but never used

Check warning on line 254 in lib/swagger-mock-validator.ts

View workflow job for this annotation

GitHub Actions / build-and-test-ubuntu (18.x)

'error' is defined but never used

Check warning on line 254 in lib/swagger-mock-validator.ts

View workflow job for this annotation

GitHub Actions / build-and-test-osx (20.x)

'error' is defined but never used

Check warning on line 254 in lib/swagger-mock-validator.ts

View workflow job for this annotation

GitHub Actions / build-and-test-osx (18.x)

'error' is defined but never used

Check warning on line 254 in lib/swagger-mock-validator.ts

View workflow job for this annotation

GitHub Actions / build-and-test-ubuntu (18.x)

'error' is defined but never used

Check warning on line 254 in lib/swagger-mock-validator.ts

View workflow job for this annotation

GitHub Actions / build-and-test-ubuntu (20.x)

'error' is defined but never used

Check warning on line 254 in lib/swagger-mock-validator.ts

View workflow job for this annotation

GitHub Actions / build-and-test-ubuntu (22.x)

'error' is defined but never used

Check warning on line 254 in lib/swagger-mock-validator.ts

View workflow job for this annotation

GitHub Actions / build-and-test-osx (22.x)

'error' is defined but never used
// do not fail tool on analytics errors
}
}
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
Loading