-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SALTO-4607: Add Author Information on Salesforce Sub-Instances (#4828)
- Loading branch information
Showing
6 changed files
with
207 additions
and
179 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
96 changes: 96 additions & 0 deletions
96
packages/salesforce-adapter/src/filters/author_information/nested_instances.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,96 @@ | ||
/* | ||
* Copyright 2023 Salto Labs Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { Element } from '@salto-io/adapter-api' | ||
import { logger } from '@salto-io/logging' | ||
import _ from 'lodash' | ||
import { RemoteFilterCreator } from '../../filter' | ||
import { apiNameSync, ensureSafeFilterFetch, isMetadataInstanceElementSync } from '../utils' | ||
import { WORKFLOW_FIELD_TO_TYPE } from '../workflow' | ||
import { NESTED_INSTANCE_VALUE_TO_TYPE_NAME } from '../custom_objects_to_object_type' | ||
import { getAuthorAnnotations, MetadataInstanceElement } from '../../transformers/transformer' | ||
import SalesforceClient from '../../client/client' | ||
|
||
const log = logger(module) | ||
|
||
export const WARNING_MESSAGE = 'Encountered an error while trying to populate author information in some of the Salesforce configuration elements.' | ||
|
||
const NESTED_INSTANCES_METADATA_TYPES = [ | ||
'CustomLabel', | ||
'AssignmentRule', | ||
'AutoResponseRule', | ||
'EscalationRule', | ||
'MatchingRule', | ||
...Object.keys(WORKFLOW_FIELD_TO_TYPE), | ||
...Object.keys(NESTED_INSTANCE_VALUE_TO_TYPE_NAME), | ||
] as const | ||
|
||
type NestedInstanceMetadataType = typeof NESTED_INSTANCES_METADATA_TYPES[number] | ||
|
||
|
||
type SetAuthorInformationForTypeParams = { | ||
client: SalesforceClient | ||
typeName: NestedInstanceMetadataType | ||
instances: MetadataInstanceElement[] | ||
} | ||
|
||
const setAuthorInformationForInstancesOfType = async ( | ||
{ | ||
client, typeName, instances, | ||
}: SetAuthorInformationForTypeParams): Promise<void> => { | ||
const { result: filesProps } = await client.listMetadataObjects([{ type: typeName }]) | ||
const filePropsByFullName = _.keyBy(filesProps, props => props.fullName) | ||
const instancesWithMissingFileProps: MetadataInstanceElement[] = [] | ||
instances.forEach(instance => { | ||
const instanceFullName = apiNameSync(instance) | ||
if (instanceFullName === undefined) { | ||
return | ||
} | ||
const fileProps = filePropsByFullName[instanceFullName] | ||
if (fileProps === undefined) { | ||
instancesWithMissingFileProps.push(instance) | ||
return | ||
} | ||
Object.assign(instance.annotations, getAuthorAnnotations(fileProps)) | ||
}) | ||
if (instancesWithMissingFileProps.length > 0) { | ||
log.debug(`Failed to populate author information for the following ${typeName} instances: ${instancesWithMissingFileProps.map(instance => apiNameSync(instance)).join(', ')}`) | ||
} | ||
} | ||
|
||
/* | ||
* add author information on nested instances | ||
*/ | ||
const filterCreator: RemoteFilterCreator = ({ client, config }) => ({ | ||
name: 'nestedInstancesAuthorFilter', | ||
remote: true, | ||
onFetch: ensureSafeFilterFetch({ | ||
warningMessage: WARNING_MESSAGE, | ||
config, | ||
filterName: 'authorInformation', | ||
fetchFilterFunc: async (elements: Element[]) => { | ||
const instancesByType = _.groupBy( | ||
elements.filter(isMetadataInstanceElementSync), | ||
e => apiNameSync(e.getTypeSync()) | ||
) | ||
await Promise.all(Object.entries(instancesByType) | ||
.map(([typeName, instances]) => ( | ||
setAuthorInformationForInstancesOfType({ client, typeName, instances }) | ||
))) | ||
}, | ||
}), | ||
}) | ||
|
||
export default filterCreator |
80 changes: 0 additions & 80 deletions
80
packages/salesforce-adapter/src/filters/author_information/validation_rules.ts
This file was deleted.
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
64 changes: 64 additions & 0 deletions
64
packages/salesforce-adapter/test/filters/author_information/nested_instances.test.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,64 @@ | ||
/* | ||
* Copyright 2023 Salto Labs Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { CORE_ANNOTATIONS, InstanceElement } from '@salto-io/adapter-api' | ||
import { mockTypes } from '../../mock_elements' | ||
import { CUSTOM_LABEL_METADATA_TYPE, INSTANCE_FULL_NAME_FIELD } from '../../../src/constants' | ||
import { mockFileProperties } from '../../connection' | ||
import mockClient from '../../client' | ||
import filterCreator from '../../../src/filters/author_information/nested_instances' | ||
import { FilterWith } from '../mocks' | ||
import { defaultFilterContext } from '../../utils' | ||
|
||
describe('nestedInstancesAuthorInformationFilter', () => { | ||
const CREATED_BY_NAME = 'Test User' | ||
const CREATED_DATE = '2021-01-01T00:00:00.000Z' | ||
const LAST_MODIFIED_BY_NAME = 'Test User 2' | ||
const LAST_MODIFIED_DATE = '2021-01-02T00:00:00.000Z' | ||
|
||
let customLabelInstance: InstanceElement | ||
let filter: FilterWith<'onFetch'> | ||
describe('onFetch', () => { | ||
beforeEach(() => { | ||
customLabelInstance = new InstanceElement( | ||
'TestCustomLabel', | ||
mockTypes.CustomLabel, | ||
{ | ||
[INSTANCE_FULL_NAME_FIELD]: 'TestCustomLabel', | ||
}, | ||
) | ||
const fileProperties = mockFileProperties({ | ||
fullName: 'TestCustomLabel', | ||
type: CUSTOM_LABEL_METADATA_TYPE, | ||
createdByName: CREATED_BY_NAME, | ||
createdDate: CREATED_DATE, | ||
lastModifiedByName: LAST_MODIFIED_BY_NAME, | ||
lastModifiedDate: LAST_MODIFIED_DATE, | ||
}) | ||
const { client, connection } = mockClient() | ||
connection.metadata.list.mockResolvedValue([fileProperties]) | ||
filter = filterCreator({ client, config: defaultFilterContext }) as FilterWith<'onFetch'> | ||
}) | ||
it('should add author information to nested instances', async () => { | ||
await filter.onFetch([customLabelInstance]) | ||
expect(customLabelInstance.annotations).toEqual({ | ||
[CORE_ANNOTATIONS.CREATED_BY]: CREATED_BY_NAME, | ||
[CORE_ANNOTATIONS.CREATED_AT]: CREATED_DATE, | ||
[CORE_ANNOTATIONS.CHANGED_BY]: LAST_MODIFIED_BY_NAME, | ||
[CORE_ANNOTATIONS.CHANGED_AT]: LAST_MODIFIED_DATE, | ||
}) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.