Skip to content

Commit 2ec7ac7

Browse files
authored
Merge pull request github#39339 from github/repo-sync
Repo sync
2 parents 8bd8e75 + 77ac78e commit 2ec7ac7

File tree

5 files changed

+86
-47
lines changed

5 files changed

+86
-47
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# ---------------------------------------------------------------
99
# To update the sha:
1010
# https://github.com/github/gh-base-image/pkgs/container/gh-base-image%2Fgh-base-noble
11-
FROM ghcr.io/github/gh-base-image/gh-base-noble:20250711-165924-g6f92253c7 AS base
11+
FROM ghcr.io/github/gh-base-image/gh-base-noble:20250715-152201-gef17a3886 AS base
1212

1313
# Install curl for Node install and determining the early access branch
1414
# Install git for cloning docs-early-access & translations repos

src/rest/lib/index.js renamed to src/rest/lib/index.ts

Lines changed: 75 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,26 @@ import { readCompressedJsonFileFallback } from '@/frame/lib/read-json-file'
55
import { getAutomatedPageMiniTocItems } from '@/frame/lib/get-mini-toc-items'
66
import { allVersions, getOpenApiVersion } from '@/versions/lib/all-versions'
77
import languages from '@/languages/lib/languages'
8+
import type { Context } from '@/types'
9+
import type { Operation } from '@/rest/components/types'
810

911
export const REST_DATA_DIR = 'src/rest/data'
1012
export const REST_SCHEMA_FILENAME = 'schema.json'
1113
const REST_CONTENT_DIR = 'content/rest'
1214

15+
// Type definitions for REST operations
16+
interface RestOperationCategory {
17+
[subcategory: string]: Operation[]
18+
}
19+
20+
interface RestOperationData {
21+
[category: string]: RestOperationCategory
22+
}
23+
24+
interface RestMiniTocData {
25+
restOperationsMiniTocItems: any[]
26+
}
27+
1328
/*
1429
Loads the schemas from the static/decorated folder into a single
1530
object organized by version. Not all products are calendar date
@@ -38,87 +53,107 @@ const REST_CONTENT_DIR = 'content/rest'
3853
}
3954
*/
4055
const NOT_API_VERSIONED = 'not_api_versioned'
41-
const restOperationData = new Map()
42-
const restOperations = new Map()
56+
const restOperationData = new Map<
57+
string,
58+
Map<string, Map<string, Map<string, Map<string, RestMiniTocData>>>>
59+
>()
60+
const restOperations = new Map<string, Map<string, RestOperationData>>()
4361

