1
0
mirror of synced 2025-12-19 18:10:59 -05:00

break version-specific data into individual files (#34717)

This commit is contained in:
Rachael Sewell
2023-02-15 11:03:08 -08:00
committed by GitHub
parent 8babc127b2
commit 6b4d76e09c
16 changed files with 28832 additions and 28832 deletions

View File

@@ -139,4 +139,11 @@ export function getDocsVersion(openApiVersion) {
return matchingVersion.version return matchingVersion.version
} }
export function getOpenApiVersion(version) {
if (!(version in allVersions)) {
throw new Error(`Unrecognized version '${version}'. Not found in ${Object.keys(allVersions)}`)
}
return allVersions[version].openApiVersionName
}
export { allVersions } export { allVersions }

View File

@@ -11,7 +11,8 @@ import {
import { MainContextT, MainContext, getMainContext } from 'components/context/MainContext' import { MainContextT, MainContext, getMainContext } from 'components/context/MainContext'
import { Link } from 'components/Link' import { Link } from 'components/Link'
import { RestRedirect } from 'components/RestRedirect' import { RestRedirect } from 'components/RestRedirect'
import { getEnabledForApps, categoriesWithoutSubcategories } from 'src/rest/lib/index.js' import { categoriesWithoutSubcategories } from 'src/rest/lib/index.js'
import { getEnabledForApps } from 'src/github-apps/lib/index.js'
type OperationT = { type OperationT = {
slug: string slug: string
@@ -85,26 +86,21 @@ export default function Category({
} }
export const getServerSideProps: GetServerSideProps<Props> = async (context) => { export const getServerSideProps: GetServerSideProps<Props> = async (context) => {
const req = context.req as object const req = context.req as any
const res = context.res as object const res = context.res as any
const mainContext = await getMainContext(req, res)
const currentVersion = context.query.versionId as string const currentVersion = context.query.versionId as string
const apiVersion = const allVersions = req.context.allVersions
context.query.apiVersion || mainContext.allVersions[currentVersion].latestApiVersion const queryApiVersion = context.query.apiVersion
const apiVersion = allVersions[currentVersion].apiVersions.includes(queryApiVersion)
? queryApiVersion
: allVersions[currentVersion].latestApiVersion
const automatedPageContext = getAutomatedPageContextFromRequest(req) const automatedPageContext = getAutomatedPageContextFromRequest(req)
const enabledForApps = await getEnabledForApps(currentVersion, apiVersion) const enabledForApps = await getEnabledForApps(currentVersion, apiVersion)
// If getEnabledForApps came back as undefined, it means that nothing
// could be found for that `apiVersion`
if (!enabledForApps) {
return {
notFound: true,
}
}
return { return {
props: { props: {
mainContext, mainContext: await getMainContext(req, res),
currentVersion, currentVersion,
enabledForApps, enabledForApps,
automatedPageContext, automatedPageContext,

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,21 @@
import { readCompressedJsonFileFallback } from '../../../lib/read-json-file.js'
import { getOpenApiVersion } from '../../../lib/all-versions.js'
export const ENABLED_APPS_DIR = 'src/github-apps/data'
export const ENABLED_APPS_FILENAME = 'server-to-server-rest.json'
const enabledForApps = new Map()
export async function getEnabledForApps(docsVersion, apiVersion) {
const openApiVersion = getOpenApiVersion(docsVersion) + (apiVersion ? `.${apiVersion}` : '')
if (!enabledForApps.has(openApiVersion)) {
// The `readCompressedJsonFileFallback()` function
// will check for both a .br and .json extension.
const data = readCompressedJsonFileFallback(
`${ENABLED_APPS_DIR}/${openApiVersion}/${ENABLED_APPS_FILENAME}`
)
enabledForApps.set(openApiVersion, data)
}
return enabledForApps.get(openApiVersion)
}

View File

@@ -3,11 +3,11 @@ import path from 'path'
import { readCompressedJsonFileFallback } from '../../../lib/read-json-file.js' import { readCompressedJsonFileFallback } from '../../../lib/read-json-file.js'
import { getAutomatedPageMiniTocItems } from '../../../lib/get-mini-toc-items.js' import { getAutomatedPageMiniTocItems } from '../../../lib/get-mini-toc-items.js'
import { allVersions } from '../../../lib/all-versions.js' import { allVersions, getOpenApiVersion } from '../../../lib/all-versions.js'
import languages from '../../../lib/languages.js' import languages from '../../../lib/languages.js'
const schemasPath = 'src/rest/data' const schemasPath = 'src/rest/data'
const ENABLED_APPS_FILENAME = 'src/github-apps/data/enabled-for-apps.json'
const contentPath = 'content/rest' const contentPath = 'content/rest'
/* /*
@@ -100,13 +100,6 @@ export default async function getRest(version, apiVersion, category, subCategory
} }
} }
function getOpenApiVersion(version) {
if (!(version in allVersions)) {
throw new Error(`Unrecognized version '${version}'. Not found in ${Object.keys(allVersions)}`)
}
return allVersions[version].openApiVersionName
}
// Generates the miniToc for a rest reference page. // Generates the miniToc for a rest reference page.
export async function getRestMiniTocItems( export async function getRestMiniTocItems(
category, category,
@@ -134,15 +127,3 @@ export async function getRestMiniTocItems(
} }
return restOperationData.get(language).get(version).get(apiDate).get(category).get(subCategory) return restOperationData.get(language).get(version).get(apiDate).get(category).get(subCategory)
} }
const enabledForApps = {}
export async function getEnabledForApps(docsVersion, apiVersion) {
if (Object.keys(enabledForApps).length === 0) {
// The `readCompressedJsonFileFallback()` function
// will check for both a .br and .json extension.
Object.assign(enabledForApps, readCompressedJsonFileFallback(ENABLED_APPS_FILENAME))
}
const openApiVersion = getOpenApiVersion(docsVersion) + (apiVersion ? `.${apiVersion}` : '')
return enabledForApps[openApiVersion]
}

View File

@@ -6,8 +6,9 @@ import { slug } from 'github-slugger'
import { allVersions } from '../../../../lib/all-versions.js' import { allVersions } from '../../../../lib/all-versions.js'
import { categoriesWithoutSubcategories } from '../../lib/index.js' import { categoriesWithoutSubcategories } from '../../lib/index.js'
import getOperations, { getWebhooks } from './get-operations.js' import getOperations, { getWebhooks } from './get-operations.js'
import { ENABLED_APPS_DIR, ENABLED_APPS_FILENAME } from '../../../github-apps/lib/index.js'
import { WEBHOOK_DATA_DIR, WEBHOOK_SCHEMA_FILENAME } from '../../../webhooks/lib/index.js' import { WEBHOOK_DATA_DIR, WEBHOOK_SCHEMA_FILENAME } from '../../../webhooks/lib/index.js'
const ENABLED_APPS = 'src/github-apps/data/enabled-for-apps.json'
const STATIC_REDIRECTS = 'lib/redirects/static/client-side-rest-api-redirects.json' const STATIC_REDIRECTS = 'lib/redirects/static/client-side-rest-api-redirects.json'
const REST_DECORATED_DIR = 'src/rest/data' const REST_DECORATED_DIR = 'src/rest/data'
const REST_DEREFERENCED_DIR = 'src/rest/data/dereferenced' const REST_DEREFERENCED_DIR = 'src/rest/data/dereferenced'
@@ -66,7 +67,6 @@ async function getWebhookOperations(webhookSchemas) {
} }
async function createStaticRestFiles(restOperations) { async function createStaticRestFiles(restOperations) {
const operationsEnabledForGitHubApps = {}
const clientSideRedirects = await getCategoryOverrideRedirects() const clientSideRedirects = await getCategoryOverrideRedirects()
for (const schemaName in restOperations) { for (const schemaName in restOperations) {
const operations = restOperations[schemaName] const operations = restOperations[schemaName]
@@ -129,15 +129,17 @@ async function createStaticRestFiles(restOperations) {
await writeFile(restFilename, JSON.stringify(operationsByCategory, null, 2)) await writeFile(restFilename, JSON.stringify(operationsByCategory, null, 2))
console.log('Wrote', path.relative(process.cwd(), restFilename)) console.log('Wrote', path.relative(process.cwd(), restFilename))
// Create the enabled-for-apps.json file used for // Create the src/github-apps/data files used for
// https://docs.github.com/en/rest/overview/endpoints-available-for-github-apps // https://docs.github.com/en/rest/overview/endpoints-available-for-github-apps
operationsEnabledForGitHubApps[schemaName] = {} const enabledAppsFilename = path.join(ENABLED_APPS_DIR, schemaName, ENABLED_APPS_FILENAME)
const enabledAppsVersionDir = path.join(ENABLED_APPS_DIR, schemaName)
const operationsEnabledForGitHubApps = {}
for (const category of categories) { for (const category of categories) {
const categoryOperations = operations.filter((operation) => operation.category === category) const categoryOperations = operations.filter((operation) => operation.category === category)
// This is a collection of operations that have `enabledForGitHubApps = true` // This is a collection of operations that have `enabledForGitHubApps = true`
// It's grouped by resource title to make rendering easier // It's grouped by resource title to make rendering easier
operationsEnabledForGitHubApps[schemaName][category] = categoryOperations operationsEnabledForGitHubApps[category] = categoryOperations
.filter((operation) => operation.enabledForGitHubApps) .filter((operation) => operation.enabledForGitHubApps)
.map((operation) => ({ .map((operation) => ({
slug: slug(operation.title), slug: slug(operation.title),
@@ -146,10 +148,14 @@ async function createStaticRestFiles(restOperations) {
requestPath: operation.requestPath, requestPath: operation.requestPath,
})) }))
} }
// When a new version is added, we need to create the directory for it
if (!existsSync(enabledAppsVersionDir)) {
mkdirSync(enabledAppsVersionDir)
}
await writeFile(enabledAppsFilename, JSON.stringify(operationsEnabledForGitHubApps, null, 2))
console.log('Wrote', enabledAppsFilename)
} }
await writeFile(ENABLED_APPS, JSON.stringify(operationsEnabledForGitHubApps, null, 2))
console.log('Wrote', ENABLED_APPS)
await writeFile(STATIC_REDIRECTS, JSON.stringify(clientSideRedirects, null, 2), 'utf8') await writeFile(STATIC_REDIRECTS, JSON.stringify(clientSideRedirects, null, 2), 'utf8')
console.log('Wrote', STATIC_REDIRECTS) console.log('Wrote', STATIC_REDIRECTS)
} }

View File

@@ -1,6 +1,6 @@
import path from 'path' import path from 'path'
import { allVersions } from '../../../lib/all-versions.js' import { getOpenApiVersion } from '../../../lib/all-versions.js'
import { readCompressedJsonFileFallback } from '../../../lib/read-json-file.js' import { readCompressedJsonFileFallback } from '../../../lib/read-json-file.js'
export const WEBHOOK_DATA_DIR = 'src/webhooks/data' export const WEBHOOK_DATA_DIR = 'src/webhooks/data'
@@ -75,10 +75,3 @@ export async function getWebhooks(version) {
return webhooksCache.get(openApiVersion) return webhooksCache.get(openApiVersion)
} }
function getOpenApiVersion(version) {
if (!(version in allVersions)) {
throw new Error(`Unrecognized version '${version}'. Not found in ${Object.keys(allVersions)}`)
}
return allVersions[version].openApiVersionName
}

View File

@@ -4,10 +4,8 @@ import { readdirSync, readFileSync } from 'fs'
import { join } from 'path' import { join } from 'path'
import { get, getDOM } from '../helpers/e2etest.js' import { get, getDOM } from '../helpers/e2etest.js'
import getRest, { import getRest, { categoriesWithoutSubcategories } from '../../src/rest/lib/index.js'
getEnabledForApps, import { getEnabledForApps } from '../../src/github-apps/lib/index.js'
categoriesWithoutSubcategories,
} from '../../src/rest/lib/index.js'
import { isApiVersioned, allVersions } from '../../lib/all-versions.js' import { isApiVersioned, allVersions } from '../../lib/all-versions.js'
import { getDiffOpenAPIContentRest } from '../../src/rest/scripts/test-open-api-schema.js' import { getDiffOpenAPIContentRest } from '../../src/rest/scripts/test-open-api-schema.js'
@@ -100,13 +98,13 @@ describe('REST references docs', () => {
} }
}) })
test('404 if unrecognized apiVersion', async () => { test('falls back when unsupported calendar version provided', async () => {
const res = await get( const res = await get(
`/en/rest/overview/endpoints-available-for-github-apps?${new URLSearchParams({ `/en/rest/overview/endpoints-available-for-github-apps?${new URLSearchParams({
apiVersion: 'junk', apiVersion: 'junk',
})}` })}`
) )
expect(res.statusCode).toBe(404) expect(res.statusCode).toBe(200)
}) })
test('test the latest version of the OpenAPI schema categories/subcategories to see if it matches the content/rest directory', async () => { test('test the latest version of the OpenAPI schema categories/subcategories to see if it matches the content/rest directory', async () => {