import { CallExpressionShape, Plugin, type Record, type Context } from '@servicenow/sdk-build-core' import { ModuleFunctionShape } from './server-module-plugin' import { NowIdShape } from './now-id-plugin' import { NowIncludeShape } from './now-include-plugin' const SCAN_COLUMN_TYPE_CHECK = 'scan_column_type_check' const SCAN_LINTER_CHECK = 'scan_linter_check' const SCAN_SCRIPT_ONLY_CHECK = 'scan_script_only_check' const SCAN_TABLE_CHECK = 'scan_table_check' export const InstanceScanPlugin = Plugin.create({ name: 'InstanceScanPlugin', records: { [SCAN_COLUMN_TYPE_CHECK]: { getUpdateName(record: Record, _context: Context) { return { success: true, value: `${SCAN_COLUMN_TYPE_CHECK}_${record.getId().getValue()}` } }, async toShape(record, { transform }) { const script = await NowIncludeShape.fromRecord(record, record.get('script'), transform) return { success: true, value: new CallExpressionShape({ source: record, callee: 'ColumnTypeCheck', args: [ record.transform(({ $ }) => ({ $id: $.val(NowIdShape.from(record)), name: $, active: $.toBoolean().def(true), category: $, priority: $, script: $.val(script).def(''), shortDescription: $.from('short_description'), description: $.def(''), resolutionDetails: $.from('resolution_details').def(''), documentationUrl: $.from('documentation_url').def(''), scoreMin: $.from('score_min').toNumber().def(0), scoreMax: $.from('score_max').toNumber().def(100), scoreScale: $.from('score_scale').toNumber().def(1), runCondition: $.from('run_condition').def(''), findingType: $.from('finding_type').def('scan_finding'), columnType: $.from('column_type'), })), ], }), } }, }, [SCAN_LINTER_CHECK]: { getUpdateName(record: Record, _context: Context) { return { success: true, value: `${SCAN_LINTER_CHECK}_${record.getId().getValue()}` } }, async toShape(record, { transform }) { const script = await NowIncludeShape.fromRecord(record, record.get('script'), transform) return { success: true, value: new CallExpressionShape({ source: record, callee: 'LinterCheck', args: [ record.transform(({ $ }) => ({ $id: $.val(NowIdShape.from(record)), name: $, active: $.toBoolean().def(true), category: $, priority: $, script: $.val(script).def(''), shortDescription: $.from('short_description'), description: $.def(''), resolutionDetails: $.from('resolution_details').def(''), documentationUrl: $.from('documentation_url').def(''), scoreMin: $.from('score_min').toNumber().def(0), scoreMax: $.from('score_max').toNumber().def(100), scoreScale: $.from('score_scale').toNumber().def(1), runCondition: $.from('run_condition').def(''), findingType: $.from('finding_type').def('scan_finding'), })), ], }), } }, }, [SCAN_SCRIPT_ONLY_CHECK]: { getUpdateName(record: Record, _context: Context) { return { success: true, value: `${SCAN_SCRIPT_ONLY_CHECK}_${record.getId().getValue()}` } }, async toShape(record, { transform }) { const script = await NowIncludeShape.fromRecord(record, record.get('script'), transform) return { success: true, value: new CallExpressionShape({ source: record, callee: 'ScriptOnlyCheck', args: [ record.transform(({ $ }) => ({ $id: $.val(NowIdShape.from(record)), name: $, active: $.toBoolean().def(true), category: $, priority: $, script: $.val(script).def(''), shortDescription: $.from('short_description'), description: $.def(''), resolutionDetails: $.from('resolution_details').def(''), documentationUrl: $.from('documentation_url').def(''), scoreMin: $.from('score_min').toNumber().def(0), scoreMax: $.from('score_max').toNumber().def(100), scoreScale: $.from('score_scale').toNumber().def(1), runCondition: $.from('run_condition').def(''), findingType: $.from('finding_type').def('scan_finding'), })), ], }), } }, }, [SCAN_TABLE_CHECK]: { getUpdateName(record: Record, _context: Context) { return { success: true, value: `${SCAN_TABLE_CHECK}_${record.getId().getValue()}` } }, async toShape(record, { transform }) { const script = await NowIncludeShape.fromRecord(record, record.get('script'), transform) return { success: true, value: new CallExpressionShape({ source: record, callee: 'TableCheck', args: [ record.transform(({ $ }) => ({ $id: $.val(NowIdShape.from(record)), name: $, active: $.toBoolean().def(true), category: $, priority: $, script: $.val(script).def(''), shortDescription: $.from('short_description'), description: $.def(''), resolutionDetails: $.from('resolution_details').def(''), documentationUrl: $.from('documentation_url').def(''), scoreMin: $.from('score_min').toNumber().def(0), scoreMax: $.from('score_max').toNumber().def(100), scoreScale: $.from('score_scale').toNumber().def(1), runCondition: $.from('run_condition').def(''), findingType: $.from('finding_type').def('scan_finding'), table: $, advanced: $.toBoolean().def(false), conditions: $.def(''), useManifest: $.from('use_manifest').toBoolean().def(false), })), ], }), } }, }, }, shapes: [ { shape: CallExpressionShape, fileTypes: ['fluent'], async toRecord(source, { factory }) { if (source.getCallee() === 'ColumnTypeCheck') { const arg = source.getArgument(0).asObject() const record = await factory.createRecord({ source, table: SCAN_COLUMN_TYPE_CHECK, explicitId: arg.get('$id'), properties: arg.transform(({ $ }) => ({ name: $, active: $.toBoolean().def(true), category: $, priority: $, script: $.map( (v) => v .if(ModuleFunctionShape) ?.toString((n) => `${n}({{PARAMS}})`, ['engine', 'current', 'columnValue']) ?? v ) .toCdata() .def(''), short_description: $.from('shortDescription'), description: $.def(''), resolution_details: $.from('resolutionDetails').def(''), documentation_url: $.from('documentationUrl').def(''), score_min: $.from('scoreMin').toNumber().def(0), score_max: $.from('scoreMax').toNumber().def(100), score_scale: $.from('scoreScale').toNumber().def(1), run_condition: $.from('runCondition').def(''), finding_type: $.from('findingType').def('scan_finding'), column_type: $.from('columnType'), })), }) return { success: true, value: record } } if (source.getCallee() === 'LinterCheck') { const arg = source.getArgument(0).asObject() const record = await factory.createRecord({ source, table: SCAN_LINTER_CHECK, explicitId: arg.get('$id'), properties: arg.transform(({ $ }) => ({ name: $, active: $.toBoolean().def(true), category: $, priority: $, script: $.map( (v) => v .if(ModuleFunctionShape) ?.toString((n) => `${n}({{PARAMS}})`, ['engine', 'current', 'rootNode']) ?? v ) .toCdata() .def(''), short_description: $.from('shortDescription'), description: $.def(''), resolution_details: $.from('resolutionDetails').def(''), documentation_url: $.from('documentationUrl').def(''), score_min: $.from('scoreMin').toNumber().def(0), score_max: $.from('scoreMax').toNumber().def(100), score_scale: $.from('scoreScale').toNumber().def(1), run_condition: $.from('runCondition').def(''), finding_type: $.from('findingType').def('scan_finding'), })), }) return { success: true, value: record } } if (source.getCallee() === 'ScriptOnlyCheck') { const arg = source.getArgument(0).asObject() const record = await factory.createRecord({ source, table: SCAN_SCRIPT_ONLY_CHECK, explicitId: arg.get('$id'), properties: arg.transform(({ $ }) => ({ name: $, active: $.toBoolean().def(true), category: $, priority: $, script: $.map( (v) => v.if(ModuleFunctionShape)?.toString((n) => `${n}({{PARAMS}})`, ['engine']) ?? v ) .toCdata() .def(''), short_description: $.from('shortDescription'), description: $.def(''), resolution_details: $.from('resolutionDetails').def(''), documentation_url: $.from('documentationUrl').def(''), score_min: $.from('scoreMin').toNumber().def(0), score_max: $.from('scoreMax').toNumber().def(100), score_scale: $.from('scoreScale').toNumber().def(1), run_condition: $.from('runCondition').def(''), finding_type: $.from('findingType').def('scan_finding'), })), }) return { success: true, value: record } } if (source.getCallee() === 'TableCheck') { const arg = source.getArgument(0).asObject() const record = await factory.createRecord({ source, table: SCAN_TABLE_CHECK, explicitId: arg.get('$id'), properties: arg.transform(({ $ }) => ({ name: $, active: $.toBoolean().def(true), category: $, priority: $, script: $.map( (v) => v .if(ModuleFunctionShape) ?.toString((n) => `${n}({{PARAMS}})`, ['engine', 'current']) ?? v ) .toCdata() .def(''), short_description: $.from('shortDescription'), description: $.def(''), resolution_details: $.from('resolutionDetails').def(''), documentation_url: $.from('documentationUrl').def(''), score_min: $.from('scoreMin').toNumber().def(0), score_max: $.from('scoreMax').toNumber().def(100), score_scale: $.from('scoreScale').toNumber().def(1), run_condition: $.from('runCondition').def(''), finding_type: $.from('findingType').def('scan_finding'), table: $, advanced: $.toBoolean().def(false), conditions: $.def(''), use_manifest: $.from('useManifest').toBoolean().def(false), })), }) return { success: true, value: record } } return { success: false } }, }, ], })