Skip to content

Commit db9c0c9

Browse files
authored
Merge pull request #106 from contentstack/development
Staging | DX-1964
2 parents e72e92f + b70e7ef commit db9c0c9

File tree

4 files changed

+130
-13
lines changed

4 files changed

+130
-13
lines changed

.github/workflows/check-branch.yml

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,24 @@ jobs:
88
runs-on: ubuntu-latest
99
steps:
1010
- name: Comment PR
11-
if: github.base_ref == 'master' && github.head_ref != 'next'
11+
if: github.base_ref == 'main' && github.head_ref != 'staging'
1212
uses: thollander/actions-comment-pull-request@v2
1313
with:
1414
message: |
15-
We regret to inform you that you are currently not able to merge your changes into the master branch due to restrictions applied by our SRE team. To proceed with merging your changes, we kindly request that you create a pull request from the next branch. Our team will then review the changes and work with you to ensure a successful merge into the master branch.
15+
We regret to inform you that you are currently not able to merge your changes into the main branch due to restrictions applied by our SRE team. To proceed with merging your changes, we kindly request that you create a pull request from the development branch. Our team will then review the changes and work with you to ensure a successful merge into the main branch.
1616
- name: Check branch
17-
if: github.base_ref == 'master' && github.head_ref != 'next'
17+
if: github.base_ref == 'main' && github.head_ref != 'staging'
1818
run: |
19-
echo "ERROR: We regret to inform you that you are currently not able to merge your changes into the master branch due to restrictions applied by our SRE team. To proceed with merging your changes, we kindly request that you create a pull request from the next branch. Our team will then review the changes and work with you to ensure a successful merge into the master branch."
19+
echo "ERROR: We regret to inform you that you are currently not able to merge your changes into the main branch due to restrictions applied by our SRE team. To proceed with merging your changes, we kindly request that you create a pull request from the development branch. Our team will then review the changes and work with you to ensure a successful merge into the main branch."
20+
exit 1
21+
- name: Comment PR for staging
22+
if: github.base_ref == 'staging' && github.head_ref != 'development'
23+
uses: thollander/actions-comment-pull-request@v2
24+
with:
25+
message: |
26+
We regret to inform you that you are currently not able to merge your changes into the staging branch due to restrictions applied by our SRE team. To proceed with merging your changes, we kindly request that you create a pull request from the development branch. Our team will then review the changes and work with you to ensure a successful merge into the staging branch.
27+
- name: Check branch for staging
28+
if: github.base_ref == 'staging' && github.head_ref != 'development'
29+
run: |
30+
echo "ERROR: We regret to inform you that you are currently not able to merge your changes into the staging branch due to restrictions applied by our SRE team. To proceed with merging your changes, we kindly request that you create a pull request from the development branch. Our team will then review the changes and work with you to ensure a successful merge into the staging branch."
2031
exit 1

