Skip to content

Commit

Permalink
Update src/archives to TypeScript (#53192)
Browse files Browse the repository at this point in the history
  • Loading branch information
heiskr authored Nov 20, 2024
1 parent fe64d24 commit 1ad5033
Show file tree
Hide file tree
Showing 15 changed files with 86 additions and 90 deletions.
2 changes: 1 addition & 1 deletion .github/actions/warmup-remotejson-cache/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ runs:
- name: Run script
if: ${{ inputs.restore-only == '' }}
shell: bash
run: node src/archives/scripts/warmup-remotejson.js
run: npm run warmup-remotejson

- name: Cache .remotejson-cache (save)
if: ${{ inputs.restore-only == '' }}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
"update-internal-links": "tsx src/links/scripts/update-internal-links.ts",
"validate-asset-images": "tsx src/assets/scripts/validate-asset-images.ts",
"validate-github-github-docs-urls": "tsx src/links/scripts/validate-github-github-docs-urls/index.ts",
"warmup-remotejson": "node src/archives/scripts/warmup-remotejson.js"
"warmup-remotejson": "tsx src/archives/scripts/warmup-remotejson.ts"
},
"lint-staged": {
"*.{js,mjs,ts,tsx}": "eslint --cache --fix",
Expand Down
9 changes: 0 additions & 9 deletions src/archives/lib/is-archived-version.d.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import patterns from '#src/frame/lib/patterns.js'
import { deprecated } from '#src/versions/lib/enterprise-server-releases.js'
import patterns from '@/frame/lib/patterns.js'
import { deprecated } from '@/versions/lib/enterprise-server-releases.js'
import type { ExtendedRequest } from '@/types'

export function isArchivedVersion(req) {
type IsArchivedInfo = {
isArchived?: boolean
requestedVersion?: string
}

export function isArchivedVersion(req: ExtendedRequest): IsArchivedInfo {
// if this is an assets path, use the referrer
// if this is a docs path, use the req.path
const pathToCheck = patterns.assetPaths.test(req.path) ? req.get('referrer') : req.path
return isArchivedVersionByPath(pathToCheck || '')
}

export function isArchivedVersionByPath(pathToCheck) {
export function isArchivedVersionByPath(pathToCheck: string): IsArchivedInfo {
// ignore paths that don't have an enterprise version number
if (
!(
Expand All @@ -22,10 +28,10 @@ export function isArchivedVersionByPath(pathToCheck) {
// extract enterprise version from path, e.g. 2.16
const requestedVersion = pathToCheck.includes('enterprise-server@')
? pathToCheck.match(patterns.getEnterpriseServerNumber)?.[1]
: pathToCheck.match(patterns.getEnterpriseVersionNumber)[1]
: pathToCheck.match(patterns.getEnterpriseVersionNumber)?.[1]

// bail if the request version is not deprecated
if (!deprecated.includes(requestedVersion)) {
if (!requestedVersion || !deprecated.includes(requestedVersion)) {
return {}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import path from 'path'
import { supported, latest } from '#src/versions/lib/enterprise-server-releases.js'
import patterns from '#src/frame/lib/patterns.js'
import nonEnterpriseDefaultVersion from '#src/versions/lib/non-enterprise-default-version.js'
import { allVersions } from '#src/versions/lib/all-versions.js'
import { supported, latest } from '@/versions/lib/enterprise-server-releases.js'
import patterns from '@/frame/lib/patterns.js'
import nonEnterpriseDefaultVersion from '@/versions/lib/non-enterprise-default-version.js'
import { allVersions } from '@/versions/lib/all-versions.js'
const latestNewVersion = `enterprise-server@${latest}`
const oldVersions = ['dotcom'].concat(supported)
const newVersions = Object.keys(allVersions)
Expand All @@ -18,7 +18,7 @@ const newVersions = Object.keys(allVersions)
// return an old version like 2.21.
// Fall back to latest GHES version if one can't be found,
// for example, if the new version is private-instances@latest.
export function getOldVersionFromNewVersion(newVersion) {
export function getOldVersionFromNewVersion(newVersion: string) {
return newVersion === nonEnterpriseDefaultVersion
? 'dotcom'
: oldVersions.find((oldVersion) => newVersion.includes(oldVersion)) || latest
Expand All @@ -27,15 +27,15 @@ export function getOldVersionFromNewVersion(newVersion) {
// Given an old version like 2.21,
// return a new version like [email protected].
// Fall back to latest GHES version if one can't be found.
export function getNewVersionFromOldVersion(oldVersion) {
export function getNewVersionFromOldVersion(oldVersion: string) {
return oldVersion === 'dotcom'
? nonEnterpriseDefaultVersion
: newVersions.find((newVersion) => newVersion.includes(oldVersion)) || latestNewVersion
}

// Given an old path like /enterprise/2.21/user/github/category/article,
// return an old version like 2.21.
export function getOldVersionFromOldPath(oldPath) {
export function getOldVersionFromOldPath(oldPath: string) {
// 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'
Expand All @@ -46,7 +46,7 @@ export function getOldVersionFromOldPath(oldPath) {

// Given an old path like /en/enterprise/2.21/user/github/category/article,
// return a new path like /en/[email protected]/github/category/article.
export function getNewVersionedPath(oldPath, languageCode = '') {
export function getNewVersionedPath(oldPath: string, languageCode = '') {
// 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,
Expand All @@ -58,7 +58,7 @@ export function getNewVersionedPath(oldPath, languageCode = '') {

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import got from 'got'
import type { Response, NextFunction } from 'express'

import patterns from '@/frame/lib/patterns.js'
import { isArchivedVersion } from '@/archives/lib/is-archived-version.js'
import { isArchivedVersion } from '@/archives/lib/is-archived-version'
import {
setFastlySurrogateKey,
SURROGATE_ENUMS,
Expand Down
2 changes: 1 addition & 1 deletion src/archives/middleware/archived-enterprise-versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from '@/versions/lib/enterprise-server-releases.js'
import patterns from '@/frame/lib/patterns.js'
import versionSatisfiesRange from '@/versions/lib/version-satisfies-range.js'
import { isArchivedVersion } from '@/archives/lib/is-archived-version.js'
import { isArchivedVersion } from '@/archives/lib/is-archived-version'
import {
setFastlySurrogateKey,
SURROGATE_ENUMS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@
// [end-readme]

import { program } from 'commander'
import semver from 'semver'
import semver, { SemVer } from 'semver'

import getRemoteJSON from '#src/frame/lib/get-remote-json.js'
import getRemoteJSON from '@/frame/lib/get-remote-json.js'
import {
deprecated,
firstReleaseStoredInBlobStorage,
lastVersionWithoutArchivedRedirectsFile,
} from '#src/versions/lib/enterprise-server-releases.js'

Expand All @@ -36,18 +35,14 @@ program

main()

function version2url(version) {
const inBlobStorage = semver.gte(
semver.coerce(version).raw,
semver.coerce(firstReleaseStoredInBlobStorage).raw,
)
function version2url(version: string | SemVer) {
return `https://github.github.com/docs-ghes-${version}/redirects.json`
}

function withArchivedRedirectsFile(version) {
function withArchivedRedirectsFile(version: string | SemVer) {
return semver.eq(
semver.coerce(version).raw,
semver.coerce(lastVersionWithoutArchivedRedirectsFile).raw,
semver.coerce(version)?.raw || '',
semver.coerce(lastVersionWithoutArchivedRedirectsFile)?.raw || '',
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { describe, expect, test, vi } from 'vitest'

import enterpriseServerReleases from '#src/versions/lib/enterprise-server-releases.js'
import { get, getDOM } from '#src/tests/helpers/e2etest.js'
import { SURROGATE_ENUMS } from '#src/frame/middleware/set-fastly-surrogate-key.js'
import enterpriseServerReleases from '@/versions/lib/enterprise-server-releases.js'
import { get, getDOM } from '@/tests/helpers/e2etest-ts'
import { SURROGATE_ENUMS } from '@/frame/middleware/set-fastly-surrogate-key.js'

describe('enterprise deprecation', () => {
vi.setConfig({ testTimeout: 60 * 1000 })
Expand Down Expand Up @@ -45,8 +45,8 @@ describe('enterprise deprecation', () => {

test('handles requests for deprecated Enterprise pages ( >=2.13 )', async () => {
expect(enterpriseServerReleases.deprecated.includes('2.13')).toBe(true)
const $ = await getDOM('/en/enterprise/2.13/user/articles/about-branches')
expect($.res.statusCode).toBe(200)
const { $, res } = await getDOM('/en/enterprise/2.13/user/articles/about-branches')
expect(res.statusCode).toBe(200)
expect($('h1').first().text()).toBe('About branches')
})

Expand All @@ -60,27 +60,27 @@ describe('enterprise deprecation', () => {

test('handles requests for deprecated Enterprise pages ( <2.13 )', async () => {
expect(enterpriseServerReleases.deprecated.includes('2.12')).toBe(true)
const $ = await getDOM('/enterprise/2.12/user/articles/about-branches')
expect($.res.statusCode).toBe(200)
const { $, res } = await getDOM('/enterprise/2.12/user/articles/about-branches')
expect(res.statusCode).toBe(200)
expect($('h2').text()).toBe('About branches')
})

test('handles requests for deprecated Enterprise version 11.10.340', async () => {
expect(enterpriseServerReleases.deprecated.includes('11.10.340')).toBe(true)
const $ = await getDOM('/enterprise/11.10.340/admin/articles/adding-teams')
expect($.res.statusCode).toBe(200)
const { $, res } = await getDOM('/enterprise/11.10.340/admin/articles/adding-teams')
expect(res.statusCode).toBe(200)
expect($('h2').text()).toBe('Adding teams')
})

test('has working admin guide links ( <2.13 )', async () => {
const guidesPath = '/enterprise/2.12/admin'
let $ = await getDOM(`${guidesPath}/guides`)
const firstLink = $('[class="guide-section"]').children('a').attr('href')
const { $: $1 } = await getDOM(`${guidesPath}/guides`)
const firstLink = $1('[class="guide-section"]').children('a').attr('href')

$ = await getDOM(`${guidesPath}/${firstLink}`)
expect($.res.statusCode).toBe(200)
const { $: $2, res } = await getDOM(`${guidesPath}/${firstLink}`)
expect(res.statusCode).toBe(200)
// this test assumes the Installation guide is the first link on the guides page
expect($('h2').text()).toBe('Installing and configuring GitHub Enterprise')
expect($2('h2').text()).toBe('Installing and configuring GitHub Enterprise')
})
})

Expand Down Expand Up @@ -139,22 +139,22 @@ describe('recently deprecated redirects', () => {

describe('deprecation banner', () => {
test('renders a deprecation warning banner on oldest supported Enterprise version', async () => {
const $ = await getDOM(`/en/enterprise/${enterpriseServerReleases.oldestSupported}`)
const { $ } = await getDOM(`/en/enterprise/${enterpriseServerReleases.oldestSupported}`)
expect($('[data-testid=deprecation-banner]').length).toBe(1)
})

test('does not render a deprecation warning banner on other Enterprise versions', async () => {
const $ = await getDOM(`/en/enterprise/${enterpriseServerReleases.latest}`)
const { $ } = await getDOM(`/en/enterprise/${enterpriseServerReleases.latest}`)
expect($('[data-testid=deprecation-banner]').length).toBe(0)
})

test('deprecation warning banner includes a date', async () => {
const $ = await getDOM(`/en/enterprise/${enterpriseServerReleases.oldestSupported}`)
const { $ } = await getDOM(`/en/enterprise/${enterpriseServerReleases.oldestSupported}`)
expect($('[data-testid=deprecation-banner] b').text().endsWith('discontinued on .')).toBe(false)
})

test('deprecation warning banner includes the right text depending on the date', async () => {
const $ = await getDOM(`/en/enterprise/${enterpriseServerReleases.oldestSupported}`)
const { $ } = await getDOM(`/en/enterprise/${enterpriseServerReleases.oldestSupported}`)
const expectedString = enterpriseServerReleases.isOldestReleaseDeprecated
? 'was discontinued'
: 'will be discontinued'
Expand All @@ -164,24 +164,28 @@ describe('deprecation banner', () => {

describe('does not render survey prompt or contribution button', () => {
test('does not render survey prompt', async () => {
let $ = await getDOM(`/en/enterprise/${enterpriseServerReleases.latest}/github`)
expect($('[data-testid="survey-form"]').length).toBeGreaterThan(0)
$ = await getDOM(`/en/enterprise/${enterpriseServerReleases.oldestSupported}/github`)
const { $: $1 } = await getDOM(`/en/enterprise/${enterpriseServerReleases.latest}/github`)
expect($1('[data-testid="survey-form"]').length).toBeGreaterThan(0)
const { $: $2 } = await getDOM(
`/en/enterprise/${enterpriseServerReleases.oldestSupported}/github`,
)
if (enterpriseServerReleases.isOldestReleaseDeprecated) {
expect($('[data-testid="survey-form"]').length).toBe(0)
expect($2('[data-testid="survey-form"]').length).toBe(0)
} else {
expect($('[data-testid="survey-form"]').length).toBeGreaterThan(0)
expect($2('[data-testid="survey-form"]').length).toBeGreaterThan(0)
}
})

test('does not render contribution button', async () => {
let $ = await getDOM(`/en/enterprise/${enterpriseServerReleases.latest}/github`)
expect($('.contribution').length).toBeGreaterThan(0)
$ = await getDOM(`/en/enterprise/${enterpriseServerReleases.oldestSupported}/github`)
const { $: $1 } = await getDOM(`/en/enterprise/${enterpriseServerReleases.latest}/github`)
expect($1('.contribution').length).toBeGreaterThan(0)
const { $: $2 } = await getDOM(
`/en/enterprise/${enterpriseServerReleases.oldestSupported}/github`,
)
if (enterpriseServerReleases.isOldestReleaseDeprecated) {
expect($('.contribution').length).toBe(0)
expect($2('.contribution').length).toBe(0)
} else {
expect($('[data-testid=survey-form]').length).toBeGreaterThan(0)
expect($2('[data-testid=survey-form]').length).toBeGreaterThan(0)
}
})
})
Expand Down
2 changes: 1 addition & 1 deletion src/content-render/unified/rewrite-local-links.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import stripAnsi from 'strip-ansi'
import { visit } from 'unist-util-visit'
import { distance } from 'fastest-levenshtein'
import { getPathWithoutLanguage, getVersionStringFromPath } from '#src/frame/lib/path-utils.js'
import { getNewVersionedPath } from '#src/archives/lib/old-versions-utils.js'
import { getNewVersionedPath } from '#src/archives/lib/old-versions-utils.ts'
import patterns from '#src/frame/lib/patterns.js'
import { deprecated, latest } from '#src/versions/lib/enterprise-server-releases.js'
import nonEnterpriseDefaultVersion from '#src/versions/lib/non-enterprise-default-version.js'
Expand Down
2 changes: 1 addition & 1 deletion src/frame/middleware/helmet.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { NextFunction, Request, Response } from 'express'
import helmet from 'helmet'
import { isArchivedVersion } from '@/archives/lib/is-archived-version.js'
import { isArchivedVersion } from '@/archives/lib/is-archived-version'
import versionSatisfiesRange from '@/versions/lib/version-satisfies-range.js'
import { languagePrefixPathRegex } from '@/languages/lib/languages.js'

Expand Down
2 changes: 1 addition & 1 deletion src/pageinfo/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import shortVersions from '@/versions/middleware/short-versions.js'
import contextualize from '@/frame/middleware/context/context'
import features from '@/versions/middleware/features.js'
import getRedirect from '@/redirects/lib/get-redirect.js'
import { isArchivedVersionByPath } from '@/archives/lib/is-archived-version.js'
import { isArchivedVersionByPath } from '@/archives/lib/is-archived-version'
import { readCompressedJsonFile } from '@/frame/lib/read-json-file.js'

const router = express.Router()
Expand Down
Loading

0 comments on commit 1ad5033

Please sign in to comment.