Skip to content

Commit b261dab

Browse files
authored
fix(3072): PipelineTemplateVersionFactory getWithMetadata() does not return a valid model (#620)
1 parent fd9df31 commit b261dab

File tree

2 files changed

+65
-31
lines changed

2 files changed

+65
-31
lines changed

lib/pipelineTemplateVersionFactory.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,16 @@ class PipelineTemplateVersionFactory extends BaseFactory {
238238

239239
const pipelineTemplateVersion = await super.get(config);
240240

241-
return { ...pipelineTemplateVersion, ...pipelineTemplateMeta };
241+
if (!pipelineTemplateVersion) {
242+
return null;
243+
}
244+
245+
// merge selected template meta fields into template version
246+
['pipelineId', 'namespace', 'name', 'maintainer', 'latestVersion'].forEach(fieldName => {
247+
pipelineTemplateVersion[fieldName] = pipelineTemplateMeta[fieldName];
248+
});
249+
250+
return pipelineTemplateVersion;
242251
}
243252

244253
/**

test/lib/pipelineTemplateVersionFactory.test.js

Lines changed: 55 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ sinon.assert.expose(assert, { prefix: '' });
77

88
describe('PipelineTemplateVersion Factory', () => {
99
const namespace = 'namespace';
10-
const name = 'testPipelineTemplateVersion';
10+
const name = 'testPipelineTemplateName';
1111
const version = '1.3';
1212
const tag = 'latest';
1313
const metaData = {
@@ -19,7 +19,6 @@ describe('PipelineTemplateVersion Factory', () => {
1919
let datastore;
2020
let factory;
2121
let PipelineTemplateVersion;
22-
let PipelineTemplateMeta;
2322
let templateMetaFactoryMock;
2423

2524
beforeEach(() => {
@@ -37,8 +36,6 @@ describe('PipelineTemplateVersion Factory', () => {
3736
// eslint-disable-next-line global-require
3837
PipelineTemplateVersion = require('../../lib/pipelineTemplateVersion');
3938
// eslint-disable-next-line global-require
40-
PipelineTemplateMeta = require('../../lib/pipelineTemplate');
41-
// eslint-disable-next-line global-require
4239
PipelineTemplateVersionFactory = require('../../lib/pipelineTemplateVersionFactory');
4340

4441
factory = new PipelineTemplateVersionFactory({ datastore });
@@ -378,33 +375,42 @@ describe('PipelineTemplateVersion Factory', () => {
378375
describe('getWithMetadata', async () => {
379376
const templateId = 1234135;
380377
const generatedVersionId = 2341351;
381-
let returnValue;
382-
const hasAllProperties = (obj, class1, class2) => {
383-
const allProps = Object.getOwnPropertyNames(class1.prototype).concat(
384-
Object.getOwnPropertyNames(class2.prototype)
385-
);
386-
387-
return allProps.every(prop => prop in obj);
378+
let templateVersionMock;
379+
380+
const pipelineTemplateMetaMock = {
381+
id: templateId,
382+
name,
383+
namespace,
384+
maintainer: 'test-user@email.com',
385+
pipelineId: 123,
386+
latestVersion: '2.1.2',
387+
trustedSinceVersion: '2.1.0'
388388
};
389389

390+
const pipelineTemplateMetaToBeCopied = Object.keys(pipelineTemplateMetaMock)
391+
.filter(key => ['pipelineId', 'namespace', 'name', 'maintainer', 'latestVersion'].includes(key))
392+
.reduce((subset, key) => {
393+
subset[key] = pipelineTemplateMetaMock[key];
394+
395+
return subset;
396+
}, {});
397+
390398
beforeEach(() => {
391-
returnValue = {
399+
templateVersionMock = {
392400
id: generatedVersionId + 3,
393-
name,
394401
version: '2.1.2',
395-
templateId
402+
templateId,
403+
config: {},
404+
createTime: '2024-03-26T23:41:55.567Z',
405+
description: 'Some description'
396406
};
397407
});
398408

399409
it('gets a pipeline template version and meta given name, version and namespace', async () => {
400-
const pipelineTemplateMetaMock = {
401-
name,
402-
namespace,
403-
id: templateId
404-
};
405-
406410
templateMetaFactoryMock.get.resolves(pipelineTemplateMetaMock);
407-
datastore.get.resolves(returnValue);
411+
datastore.get.resolves(templateVersionMock);
412+
413+
const expectedTemplateVersionWithMetadata = { ...templateVersionMock, ...pipelineTemplateMetaToBeCopied };
408414

409415
const model = await factory.getWithMetadata(
410416
{
@@ -420,18 +426,17 @@ describe('PipelineTemplateVersion Factory', () => {
420426
namespace
421427
});
422428
assert.calledOnce(datastore.get);
423-
assert.isTrue(hasAllProperties(model, PipelineTemplateVersion, PipelineTemplateMeta));
429+
430+
assert.instanceOf(model, PipelineTemplateVersion);
431+
Object.keys(key => {
432+
assert.equal(model[key], expectedTemplateVersionWithMetadata[key]);
433+
});
424434
});
425435

426436
it('gets a pipeline template version and meta given templateId', async () => {
427-
const pipelineTemplateMetaMock = {
428-
name,
429-
namespace,
430-
id: templateId
431-
};
432-
433437
templateMetaFactoryMock.get.resolves(pipelineTemplateMetaMock);
434-
datastore.get.resolves(returnValue);
438+
datastore.get.resolves(templateVersionMock);
439+
const expectedTemplateVersionWithMetadata = { ...pipelineTemplateMetaToBeCopied, ...templateVersionMock };
435440

436441
const model = await factory.getWithMetadata(
437442
{
@@ -444,7 +449,10 @@ describe('PipelineTemplateVersion Factory', () => {
444449
id: templateId
445450
});
446451
assert.calledOnce(datastore.get);
447-
assert.isTrue(hasAllProperties(model, PipelineTemplateVersion, PipelineTemplateMeta));
452+
assert.instanceOf(model, PipelineTemplateVersion);
453+
Object.keys(key => {
454+
assert.equal(model[key], expectedTemplateVersionWithMetadata[key]);
455+
});
448456
});
449457

450458
it('Returns null if pipeline template does not exist', async () => {
@@ -462,6 +470,23 @@ describe('PipelineTemplateVersion Factory', () => {
462470
assert.notCalled(datastore.get);
463471
assert.isNull(model);
464472
});
473+
474+
it('Returns null if pipeline template version does not exist', async () => {
475+
templateMetaFactoryMock.get.resolves(pipelineTemplateMetaMock);
476+
datastore.get.resolves(null);
477+
478+
const model = await factory.get(
479+
{
480+
name,
481+
namespace
482+
},
483+
templateMetaFactoryMock
484+
);
485+
486+
assert.calledOnce(templateMetaFactoryMock.get);
487+
assert.calledOnce(datastore.get);
488+
assert.isNull(model);
489+
});
465490
});
466491

467492
describe('getInstance', () => {

0 commit comments

Comments
 (0)