Skip to content

Commit 1945974

Browse files
authored
Merge pull request #19046 from github/repo-sync
repo sync
2 parents 9672ebc + b6b2013 commit 1945974

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

middleware/redirects/handle-redirects.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import patterns from '../../lib/patterns.js'
22
import { URL } from 'url'
33
import { pathLanguagePrefixed } from '../../lib/languages.js'
4+
import { deprecatedWithFunctionalRedirects } from '../../lib/enterprise-server-releases.js'
45
import getRedirect from '../../lib/get-redirect.js'
56
import { cacheControlFactory } from '../cache-control.js'
67

@@ -65,7 +66,7 @@ export default function handleRedirects(req, res, next) {
6566
// But for example, a `/authentication/connecting-to-github-with-ssh`
6667
// needs to become `/en/authentication/connecting-to-github-with-ssh`
6768
const possibleRedirectTo = `/en${req.path}`
68-
if (possibleRedirectTo in req.context.pages) {
69+
if (possibleRedirectTo in req.context.pages || isDeprecatedAdminReleaseNotes(req.path)) {
6970
const language = getLanguage(req)
7071

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

8485
// do not redirect if the redirected page can't be found
85-
if (!req.context.pages[removeQueryParams(redirect)] && !redirect.includes('://')) {
86+
if (
87+
!(req.context.pages[removeQueryParams(redirect)] || isDeprecatedAdminReleaseNotes(req.path)) &&
88+
!redirect.includes('://')
89+
) {
8690
// display error on the page in development, but not in production
8791
// include final full redirect path in the message
8892
if (process.env.NODE_ENV !== 'production' && req.context) {
@@ -134,3 +138,20 @@ function usePermanentRedirect(req) {
134138
function removeQueryParams(redirect) {
135139
return new URL(redirect, 'https://docs.github.com').pathname
136140
}
141+
142+
function isDeprecatedAdminReleaseNotes(path) {
143+
// When we rewrote how redirects work, from a lookup model to a
144+
// functional model, the enterprise-server releases that got
145+
// deprecated since then fall between the cracks. Especially
146+
// for custom NextJS page-like pages like /admin/release-notes
147+
// These URLs don't come from any remaining .json lookup file
148+
// and they're not active pages either (e.g. req.context.pages)
149+
if (path.includes('admin/release-notes')) {
150+
for (const version of deprecatedWithFunctionalRedirects) {
151+
if (path.includes(`enterprise-server@${version}`)) {
152+
return true
153+
}
154+
}
155+
}
156+
return false
157+
}

tests/routing/redirects.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import path from 'path'
33
import { isPlainObject } from 'lodash-es'
44
import { describe, expect, jest, test } from '@jest/globals'
55

6-
import enterpriseServerReleases from '../../lib/enterprise-server-releases.js'
6+
import enterpriseServerReleases, {
7+
deprecatedWithFunctionalRedirects,
8+
} from '../../lib/enterprise-server-releases.js'
79
import Page from '../../lib/page.js'
810
import { get, head } from '../helpers/e2etest.js'
911
import versionSatisfiesRange from '../../lib/version-satisfies-range.js'
@@ -564,6 +566,27 @@ describe('redirects', () => {
564566
})
565567
})
566568

569+
describe('admin/release-notes redirects that lack language', () => {
570+
test('test redirect to an active enterprise-server version', async () => {
571+
const url = `/enterprise-server@${enterpriseServerReleases.latest}/admin/release-notes`
572+
const res = await get(url)
573+
expect(res.statusCode).toBe(302)
574+
expect(res.headers.location).toBe(`/en${url}`)
575+
})
576+
test('test redirect to a deprecated old enterprise-server version', async () => {
577+
const url = `/enterprise-server@2.22/admin/release-notes`
578+
const res = await get(url)
579+
expect(res.statusCode).toBe(302)
580+
expect(res.headers.location).toBe(`/en${url}`)
581+
})
582+
test('test redirect to a recently deprecated enterprise-server version', async () => {
583+
const url = `/enterprise-server@${deprecatedWithFunctionalRedirects[0]}/admin/release-notes`
584+
const res = await get(url)
585+
expect(res.statusCode).toBe(302)
586+
expect(res.headers.location).toBe(`/en${url}`)
587+
})
588+
})
589+
567590
// These tests exists because of issue #1960
568591
describe('rest reference redirects with default product', () => {
569592
test('rest subcategory with fpt in URL', async () => {

0 commit comments

Comments
 (0)