automate github apps docs (#35530)
Co-authored-by: Sarah Edwards <skedwards88@github.com>
This commit is contained in:
@@ -1,42 +1,48 @@
|
||||
import { jest, test } from '@jest/globals'
|
||||
import { readFile } from 'fs/promises'
|
||||
import { allVersions } from '../../../lib/all-versions.js'
|
||||
|
||||
import { get, getDOM } from '../../../tests/helpers/e2etest.js'
|
||||
import { categoriesWithoutSubcategories } from '../../rest/lib/index.js'
|
||||
import { getEnabledForApps } from '../lib/index.js'
|
||||
import { isApiVersioned, allVersions } from '../../../lib/all-versions.js'
|
||||
import { categoriesWithoutSubcategories } from '#src/rest/lib/index.js'
|
||||
import { getAppsData } from '#src/github-apps/lib/index.js'
|
||||
|
||||
const configContent = JSON.parse(await readFile('src/github-apps/lib/config.json', 'utf8'))
|
||||
const pageTypes = Object.keys(configContent.pages)
|
||||
const permissionPages = pageTypes.filter((pageType) => pageType.includes('permissions'))
|
||||
const enabledPages = pageTypes.filter((pageType) => !pageType.includes('permissions'))
|
||||
const defaultVersion = Object.values(allVersions)
|
||||
.filter((version) => version.nonEnterpriseDefault)
|
||||
.shift()
|
||||
const version = defaultVersion.version
|
||||
const apiVersion = defaultVersion.latestApiVersion
|
||||
|
||||
describe('REST references docs', () => {
|
||||
jest.setTimeout(3 * 60 * 1000)
|
||||
|
||||
// This test ensures that the page component and the Markdown file are
|
||||
// in sync. It also checks that all expected items are present.
|
||||
// For every version of /rest/overview/endpoints-available-for-github-apps
|
||||
test('loads operations enabled for GitHub Apps', async () => {
|
||||
const flatMapping = getFlatMappingWithCalendarDates()
|
||||
|
||||
for (const [version, versionValue] of Object.entries(flatMapping)) {
|
||||
test('loads enabled list pages', async () => {
|
||||
for (const page of enabledPages) {
|
||||
const schemaSlugs = []
|
||||
const enabledForApps = await getEnabledForApps(version, versionValue.apiVersion)
|
||||
|
||||
const enabledForApps = await getAppsData(page, version, apiVersion)
|
||||
|
||||
// using the static file, generate the expected slug for each operation
|
||||
for (const [key, value] of Object.entries(enabledForApps)) {
|
||||
schemaSlugs.push(
|
||||
...value.map(
|
||||
(item) =>
|
||||
`/en${
|
||||
versionValue.url === '' ? versionValue.url : `/${versionValue.url}`
|
||||
}/rest/${key}${
|
||||
`/en/rest/${key}${
|
||||
categoriesWithoutSubcategories.includes(key) ? '' : '/' + item.subcategory
|
||||
}#${item.slug}`
|
||||
)
|
||||
)
|
||||
}
|
||||
// get all of the href attributes in the anchor tags
|
||||
const $ = await getDOM(
|
||||
`/en/${versionValue.url}/rest/overview/endpoints-available-for-github-apps${
|
||||
versionValue.apiVersion ? `?apiVersion=${versionValue.apiVersion}` : ''
|
||||
}`
|
||||
)
|
||||
const contentPath = configContent.pages[page].targetFilename
|
||||
.replace('content/', '')
|
||||
.replace('.md', '')
|
||||
const $ = await getDOM(`/en/${contentPath}${apiVersion ? `?apiVersion=${apiVersion}` : ''}`)
|
||||
const domH3Ids = $('#article-contents a')
|
||||
.map((i, a) => $(a).attr('href'))
|
||||
.get()
|
||||
@@ -44,40 +50,46 @@ describe('REST references docs', () => {
|
||||
}
|
||||
})
|
||||
|
||||
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(200)
|
||||
})
|
||||
})
|
||||
test('loads permission list pages', async () => {
|
||||
// permissions pages
|
||||
for (const page of permissionPages) {
|
||||
const schemaSlugs = []
|
||||
|
||||
// This gets a flat mapping for all REST versions (versioned and unversioned) and creates a mapping between
|
||||
// the full name of the version including calendar dates to its url name and apiVersion if it exists.
|
||||
// Example:
|
||||
// {
|
||||
// free-pro-team@latest: { url: '', apiVersion: 2022-08-09 }
|
||||
// free-pro-team@latest: { url: '', apiVersion: 2022-11-14 }
|
||||
// enterprise-cloud@latest: { url: 'enterprise-cloud@latest, apiVersion: 2022-11-14 }
|
||||
// enterprise-server@3.6: { url: enterprise-server@3.6 }
|
||||
// }
|
||||
function getFlatMappingWithCalendarDates() {
|
||||
const flatMapping = {}
|
||||
const permissionsData = await getAppsData(page, version, apiVersion)
|
||||
|
||||
for (const version in allVersions) {
|
||||
if (isApiVersioned(version)) {
|
||||
for (const apiVersion of allVersions[version].apiVersions) {
|
||||
flatMapping[allVersions[version].version] = {
|
||||
url: `${version === 'free-pro-team@latest' ? '' : allVersions[version].version}`,
|
||||
apiVersion,
|
||||
}
|
||||
// using the static file, generate the expected slug for each operation
|
||||
for (const value of Object.values(permissionsData)) {
|
||||
schemaSlugs.push(
|
||||
...value.permissions.map(
|
||||
(item) =>
|
||||
`/en/rest/${item.category}${
|
||||
categoriesWithoutSubcategories.includes(item.category) ? '' : '/' + item.subcategory
|
||||
}#${item.slug}`
|
||||
)
|
||||
)
|
||||
}
|
||||
} else {
|
||||
flatMapping[allVersions[version].version] = { url: allVersions[version].version }
|
||||
}
|
||||
}
|
||||
|
||||
return flatMapping
|
||||
}
|
||||
// get all of the href attributes in the anchor tags
|
||||
const contentPath = configContent.pages[page].targetFilename
|
||||
.replace('content/', '')
|
||||
.replace('.md', '')
|
||||
const $ = await getDOM(`/en/${contentPath}${apiVersion ? `?apiVersion=${apiVersion}` : ''}`)
|
||||
const domH3Ids = $('#article-contents table tbody tr td a')
|
||||
.map((i, a) => $(a).attr('href'))
|
||||
.get()
|
||||
expect(schemaSlugs.every((slug) => domH3Ids.includes(slug))).toBe(true)
|
||||
}
|
||||
})
|
||||
|
||||
for (const page of pageTypes) {
|
||||
const contentPath = configContent.pages[page].targetFilename
|
||||
.replace('content/', '')
|
||||
.replace('.md', '')
|
||||
const url = `/${contentPath}?${new URLSearchParams({ apiVersion: 'junk' })}`
|
||||
|
||||
test(`falls back when unsupported calendar version provided for ${contentPath}`, async () => {
|
||||
const res = await get(url, { followRedirects: true })
|
||||
expect(res.statusCode).toBe(200)
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user