Skip to content

Commit

Permalink
feat(2862): Set templateType filter while fetching template tag from …
Browse files Browse the repository at this point in the history
…DB (#586)
  • Loading branch information
sagar1312 authored Aug 23, 2023
1 parent 1f25738 commit 48fceb6
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 28 deletions.
25 changes: 22 additions & 3 deletions lib/templateTagFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ class TemplateTagFactory extends BaseFactory {
.list({
params: {
namespace: 'default',
name: parsedTemplate.name
name: parsedTemplate.name,
templateType: this.getTemplateType()
}
})
.then(namespaceExists => {
Expand All @@ -77,7 +78,8 @@ class TemplateTagFactory extends BaseFactory {
.list({
params: {
namespace,
name
name,
templateType: this.getTemplateType()
}
})
.then(namespaceExists => {
Expand All @@ -90,6 +92,16 @@ class TemplateTagFactory extends BaseFactory {
});
}

getTemplateType() {
return this._getTemplateType();
}

_getTemplateType() {
// Note: To keep it backward compatible, returning 'JOB'.
// TODO: After PR screwdriver-cd/models#585 is merged https://github.com/screwdriver-cd/models/pull/585 and the code refactored to use jobTemplateTagFactory, this needs to be updated
return 'JOB';
}

/**
* Get an instance of the TemplateTagFactory
* @method getInstance
Expand Down Expand Up @@ -133,7 +145,12 @@ class TemplateTagFactory extends BaseFactory {
* @return {Promise}
*/
list(config) {
if (config.params && config.params.name && !config.params.namespace) {
if (!config.params) {
config.params = {};
}
config.params.templateType = this.getTemplateType();

if (config.params.name && !config.params.namespace) {
// eslint-disable-next-line no-underscore-dangle
return this._getNameAndNamespace(config.params.name).then(parsedTemplateName => {
const { namespace, name } = parsedTemplateName;
Expand All @@ -160,6 +177,8 @@ class TemplateTagFactory extends BaseFactory {
* @return {Promise}
*/
get(config) {
config.templateType = this.getTemplateType();

if (config.name && !config.namespace) {
// eslint-disable-next-line no-underscore-dangle
return this._getNameAndNamespace(config.name).then(parsedTemplateName => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"js-yaml": "^4.1.0",
"lodash": "^4.17.21",
"screwdriver-config-parser": "^8.0.0",
"screwdriver-data-schema": "^22.3.0",
"screwdriver-data-schema": "^22.6.1",
"screwdriver-logger": "^2.0.0",
"screwdriver-workflow-parser": "^4.0.0"
}
Expand Down
67 changes: 43 additions & 24 deletions test/lib/templateTagFactory.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,10 @@ describe('TemplateTag Factory', () => {
expected = { ...expected };

return factory.get(config).then(model => {
assert.calledWith(
datastore.get,
sinon.match({
params: { name, namespace: 'default', tag }
})
);
assert.calledWith(datastore.get, {
table: 'templateTags',
params: { name, namespace: 'default', tag, templateType: 'JOB' }
});
assert.instanceOf(model, TemplateTag);
Object.keys(expected).forEach(key => {
assert.strictEqual(model[key], expected[key]);
Expand All @@ -237,12 +235,10 @@ describe('TemplateTag Factory', () => {
config.name = fullTemplateName;

return factory.get(config).then(model => {
assert.calledWith(
datastore.get,
sinon.match({
params: { name: fullTemplateName, namespace: null, tag }
})
);
assert.calledWith(datastore.get, {
table: 'templateTags',
params: { name: fullTemplateName, namespace: null, tag, templateType: 'JOB' }
});
assert.instanceOf(model, TemplateTag);
Object.keys(expected).forEach(key => {
assert.strictEqual(model[key], expected[key]);
Expand All @@ -257,12 +253,10 @@ describe('TemplateTag Factory', () => {
config.name = fullTemplateName;

return factory.get(config).then(model => {
assert.calledWith(
datastore.get,
sinon.match({
params: { name, namespace, tag }
})
);
assert.calledWith(datastore.get, {
table: 'templateTags',
params: { name, namespace, tag, templateType: 'JOB' }
});
assert.instanceOf(model, TemplateTag);
Object.keys(expected).forEach(key => {
assert.strictEqual(model[key], expected[key]);
Expand All @@ -277,12 +271,10 @@ describe('TemplateTag Factory', () => {
config.namespace = namespace;

return factory.get(config).then(model => {
assert.calledWith(
datastore.get,
sinon.match({
params: { name, namespace, tag }
})
);
assert.calledWith(datastore.get, {
table: 'templateTags',
params: { name, namespace, tag, templateType: 'JOB' }
});
assert.instanceOf(model, TemplateTag);
Object.keys(expected).forEach(key => {
assert.strictEqual(model[key], expected[key]);
Expand Down Expand Up @@ -318,6 +310,15 @@ describe('TemplateTag Factory', () => {

return factory.list(config).then(model => {
assert.instanceOf(model[0], TemplateTag);
assert.callCount(datastore.scan, 2);
assert.calledWith(datastore.scan.firstCall, {
table: 'templateTags',
params: { name, namespace: 'default', templateType: 'JOB' }
});
assert.calledWith(datastore.scan.secondCall, {
table: 'templateTags',
params: { name, namespace: 'default', templateType: 'JOB' }
});
});
});

Expand All @@ -329,6 +330,16 @@ describe('TemplateTag Factory', () => {

return factory.list(config).then(model => {
assert.instanceOf(model[0], TemplateTag);

assert.callCount(datastore.scan, 2);
assert.calledWith(datastore.scan.firstCall, {
table: 'templateTags',
params: { name, namespace: 'default', templateType: 'JOB' }
});
assert.calledWith(datastore.scan.secondCall, {
table: 'templateTags',
params: { name, templateType: 'JOB' }
});
});
});

Expand All @@ -339,6 +350,10 @@ describe('TemplateTag Factory', () => {

return factory.list(config).then(model => {
assert.instanceOf(model[0], TemplateTag);
assert.calledWith(datastore.scan, {
table: 'templateTags',
params: { name, namespace, templateType: 'JOB' }
});
});
});

Expand All @@ -348,6 +363,10 @@ describe('TemplateTag Factory', () => {

return factory.list(config).then(model => {
assert.instanceOf(model[0], TemplateTag);
assert.calledWith(datastore.scan, {
table: 'templateTags',
params: { name, namespace, templateType: 'JOB' }
});
});
});
});
Expand Down

0 comments on commit 48fceb6

Please sign in to comment.