@@ -15,7 +15,8 @@ const newVersions = Object.keys(require('./all-versions'))
15
15
16
16
// Given a new version like enterprise-server@2.21,
17
17
// return an old version like 2.21.
18
- // Fall back to latest version if one can't be found.
18
+ // Fall back to latest GHES version if one can't be found,
19
+ // for example, if the new version is private-instances@latest.
19
20
function getOldVersionFromNewVersion ( newVersion ) {
20
21
return newVersion === nonEnterpriseDefaultVersion
21
22
? 'dotcom'
@@ -24,7 +25,7 @@ function getOldVersionFromNewVersion (newVersion) {
24
25
25
26
// Given an old version like 2.21,
26
27
// return a new version like enterprise-server@2.21.
27
- // Fall back to latest version if one can't be found.
28
+ // Fall back to latest GHES version if one can't be found.
28
29
function getNewVersionFromOldVersion ( oldVersion ) {
29
30
return oldVersion === 'dotcom'
30
31
? nonEnterpriseDefaultVersion
@@ -33,17 +34,32 @@ function getNewVersionFromOldVersion (oldVersion) {
33
34
34
35
// Given an old path like /enterprise/2.21/user/github/category/article,
35
36
// return an old version like 2.21.
36
- function getOldVersionFromOldPath ( oldPath ) {
37
+ function getOldVersionFromOldPath ( oldPath , languageCode ) {
38
+ // We should never be calling this function on a path that starts with a new version,
39
+ // so we can assume the path either uses the old /enterprise format or it's dotcom.
37
40
if ( ! patterns . enterprise . test ( oldPath ) ) return 'dotcom'
41
+
38
42
const ghesNumber = oldPath . match ( patterns . getEnterpriseVersionNumber )
39
43
return ghesNumber ? ghesNumber [ 1 ] : latest
40
44
}
41
45
42
46
// Given an old path like /en/enterprise/2.21/user/github/category/article,
43
47
// return a new path like /en/enterprise-server@2.21/github/category/article.
44
48
function getNewVersionedPath ( oldPath , languageCode = '' ) {
45
- const oldVersion = getOldVersionFromOldPath ( oldPath )
46
- const newVersion = getNewVersionFromOldVersion ( oldVersion )
49
+ // It's possible a new version has been injected into an old path
50
+ // via syntax like: /en/enterprise/{{ currentVersion }}/admin/category/article
51
+ // which could resolve to /en/enterprise/private-instances@latest/admin/category/article,
52
+ // in which case the new version is the `private-instances@latest` segment.
53
+ // Get the second or third segment depending on whether there is a lang code.
54
+ const pathParts = oldPath . split ( '/' )
55
+ const possibleVersion = languageCode ? pathParts [ 3 ] : pathParts [ 2 ]
56
+ let newVersion = newVersions . includes ( possibleVersion ) ? possibleVersion : ''
57
+
58
+ // If no new version was found, assume path contains an old version, like 2.21
59
+ if ( ! newVersion ) {
60
+ const oldVersion = getOldVersionFromOldPath ( oldPath , languageCode )
61
+ newVersion = getNewVersionFromOldVersion ( oldVersion )
62
+ }
47
63
48
64
// Remove /<lang>?/enterprise?/<version>?/user? if present.
49
65
// This leaves only the part of the string that starts with the product.
0 commit comments