Skip to content

Commit

Permalink
Throw error on mismatched versioning (github#16191)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
sarahs committed Oct 23, 2020
1 parent 8d4e30f commit 095410d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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: '*'
---

Expand Down
20 changes: 16 additions & 4 deletions lib/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: Some GitHub article
versions:
free-pro-team: '*'
enterprise-server: '*'
---
12 changes: 12 additions & 0 deletions tests/unit/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
})
})

0 comments on commit 095410d

Please sign in to comment.