Skip to content

Commit

Permalink
SALTO-4903: Do not attempt to list unknown types within the nestedIns…
Browse files Browse the repository at this point in the history
…tancesFilter in Salesforce (#5050)
  • Loading branch information
tamtamirr authored Nov 6, 2023
1 parent 6021cbf commit fa4f18c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ const NESTED_INSTANCES_METADATA_TYPES = [
'AutoResponseRule',
'EscalationRule',
'MatchingRule',
...Object.keys(WORKFLOW_FIELD_TO_TYPE),
...Object.keys(NESTED_INSTANCE_VALUE_TO_TYPE_NAME),
...Object.values(WORKFLOW_FIELD_TO_TYPE),
...Object.values(NESTED_INSTANCE_VALUE_TO_TYPE_NAME),
] as const

type NestedInstanceMetadataType = typeof NESTED_INSTANCES_METADATA_TYPES[number]
Expand Down Expand Up @@ -81,12 +81,17 @@ const filterCreator: RemoteFilterCreator = ({ client, config }) => ({
config,
filterName: 'authorInformation',
fetchFilterFunc: async (elements: Element[]) => {
const instancesByType = _.groupBy(
elements.filter(isMetadataInstanceElementSync),
e => apiNameSync(e.getTypeSync())
const nestedInstancesByType = _.pick(
_.groupBy(
elements.filter(isMetadataInstanceElementSync),
e => apiNameSync(e.getTypeSync())
),
NESTED_INSTANCES_METADATA_TYPES
)
await Promise.all(Object.entries(instancesByType)
.map(([typeName, instances]) => (
await Promise.all(NESTED_INSTANCES_METADATA_TYPES
.map(typeName => ({ typeName, instances: nestedInstancesByType[typeName] ?? [] }))
.filter(({ instances }) => instances.length > 0)
.map(({ typeName, instances }) => (
setAuthorInformationForInstancesOfType({ client, typeName, instances })
)))
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/
import { CORE_ANNOTATIONS, InstanceElement } from '@salto-io/adapter-api'
import { MockInterface } from '@salto-io/test-utils'
import Connection from '../../../src/client/jsforce'
import { mockTypes } from '../../mock_elements'
import { CUSTOM_LABEL_METADATA_TYPE, INSTANCE_FULL_NAME_FIELD } from '../../../src/constants'
import { mockFileProperties } from '../../connection'
Expand All @@ -29,7 +31,9 @@ describe('nestedInstancesAuthorInformationFilter', () => {
const LAST_MODIFIED_DATE = '2021-01-02T00:00:00.000Z'

let customLabelInstance: InstanceElement
let nonNestedInstance: InstanceElement
let filter: FilterWith<'onFetch'>
let connection: MockInterface<Connection>
describe('onFetch', () => {
beforeEach(() => {
customLabelInstance = new InstanceElement(
Expand All @@ -39,6 +43,14 @@ describe('nestedInstancesAuthorInformationFilter', () => {
[INSTANCE_FULL_NAME_FIELD]: 'TestCustomLabel',
},
)
// Make sure we don't attempt to add missing internal ids to non nested instances
nonNestedInstance = new InstanceElement(
'TestNonNestedInstance',
mockTypes.ApexClass,
{
[INSTANCE_FULL_NAME_FIELD]: 'TestNonNestedInstance',
},
)
const fileProperties = mockFileProperties({
fullName: 'TestCustomLabel',
type: CUSTOM_LABEL_METADATA_TYPE,
Expand All @@ -47,18 +59,22 @@ describe('nestedInstancesAuthorInformationFilter', () => {
lastModifiedByName: LAST_MODIFIED_BY_NAME,
lastModifiedDate: LAST_MODIFIED_DATE,
})
const { client, connection } = mockClient()
const mockClientAndConnection = mockClient()
const { client } = mockClientAndConnection
connection = mockClientAndConnection.connection
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])
await filter.onFetch([customLabelInstance, nonNestedInstance])
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,
})
expect(connection.metadata.list).toHaveBeenCalledOnce()
expect(connection.metadata.list).toHaveBeenCalledWith([{ type: CUSTOM_LABEL_METADATA_TYPE }])
})
})
})

0 comments on commit fa4f18c

Please sign in to comment.