Skip to content

Commit 58b01d3

Browse files
authored
DXCDT-421: Preserve identifier fields (#784)
* Initial work to preserve identifier fields * Fixing test case * Enabling more preservation if identifier field is updated * Adding e2e test for preservation of identifier fields * Adding e2e test for preservation of identifier fields * Adding one more test case for custom domains * Adding last test case * Rerecording tests * Simplifying change, updating comments * Removing extraneous comment --------- Co-authored-by: Will Vedder <[email protected]>
1 parent 436fd2b commit 58b01d3

7 files changed

+1870
-120
lines changed

src/keywordPreservation.ts

+29-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import APIHandler from './tools/auth0/handlers/default';
1010
Original Github Issue: https://github.com/auth0/auth0-deploy-cli/issues/328
1111
*/
1212

13-
export const shouldFieldBePreserved = (
13+
export const doesHaveKeywordMarker = (
1414
string: string,
1515
keywordMappings: KeywordMappings
1616
): boolean => {
@@ -29,7 +29,7 @@ export const getPreservableFieldsFromAssets = (
2929
address = ''
3030
): string[] => {
3131
if (typeof asset === 'string') {
32-
if (shouldFieldBePreserved(asset, keywordMappings)) {
32+
if (doesHaveKeywordMarker(asset, keywordMappings)) {
3333
return [address];
3434
}
3535
return [];
@@ -91,7 +91,7 @@ export const getAssetsValueByAddress = (address: string, assets: any): any => {
9191

9292
// It is easier to handle an address piece-by-piece by
9393
// splitting on the period into separate "directions"
94-
const directions = address.split('.');
94+
const directions = address.split(/\.(?![^\[]*\])/g);
9595

9696
// If the the next directions are the proprietary array syntax (ex: `[name=foo]`)
9797
// then perform lookup against unique array-item property
@@ -123,7 +123,7 @@ export const convertAddressToDotNotation = (
123123
address: string,
124124
finalAddressTrail = ''
125125
): string | null => {
126-
const directions = address.split('.');
126+
const directions = address.split(/\.(?![^\[]*\])/g);
127127

128128
if (directions[0] === '') return finalAddressTrail;
129129

@@ -213,7 +213,15 @@ export const preserveKeywords = ({
213213

214214
addresses.forEach((address) => {
215215
const localValue = getAssetsValueByAddress(address, localAssets);
216-
const remoteValue = getAssetsValueByAddress(address, remoteAssets);
216+
217+
const remoteAssetsAddress = (() => {
218+
const doesAddressHaveKeyword = doesHaveKeywordMarker(address, keywordMappings);
219+
if (doesAddressHaveKeyword) {
220+
return keywordReplace(address, keywordMappings);
221+
}
222+
return address;
223+
})();
224+
const remoteValue = getAssetsValueByAddress(remoteAssetsAddress, remoteAssets);
217225

218226
const localValueWithReplacement = keywordReplace(localValue, keywordMappings);
219227

@@ -230,7 +238,22 @@ export const preserveKeywords = ({
230238
);
231239
}
232240

233-
updatedRemoteAssets = updateAssetsByAddress(updatedRemoteAssets, address, localValue);
241+
// Two address possibilities are provided to account for cases when there is a keyword
242+
// in the resources's identifier field. When the resource identifier's field is preserved
243+
// on the remote assets tree, it loses its identify, so we'll need to try two addresses:
244+
// one where the identifier field has a keyword and one where the identifier field has
245+
// the literal replaced value.
246+
// Example: `customDomains.[domain=##DOMAIN].domain` and `customDomains.[domain=travel0.com].domain`
247+
updatedRemoteAssets = updateAssetsByAddress(
248+
updatedRemoteAssets,
249+
address, //Two possible addresses need to be passed, one with identifier field keyword replaced and one where it is not replaced. Ex: `customDomains.[domain=##DOMAIN].domain` and `customDomains.[domain=travel0.com].domain`
250+
localValue
251+
);
252+
updatedRemoteAssets = updateAssetsByAddress(
253+
updatedRemoteAssets,
254+
remoteAssetsAddress, //Two possible addresses need to be passed, one with identifier field keyword replaced and one where it is not replaced. Ex: `customDomains.[domain=##DOMAIN].domain` and `customDomains.[domain=travel0.com].domain`
255+
localValue
256+
);
234257
});
235258

236259
return updatedRemoteAssets;

test/e2e/e2e.test.ts

+25-1
Original file line numberDiff line numberDiff line change
@@ -366,11 +366,12 @@ describe('keyword preservation', () => {
366366
AUTH0_CLIENT_SECRET,
367367
AUTH0_ACCESS_TOKEN,
368368
AUTH0_PRESERVE_KEYWORDS: true,
369-
AUTH0_INCLUDED_ONLY: ['tenant', 'emailTemplates'] as AssetTypes[],
369+
AUTH0_INCLUDED_ONLY: ['tenant', 'emailTemplates', 'clients'] as AssetTypes[],
370370
AUTH0_KEYWORD_REPLACE_MAPPINGS: {
371371
TENANT_NAME: 'This tenant name should be preserved',
372372
DOMAIN: 'travel0.com',
373373
LANGUAGES: ['en', 'es'],
374+
ENV: 'dev',
374375
},
375376
};
376377

@@ -431,6 +432,13 @@ describe('keyword preservation', () => {
431432
.toString();
432433
expect(emailTemplateHTML).to.contain('##TENANT_NAME##');
433434

435+
expect(
436+
yaml.clients.some(
437+
({ name, logo_uri }) =>
438+
name === 'Auth0 CLI - ##ENV##' && logo_uri === 'https://##ENV##.assets.com/photos/foo'
439+
)
440+
).to.be.true;
441+
434442
recordingDone();
435443
});
436444

@@ -483,6 +491,16 @@ describe('keyword preservation', () => {
483491
fs.readFileSync(path.join(workDirectory, 'emails', 'welcome_email.html')).toString()
484492
).to.contain(config.AUTH0_KEYWORD_REPLACE_MAPPINGS.TENANT_NAME);
485493

494+
const clientsJsonWithoutPreservation = JSON.parse(
495+
fs.readFileSync(path.join(workDirectory, 'clients', 'Auth0 CLI - dev.json')).toString()
496+
);
497+
expect(clientsJsonWithoutPreservation.name).to.equal(
498+
`Auth0 CLI - ${config.AUTH0_KEYWORD_REPLACE_MAPPINGS.ENV}`
499+
);
500+
expect(clientsJsonWithoutPreservation.logo_uri).to.equal(
501+
`https://${config.AUTH0_KEYWORD_REPLACE_MAPPINGS.ENV}.assets.com/photos/foo`
502+
);
503+
486504
emptyDirSync(workDirectory);
487505
copySync(`${__dirname}/testdata/should-preserve-keywords/directory`, workDirectory); //It is necessary to copy directory contents to work directory to prevent overwriting of Git-committed files
488506

@@ -512,6 +530,12 @@ describe('keyword preservation', () => {
512530
.toString();
513531
expect(emailTemplateHTML).to.contain('##TENANT_NAME##');
514532

533+
const clientsJSON = JSON.parse(
534+
fs.readFileSync(path.join(workDirectory, 'clients', 'Auth0 CLI - dev.json')).toString()
535+
);
536+
expect(clientsJSON.name).to.equal('Auth0 CLI - ##ENV##');
537+
expect(clientsJSON.logo_uri).to.equal('https://##ENV##.assets.com/photos/foo');
538+
515539
recordingDone();
516540
});
517541

0 commit comments

Comments
 (0)