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([asyncapi]): keep latest service version in domain services ma… #75

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
45 changes: 27 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export default async (config: any, options: Props) => {
addSchemaToQuery,
addFileToService,
versionDomain,
rmDomainById,
getSpecificationFilesForService,
} = utils(process.env.PROJECT_DIR);

Expand Down Expand Up @@ -121,7 +122,6 @@ export default async (config: any, options: Props) => {
// Should the file that is written to the catalog be parsed (https://github.com/asyncapi/parser-js) or as it is?
validateOptions(options);
const { services, saveParsedSpecFile = false } = options;
// const asyncAPIFiles = Array.isArray(options.path) ? options.path : [options.path];
console.log(chalk.green(`Processing ${services.length} AsyncAPI files...`));
for (const service of services) {
console.log(chalk.gray(`Processing ${service.path}`));
Expand All @@ -142,25 +142,29 @@ export default async (config: any, options: Props) => {

const operations = document.allOperations();
const documentTags = document.info().tags().all() || [];

const serviceId = service.id;

const serviceName = service.name || document.info().title();
const version = document.info().version();
const serviceId = service.id;

// What messages does this service send and receive
let sends = [];
let receives = [];
let serviceMarkdown = generateMarkdownForService(document);

// service markdown specifications
let serviceSpecifications = {};
// service specification files
let serviceSpecificationsFiles = [];
let serviceMarkdown = generateMarkdownForService(document);

// Domain Variables
let domainMarkDown;
let domainServices: { id: string; version: string }[] = [];

// Manage domain
if (options.domain) {
// Try and get the domain
const { id: domainId, name: domainName, version: domainVersion } = options.domain;
const domain = await getDomain(options.domain.id, domainVersion || 'latest');
const domain = await getDomain(options.domain.id, domainVersion);
const currentDomain = await getDomain(options.domain.id, 'latest');

console.log(chalk.blue(`\nProcessing domain: ${domainName} (v${domainVersion})`));
Expand All @@ -172,21 +176,26 @@ export default async (config: any, options: Props) => {
}

// Do we need to create a new domain?
if (!domain || (domain && domain.version !== domainVersion)) {
await writeDomain({
id: domainId,
name: domainName,
version: domainVersion,
markdown: generateMarkdownForDomain(document),
// services: [{ id: serviceId, version: version }],
});
console.log(chalk.cyan(` - Domain (v${domainVersion}) created`));
}
if (domain) {
console.log(chalk.yellow(` - Domain (v${domainVersion}) already exists, fetching existing details...`));
domainMarkDown = domain.markdown;
domainServices = domain.services || [];

if (currentDomain && currentDomain.version === domainVersion) {
console.log(chalk.yellow(` - Domain (v${domainVersion}) already exists, skipped creation...`));
await rmDomainById(domainId, domainVersion);
}

domainServices = domainServices.filter((service) => service.id !== serviceId);
console.log(chalk.red(` - Domain (${JSON.stringify(domainServices)}) created`));

await writeDomain({
id: domainId,
name: domainName,
version: domainVersion,
markdown: generateMarkdownForDomain(document),
services: [...domainServices, { id: serviceId, version: version }],
});
console.log(chalk.cyan(` - Domain (v${domainVersion}) created`));

// Add the service to the domain
await addServiceToDomain(domainId, { id: serviceId, version: version }, domainVersion);
}
Expand Down
25 changes: 25 additions & 0 deletions src/test/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,31 @@ describe('AsyncAPI EventCatalog Plugin', () => {
expect(newDomain.services).toEqual([{ id: 'account-service', version: '1.0.0' }]);
});

it('if a domain is defined in the AsyncAPI file but the versions do not match, the existing domain is version and a new one is created', async () => {
const { writeDomain, getDomain } = utils(catalogDir);

await writeDomain({
id: 'orders',
name: 'Orders Domain',
version: '0.0.1',
markdown: '',
services: [{ id: 'account-service', version: '0.0.1' }],
});

await plugin(config, {
services: [{ path: join(asyncAPIExamplesDir, 'simple.asyncapi.yml'), id: 'account-service' }],
domain: { id: 'orders', name: 'Orders Domain', version: '1.0.0' },
});

const versionedDomain = await getDomain('orders', '0.0.1');
const newDomain = await getDomain('orders', '1.0.0');

expect(versionedDomain.version).toEqual('0.0.1');
expect(newDomain.version).toEqual('1.0.0');
expect(versionedDomain.services).toEqual([{ id: 'account-service', version: '0.0.1' }]);
expect(newDomain.services).toEqual([{ id: 'account-service', version: '1.0.0' }]);
});

it('if a domain is defined in the AsyncAPI plugin configuration and that domain exists the AsyncAPI Service is added to that domain', async () => {
const { writeDomain, getDomain } = utils(catalogDir);

Expand Down