Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions .github/workflows/check-branch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,24 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Comment PR
if: github.base_ref == 'master' && github.head_ref != 'next'
if: github.base_ref == 'main' && github.head_ref != 'staging'
uses: thollander/actions-comment-pull-request@v2
with:
message: |
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.
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.
- name: Check branch
if: github.base_ref == 'master' && github.head_ref != 'next'
if: github.base_ref == 'main' && github.head_ref != 'staging'
run: |
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."
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."
exit 1
- name: Comment PR for staging
if: github.base_ref == 'staging' && github.head_ref != 'development'
uses: thollander/actions-comment-pull-request@v2
with:
message: |
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.
- name: Check branch for staging
if: github.base_ref == 'staging' && github.head_ref != 'development'
run: |
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."
exit 1
35 changes: 28 additions & 7 deletions src/lib/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,22 +145,43 @@ export class Stack {

livePreviewQuery(query: LivePreviewQuery) {
if (this.config.live_preview) {
const livePreviewParams: any = {
...this.config.live_preview,
live_preview: query.live_preview || 'init',
contentTypeUid: query.contentTypeUid,
entryUid: query.entryUid,
preview_timestamp: query.preview_timestamp || "",
include_applied_variants: query.include_applied_variants || false,
let livePreviewParams: any = { ...this.config.live_preview };

if (query.live_preview) {
livePreviewParams = {
...livePreviewParams,
live_preview: query.live_preview,
contentTypeUid: query.contentTypeUid || query.content_type_uid,
entryUid: query.entryUid || query.entry_uid,
preview_timestamp: query.preview_timestamp || "",
include_applied_variants: query.include_applied_variants || false,
};
} else {
livePreviewParams = {
live_preview: null,
contentTypeUid: null,
entryUid: null,
preview_timestamp: null,
include_applied_variants: false,
};
}
this._client.stackConfig.live_preview = livePreviewParams;
}

if (query.hasOwnProperty('release_id')) {
this._client.defaults.headers['release_id'] = query.release_id;
} else {
delete this._client.defaults.headers['release_id'];
}

if (query.hasOwnProperty('preview_timestamp')) {
this._client.defaults.headers['preview_timestamp'] = query.preview_timestamp;
} else {
delete this._client.defaults.headers['preview_timestamp'];
}
}

getClient(): any {
return this._client;
}
}
4 changes: 3 additions & 1 deletion src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,9 @@ export interface FindResponse<T> {
export interface LivePreviewQuery {
live_preview: string
include_applied_variants?: boolean;
contentTypeUid: string
contentTypeUid?: string
content_type_uid?: string
entry_uid?: string
entryUid?: any;
preview_timestamp?: string
release_id?: string
Expand Down
85 changes: 84 additions & 1 deletion test/unit/stack.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { httpClient, AxiosInstance } from '@contentstack/core';
import { jest } from '@jest/globals';
import MockAdapter from 'axios-mock-adapter';
import { Stack } from '../../src/lib/stack';
import { Asset } from '../../src/lib/asset';
Expand All @@ -8,6 +9,7 @@ import { syncResult } from '../utils/mocks';
import { synchronization } from '../../src/lib/synchronization';
import { ContentTypeQuery } from '../../src/lib/contenttype-query';
import { AssetQuery } from '../../src/lib/asset-query';
import { StackConfig } from '../../src/lib/types';

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

stack = new Stack(client, config());
stack = new Stack(client, config() as StackConfig);
});
it('should test import of class Stack', (done) => {
expect(stack).toBeInstanceOf(Stack);
Expand Down Expand Up @@ -60,4 +62,85 @@ describe('Stack class tests', () => {
expect(result).toEqual(syncResult);
syncMock.mockReset();
});

it('should set live preview parameters correctly when live_preview is true', (done) => {
const query = {
live_preview: 'live_preview_hash',
contentTypeUid: 'contentTypeUid',
entryUid: 'entryUid',
preview_timestamp: 'timestamp',
include_applied_variants: true,
};

stack.config.live_preview = { enable: true, live_preview: 'true' };
stack.livePreviewQuery(query);

expect(stack.getClient().stackConfig.live_preview).toEqual({
live_preview: 'live_preview_hash',
contentTypeUid: 'contentTypeUid',
enable: true,
entryUid: 'entryUid',
preview_timestamp: 'timestamp',
include_applied_variants: true,
});
done();
});

it('should set live preview parameters to null when live_preview is false', () => {
const query = {
live_preview: '',
};

stack.config.live_preview = { enable: false, live_preview: '' };
stack.livePreviewQuery(query);

expect(stack.getClient().stackConfig.live_preview).toEqual({
live_preview: null,
contentTypeUid: null,
entryUid: null,
preview_timestamp: null,
include_applied_variants: false,
});
});

it('should set release_id header when release_id is present in query', () => {
const query = {
live_preview: 'live_preview_hash',
release_id: 'releaseId',
};

stack.livePreviewQuery(query);

expect(stack.getClient().defaults.headers['release_id']).toEqual('releaseId');
});

it('should delete release_id header when release_id is not present in query', () => {
stack.getClient().defaults.headers['release_id'] = 'releaseId';
const query = { live_preview: 'live_preview_hash'};

stack.livePreviewQuery(query);

expect(stack.getClient().defaults.headers['release_id']).toBeUndefined();
});

it('should set preview_timestamp header when preview_timestamp is present in query', () => {
const query = {
live_preview: 'live_preview_hash',
preview_timestamp: 'timestamp',
};

stack.livePreviewQuery(query);

expect(stack.getClient().defaults.headers['preview_timestamp']).toEqual('timestamp');
});

it('should delete preview_timestamp header when preview_timestamp is not present in query', () => {
stack.getClient().defaults.headers['preview_timestamp'] = 'timestamp';
const query = { live_preview: 'live_preview_hash' };

stack.livePreviewQuery(query);

expect(stack.getClient().defaults.headers['preview_timestamp']).toBeUndefined();
});
});

Loading