Skip to content

Commit

Permalink
redirect to language url for ghes 3.0 and 3.1 (github#29136)
Browse files Browse the repository at this point in the history
Co-authored-by: Hector Alfaro <[email protected]>
  • Loading branch information
peterbe and hectorsector authored Jul 19, 2022
1 parent 44a91fd commit 9dce4cd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
15 changes: 7 additions & 8 deletions middleware/redirects/handle-redirects.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default function handleRedirects(req, res, next) {
// But for example, a `/authentication/connecting-to-github-with-ssh`
// needs to become `/en/authentication/connecting-to-github-with-ssh`
const possibleRedirectTo = `/en${req.path}`
if (possibleRedirectTo in req.context.pages || isDeprecatedAdminReleaseNotes(req.path)) {
if (possibleRedirectTo in req.context.pages || isDeprecatedVersion(req.path)) {
const language = getLanguage(req)

// Note, it's important to use `req.url` here and not `req.path`
Expand All @@ -84,7 +84,7 @@ export default function handleRedirects(req, res, next) {

// do not redirect if the redirected page can't be found
if (
!(req.context.pages[removeQueryParams(redirect)] || isDeprecatedAdminReleaseNotes(req.path)) &&
!(req.context.pages[removeQueryParams(redirect)] || isDeprecatedVersion(req.path)) &&
!redirect.includes('://')
) {
// display error on the page in development, but not in production
Expand Down Expand Up @@ -139,18 +139,17 @@ function removeQueryParams(redirect) {
return new URL(redirect, 'https://docs.github.com').pathname
}

function isDeprecatedAdminReleaseNotes(path) {
function isDeprecatedVersion(path) {
// When we rewrote how redirects work, from a lookup model to a
// functional model, the enterprise-server releases that got
// deprecated since then fall between the cracks. Especially
// for custom NextJS page-like pages like /admin/release-notes
// These URLs don't come from any remaining .json lookup file
// and they're not active pages either (e.g. req.context.pages)
if (path.includes('admin/release-notes')) {
for (const version of deprecatedWithFunctionalRedirects) {
if (path.includes(`enterprise-server@${version}`)) {
return true
}
const split = path.split('/')
for (const version of deprecatedWithFunctionalRedirects) {
if (split.includes(`enterprise-server@${version}`)) {
return true
}
}
return false
Expand Down
8 changes: 7 additions & 1 deletion tests/routing/redirects.js
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ describe('redirects', () => {
})
})

describe('admin/release-notes redirects that lack language', () => {
describe('recently deprecated ghes version redirects that lack language', () => {
test('test redirect to an active enterprise-server version', async () => {
const url = `/enterprise-server@${enterpriseServerReleases.latest}/admin/release-notes`
const res = await get(url)
Expand All @@ -585,6 +585,12 @@ describe('redirects', () => {
expect(res.statusCode).toBe(302)
expect(res.headers.location).toBe(`/en${url}`)
})
test('any enterprise-server deprecated with functional redirects', async () => {
const url = `/enterprise-server@${deprecatedWithFunctionalRedirects[0]}`
const res = await get(url)
expect(res.statusCode).toBe(302)
expect(res.headers.location).toBe(`/en${url}`)
})
})

// These tests exists because of issue #1960
Expand Down

0 comments on commit 9dce4cd

Please sign in to comment.