From 4c052c20da53bad37300814e593026c0993bdbe0 Mon Sep 17 00:00:00 2001 From: Sarah Schneider Date: Mon, 22 Feb 2021 11:13:20 -0500 Subject: [PATCH 1/4] update archival script to create top-level redirects.json file instead of a million stubbed redirects --- .../archive-version.js | 34 ++++--------------- 1 file changed, 6 insertions(+), 28 deletions(-) diff --git a/script/enterprise-server-deprecations/archive-version.js b/script/enterprise-server-deprecations/archive-version.js index 2cbc93904932..8493a9dc609a 100755 --- a/script/enterprise-server-deprecations/archive-version.js +++ b/script/enterprise-server-deprecations/archive-version.js @@ -9,7 +9,6 @@ const host = `http://localhost:${port}` const scrape = require('website-scraper') const program = require('commander') const rimraf = require('rimraf').sync -const mkdirp = require('mkdirp').sync const version = require('../../lib/enterprise-server-releases').oldestSupported const archivalRepoName = 'help-docs-archived-enterprise-versions' const archivalRepoUrl = `https://github.com/github/${archivalRepoName}` @@ -173,7 +172,7 @@ async function main () { console.log(`\n\ndone scraping! added files to ${path.relative(process.cwd(), finalDirectory)}\n`) // create redirect html files to preserve frontmatter redirects - await createRedirectPages(permalinksPerVersion, pageMap, finalDirectory) + await createRedirectsFile(permalinksPerVersion, pageMap, finalDirectory) console.log(`next step: deprecate ${version} in lib/enterprise-server-releases.js`) @@ -181,10 +180,12 @@ async function main () { }) } -async function createRedirectPages (permalinks, pageMap, finalDirectory) { +async function createRedirectsFile (permalinks, pageMap, finalDirectory) { const pagesPerVersion = permalinks.map(permalink => pageMap[permalink]) const redirects = await loadRedirects(pagesPerVersion, pageMap) + const redirectsPerVersion = {} + Object.entries(redirects).forEach(([oldPath, newPath]) => { // remove any liquid variables that sneak in oldPath = oldPath @@ -193,31 +194,8 @@ async function createRedirectPages (permalinks, pageMap, finalDirectory) { // ignore any old paths that are not in this version if (!(oldPath.includes(`/enterprise-server@${version}`) || oldPath.includes(`/enterprise/${version}`))) return - const fullPath = path.join(finalDirectory, oldPath) - const filename = `${fullPath}/index.html` - const html = getRedirectHtml(newPath) - - mkdirp(fullPath) - fs.writeFileSync(filename, html) + redirectsPerVersion[oldPath] = newPath }) - console.log('done creating redirect files!\n') -} - -// redirect html files already exist in <=2.12 because these versions were deprecated on the old static site -function getRedirectHtml (newPath) { - return ` - - - -Redirecting... - - - - -

Redirecting...

-Click here if you are not redirected. - - -` + fs.writeFileSync(path.posix.join(finalDirectory, 'redirects.json'), JSON.stringify(redirectsPerVersion, null, 2)) } From dce6e7d5bd10fe596d25dfbdd627f9afcb655acb Mon Sep 17 00:00:00 2001 From: Sarah Schneider Date: Mon, 22 Feb 2021 11:13:52 -0500 Subject: [PATCH 2/4] udpate middleware to read redirects from top-level redirects.json in archival repo --- lib/enterprise-server-releases.js | 6 ++--- middleware/archived-enterprise-versions.js | 28 +++++++++++++++++----- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/lib/enterprise-server-releases.js b/lib/enterprise-server-releases.js index 5d315fc511ae..3e6f83c31a5a 100644 --- a/lib/enterprise-server-releases.js +++ b/lib/enterprise-server-releases.js @@ -47,8 +47,8 @@ const nextDeprecationDate = dates[oldestSupported].deprecationDate const isOldestReleaseDeprecated = new Date() > new Date(nextDeprecationDate) const deprecatedOnNewSite = deprecated.filter(version => versionSatisfiesRange(version, '>=2.13')) const firstVersionDeprecatedOnNewSite = '2.13' -// starting from 2.18, we updated the archival script to create stubbed HTML redirect files -const lastVersionWithoutStubbedRedirectFiles = '2.17' +// starting from 2.18, we updated the archival script to create a redirects.json top-level file in the archived repo +const lastVersionWithoutArchivedRedirectsFile = '2.17' // last version using paths like /enterprise/////
// instead of /enterprise-server@///
const lastReleaseWithLegacyFormat = '2.18' @@ -68,7 +68,7 @@ module.exports = { deprecatedOnNewSite, dates, firstVersionDeprecatedOnNewSite, - lastVersionWithoutStubbedRedirectFiles, + lastVersionWithoutArchivedRedirectsFile, lastReleaseWithLegacyFormat, deprecatedReleasesWithLegacyFormat, deprecatedReleasesWithNewFormat, diff --git a/middleware/archived-enterprise-versions.js b/middleware/archived-enterprise-versions.js index 682793d9757b..2f51d4a421e5 100644 --- a/middleware/archived-enterprise-versions.js +++ b/middleware/archived-enterprise-versions.js @@ -1,6 +1,6 @@ const path = require('path') const slash = require('slash') -const { firstVersionDeprecatedOnNewSite, lastVersionWithoutStubbedRedirectFiles } = require('../lib/enterprise-server-releases') +const { firstVersionDeprecatedOnNewSite, lastVersionWithoutArchivedRedirectsFile } = require('../lib/enterprise-server-releases') const patterns = require('../lib/patterns') const versionSatisfiesRange = require('../lib/version-satisfies-range') const isArchivedVersion = require('../lib/is-archived-version') @@ -25,21 +25,37 @@ module.exports = async (req, res, next) => { } // find redirects for versions between 2.13 and 2.17 - // starting with 2.18, we updated the archival script to create stubbed HTML redirect files + // starting with 2.18, we updated the archival script to create a redirects.json file if (versionSatisfiesRange(requestedVersion, `>=${firstVersionDeprecatedOnNewSite}`) && - versionSatisfiesRange(requestedVersion, `<=${lastVersionWithoutStubbedRedirectFiles}`)) { + versionSatisfiesRange(requestedVersion, `<=${lastVersionWithoutArchivedRedirectsFile}`)) { const redirect = archvivedRedirects[req.path] if (redirect && redirect !== req.path) { return res.redirect(301, redirect) } } + let reqPath = req.path + let isRedirect = false + if (versionSatisfiesRange(requestedVersion, `>${lastVersionWithoutArchivedRedirectsFile}`)) { + const redirectJson = await got(getProxyPath('redirects.json', requestedVersion)) + if (redirectJson[req.path]) { + isRedirect = true + } + reqPath = redirectJson[req.path] || req.path + } + try { - const r = await got(getProxyPath(req.path, requestedVersion)) + const r = await got(getProxyPath(reqPath, requestedVersion)) res.set('content-type', r.headers['content-type']) res.set('x-robots-tag', 'noindex') - // make the stubbed redirect files added in >=2.18 return 301 instead of 200 + // make redirects found via redirects.json return 301 instead of 200 + if (isRedirect) { + res.status(301) + res.set('location', reqPath) + } + + // make stubbed redirect files (which exist in versions <2.13) return 301 instead of 200 const staticRedirect = r.body.match(patterns.staticRedirect) if (staticRedirect) { res.status(301) @@ -73,7 +89,7 @@ function getProxyPath (reqPath, requestedVersion) { // this workaround finds potentially relevant frontmatter redirects in currently supported pages function getFallbackRedirects (req, requestedVersion) { if (versionSatisfiesRange(requestedVersion, `<${firstVersionDeprecatedOnNewSite}`)) return - if (versionSatisfiesRange(requestedVersion, `>${lastVersionWithoutStubbedRedirectFiles}`)) return + if (versionSatisfiesRange(requestedVersion, `>${lastVersionWithoutArchivedRedirectsFile}`)) return return archivedFrontmatterFallbacks.find(arrayOfFallbacks => arrayOfFallbacks.includes(req.path)) } From a28c7ac2a260af2e8e35dd3563383291816ebbf8 Mon Sep 17 00:00:00 2001 From: Sarah Schneider Date: Mon, 22 Feb 2021 11:14:01 -0500 Subject: [PATCH 3/4] add test --- tests/routing/deprecated-enterprise-versions.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/routing/deprecated-enterprise-versions.js b/tests/routing/deprecated-enterprise-versions.js index 8969b732b4e2..6ef55122e0b3 100644 --- a/tests/routing/deprecated-enterprise-versions.js +++ b/tests/routing/deprecated-enterprise-versions.js @@ -30,6 +30,12 @@ describe('enterprise deprecation', () => { expect(res.headers.location).toBe('/en/enterprise/2.15/user/articles/viewing-contributions-on-your-profile') }) + test('can access redirects from redirects.json in deprecated enterprise content >2.17', async () => { + const res = await get('/enterprise/2.19/admin/categories/time') + expect(res.statusCode).toBe(301) + expect(res.headers.location).toBe('/en/enterprise-server@2.19/admin/configuration/configuring-time-synchronization') + }) + test('handles requests for deprecated Enterprise pages ( >=2.13 )', async () => { expect(enterpriseServerReleases.deprecated.includes('2.13')).toBe(true) const $ = await getDOM('/en/enterprise/2.13/user/articles/about-branches') From 08bbe0396ff6a55990d8d969e20e04d2694da153 Mon Sep 17 00:00:00 2001 From: Sarah Schneider Date: Wed, 24 Feb 2021 10:58:02 -0500 Subject: [PATCH 4/4] put the got request in a try to prevent timeouts --- middleware/archived-enterprise-versions.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/middleware/archived-enterprise-versions.js b/middleware/archived-enterprise-versions.js index 2f51d4a421e5..48f497dbf37b 100644 --- a/middleware/archived-enterprise-versions.js +++ b/middleware/archived-enterprise-versions.js @@ -37,11 +37,16 @@ module.exports = async (req, res, next) => { let reqPath = req.path let isRedirect = false if (versionSatisfiesRange(requestedVersion, `>${lastVersionWithoutArchivedRedirectsFile}`)) { - const redirectJson = await got(getProxyPath('redirects.json', requestedVersion)) - if (redirectJson[req.path]) { - isRedirect = true + try { + const redirectJson = await got(getProxyPath('redirects.json', requestedVersion)) + + if (redirectJson[req.path]) { + isRedirect = true + } + reqPath = redirectJson[req.path] || req.path + } catch (err) { + // nooop } - reqPath = redirectJson[req.path] || req.path } try {