Skip to content

Commit

Permalink
Merge pull request #102 from github/repo-sync
Browse files Browse the repository at this point in the history
repo sync
  • Loading branch information
Octomerger authored Oct 2, 2020
2 parents a8acccc + cf7ffc9 commit 10d6370
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
26 changes: 21 additions & 5 deletions lib/old-versions-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const newVersions = Object.keys(require('./all-versions'))

// Given a new version like enterprise-server@2.21,
// return an old version like 2.21.
// Fall back to latest version if one can't be found.
// Fall back to latest GHES version if one can't be found,
// for example, if the new version is private-instances@latest.
function getOldVersionFromNewVersion (newVersion) {
return newVersion === nonEnterpriseDefaultVersion
? 'dotcom'
Expand All @@ -24,7 +25,7 @@ function getOldVersionFromNewVersion (newVersion) {

// Given an old version like 2.21,
// return a new version like enterprise-server@2.21.
// Fall back to latest version if one can't be found.
// Fall back to latest GHES version if one can't be found.
function getNewVersionFromOldVersion (oldVersion) {
return oldVersion === 'dotcom'
? nonEnterpriseDefaultVersion
Expand All @@ -33,17 +34,32 @@ function getNewVersionFromOldVersion (oldVersion) {

// Given an old path like /enterprise/2.21/user/github/category/article,
// return an old version like 2.21.
function getOldVersionFromOldPath (oldPath) {
function getOldVersionFromOldPath (oldPath, languageCode) {
// We should never be calling this function on a path that starts with a new version,
// so we can assume the path either uses the old /enterprise format or it's dotcom.
if (!patterns.enterprise.test(oldPath)) return 'dotcom'

const ghesNumber = oldPath.match(patterns.getEnterpriseVersionNumber)
return ghesNumber ? ghesNumber[1] : latest
}

// Given an old path like /en/enterprise/2.21/user/github/category/article,
// return a new path like /en/enterprise-server@2.21/github/category/article.
function getNewVersionedPath (oldPath, languageCode = '') {
const oldVersion = getOldVersionFromOldPath(oldPath)
const newVersion = getNewVersionFromOldVersion(oldVersion)
// It's possible a new version has been injected into an old path
// via syntax like: /en/enterprise/{{ currentVersion }}/admin/category/article
// which could resolve to /en/enterprise/private-instances@latest/admin/category/article,
// in which case the new version is the `private-instances@latest` segment.
// Get the second or third segment depending on whether there is a lang code.
const pathParts = oldPath.split('/')
const possibleVersion = languageCode ? pathParts[3] : pathParts[2]
let newVersion = newVersions.includes(possibleVersion) ? possibleVersion : ''

// If no new version was found, assume path contains an old version, like 2.21
if (!newVersion) {
const oldVersion = getOldVersionFromOldPath(oldPath, languageCode)
newVersion = getNewVersionFromOldVersion(oldVersion)
}

// Remove /<lang>?/enterprise?/<version>?/user? if present.
// This leaves only the part of the string that starts with the product.
Expand Down
5 changes: 3 additions & 2 deletions lib/path-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ function getVersionedPathWithoutLanguage (href, version) {
// example: enterprise-server@2.22 or free-pro-team@latest
let versionFromPath = getVersionStringFromPath(href)

// if this is an old versioned path, convert to new versioned path
// OLD: /enterprise/2.22/admin/installation or /enterprise/admin/installation
// if versionFromPath doesn't match any current versions, this may be an old
// versioned path that should be converted to new versioned path. Examples:
// OLD: /enterprise/2.22/admin/installation OR /enterprise/admin/installation
// NEW: /enterprise-server@2.22/admin/installation
// OLD: /desktop/installing-and-configuring-github-desktop
// NEW: /free-pro-team@latest/desktop/installing-and-configuring-github-desktop
Expand Down
6 changes: 4 additions & 2 deletions lib/patterns.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ module.exports = {
oldApiPath: /\/v[34]\/(?!guides|overview).+?\/.+/,
staticRedirect: /<link rel="canonical" href="(.+?)">/,
enterpriseNoVersion: /\/enterprise\/([^\d].*$)/,
// currentVersion in Liquid statements may inject 'enterprise-server@release' into old paths
oldEnterprisePath: /\/([a-z]{2}\/)?(enterprise\/)?(enterprise-server@)?(\d.\d+\/)?(user[/$])?/,
// a {{ currentVersion }} in internal links may inject '<new-version@release>' into old paths,
// so the oldEnterprisePath regex must match: /enterprise/private-instances@latest/user,
// /enterprise/enterprise-server@2.22/user, /enterprise/2.22/user, and /enterprise/user
oldEnterprisePath: /\/([a-z]{2}\/)?(enterprise\/)?(\S+?@(\S+?\/))?(\d.\d+\/)?(user[/$])?/,
// new versioning format patterns
adminProduct: /\/admin(\/|$|\?|#)/,
enterpriseServer: /\/enterprise-server@/,
Expand Down

0 comments on commit 10d6370

Please sign in to comment.