@case-contract-testing/case
Version:
Next-generation contract testing suite
100 lines (99 loc) • 5.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BrokerService = void 0;
const entities_1 = require("../../entities");
const context_1 = require("../../entities/context");
const downloadCaseContracts_1 = require("./downloadCaseContracts");
class BrokerService {
broker;
environment;
constructor(broker, environment) {
this.broker = broker;
this.environment = environment;
}
publishVerificationResults(contract, success, context) {
if (context['case:currentRun:context:publish'] === false ||
context['case:currentRun:context:publish'] === 'NEVER') {
context.logger.debug(`Not publishing verification results for ${contract.description.consumerName} -> ${contract.description.providerName} as publish: 'NEVER' is set (or false)`);
return Promise.resolve();
}
if (context['case:currentRun:context:publish'] === 'ONLY_IN_CI' &&
!this.environment.isCi()) {
context.logger.debug(`Not publishing verification results for ${contract.description.consumerName} -> ${contract.description.providerName} as publish: 'ONLY_IN_CI' is set, and this is not a detected CI environment`);
return Promise.resolve();
}
if (context['case:currentRun:context:publish'] === true ||
context['case:currentRun:context:publish'] === 'ALWAYS' ||
(context['case:currentRun:context:publish'] === 'ONLY_IN_CI' &&
this.environment.isCi())) {
return this.broker
.publishVerificationResults(contract, success, this.environment.version(), this.environment.branch(), (0, context_1.addLocation)(':PublishingContractAdvanced', context))
.then(() => { });
}
const message = `Configuration property 'publish' was set to the unexpected value '${context['case:currentRun:context:publish']}'`;
context.logger.error(message);
return Promise.reject(new entities_1.CaseConfigurationError(message));
}
publishContract(contract, context) {
if (context['case:currentRun:context:publish'] === false ||
context['case:currentRun:context:publish'] === 'NEVER') {
context.logger.debug(`Not publishing contract for ${contract.description.consumerName} -> ${contract.description.providerName} as publish: 'NEVER' is set (or false)`);
return Promise.resolve();
}
if (context['case:currentRun:context:publish'] === 'ONLY_IN_CI' &&
!this.environment.isCi()) {
context.logger.debug(`Not publishing contract for ${contract.description.consumerName} -> ${contract.description.providerName} as publish: 'ONLY_IN_CI' is set, and this is not a detected CI environment`);
return Promise.resolve();
}
if (context['case:currentRun:context:publish'] === true ||
context['case:currentRun:context:publish'] === 'ALWAYS' ||
(context['case:currentRun:context:publish'] === 'ONLY_IN_CI' &&
this.environment.isCi())) {
return this.broker
.publishContractAdvanced(contract, this.environment.version(), this.environment.branch(), (0, context_1.addLocation)(':PublishingContractAdvanced', context))
.then((publishResults) => {
const brokerResponse = (0, context_1.addLocation)('Response', context);
publishResults.notices.forEach((notice) => {
switch (notice.type) {
case 'debug':
brokerResponse.logger.debug(`[Broker] ${notice.text}`);
break;
case 'info':
brokerResponse.logger.debug(`[Broker] ${notice.text}`);
break;
case 'prompt':
brokerResponse.logger.warn(`[Broker] ${notice.text}`);
break;
case 'success':
brokerResponse.logger.debug(`[Broker] ${notice.text}`);
break;
case 'error':
brokerResponse.logger.error(`[Broker] ${notice.text}`);
break;
case 'danger':
brokerResponse.logger.error(`From-Broker] [DANGER] ${notice.text}`);
break;
case 'warning':
brokerResponse.logger.warn(`From-Broker] ${notice.text}`);
break;
default:
throw new entities_1.CaseCoreError(`The broker returned a log level ('${notice.type}') that Case doesn't know how to handle.\n\nThe problem is in the following notice object:\n\n${JSON.stringify(notice)}`);
}
});
});
}
const message = `Configuration property 'publish' was set to the unexpected value '${context['case:currentRun:context:publish']}'`;
context.logger.error(message);
return Promise.reject(new entities_1.CaseConfigurationError(message));
}
async downloadContracts(serviceName, context) {
const contractUrls = await this.broker.urlsForVerification(serviceName, context);
context.logger.maintainerDebug('URLs are:', contractUrls);
if (contractUrls.length === 0) {
context.logger.warn('No contracts to verify. This may be normal if there is nothing to verify for this service');
return [];
}
return (0, downloadCaseContracts_1.downloadCaseContracts)(contractUrls, this.broker, context);
}
}
exports.BrokerService = BrokerService;