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

This commit is contained in:
Rachael Sewell 2023-02-15 11:03:08 -08:00 коммит произвёл GitHub
Родитель 8babc127b2
Коммит 6b4d76e09c
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
16 изменённых файлов: 28832 добавлений и 28832 удалений

Просмотреть файл

@ -139,4 +139,11 @@ export function getDocsVersion(openApiVersion) {
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 }

Просмотреть файл

@ -11,7 +11,8 @@ import {
import { MainContextT, MainContext, getMainContext } from 'components/context/MainContext'
import { Link } from 'components/Link'
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 = {
slug: string
@ -85,26 +86,21 @@ export default function Category({
}
export const getServerSideProps: GetServerSideProps<Props> = async (context) => {
const req = context.req as object
const res = context.res as object
const mainContext = await getMainContext(req, res)
const req = context.req as any
const res = context.res as any
const currentVersion = context.query.versionId as string
const apiVersion =
context.query.apiVersion || mainContext.allVersions[currentVersion].latestApiVersion
const allVersions = req.context.allVersions
const queryApiVersion = context.query.apiVersion
const apiVersion = allVersions[currentVersion].apiVersions.includes(queryApiVersion)
? queryApiVersion
: allVersions[currentVersion].latestApiVersion
const automatedPageContext = getAutomatedPageContextFromRequest(req)
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 {
props: {
mainContext,
mainContext: await getMainContext(req, res),
currentVersion,
enabledForApps,
automatedPageContext,

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Просмотреть файл

@ -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)
}

Просмотреть файл

@ -3,11 +3,11 @@ import path from 'path'
import { readCompressedJsonFileFallback } from '../../../lib/read-json-file.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'
const schemasPath = 'src/rest/data'
const ENABLED_APPS_FILENAME = 'src/github-apps/data/enabled-for-apps.json'
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.
export async function getRestMiniTocItems(
category,
@ -134,15 +127,3 @@ export async function getRestMiniTocItems(
}
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]
}

Просмотреть файл

@ -6,8 +6,9 @@ import { slug } from 'github-slugger'
import { allVersions } from '../../../../lib/all-versions.js'
import { categoriesWithoutSubcategories } from '../../lib/index.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'
const ENABLED_APPS = 'src/github-apps/data/enabled-for-apps.json'
const STATIC_REDIRECTS = 'lib/redirects/static/client-side-rest-api-redirects.json'
const REST_DECORATED_DIR = 'src/rest/data'
const REST_DEREFERENCED_DIR = 'src/rest/data/dereferenced'
@ -66,7 +67,6 @@ async function getWebhookOperations(webhookSchemas) {
}
async function createStaticRestFiles(restOperations) {
const operationsEnabledForGitHubApps = {}
const clientSideRedirects = await getCategoryOverrideRedirects()
for (const schemaName in restOperations) {
const operations = restOperations[schemaName]
@ -129,15 +129,17 @@ async function createStaticRestFiles(restOperations) {
await writeFile(restFilename, JSON.stringify(operationsByCategory, null, 2))
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
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) {
const categoryOperations = operations.filter((operation) => operation.category === category)
// This is a collection of operations that have `enabledForGitHubApps = true`
// It's grouped by resource title to make rendering easier
operationsEnabledForGitHubApps[schemaName][category] = categoryOperations
operationsEnabledForGitHubApps[category] = categoryOperations
.filter((operation) => operation.enabledForGitHubApps)
.map((operation) => ({
slug: slug(operation.title),
@ -146,10 +148,14 @@ async function createStaticRestFiles(restOperations) {
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')
console.log('Wrote', STATIC_REDIRECTS)
}

Просмотреть файл

@ -1,6 +1,6 @@
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'
export const WEBHOOK_DATA_DIR = 'src/webhooks/data'
@ -75,10 +75,3 @@ export async function getWebhooks(version) {
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
}

Просмотреть файл

@ -4,10 +4,8 @@ import { readdirSync, readFileSync } from 'fs'
import { join } from 'path'
import { get, getDOM } from '../helpers/e2etest.js'
import getRest, {
getEnabledForApps,
categoriesWithoutSubcategories,
} from '../../src/rest/lib/index.js'
import getRest, { categoriesWithoutSubcategories } from '../../src/rest/lib/index.js'
import { getEnabledForApps } from '../../src/github-apps/lib/index.js'
import { isApiVersioned, allVersions } from '../../lib/all-versions.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(
`/en/rest/overview/endpoints-available-for-github-apps?${new URLSearchParams({
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 () => {