From 8d5bf2385047208f15a0af80fd5e9ae49f603e9e Mon Sep 17 00:00:00 2001 From: Jonathan Ostertag Date: Tue, 28 Nov 2023 11:35:37 +0100 Subject: [PATCH 1/4] fix: adapt base url to netlify --- gatsby/config-options/constants.ts | 9 +++-- .../build-gatsby-cloud-preview-url.test.ts | 35 ----------------- gatsby/util/build-gatsby-cloud-preview-url.ts | 23 ----------- gatsby/util/build-netlify-preview-url.test.ts | 39 +++++++++++++++++++ gatsby/util/build-netlify-preview-url.ts | 17 ++++++++ 5 files changed, 61 insertions(+), 62 deletions(-) delete mode 100644 gatsby/util/build-gatsby-cloud-preview-url.test.ts delete mode 100644 gatsby/util/build-gatsby-cloud-preview-url.ts create mode 100644 gatsby/util/build-netlify-preview-url.test.ts create mode 100644 gatsby/util/build-netlify-preview-url.ts diff --git a/gatsby/config-options/constants.ts b/gatsby/config-options/constants.ts index df6b6b053..ba0c62310 100644 --- a/gatsby/config-options/constants.ts +++ b/gatsby/config-options/constants.ts @@ -1,10 +1,11 @@ -import { buildGatsbyCloudPreviewUrl } from '../util/build-gatsby-cloud-preview-url'; +import { buildNetlifyPreviewUrl } from '../util/build-netlify-preview-url'; export const RSS_FEED_URL = '/blog/rss.xml'; export const DEFAULT_META_IMAGE_URL_PATH = '/sy-share-image.jpg'; -export const GATSBY_SITE_PREFIX = process.env.GATSBY_SITE_PREFIX || ''; -export const BRANCH_PREVIEW_URL = buildGatsbyCloudPreviewUrl({ - prefix: GATSBY_SITE_PREFIX, +export const NETLIFY_DOMAIN_NAME = process.env.GATSBY_NETLIFY_DOMAIN_NAME || ''; +export const BRANCH_PREVIEW_URL = buildNetlifyPreviewUrl({ + domainName: NETLIFY_DOMAIN_NAME, branch: process.env.BRANCH, + previewId: process.env.PREVIEW_ID, }); // either use a branch preview url if any diff --git a/gatsby/util/build-gatsby-cloud-preview-url.test.ts b/gatsby/util/build-gatsby-cloud-preview-url.test.ts deleted file mode 100644 index 05bf9414d..000000000 --- a/gatsby/util/build-gatsby-cloud-preview-url.test.ts +++ /dev/null @@ -1,35 +0,0 @@ -import { buildGatsbyCloudPreviewUrl } from './build-gatsby-cloud-preview-url'; - -describe('build-gatsby-cloud-preview-url', () => { - test('should return a valid url', () => { - const url = buildGatsbyCloudPreviewUrl({ - prefix: 'satellytescommain', - branch: 'feature-branch-1', - }); - expect(url).toBe('https://satellytescommain-featurebranch1.gtsb.io'); - }); - - test('should return null for the production branch', () => { - const url = buildGatsbyCloudPreviewUrl({ - prefix: 'satellytescommain', - branch: 'main', - }); - expect(url).toBe(null); - }); - - test('should return null without a branch given', () => { - const url = buildGatsbyCloudPreviewUrl({ - prefix: 'satellytescommain', - branch: null, - }); - expect(url).toBe(null); - }); - - test('should return null if no prefix is given', () => { - const url = buildGatsbyCloudPreviewUrl({ - prefix: null, - branch: 'something', - }); - expect(url).toBe(null); - }); -}); diff --git a/gatsby/util/build-gatsby-cloud-preview-url.ts b/gatsby/util/build-gatsby-cloud-preview-url.ts deleted file mode 100644 index 1cce25385..000000000 --- a/gatsby/util/build-gatsby-cloud-preview-url.ts +++ /dev/null @@ -1,23 +0,0 @@ -/** - * Get the Gatsby Cloud Preview URL by branchName. If no branchName is given the - * fallback URL gets return. - * - * On gatsby cloud the deployment urls follow a strict pattern: - * - https://${DOMAIN_NAME}-${BRANCH_NAME}.gatsby.io - * - * Only lower case letters and numbers are used, everything else is filtered out. - * - */ - -const PRODUCTION_BRANCH = 'main'; -export const buildGatsbyCloudPreviewUrl = ({ prefix, branch }) => { - if (!prefix || !branch || branch === PRODUCTION_BRANCH) { - return null; - } - - const formattedBranchName = branch - .toLowerCase() - .replace(/[^a-zA-Z0-9]/gi, ''); - - return `https://${prefix}-${formattedBranchName}.gtsb.io`; -}; diff --git a/gatsby/util/build-netlify-preview-url.test.ts b/gatsby/util/build-netlify-preview-url.test.ts new file mode 100644 index 000000000..06f8db0e8 --- /dev/null +++ b/gatsby/util/build-netlify-preview-url.test.ts @@ -0,0 +1,39 @@ +import { buildNetlifyPreviewUrl } from './build-netlify-preview-url'; + +describe('build-netlify-preview-url', () => { + test('should return a valid url', () => { + const url = buildNetlifyPreviewUrl({ + domainName: 'satellytes', + branch: 'feature-branch-1', + previewId: '123', + }); + expect(url).toBe('https://deploy-preview-123--satellytes.netlify.app/'); + }); + + test('should return null for the production branch', () => { + const url = buildNetlifyPreviewUrl({ + domainName: 'satellytescommain', + branch: 'main', + previewId: '123', + }); + expect(url).toBe(null); + }); + + test('should return null without a branch given', () => { + const url = buildNetlifyPreviewUrl({ + domainName: 'satellytescommain', + branch: null, + previewId: '123', + }); + expect(url).toBe(null); + }); + + test('should return null if no prefix is given', () => { + const url = buildNetlifyPreviewUrl({ + domainName: null, + branch: 'something', + previewId: '123', + }); + expect(url).toBe(null); + }); +}); diff --git a/gatsby/util/build-netlify-preview-url.ts b/gatsby/util/build-netlify-preview-url.ts new file mode 100644 index 000000000..fa76ee44e --- /dev/null +++ b/gatsby/util/build-netlify-preview-url.ts @@ -0,0 +1,17 @@ +/** + * Get the Netlify Preview URL by previewId + * (this equals the github entity id, e.g. PR id). + * If no previewId is given the fallback URL gets return. + * + * On netlify the deployment urls follow a strict pattern: + * - https://deploy-preview-${PREVIEW_ID}--${DOMAIN_NAME}.netlify.app/ + */ + +const PRODUCTION_BRANCH = 'main'; +export const buildNetlifyPreviewUrl = ({ domainName, previewId, branch }) => { + if (!domainName || !previewId || branch === PRODUCTION_BRANCH) { + return null; + } + + return `https://deploy-preview-${previewId}--${domainName}.netlify.app/`; +}; From c189f4b13b957660a8887a0036608481e101e21d Mon Sep 17 00:00:00 2001 From: Jonathan Ostertag Date: Tue, 28 Nov 2023 11:47:09 +0100 Subject: [PATCH 2/4] fix: tests --- gatsby/util/build-netlify-preview-url.test.ts | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/gatsby/util/build-netlify-preview-url.test.ts b/gatsby/util/build-netlify-preview-url.test.ts index 06f8db0e8..244c300a9 100644 --- a/gatsby/util/build-netlify-preview-url.test.ts +++ b/gatsby/util/build-netlify-preview-url.test.ts @@ -19,16 +19,7 @@ describe('build-netlify-preview-url', () => { expect(url).toBe(null); }); - test('should return null without a branch given', () => { - const url = buildNetlifyPreviewUrl({ - domainName: 'satellytescommain', - branch: null, - previewId: '123', - }); - expect(url).toBe(null); - }); - - test('should return null if no prefix is given', () => { + test('should return null if no domainName is given', () => { const url = buildNetlifyPreviewUrl({ domainName: null, branch: 'something', From fe5d924b897e8c15322b3c28b3fe0ded36803c66 Mon Sep 17 00:00:00 2001 From: Jonathan Ostertag Date: Tue, 28 Nov 2023 11:49:40 +0100 Subject: [PATCH 3/4] fix: netlify constants --- gatsby/config-options/constants.ts | 2 +- gatsby/util/build-netlify-preview-url.test.ts | 6 +++--- gatsby/util/build-netlify-preview-url.ts | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/gatsby/config-options/constants.ts b/gatsby/config-options/constants.ts index ba0c62310..78bd5952e 100644 --- a/gatsby/config-options/constants.ts +++ b/gatsby/config-options/constants.ts @@ -5,7 +5,7 @@ export const NETLIFY_DOMAIN_NAME = process.env.GATSBY_NETLIFY_DOMAIN_NAME || ''; export const BRANCH_PREVIEW_URL = buildNetlifyPreviewUrl({ domainName: NETLIFY_DOMAIN_NAME, branch: process.env.BRANCH, - previewId: process.env.PREVIEW_ID, + reviewId: process.env.REVIEW_ID, }); // either use a branch preview url if any diff --git a/gatsby/util/build-netlify-preview-url.test.ts b/gatsby/util/build-netlify-preview-url.test.ts index 244c300a9..2aba8b28e 100644 --- a/gatsby/util/build-netlify-preview-url.test.ts +++ b/gatsby/util/build-netlify-preview-url.test.ts @@ -5,7 +5,7 @@ describe('build-netlify-preview-url', () => { const url = buildNetlifyPreviewUrl({ domainName: 'satellytes', branch: 'feature-branch-1', - previewId: '123', + reviewId: '123', }); expect(url).toBe('https://deploy-preview-123--satellytes.netlify.app/'); }); @@ -14,7 +14,7 @@ describe('build-netlify-preview-url', () => { const url = buildNetlifyPreviewUrl({ domainName: 'satellytescommain', branch: 'main', - previewId: '123', + reviewId: '123', }); expect(url).toBe(null); }); @@ -23,7 +23,7 @@ describe('build-netlify-preview-url', () => { const url = buildNetlifyPreviewUrl({ domainName: null, branch: 'something', - previewId: '123', + reviewId: '123', }); expect(url).toBe(null); }); diff --git a/gatsby/util/build-netlify-preview-url.ts b/gatsby/util/build-netlify-preview-url.ts index fa76ee44e..f00c1c70b 100644 --- a/gatsby/util/build-netlify-preview-url.ts +++ b/gatsby/util/build-netlify-preview-url.ts @@ -8,10 +8,10 @@ */ const PRODUCTION_BRANCH = 'main'; -export const buildNetlifyPreviewUrl = ({ domainName, previewId, branch }) => { - if (!domainName || !previewId || branch === PRODUCTION_BRANCH) { +export const buildNetlifyPreviewUrl = ({ domainName, reviewId, branch }) => { + if (!domainName || !reviewId || branch === PRODUCTION_BRANCH) { return null; } - return `https://deploy-preview-${previewId}--${domainName}.netlify.app/`; + return `https://deploy-preview-${reviewId}--${domainName}.netlify.app/`; }; From 714b21acd0baff6959ad9d4b928edb6702724d17 Mon Sep 17 00:00:00 2001 From: Jonathan Ostertag Date: Tue, 28 Nov 2023 12:36:53 +0100 Subject: [PATCH 4/4] chore: remove comment --- gatsby/util/build-netlify-preview-url.ts | 9 --------- 1 file changed, 9 deletions(-) diff --git a/gatsby/util/build-netlify-preview-url.ts b/gatsby/util/build-netlify-preview-url.ts index f00c1c70b..84442ebca 100644 --- a/gatsby/util/build-netlify-preview-url.ts +++ b/gatsby/util/build-netlify-preview-url.ts @@ -1,12 +1,3 @@ -/** - * Get the Netlify Preview URL by previewId - * (this equals the github entity id, e.g. PR id). - * If no previewId is given the fallback URL gets return. - * - * On netlify the deployment urls follow a strict pattern: - * - https://deploy-preview-${PREVIEW_ID}--${DOMAIN_NAME}.netlify.app/ - */ - const PRODUCTION_BRANCH = 'main'; export const buildNetlifyPreviewUrl = ({ domainName, reviewId, branch }) => { if (!domainName || !reviewId || branch === PRODUCTION_BRANCH) {