Skip to content

Commit ba6ee3f

Browse files
committed
Added branches support for entry variant and variant group
1 parent accec1b commit ba6ee3f

3 files changed

Lines changed: 19 additions & 2 deletions

File tree

packages/contentstack-variants/src/export/experiences.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ export default class ExportExperiences extends PersonalizationAdapter<ExportConf
1616
headers: { 'X-Project-Uid': exportConfig.project_id },
1717
cmaConfig: {
1818
baseURL: exportConfig.region.cma + `/v3`,
19-
headers: { api_key: exportConfig.apiKey },
19+
headers: {
20+
api_key: exportConfig.apiKey,
21+
...(exportConfig.branchName ? { branch: exportConfig.branchName } : {}),
22+
},
2023
},
2124
});
2225
this.exportConfig = exportConfig;

packages/contentstack-variants/src/import/experiences.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ export default class Experiences extends PersonalizationAdapter<ImportConfig> {
5050
headers: { 'X-Project-Uid': config.modules.personalize.project_id },
5151
cmaConfig: {
5252
baseURL: config.region.cma + `/v3`,
53-
headers: { api_key: config.apiKey },
53+
headers: {
54+
api_key: config.apiKey,
55+
...(config.branchName ? { branch: config.branchName } : {}),
56+
},
5457
},
5558
};
5659
super(Object.assign(config, conf));

packages/contentstack-variants/src/utils/personalization-api-adapter.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,17 @@ export class PersonalizationAdapter<T> extends AdapterHelper<T, HttpClient> impl
155155
this.cmaAPIClient?.headers({ authtoken: token });
156156
}
157157
}
158+
159+
// Ensure the CMA client targets the configured branch. Variant groups, CT
160+
// links and other stack-CMA endpoints are branch-scoped, so without this
161+
// header the requests would fall back to the default (main) branch even
162+
// when the import/export is running against a non-main branch.
163+
const branchName = (this.config as any)?.branchName;
164+
if (this.adapterConfig.cmaConfig && branchName) {
165+
log.debug(`Setting branch header for CMA client: ${branchName}`, this.exportConfig?.context);
166+
this.cmaAPIClient?.headers({ branch: branchName });
167+
}
168+
158169
log.debug('Personalization adapter initialization completed', this.exportConfig?.context);
159170
} catch (error: any) {
160171
log.debug(`Personalization adapter initialization failed: ${error}`, this.exportConfig?.context);

0 commit comments

Comments
 (0)