Skip to content

Commit

Permalink
Merge pull request #114 from storyblok/bugfix/PRO-615-push-pull-tag-c…
Browse files Browse the repository at this point in the history
…omponents

fix: PRO-615 push pull tag components
  • Loading branch information
alvarosabu authored Sep 12, 2024
2 parents 3016650 + 1c9d51f commit 5f3f2b1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
11 changes: 11 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@
"windows": {
"program": "${workspaceFolder}\\node_modules\\jest\\bin\\jest.js"
}
},
{
"type": "node",
"request": "launch",
"name": "Debug pull-components",
"program": "${workspaceFolder}/dist/cli.mjs",
"args": ["push-components", "components.295017.json", "--space", "295018"],
"cwd": "${workspaceFolder}",
"console": "integratedTerminal",
"sourceMaps": true,
"outFiles": ["${workspaceFolder}/dist/**/*.js"]
}
]
}
42 changes: 41 additions & 1 deletion src/tasks/push-components.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,21 @@ const createContentList = (content, key) => {
else return !isEmpty(content) ? [content] : []
}

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

const createComponentInternalTag =(client, spaceId, tag) => {
return 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));
}

const pushComponents = async (api, { source, presetsSource }) => {
try {
const rawComponents = await getDataFromPath(source)
Expand Down Expand Up @@ -133,7 +148,9 @@ const pushComponentsGroups = async (api, group) => {
}

const push = async (api, components, componentsGroups = [], presets = []) => {
const presetsLib = new PresetsLib({ oauthToken: api.accessToken, targetSpaceId: api.spaceId })
const targetSpaceId = api.spaceId
const presetsLib = new PresetsLib({ oauthToken: api.accessToken, targetSpaceId, })
const apiClient = api.getClient()
try {
const componentGroupsTree = buildComponentsGroupsTree(componentsGroups)

Expand All @@ -157,6 +174,29 @@ const push = async (api, components, componentsGroups = [], presets = []) => {
delete components[i].component_group_name
}

const { internal_tags_list, internal_tag_ids } = components[i];
const existingTags = await getSpaceInternalTags(apiClient, 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 createComponentInternalTag(apiClient, 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);
}
})
}

components[i].internal_tag_ids = processedInternalTagsIds || internal_tag_ids;


const schema = components[i].schema
if (schema) {
Object.keys(schema).forEach(field => {
Expand Down

0 comments on commit 5f3f2b1

Please sign in to comment.