Skip to content

Commit

Permalink
Merge pull request #102 from storyblok/bugfix/support-sync-component-…
Browse files Browse the repository at this point in the history
…tags

[PRO-284] fix support sync component internal tags
  • Loading branch information
Christian Zoppi authored Jul 4, 2024
2 parents 65b31e4 + 3b88fd0 commit a81c099
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 3 deletions.
23 changes: 23 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug Jest Tests",
"runtimeArgs": [
"--experimental-vm-modules"
],
"args": [
"--silent",
"--runInBand"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"windows": {
"program": "${workspaceFolder}\\node_modules\\jest\\bin\\jest.js"
}
}
]
}
50 changes: 48 additions & 2 deletions src/tasks/sync-commands/components.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,35 @@ class SyncComponents {
component.component_group_uuid = targetGroupData.uuid
}

const { internal_tags_list, internal_tag_ids, ...rest } = component;
const existingTags = await this.getSpaceInternalTags(this.targetSpaceId);

let processedInternalTagsIds = [];
if(internal_tags_list.length > 0) {
await internal_tags_list.forEach(async (tag) => {
const existingTag = existingTags.find(({ name }) => tag.name === name);
if(!existingTag) {
try {
const response = await this.createComponentInternalTag(this.targetSpaceId, tag);
processedInternalTagsIds.push(response.id);
} catch (e) {
console.error(chalk.red("X") + ` Internal tag ${tag} creation failed: ${e.message}`);
}
} else {
processedInternalTagsIds.push(existingTag.id);
}
})
}

// Create new component on target space
const componentData = {
...rest,
internal_tag_ids: processedInternalTagsIds || internal_tag_ids
}
try {
const componentCreated = await this.createComponent(
this.targetSpaceId,
component
componentData
)

console.log(chalk.green('✓') + ` Component ${component.name} created`)
Expand All @@ -124,7 +148,7 @@ class SyncComponents {
await this.updateComponent(
this.targetSpaceId,
componentTarget.id,
component,
componentData,
componentTarget
)
console.log(chalk.green('✓') + ` Component ${component.name} synced`)
Expand Down Expand Up @@ -196,9 +220,16 @@ class SyncComponents {
targetComponentData
)
}
// Unfortunatelly, the internal_tag_ids is not recursive and bot being merged correctly
payload.component.internal_tag_ids = sourceComponentData.internal_tag_ids
return this
.client
.put(`spaces/${spaceId}/components/${componentId}`, payload)
.then(response => {
const component = response.data.component || {}

return component
}).catch(error => Promise.reject(error))
}

mergeComponents (sourceComponent, targetComponent = {}) {
Expand Down Expand Up @@ -263,6 +294,21 @@ class SyncComponents {
return targetGroupData.uuid
})
}

getSpaceInternalTags(spaceId) {
return this.client.get(`spaces/${spaceId}/internal_tags`).then((response) => response.data.internal_tags || []);
}

createComponentInternalTag(spaceId, tag) {
return this.client.post(`spaces/${spaceId}/internal_tags`, {
internal_tag: {
name: tag.name,
object_type: "component"
}
})
.then((response) => response.data.internal_tag || {})
.catch((error) => Promise.reject(error));
}
}

export default SyncComponents
10 changes: 10 additions & 0 deletions tests/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export const FAKE_COMPONENTS = () => [
preview_tmpl: null,
is_nestable: true,
all_presets: [],
internal_tag_ids: [],
internal_tags_list: [],
preset_id: null,
real_name: 'teaser',
component_group_uuid: null
Expand Down Expand Up @@ -56,6 +58,8 @@ export const FAKE_COMPONENTS = () => [
preview_tmpl: null,
is_nestable: true,
all_presets: [],
internal_tag_ids: [],
internal_tags_list: [],
preset_id: null,
real_name: 'feature',
component_group_uuid: null
Expand All @@ -75,6 +79,8 @@ export const FAKE_COMPONENTS = () => [
preview_tmpl: null,
is_nestable: true,
all_presets: [],
internal_tag_ids: [],
internal_tags_list: [],
preset_id: null,
real_name: 'logo',
component_group_uuid: '529cc32a-1d97-4b4a-b0b6-28e33dc56c0d'
Expand All @@ -101,6 +107,8 @@ export const FAKE_COMPONENTS = () => [
preview_tmpl: null,
is_nestable: true,
all_presets: [],
internal_tag_ids: [],
internal_tags_list: [],
preset_id: null,
real_name: 'blocks',
component_group_uuid: null
Expand Down Expand Up @@ -132,6 +140,8 @@ export const FAKE_COMPONENTS = () => [
image: null
}
],
internal_tag_ids: [],
internal_tags_list: [],
preset_id: null,
real_name: 'hero',
component_group_uuid: null
Expand Down
44 changes: 43 additions & 1 deletion tests/units/sync-components.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ const FAKE_COMPONENTS_TO_TEST = {
preview_tmpl: null,
is_nestable: true,
all_presets: [],
internal_tag_ids: [],
internal_tags_list: [],
preset_id: null,
real_name: 'feature',
component_group_uuid: null
Expand All @@ -62,10 +64,24 @@ const FAKE_COMPONENTS_TO_TEST = {
preview_tmpl: null,
is_nestable: true,
all_presets: [],
internal_tag_ids: [],
internal_tags_list: [],
preset_id: null,
real_name: 'teaser',
component_group_uuid: null
}
],
internal_tags: [
{
id: 1,
name: 'tag1',
object_type: 'component',
},
{
id: 2,
name: 'tag2',
object_type: 'component',
}
]
}
}
Expand Down Expand Up @@ -132,6 +148,14 @@ const mockGetRequest = (path) => {
return Promise.resolve(FAKE_COMPONENTS_TO_TEST[extractSpace(path)])
}

if (
path === 'spaces/001/internal_tags' ||
path === 'spaces/002/internal_tags'
) {
return Promise.resolve(FAKE_COMPONENTS_TO_TEST[extractSpace(path)])
}


if (path === 'spaces/001/presets') {
return Promise.resolve(FAKE_PRESETS[extractSpace(path)])
}
Expand Down Expand Up @@ -175,7 +199,13 @@ const mockPostRequest = (path, payload) => {
}

const mockPutRequest = (path, payload) => {
return Promise.resolve(true)
return Promise.resolve({
data: {
component: {
...payload.component,
}
}
})
}

const spyGet = jest.spyOn(Storyblok.prototype, 'get').mockImplementation(mockGetRequest)
Expand Down Expand Up @@ -209,6 +239,14 @@ describe('testing syncComponents', () => {
})
})

afterAll(() => {
spyGet.mockClear()
spyPost.mockClear()
spyPut.mockClear()
spyGetPresets.mockClear()
spyCreatePresets.mockClear()
})

it('shoud be get the all data correctly', () => {
// it must be get each component group
expect(spyGet).toHaveBeenCalledWith('spaces/001/component_groups')
Expand Down Expand Up @@ -245,6 +283,8 @@ describe('testing syncComponents', () => {
display_name: null,
id: 0,
image: null,
internal_tag_ids: [],
internal_tags_list: [],
is_nestable: true,
is_root: false,
name: 'teaser',
Expand Down Expand Up @@ -303,6 +343,8 @@ describe('testing syncComponents', () => {
preview_tmpl: null,
is_nestable: true,
all_presets: [],
internal_tag_ids: [],
internal_tags_list: [],
preset_id: null,
real_name: 'logo',
component_group_uuid: '000000002'
Expand Down

0 comments on commit a81c099

Please sign in to comment.