Skip to content

Commit

Permalink
Merge pull request #17940 from github/use-redirects-file-instead-of-s…
Browse files Browse the repository at this point in the history
…tubbed-archived-redirects

Use archived redirects.json files instead of stubbed redirect files
  • Loading branch information
sarahs authored Feb 24, 2021
2 parents 7ec38c4 + 9521bdc commit ecc5d01
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 37 deletions.
6 changes: 3 additions & 3 deletions lib/enterprise-server-releases.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/<release>/<user>/<product>/<category>/<article>
// instead of /enterprise-server@<release>/<product>/<category>/<article>
const lastReleaseWithLegacyFormat = '2.18'
Expand All @@ -68,7 +68,7 @@ module.exports = {
deprecatedOnNewSite,
dates,
firstVersionDeprecatedOnNewSite,
lastVersionWithoutStubbedRedirectFiles,
lastVersionWithoutArchivedRedirectsFile,
lastReleaseWithLegacyFormat,
deprecatedReleasesWithLegacyFormat,
deprecatedReleasesWithNewFormat,
Expand Down
33 changes: 27 additions & 6 deletions middleware/archived-enterprise-versions.js
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -25,21 +25,42 @@ 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}`)) {
try {
const redirectJson = await got(getProxyPath('redirects.json', requestedVersion))

if (redirectJson[req.path]) {
isRedirect = true
}
reqPath = redirectJson[req.path] || req.path
} catch (err) {
// nooop
}
}

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)
Expand Down Expand Up @@ -73,7 +94,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))
}
34 changes: 6 additions & 28 deletions script/enterprise-server-deprecations/archive-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
Expand Down Expand Up @@ -173,18 +172,20 @@ 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`)

process.exit()
})
}

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
Expand All @@ -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 `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Redirecting...</title>
<link rel="canonical" href="${newPath}">
<meta http-equiv="refresh" content="0; url=${newPath}">
</head>
<body>
<h1>Redirecting...</h1>
<a href="${newPath}">Click here if you are not redirected.</a>
<script>location='${newPath}'</script>
</body>
</html>`
fs.writeFileSync(path.posix.join(finalDirectory, 'redirects.json'), JSON.stringify(redirectsPerVersion, null, 2))
}
6 changes: 6 additions & 0 deletions tests/routing/deprecated-enterprise-versions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit ecc5d01

Please sign in to comment.