-
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 #1306 from contentstack/feat/CS-43976
CS-43976 - added audit for workflow
- Loading branch information
Showing
21 changed files
with
12,818 additions
and
10,956 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import Entries from './entries'; | ||
import GlobalField from './global-fields'; | ||
import ContentType from './content-types'; | ||
import Workflows from './workflows'; | ||
import Extensions from './extensions'; | ||
export { Entries, GlobalField, ContentType, Extensions }; | ||
export { Entries, GlobalField, ContentType, Workflows, Extensions }; |
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,127 @@ | ||
import { join, resolve } from 'path'; | ||
import { existsSync, readFileSync, writeFileSync } from 'fs'; | ||
import { cloneDeep } from 'lodash'; | ||
import { LogFn, ConfigType, ContentTypeStruct, CtConstructorParam, ModuleConstructorParam, Workflow } from '../types'; | ||
import { ux } from '@contentstack/cli-utilities'; | ||
|
||
import auditConfig from '../config'; | ||
import { $t, auditMsg, commonMsg } from '../messages'; | ||
import { values } from 'lodash'; | ||
|
||
export default class Workflows { | ||
public log: LogFn; | ||
protected fix: boolean; | ||
public fileName: any; | ||
public config: ConfigType; | ||
public folderPath: string; | ||
public workflowSchema: Workflow[]; | ||
public ctSchema: ContentTypeStruct[]; | ||
public moduleName: keyof typeof auditConfig.moduleConfig; | ||
public ctUidSet: Set<string>; | ||
public missingCtInWorkflows: Workflow[]; | ||
public missingCts: Set<string>; | ||
public workflowPath: string; | ||
|
||
constructor({ | ||
log, | ||
fix, | ||
config, | ||
moduleName, | ||
ctSchema, | ||
}: ModuleConstructorParam & Pick<CtConstructorParam, 'ctSchema'>) { | ||
this.log = log; | ||
this.config = config; | ||
this.fix = fix ?? false; | ||
this.ctSchema = ctSchema; | ||
this.workflowSchema = []; | ||
this.moduleName = moduleName ?? 'workflows'; | ||
this.fileName = config.moduleConfig[this.moduleName].fileName; | ||
this.folderPath = resolve(config.basePath, config.moduleConfig[this.moduleName].dirName); | ||
this.ctUidSet = new Set(['$all']); | ||
this.missingCtInWorkflows = []; | ||
this.missingCts = new Set(); | ||
this.workflowPath = ''; | ||
} | ||
/** | ||
* Check whether the given path for the workflow exists or not | ||
* If path exist read | ||
* From the ctSchema add all the content type UID into ctUidSet to check whether the content-type is present or not | ||
* @returns Array of object containing the workflow name, uid and content_types that are missing | ||
*/ | ||
async run() { | ||
if (!existsSync(this.folderPath)) { | ||
this.log(`Skipping ${this.moduleName} audit`, 'warn'); | ||
this.log($t(auditMsg.NOT_VALID_PATH, { path: this.folderPath }), { color: 'yellow' }); | ||
return {}; | ||
} | ||
|
||
this.workflowPath = join(this.folderPath, this.fileName); | ||
this.workflowSchema = existsSync(this.workflowPath) ? values(JSON.parse(readFileSync(this.workflowPath, 'utf8')) as Workflow[]) : []; | ||
|
||
this.ctSchema.forEach((ct) => this.ctUidSet.add(ct.uid)); | ||
|
||
for (const workflow of this.workflowSchema) { | ||
const ctNotPresent = workflow.content_types.filter((ct) => !this.ctUidSet.has(ct)); | ||
if (ctNotPresent.length) { | ||
const tempwf = cloneDeep(workflow); | ||
tempwf.content_types = ctNotPresent; | ||
ctNotPresent.forEach((ct) => this.missingCts.add(ct)); | ||
this.missingCtInWorkflows.push(tempwf); | ||
} | ||
|
||
this.log( | ||
$t(auditMsg.SCAN_WF_SUCCESS_MSG, { | ||
name: workflow.name, | ||
module: this.config.moduleConfig[this.moduleName].name, | ||
}), | ||
'info' | ||
); | ||
} | ||
|
||
if (this.fix && this.missingCtInWorkflows.length) { | ||
await this.fixWorkflowSchema(); | ||
this.missingCtInWorkflows.forEach((wf) => (wf.fixStatus = 'Fixed')); | ||
} | ||
|
||
return this.missingCtInWorkflows; | ||
} | ||
|
||
async fixWorkflowSchema() { | ||
const newWorkflowSchema: Record<string, Workflow> = existsSync(this.workflowPath) | ||
? JSON.parse(readFileSync(this.workflowPath, 'utf8')) | ||
: {}; | ||
|
||
if (Object.keys(newWorkflowSchema).length !== 0) { | ||
for (const workflow of this.workflowSchema) { | ||
const fixedCts = workflow.content_types.filter((ct) => !this.missingCts.has(ct)); | ||
if (fixedCts.length) { | ||
newWorkflowSchema[workflow.uid].content_types = fixedCts; | ||
} else { | ||
const { name, uid } = workflow; | ||
const warningMessage = $t(commonMsg.WORKFLOW_FIX_WARN, { name, uid }); | ||
|
||
this.log(warningMessage, { color: 'yellow' }); | ||
|
||
if (this.config.flags.yes || (await ux.confirm(commonMsg.WORKFLOW_FIX_CONFIRMATION))) { | ||
delete newWorkflowSchema[workflow.uid]; | ||
} | ||
} | ||
} | ||
} | ||
|
||
await this.writeFixContent(newWorkflowSchema); | ||
} | ||
|
||
async writeFixContent(newWorkflowSchema: Record<string, Workflow>) { | ||
if ( | ||
this.fix || | ||
!(this.config.flags['copy-dir'] || this.config.flags['external-config']?.skipConfirm) && | ||
(this.config.flags.yes || (await ux.confirm(commonMsg.FIX_CONFIRMATION))) | ||
) { | ||
writeFileSync( | ||
join(this.folderPath, this.config.moduleConfig[this.moduleName].fileName), | ||
JSON.stringify(newWorkflowSchema), | ||
); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export interface Workflow { | ||
uid: string; | ||
name: string; | ||
content_types: string[]; | ||
org_uid?: string; | ||
api_key?: string; | ||
workflow_stages?: Record<string, unknown>; | ||
admin_users?: any; | ||
enabled?: boolean; | ||
deleted_at?: any; | ||
missingRefs?: any; | ||
fixStatus?:string | ||
} |
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.