44-
Object.keys(languages).forEach((language) => {
62+
Object.keys(languages).forEach((language: string) => {
4563
restOperationData.set(language, new Map())
46-
Object.keys(allVersions).forEach((version) => {
64+
Object.keys(allVersions).forEach((version: string) => {
4765
// setting to undefined will allow us to perform checks
4866
// more easily later on
49-
restOperationData.get(language).set(version, new Map())
67+
restOperationData.get(language)!.set(version, new Map())
5068
if (allVersions[version].apiVersions && allVersions[version].apiVersions.length > 0) {
51-
allVersions[version].apiVersions.forEach((date) => {
52-
restOperationData.get(language).get(version).set(date, new Map())
69+
allVersions[version].apiVersions.forEach((date: string) => {
70+
restOperationData.get(language)!.get(version)!.set(date, new Map())
5371
})
5472
} else {
5573
// Products that are not been calendar date versioned
56-
restOperationData.get(language).get(version).set(NOT_API_VERSIONED, new Map())
74+
restOperationData.get(language)!.get(version)!.set(NOT_API_VERSIONED, new Map())
5775
}
5876
})
5977
})
6078

61-
export const categoriesWithoutSubcategories = fs
79+
export const categoriesWithoutSubcategories: string[] = fs
6280
.readdirSync(REST_CONTENT_DIR)
63-
.filter((file) => {
81+
.filter((file: string) => {
6482
return file.endsWith('.md') && !file.includes('index.md') && !file.includes('README.md')
6583
})
66-
.map((filteredFile) => filteredFile.replace('.md', ''))
84+
.map((filteredFile: string) => filteredFile.replace('.md', ''))
6785

6886
// version: plan + release e.g. For ghes-3.5, ghes is the plan and 3.5 is the release
6987
// apiVersion (not all versions have apiVersions): REST API Calendar Dates
7088
// openApiVersion (below, every version has an openApiVersion mapping): There's a mapping between our Docs versions
7189
// and the OpenApi Version bc it's not the same
72-
export default async function getRest(version, apiVersion, category, subCategory) {
90+
91+
export default async function getRest(
92+
version: string,
93+
apiVersion?: string,
94+
): Promise<RestOperationData> {
7395
const openApiVersion = getOpenApiVersion(version)
7496
const openapiSchemaName = apiVersion ? `${openApiVersion}-${apiVersion}` : `${openApiVersion}`
7597
const apiDate = apiVersion || NOT_API_VERSIONED
7698
const fileName = path.join(REST_DATA_DIR, openapiSchemaName, REST_SCHEMA_FILENAME)
99+
77100
if (!restOperations.has(openApiVersion)) {
78101
restOperations.set(openApiVersion, new Map())
79-
restOperations.get(openApiVersion).set(apiDate, new Map())
80102
// The `readCompressedJsonFileFallback()` function
81103
// will check for both a .br and .json extension.
82-
restOperations.get(openApiVersion).set(apiDate, readCompressedJsonFileFallback(fileName))
83-
} else if (!restOperations.get(openApiVersion).has(apiDate)) {
84-
restOperations.get(openApiVersion).set(apiDate, new Map())
104+
restOperations
105+
.get(openApiVersion)!
106+
.set(apiDate, readCompressedJsonFileFallback(fileName) as RestOperationData)
107+
} else if (!restOperations.get(openApiVersion)!.has(apiDate)) {
85108
// The `readCompressedJsonFileFallback()` function
86109
// will check for both a .br and .json extension.
87-
restOperations.get(openApiVersion).set(apiDate, readCompressedJsonFileFallback(fileName))
88-
}
89-
if (subCategory) {
90-
return restOperations.get(openApiVersion).get(apiDate)[category][subCategory]
91-
} else if (category) {
92-
return restOperations.get(openApiVersion).get(apiDate)[category]
93-
} else {
94-
return restOperations.get(openApiVersion).get(apiDate)
110+
restOperations
111+
.get(openApiVersion)!
112+
.set(apiDate, readCompressedJsonFileFallback(fileName) as RestOperationData)
95113
}
114+
115+
return restOperations.get(openApiVersion)!.get(apiDate)!
96116
}
97117

98118
// Generates the miniToc for a rest reference page.
99119
export async function getRestMiniTocItems(
100-
category,
101-
subCategory,
102-
apiVersion,
103-
restOperations,
104-
language,
105-
version,
106-
context,
107-
) {
120+
category: string,
121+
subCategory: string,
122+
apiVersion: string | undefined,
123+
restOperations: Operation[],
124+
language: string,
125+
version: string,
126+
context: Context,
127+
): Promise<RestMiniTocData> {
108128
const apiDate = apiVersion || NOT_API_VERSIONED
109129

110-
if (!restOperationData.get(language).get(version).get(apiDate).has(category)) {
111-
restOperationData.get(language).get(version).get(apiDate).set(category, new Map())
130+
const languageData = restOperationData.get(language)
131+
if (!languageData) {
132+
throw new Error(`Language ${language} not found in rest operation data`)
133+
}
134+
135+
const versionData = languageData.get(version)
136+
if (!versionData) {
137+
throw new Error(`Version ${version} not found for language ${language}`)
138+
}
139+
140+
const apiData = versionData.get(apiDate)
141+
if (!apiData) {
142+
throw new Error(`API date ${apiDate} not found for version ${version} and language ${language}`)
143+
}
144+
145+
if (!apiData.has(category)) {
146+
apiData.set(category, new Map())
112147
}
113148

114-
if (!restOperationData.get(language).get(version).get(apiDate).get(category).get(subCategory)) {
115-
const languageTree = restOperationData.get(language)
116-
const titles = restOperations.map((operation) => operation.title)
149+
const categoryData = apiData.get(category)!
150+
if (!categoryData.get(subCategory)) {
151+
const titles = restOperations.map((operation: Operation) => operation.title)
117152
const restOperationsMiniTocItems = await getAutomatedPageMiniTocItems(titles, context, 3)
118-
languageTree.get(version).get(apiDate).get(category).set(subCategory, {
153+
categoryData.set(subCategory, {
119154
restOperationsMiniTocItems,
120155
})
121-
restOperationData.set(restOperationData, languageTree)
122156
}
123-
return restOperationData.get(language).get(version).get(apiDate).get(category).get(subCategory)
157+
158+
return categoryData.get(subCategory)!
124159
}

src/rest/pages/category.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export default function Category({
6060
}
6161

6262
export const getServerSideProps: GetServerSideProps<Props> = async (context) => {
63-
const { default: getRest, getRestMiniTocItems } = await import('@/rest/lib/index.js')
63+
const { default: getRest, getRestMiniTocItems } = await import('@/rest/lib/index')
6464
const nonEnterpriseDefaultVersionModule = await import(
6565
'src/versions/lib/non-enterprise-default-version.js'
6666
)
@@ -86,15 +86,16 @@ export const getServerSideProps: GetServerSideProps<Props> = async (context) =>
8686
subcategory = category
8787
}
8888

89-
const restOperations = (await getRest(currentVersion, apiVersion, category, subcategory)) || []
89+
const restData = await getRest(currentVersion, apiVersion)
90+
const restOperations = (restData && restData[category] && restData[category][subcategory]) || []
9091

9192
// Build table of contents for all category operations for TocLanding:
9293
//
9394
// * get all operations for a category (will be broken up by subcategory)
9495
// * loop over subcategories and get the operations per subcategory
9596
// * get the minitoc items per set of subcategory operations
9697
// * with this data, build a collection of toc items that can be used by TocLanding
97-
const restCategoryOperations = (await getRest(currentVersion, apiVersion, category)) || []
98+
const restCategoryOperations = (restData && restData[category]) || {}
9899
const restCategoryTocItems = []
99100

100101
for (const [subCat, subCatOperations] of Object.entries(restCategoryOperations)) {

src/rest/pages/subcategory.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export default function SubCategory({ mainContext, automatedPageContext, restOpe
3535
}
3636

3737
export const getServerSideProps: GetServerSideProps<Props> = async (context) => {
38-
const { default: getRest, getRestMiniTocItems } = await import('@/rest/lib/index.js')
38+
const { default: getRest, getRestMiniTocItems } = await import('@/rest/lib/index')
3939

4040
const req = context.req as any
4141
const res = context.res as any
@@ -55,7 +55,8 @@ export const getServerSideProps: GetServerSideProps<Props> = async (context) =>
5555
subCategory = category
5656
}
5757

58-
const restOperations = (await getRest(currentVersion, apiVersion, category, subCategory)) || []
58+
const restData = await getRest(currentVersion, apiVersion)
59+
const restOperations = (restData && restData[category] && restData[category][subCategory]) || []
5960

6061
// Gets the miniTocItems in the article context. At this point it will only
6162
// include miniTocItems generated from the Markdown pages in

src/rest/tests/rendering.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ describe('REST references docs', () => {
1515
test('loads schema data for all versions', async () => {
1616
for (const version of Object.keys(allVersions)) {
1717
const calendarDate = allVersions[version].latestApiVersion
18-
const checksRestOperations = await getRest(version, calendarDate, 'checks', 'runs')
18+
const restData = await getRest(version, calendarDate)
19+
const checksRestOperations =
20+
(restData && restData['checks'] && restData['checks']['runs']) || []
1921
const $ = await getDOM(`/en/${version}/rest/checks/runs?restVersion=${calendarDate}`)
2022
const domH2Ids = $('h2')
2123
.map((i, h2) => $(h2).attr('id'))

0 commit comments

Comments
 (0)