MCPcopy
hub / github.com/github/docs / getVersionStringFromPath

Function getVersionStringFromPath

lib/path-utils.js:36–82  ·  view source on GitHub ↗
(href, supportedOnly = false)

Source from the content-addressed store, hash-verified

34
35// Return the version segment in a path
36export function getVersionStringFromPath(href, supportedOnly = false) {
37 href = getPathWithoutLanguage(href)
38
39 // Some URLs don't ever have a version in the URL and it won't be found
40 // if you continue digging deeper. So exit early with the default on these.
41 if (['/', '/categories.json'].includes(href)) {
42 return nonEnterpriseDefaultVersion
43 }
44
45 // Get the first segment
46 const versionFromPath = href.split('/')[1]
47
48 // If the first segment is a supported product, assume this is FPT
49 if (productIds.includes(versionFromPath)) {
50 return nonEnterpriseDefaultVersion
51 }
52
53 // Otherwise, check if it's a supported version
54 if (supportedVersions.has(versionFromPath)) {
55 return versionFromPath
56 }
57
58 // If the version segment is the latest enterprise-server release, return the latest release
59 if (versionFromPath === 'enterprise-server@latest') {
60 return `enterprise-server@${latest}`
61 }
62
63 // If it's just a plan with no @release (e.g., `enterprise-server`), return the latest release
64 const planObject = Object.values(allVersions).find((v) => v.plan === versionFromPath)
65 if (planObject) {
66 return allVersions[planObject.latestVersion].version
67 }
68
69 // If the caller of this function explicitly wants to know if the
70 // version part is *not* supported, they get back `undefined`.
71 // But this function is used in many other places where it potentially
72 // doesn't care if the version is supported.
73 // For example, in lib/redirects/permalinks.js it needs to know if the
74 // URL didn't contain a valid version.
75 if (supportedOnly) {
76 return
77 }
78
79 // Otherwise, return the first segment as-is, which may not be a real supported version,
80 // but additional checks are done on this segment in getVersionedPathWithoutLanguage
81 return versionFromPath
82}
83
84// Return the corresponding object for the version segment in a path
85export function getVersionObjectFromPath(href) {

Callers 4

getNewHrefFunction · 0.90
contextualizeFunction · 0.85
getPathWithoutVersionFunction · 0.85
getVersionObjectFromPathFunction · 0.85

Calls 1

getPathWithoutLanguageFunction · 0.85

Tested by

no test coverage detected