From 06a9a4e7b5a8fa60d016b5f9dbe86e3b549a5938 Mon Sep 17 00:00:00 2001 From: Nadeem Patwekar Date: Tue, 3 Dec 2024 12:31:55 +0530 Subject: [PATCH 1/3] refactor: update check-branch.yml --- .github/workflows/check-branch.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/check-branch.yml b/.github/workflows/check-branch.yml index 4c087e5..acaff69 100644 --- a/.github/workflows/check-branch.yml +++ b/.github/workflows/check-branch.yml @@ -8,13 +8,13 @@ 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 next 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 next branch. Our team will then review the changes and work with you to ensure a successful merge into the main branch." exit 1 From 441a85cb9aedb94d50b715d9f7a869a6baf2e238 Mon Sep 17 00:00:00 2001 From: Nadeem Patwekar Date: Fri, 3 Jan 2025 20:13:50 +0530 Subject: [PATCH 2/3] fix: reset livePreviewConfig properties if not received in params --- src/lib/stack.ts | 35 +++++++++++++---- src/lib/types.ts | 4 +- test/unit/stack.spec.ts | 85 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 115 insertions(+), 9 deletions(-) diff --git a/src/lib/stack.ts b/src/lib/stack.ts index 7b639cc..9a6ec82 100644 --- a/src/lib/stack.ts +++ b/src/lib/stack.ts @@ -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; + } } diff --git a/src/lib/types.ts b/src/lib/types.ts index 82f571d..2fcd75f 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -260,7 +260,9 @@ export interface FindResponse { 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 diff --git a/test/unit/stack.spec.ts b/test/unit/stack.spec.ts index c61705f..35a3476 100644 --- a/test/unit/stack.spec.ts +++ b/test/unit/stack.spec.ts @@ -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'; @@ -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 = >(synchronization); @@ -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); @@ -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(); + }); }); + From 48ea92f1380b03ced124f4d4126a936003f107f5 Mon Sep 17 00:00:00 2001 From: Nadeem Patwekar Date: Fri, 3 Jan 2025 20:24:43 +0530 Subject: [PATCH 3/3] feat: add more branch checks --- .github/workflows/check-branch.yml | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/.github/workflows/check-branch.yml b/.github/workflows/check-branch.yml index acaff69..e46fdcb 100644 --- a/.github/workflows/check-branch.yml +++ b/.github/workflows/check-branch.yml @@ -12,9 +12,20 @@ jobs: 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 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 next branch. Our team will then review the changes and work with you to ensure a successful merge into the main 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 == '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 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 next branch. Our team will then review the changes and work with you to ensure a successful merge into the main 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