src/lib/stack.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,22 +145,43 @@ export class Stack {
145145

146146
livePreviewQuery(query: LivePreviewQuery) {
147147
if (this.config.live_preview) {
148-
const livePreviewParams: any = {
149-
...this.config.live_preview,
150-
live_preview: query.live_preview || 'init',
151-
contentTypeUid: query.contentTypeUid,
152-
entryUid: query.entryUid,
153-
preview_timestamp: query.preview_timestamp || "",
154-
include_applied_variants: query.include_applied_variants || false,
148+
let livePreviewParams: any = { ...this.config.live_preview };
149+
150+
if (query.live_preview) {
151+
livePreviewParams = {
152+
...livePreviewParams,
153+
live_preview: query.live_preview,
154+
contentTypeUid: query.contentTypeUid || query.content_type_uid,
155+
entryUid: query.entryUid || query.entry_uid,
156+
preview_timestamp: query.preview_timestamp || "",
157+
include_applied_variants: query.include_applied_variants || false,
158+
};
159+
} else {
160+
livePreviewParams = {
161+
live_preview: null,
162+
contentTypeUid: null,
163+
entryUid: null,
164+
preview_timestamp: null,
165+
include_applied_variants: false,
166+
};
155167
}
156168
this._client.stackConfig.live_preview = livePreviewParams;
157169
}
158170

159171
if (query.hasOwnProperty('release_id')) {
160172
this._client.defaults.headers['release_id'] = query.release_id;
173+
} else {
174+
delete this._client.defaults.headers['release_id'];
161175
}
176+
162177
if (query.hasOwnProperty('preview_timestamp')) {
163178
this._client.defaults.headers['preview_timestamp'] = query.preview_timestamp;
179+
} else {
180+
delete this._client.defaults.headers['preview_timestamp'];
164181
}
165182
}
183+
184+
getClient(): any {
185+
return this._client;
186+
}
166187
}

src/lib/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,9 @@ export interface FindResponse<T> {
260260
export interface LivePreviewQuery {
261261
live_preview: string
262262
include_applied_variants?: boolean;
263-
contentTypeUid: string
263+
contentTypeUid?: string
264+
content_type_uid?: string
265+
entry_uid?: string
264266
entryUid?: any;
265267
preview_timestamp?: string
266268
release_id?: string

test/unit/stack.spec.ts

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { httpClient, AxiosInstance } from '@contentstack/core';
2+
import { jest } from '@jest/globals';
23
import MockAdapter from 'axios-mock-adapter';
34
import { Stack } from '../../src/lib/stack';
45
import { Asset } from '../../src/lib/asset';
@@ -8,6 +9,7 @@ import { syncResult } from '../utils/mocks';
89
import { synchronization } from '../../src/lib/synchronization';
910
import { ContentTypeQuery } from '../../src/lib/contenttype-query';
1011
import { AssetQuery } from '../../src/lib/asset-query';
12+
import { StackConfig } from '../../src/lib/types';
1113

1214
jest.mock('../../src/lib/synchronization');
1315
const syncMock = <jest.Mock<typeof synchronization>>(<unknown>synchronization);
@@ -29,7 +31,7 @@ describe('Stack class tests', () => {
2931
environment: '',
3032
});
3133

32-
stack = new Stack(client, config());
34+
stack = new Stack(client, config() as StackConfig);
3335
});
3436
it('should test import of class Stack', (done) => {
3537
expect(stack).toBeInstanceOf(Stack);
@@ -60,4 +62,85 @@ describe('Stack class tests', () => {
6062
expect(result).toEqual(syncResult);
6163
syncMock.mockReset();
6264
});
65+
66+
it('should set live preview parameters correctly when live_preview is true', (done) => {
67+
const query = {
68+
live_preview: 'live_preview_hash',
69+
contentTypeUid: 'contentTypeUid',
70+
entryUid: 'entryUid',
71+
preview_timestamp: 'timestamp',
72+
include_applied_variants: true,
73+
};
74+
75+
stack.config.live_preview = { enable: true, live_preview: 'true' };
76+
stack.livePreviewQuery(query);
77+
78+
expect(stack.getClient().stackConfig.live_preview).toEqual({
79+
live_preview: 'live_preview_hash',
80+
contentTypeUid: 'contentTypeUid',
81+
enable: true,
82+
entryUid: 'entryUid',
83+
preview_timestamp: 'timestamp',
84+
include_applied_variants: true,
85+
});
86+
done();
87+
});
88+
89+
it('should set live preview parameters to null when live_preview is false', () => {
90+
const query = {
91+
live_preview: '',
92+
};
93+
94+
stack.config.live_preview = { enable: false, live_preview: '' };
95+
stack.livePreviewQuery(query);
96+
97+
expect(stack.getClient().stackConfig.live_preview).toEqual({
98+
live_preview: null,
99+
contentTypeUid: null,
100+
entryUid: null,
101+
preview_timestamp: null,
102+
include_applied_variants: false,
103+
});
104+
});
105+
106+
it('should set release_id header when release_id is present in query', () => {
107+
const query = {
108+
live_preview: 'live_preview_hash',
109+
release_id: 'releaseId',
110+
};
111+
112+
stack.livePreviewQuery(query);
113+
114+
expect(stack.getClient().defaults.headers['release_id']).toEqual('releaseId');
115+
});
116+
117+
it('should delete release_id header when release_id is not present in query', () => {
118+
stack.getClient().defaults.headers['release_id'] = 'releaseId';
119+
const query = { live_preview: 'live_preview_hash'};
120+
121+
stack.livePreviewQuery(query);
122+
123+
expect(stack.getClient().defaults.headers['release_id']).toBeUndefined();
124+
});
125+
126+
it('should set preview_timestamp header when preview_timestamp is present in query', () => {
127+
const query = {
128+
live_preview: 'live_preview_hash',
129+
preview_timestamp: 'timestamp',
130+
};
131+
132+
stack.livePreviewQuery(query);
133+
134+
expect(stack.getClient().defaults.headers['preview_timestamp']).toEqual('timestamp');
135+
});
136+
137+
it('should delete preview_timestamp header when preview_timestamp is not present in query', () => {
138+
stack.getClient().defaults.headers['preview_timestamp'] = 'timestamp';
139+
const query = { live_preview: 'live_preview_hash' };
140+
141+
stack.livePreviewQuery(query);
142+
143+
expect(stack.getClient().defaults.headers['preview_timestamp']).toBeUndefined();
144+
});
63145
});
146+

0 commit comments

Comments
 (0)