-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #956 from contentstack/feat/CS-39984
Workflows export & import rewrite and export unit test cases fix
- Loading branch information
Showing
9 changed files
with
391 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
packages/contentstack-export/src/export/modules/workflows.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import omit from 'lodash/omit'; | ||
import isEmpty from 'lodash/isEmpty'; | ||
import { resolve as pResolve } from 'node:path'; | ||
|
||
import config from '../../config'; | ||
import BaseClass from './base-class'; | ||
import { log, formatError, fsUtil } from '../../utils'; | ||
import { WorkflowConfig, ModuleClassParams } from '../../types'; | ||
|
||
export default class ExportWorkFlows extends BaseClass { | ||
private workflows: Record<string, Record<string, string>>; | ||
private workflowConfig: WorkflowConfig; | ||
public webhooksFolderPath: string; | ||
private qs: { | ||
include_count: boolean; | ||
skip?: number; | ||
}; | ||
|
||
constructor({ exportConfig, stackAPIClient }: ModuleClassParams) { | ||
super({ exportConfig, stackAPIClient }); | ||
this.workflows = {}; | ||
this.workflowConfig = config.modules.workflows; | ||
this.qs = { include_count: true }; | ||
} | ||
|
||
async start(): Promise<void> { | ||
log(this.exportConfig, 'Starting workflows export', 'info'); | ||
|
||
this.webhooksFolderPath = pResolve( | ||
this.exportConfig.data, | ||
this.exportConfig.branchName || '', | ||
this.workflowConfig.dirName, | ||
); | ||
|
||
await fsUtil.makeDirectory(this.webhooksFolderPath); | ||
await this.getWorkflows(); | ||
|
||
if (this.workflows === undefined || isEmpty(this.workflows)) { | ||
log(this.exportConfig, 'No workflows found', 'info'); | ||
} else { | ||
fsUtil.writeFile(pResolve(this.webhooksFolderPath, this.workflowConfig.fileName), this.workflows); | ||
log(this.exportConfig, 'All the workflows have been exported successfully!', 'success'); | ||
} | ||
} | ||
|
||
async getWorkflows(skip = 0): Promise<void> { | ||
if (skip) { | ||
this.qs.skip = skip; | ||
} | ||
|
||
await this.stack | ||
.workflow() | ||
.fetchAll(this.qs) | ||
.then(async (data: any) => { | ||
const { items, count } = data; | ||
if (items?.length) { | ||
await this.sanitizeAttribs(items); | ||
skip += this.workflowConfig.limit || 100; | ||
if (skip >= count) { | ||
return; | ||
} | ||
return await this.getWorkflows(skip); | ||
} | ||
}) | ||
.catch(({ error }: any) => { | ||
log(this.exportConfig, `Failed to export workflows.${formatError(error)}`, 'error'); | ||
log(this.exportConfig, error, 'error'); | ||
}); | ||
} | ||
|
||
async sanitizeAttribs(workflows: Record<string, string>[]) { | ||
for (let index = 0; index < workflows?.length; index++) { | ||
await this.getWorkflowRoles(workflows[index]); | ||
const workflowUid = workflows[index].uid; | ||
const workflowName = workflows[index]?.name || ''; | ||
this.workflows[workflowUid] = omit(workflows[index], this.workflowConfig.invalidKeys); | ||
log(this.exportConfig, `'${workflowName}' workflow was exported successfully`, 'success'); | ||
} | ||
} | ||
|
||
async getWorkflowRoles(workflow: Record<string, any>) { | ||
for (const stage of workflow?.workflow_stages) { | ||
for (let i = 0; i < stage?.SYS_ACL?.roles?.uids?.length; i++) { | ||
const roleUid = stage.SYS_ACL.roles.uids[i]; | ||
const roleData = await this.getRoles(roleUid); | ||
stage.SYS_ACL.roles.uids[i] = roleData; | ||
} | ||
} | ||
} | ||
|
||
async getRoles(roleUid: number): Promise<any> { | ||
return await this.stack | ||
.role(roleUid) | ||
.fetch({ include_rules: true, include_permissions: true }) | ||
.then((data: any) => data) | ||
.catch((err: any) => | ||
log(this.exportConfig, `Failed to fetch roles.${formatError(err)}`, 'error'), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.