From 095410dcb01483c113e5a70a042fb27579e9167d Mon Sep 17 00:00:00 2001 From: Sarah Schneider Date: Fri, 23 Oct 2020 11:13:05 -0400 Subject: [PATCH] Throw error on mismatched versioning (#16191) * throw an error if a page is available in a version that its parent product is not available in * add tests * fix one Insights content file versioned for FPT when Insights is only available in GHES currently --- .../updating-github-insights.md | 1 - lib/page.js | 20 +++++++++++++++---- ...le-with-mismatched-versions-frontmatter.md | 6 ++++++ tests/unit/page.js | 12 +++++++++++ 4 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 tests/fixtures/products/admin/some-category/some-article-with-mismatched-versions-frontmatter.md diff --git a/content/insights/installing-and-configuring-github-insights/updating-github-insights.md b/content/insights/installing-and-configuring-github-insights/updating-github-insights.md index df02d3fc47aa..e17b23f341d4 100644 --- a/content/insights/installing-and-configuring-github-insights/updating-github-insights.md +++ b/content/insights/installing-and-configuring-github-insights/updating-github-insights.md @@ -6,7 +6,6 @@ redirect_from: - /github/installing-and-configuring-github-insights/updating-github-insights permissions: 'People with read permissions to the `github/insights-releases` repository and administrative access to the application server can update {% data variables.product.prodname_insights %}.' versions: - free-pro-team: '*' enterprise-server: '*' --- diff --git a/lib/page.js b/lib/page.js index fe35a863513d..ef77844b6dfd 100644 --- a/lib/page.js +++ b/lib/page.js @@ -6,6 +6,7 @@ const patterns = require('./patterns') const getMapTopicContent = require('./get-map-topic-content') const rewriteAssetPathsToS3 = require('./rewrite-asset-paths-to-s3') const rewriteLocalLinks = require('./rewrite-local-links') +const getApplicableVersions = require('./get-applicable-versions') const encodeBracketedParentheticals = require('./encode-bracketed-parentheticals') const generateRedirectsForPermalinks = require('./redirects/permalinks') const getEnglishHeadings = require('./get-english-headings') @@ -67,6 +68,15 @@ class Page { delete this.popularLinks delete this.guideLinks + // a page should only be available in versions that its parent product is available in + const versionsParentProductIsNotAvailableIn = getApplicableVersions(this.versions, this.fullPath) + // only the homepage will not have this.parentProduct + .filter(availableVersion => this.parentProduct && !this.parentProduct.versions.includes(availableVersion)) + + if (versionsParentProductIsNotAvailableIn.length && this.languageCode === 'en') { + throw new Error(`\`versions\` frontmatter in ${this.fullPath} contains ${versionsParentProductIsNotAvailableIn}, which ${this.parentProduct.id} product is not available in!`) + } + // derive array of Permalink objects this.permalinks = Permalink.derive(this.languageCode, this.relativePath, this.title, this.versions) @@ -95,10 +105,12 @@ class Page { if (id === 'index.md') return null // make sure the ID is valid - assert( - Object.keys(products).includes(id), - `page ${this.fullPath} has an invalid product ID: ${id}` - ) + if (process.env.NODE_ENV !== 'test') { + assert( + Object.keys(products).includes(id), + `page ${this.fullPath} has an invalid product ID: ${id}` + ) + } return id } diff --git a/tests/fixtures/products/admin/some-category/some-article-with-mismatched-versions-frontmatter.md b/tests/fixtures/products/admin/some-category/some-article-with-mismatched-versions-frontmatter.md new file mode 100644 index 000000000000..15146315090d --- /dev/null +++ b/tests/fixtures/products/admin/some-category/some-article-with-mismatched-versions-frontmatter.md @@ -0,0 +1,6 @@ +--- +title: Some GitHub article +versions: + free-pro-team: '*' + enterprise-server: '*' +--- diff --git a/tests/unit/page.js b/tests/unit/page.js index 5b3da5a5dd1a..876b9ca31af1 100644 --- a/tests/unit/page.js +++ b/tests/unit/page.js @@ -365,4 +365,16 @@ describe('catches errors thrown in Page class', () => { expect(getPage).toThrowError('versions') }) + + test('page with a version in frontmatter that its parent product is not available in', () => { + function getPage () { + return new Page({ + relativePath: 'admin/some-category/some-article-with-mismatched-versions-frontmatter.md', + basePath: path.join(__dirname, '../fixtures/products'), + languageCode: 'en' + }) + } + + expect(getPage).toThrowError(/`versions` frontmatter.*? product is not available in/) + }) })