From 6a34e06f0d5688dede3276b0b1ecff109920c593 Mon Sep 17 00:00:00 2001 From: Willie Ruemmele Date: Mon, 9 Sep 2024 15:53:31 -0600 Subject: [PATCH] chore: code review I --- src/Presets.md | 4 ++-- .../decomposedPermissionSetFinalizer.ts | 21 +++++++++++++++++-- .../decomposedMetadataTransformer.ts | 13 +----------- .../decomposedPermissionSetTransformer.ts | 19 ++++++++++++++--- src/convert/transformers/types.ts | 21 +++++++++++++++++++ 5 files changed, 59 insertions(+), 19 deletions(-) create mode 100644 src/convert/transformers/types.ts diff --git a/src/Presets.md b/src/Presets.md index 899691b65..8a006d7f1 100644 --- a/src/Presets.md +++ b/src/Presets.md @@ -52,9 +52,9 @@ source format Simple fields (ex: `description`, `userLicense`) remain in the top-level `PO_Manager.permissionset-meta.xml` -Entries not specific to objects remain at the top-level, grouped into files, e.g. `ClassAccess`, `PageAccess`, `UserPermissions`... +Entries not specific to object's settings remain at the top-level, grouped into files, e.g. `ClassAccess`, `PageAccess`, `UserPermissions`... -Entries specific to objects are grouped in the `objectSettings` directory and grouped into object-specific files, e.g. `PO_Line_Item__c.objectSettings`, in there you'll find entries related to `FieldPermissions`, `TabSettings`, `ObjetPermissions` and other object-specific fields. +Entries specific to object's settings are grouped in the `objectSettings` directory and grouped into object-specific files, e.g. `PO_Line_Item__c.objectSettings`, in there you'll find entries related to `FieldPermissions`, `TabSettings`, `ObjetPermissions` and other object-specific fields. ## `decomposeSharingRulesBeta` diff --git a/src/convert/convertContext/decomposedPermissionSetFinalizer.ts b/src/convert/convertContext/decomposedPermissionSetFinalizer.ts index 386549664..8b8a835b2 100644 --- a/src/convert/convertContext/decomposedPermissionSetFinalizer.ts +++ b/src/convert/convertContext/decomposedPermissionSetFinalizer.ts @@ -7,6 +7,7 @@ import { join } from 'node:path'; import { ensure, JsonMap } from '@salesforce/ts-types'; import type { PermissionSet } from '@jsforce/jsforce-node/lib/api/metadata/schema'; +import { SfError } from '@salesforce/core'; import { MetadataType } from '../../registry'; import { XML_NS_KEY, XML_NS_URL } from '../../common/constants'; import { JsToXml } from '../streams'; @@ -21,7 +22,7 @@ type PermissionSetState = { }; /** - * Merges child components that share the same object in the conversion pipeline + * Merges child components that share the same related object (/objectSettings/.objectSettings) in the conversion pipeline * into a single file. * * Inserts unclaimed child components into the parent that belongs to the default package @@ -41,7 +42,7 @@ export class DecomposedPermissionSetFinalizer extends ConvertTransactionFinalize return []; } - const fullName = Array.from(this.transactionState.permissionSetChildByPath.keys())[0].split(':')[1].split('.')[0]; + const fullName = this.getName(); return [ { @@ -61,6 +62,22 @@ export class DecomposedPermissionSetFinalizer extends ConvertTransactionFinalize }, ]; } + + private getName(): string { + let name: string; + try { + name = Array.from(this.transactionState.permissionSetChildByPath.keys())[0].split(':')[1].split('.')[0]; + } catch (e) { + throw SfError.create({ + cause: e, + message: `unable to parse name between : and . in ${ + Array.from(this.transactionState.permissionSetChildByPath.keys())[0] ?? + this.transactionState.permissionSetChildByPath.keys() + }`, + }); + } + return name; + } } /** Return a json object that's built up from the mergeMap children */ diff --git a/src/convert/transformers/decomposedMetadataTransformer.ts b/src/convert/transformers/decomposedMetadataTransformer.ts index 2bfcf7685..67b4bcc0e 100644 --- a/src/convert/transformers/decomposedMetadataTransformer.ts +++ b/src/convert/transformers/decomposedMetadataTransformer.ts @@ -23,6 +23,7 @@ import type { SourcePath } from '../../common/types'; import { ComponentSet } from '../../collections/componentSet'; import type { DecompositionState, DecompositionStateValue } from '../convertContext/decompositionFinalizer'; import { BaseMetadataTransformer } from './baseMetadataTransformer'; +import type { ComposedMetadata, ComposedMetadataWithChildType, InfoContainer } from './types'; type StateSetter = (forComponent: MetadataComponent, props: Partial>) => void; @@ -286,18 +287,6 @@ export const addChildType = (cm: Required): ComposedMetadataWi throw messages.createError('error_missing_child_type_definition', [cm.parentType.name, cm.childTypeId]); }; -export type ComposedMetadata = { tagKey: string; tagValue: AnyJson; parentType: MetadataType; childTypeId?: string }; -export type ComposedMetadataWithChildType = ComposedMetadata & { childType: MetadataType }; - -export type InfoContainer = { - entryName: string; - childComponent: MetadataComponent; - /** the parsed xml */ - value: JsonMap; - parentComponent: SourceComponent; - mergeWith?: SourceComponent; -}; - /** returns an data structure with lots of context information in it */ const toInfoContainer = (mergeWith: SourceComponent | undefined) => diff --git a/src/convert/transformers/decomposedPermissionSetTransformer.ts b/src/convert/transformers/decomposedPermissionSetTransformer.ts index 46dab5875..4b8f8ecfd 100644 --- a/src/convert/transformers/decomposedPermissionSetTransformer.ts +++ b/src/convert/transformers/decomposedPermissionSetTransformer.ts @@ -23,16 +23,15 @@ import type { DecompositionStateValue } from '../convertContext/decompositionFin import { BaseMetadataTransformer } from './baseMetadataTransformer'; import { addChildType, - ComposedMetadata, forceIgnoreAllowsComponent, getOutputFile, getWriteInfosFromMerge, getWriteInfosWithoutMerge, hasChildTypeId, - InfoContainer, setDecomposedState, tagToChildTypeId, } from './decomposedMetadataTransformer'; +import type { InfoContainer, ComposedMetadata } from './types'; type StateSetter = (forComponent: MetadataComponent, props: Partial>) => void; @@ -40,6 +39,12 @@ Messages.importMessagesDirectory(__dirname); const messages = Messages.loadMessages('@salesforce/source-deploy-retrieve', 'sdr'); export class DecomposedPermissionSetTransformer extends BaseMetadataTransformer { + /** + * Combines a decomposed Permission Set into a singular .permissonset metadata-formatted file + * + * @param {SourceComponent} component - either the parent or child of a decomposed permission set to be combined with + * @returns {Promise} will be an array with one WriteInfo in it, because they're ending in one file + */ // eslint-disable-next-line @typescript-eslint/require-await public async toMetadataFormat(component: SourceComponent): Promise { // only need to do this once @@ -65,6 +70,14 @@ export class DecomposedPermissionSetTransformer extends BaseMetadataTransformer // noop since the finalizer will push the writes to the component writer return []; } + + /** + * will decomopse a .permissionset into a directory containing files, and an 'objectSettings' folder for object-specific settings + * + * @param {SourceComponent} component A SourceComponent representing a metadata-formatted permission set + * @param {SourceComponent | undefined} mergeWith any existing source-formatted permission sets to be merged with, think existing source merging with new information from a retrieve + * @returns {Promise} Will contain file content information, and file paths + */ public async toSourceFormat({ component, mergeWith }: ToSourceFormatInput): Promise { const forceIgnore = component.getForceIgnore(); @@ -228,7 +241,7 @@ const getAndCombineChildWriteInfos = ( return writeInfos; }; -/** returns a data structure with lots of context information in it */ +/** returns a data structure with lots of context information in it - this is also where the name of the file/component is calculated */ const toInfoContainer = (mergeWith: SourceComponent | undefined) => (parent: SourceComponent) => diff --git a/src/convert/transformers/types.ts b/src/convert/transformers/types.ts new file mode 100644 index 000000000..a58208a0b --- /dev/null +++ b/src/convert/transformers/types.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2023, salesforce.com, inc. + * All rights reserved. + * Licensed under the BSD 3-Clause license. + * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause + */ +import { AnyJson, JsonMap } from '@salesforce/ts-types'; +import { MetadataType } from '../../registry'; +import { MetadataComponent, SourceComponent } from '../../resolve'; + +export type ComposedMetadata = { tagKey: string; tagValue: AnyJson; parentType: MetadataType; childTypeId?: string }; +export type ComposedMetadataWithChildType = ComposedMetadata & { childType: MetadataType }; + +export type InfoContainer = { + entryName: string; + childComponent: MetadataComponent; + /** the parsed xml */ + value: JsonMap; + parentComponent: SourceComponent; + mergeWith?: SourceComponent; +};