Skip to content

repo sync #19046

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions middleware/redirects/handle-redirects.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import patterns from '../../lib/patterns.js'
import { URL } from 'url'
import { pathLanguagePrefixed } from '../../lib/languages.js'
import { deprecatedWithFunctionalRedirects } from '../../lib/enterprise-server-releases.js'
import getRedirect from '../../lib/get-redirect.js'
import { cacheControlFactory } from '../cache-control.js'

Expand Down Expand Up @@ -65,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) {
if (possibleRedirectTo in req.context.pages || isDeprecatedAdminReleaseNotes(req.path)) {
const language = getLanguage(req)

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

// do not redirect if the redirected page can't be found
if (!req.context.pages[removeQueryParams(redirect)] && !redirect.includes('://')) {
if (
!(req.context.pages[removeQueryParams(redirect)] || isDeprecatedAdminReleaseNotes(req.path)) &&
!redirect.includes('://')
) {
// display error on the page in development, but not in production
// include final full redirect path in the message
if (process.env.NODE_ENV !== 'production' && req.context) {
Expand Down Expand Up @@ -134,3 +138,20 @@ function usePermanentRedirect(req) {
function removeQueryParams(redirect) {
return new URL(redirect, 'https://docs.github.com').pathname
}

function isDeprecatedAdminReleaseNotes(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
}
}
}
return false
}
25 changes: 24 additions & 1 deletion tests/routing/redirects.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import path from 'path'
import { isPlainObject } from 'lodash-es'
import { describe, expect, jest, test } from '@jest/globals'

import enterpriseServerReleases from '../../lib/enterprise-server-releases.js'
import enterpriseServerReleases, {
deprecatedWithFunctionalRedirects,
} from '../../lib/enterprise-server-releases.js'
import Page from '../../lib/page.js'
import { get, head } from '../helpers/e2etest.js'
import versionSatisfiesRange from '../../lib/version-satisfies-range.js'
Expand Down Expand Up @@ -564,6 +566,27 @@ describe('redirects', () => {
})
})

describe('admin/release-notes 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)
expect(res.statusCode).toBe(302)
expect(res.headers.location).toBe(`/en${url}`)
})
test('test redirect to a deprecated old enterprise-server version', async () => {
const url = `/enterprise-server@2.22/admin/release-notes`
const res = await get(url)
expect(res.statusCode).toBe(302)
expect(res.headers.location).toBe(`/en${url}`)
})
test('test redirect to a recently deprecated enterprise-server version', async () => {
const url = `/enterprise-server@${deprecatedWithFunctionalRedirects[0]}/admin/release-notes`
const res = await get(url)
expect(res.statusCode).toBe(302)
expect(res.headers.location).toBe(`/en${url}`)
})
})

// These tests exists because of issue #1960
describe('rest reference redirects with default product', () => {
test('rest subcategory with fpt in URL', async () => {
Expand Down