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

fix: PRO-615 push pull tag components #114

Merged
merged 2 commits into from
Sep 12, 2024
Merged
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
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
Loading