Skip to content

Commit 50210d1

Browse files
authored
refactor openapi scripts (github#34803)
1 parent 03c3857 commit 50210d1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+17529
-45615
lines changed

.github/workflows/openapi-decorate.yml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: Sync OpenAPI schema
22

3-
# **What it does**: Once a day, this workflow syncs the dereferenced files from github/rest-api-description and creates a pull request if there are updates to any of the static files we generate from the OpenAPI.
4-
# **Why we have it**: So we can consume OpenAPI changes.
3+
# **What it does**: Once a day, this workflow syncs the REST, Webhooks, and GitHub Apps automated pipelines with the github/rest-api-description repository, and creates a pull request if there are updates to any of the data files we generate from the OpenAPI.
4+
# **Why we have it**: So we can automate updates to REST, Webhooks, and GitHub Apps documentation
55
# **Who does it impact**: Anyone making OpenAPI changes in `github/github`, and wanting to get them published on the docs site.
66

77
on:
@@ -49,19 +49,17 @@ jobs:
4949

5050
- uses: ./.github/actions/node-npm-setup
5151

52-
- name: Copy dereferenced OpenAPI files
52+
- name: Get the rest-api-description SHA being synced
5353
id: rest-api-description
5454
run: |
55-
mkdir ./src/rest/data/dereferenced
56-
find rest-api-description/descriptions-next -type f -name "*.deref.json" -exec sh -c 'cp $1 ./src/rest/data/dereferenced' sh {} \;
5755
cd rest-api-description
5856
OPENAPI_COMMIT_SHA=$(git rev-parse HEAD)
5957
echo "OPENAPI_COMMIT_SHA=$OPENAPI_COMMIT_SHA" >> $GITHUB_OUTPUT
6058
echo "Copied files from github/rest-api-description repo. Commit SHA: $OPENAPI_COMMIT_SHA"
6159
62-
- name: Decorate the dereferenced OpenAPI schemas
60+
- name: Sync the REST, Webhooks, and GitHub Apps schemas
6361
run: |
64-
src/rest/scripts/update-files.js --decorate-only --open-source
62+
src/rest/scripts/update-files.js --source-repo rest-api-description --output rest github-apps webhooks rest-redirects
6563
git status
6664
echo "Deleting the cloned github/rest-api-description repo..."
6765
rm -rf rest-api-description

lib/all-versions.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const plans = [
2626
latestRelease: latestNonNumberedRelease,
2727
nonEnterpriseDefault: true, // permanent way to refer to this plan if the name changes
2828
hasNumberedReleases: false,
29-
openApiBaseName: 'api.github.com', // used for REST
29+
openApiBaseName: 'fpt', // used for REST
3030
miscBaseName: 'dotcom', // used for GraphQL and webhooks
3131
},
3232
{
@@ -59,7 +59,7 @@ const plans = [
5959
// It lets us do semantic comparison internally while only exposing `@latest` in the UI.
6060
internalLatestRelease: '3.4',
6161
hasNumberedReleases: false,
62-
openApiBaseName: 'github.ae',
62+
openApiBaseName: 'ghae',
6363
miscBaseName: 'ghae',
6464
},
6565
]

lib/redirects/static/client-side-rest-api-redirects.json

Lines changed: 0 additions & 1172 deletions
This file was deleted.

middleware/anchor-redirect.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import express from 'express'
2+
import path from 'path'
23

34
import { readCompressedJsonFileFallbackLazily } from '../lib/read-json-file.js'
45
import { defaultCacheControl } from './cache-control.js'
6+
import { REST_DATA_DIR } from '../src/rest/lib/index.js'
57

68
const clientSideRestAPIRedirects = readCompressedJsonFileFallbackLazily(
7-
'./lib/redirects/static/client-side-rest-api-redirects.json'
9+
path.join(REST_DATA_DIR, 'client-side-rest-api-redirects.json')
810
)
911

1012
const router = express.Router()

script/README.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -609,13 +609,6 @@ Run this script to pull openAPI files from github/github, dereference them, and
609609

610610

611611

612-
---
613-
614-
615-
### [`rest/utils/rest-api-overrides.json`](rest/utils/rest-api-overrides.json)
616-
617-
618-
619612
---
620613

621614

src/github-apps/data/api.github.com.2022-11-28/server-to-server-rest.json renamed to src/github-apps/data/fpt-2022-11-28/server-to-server-rest.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,6 @@
893893
"requestPath": "/installation/token"
894894
}
895895
],
896-
"billing": [],
897896
"branches": [
898897
{
899898
"slug": "list-branches",
@@ -1716,7 +1715,6 @@
17161715
"requestPath": "/emojis"
17171716
}
17181717
],
1719-
"gists": [],
17201718
"git": [
17211719
{
17221720
"slug": "create-a-blob",
@@ -2493,7 +2491,6 @@
24932491
"requestPath": "/users/{username}/orgs"
24942492
}
24952493
],
2496-
"packages": [],
24972494
"pages": [
24982495
{
24992496
"slug": "get-a-github-pages-site",

src/github-apps/data/github.ae/server-to-server-rest.json renamed to src/github-apps/data/ghae/server-to-server-rest.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,8 +1133,6 @@
11331133
"requestPath": "/emojis"
11341134
}
11351135
],
1136-
"enterprise-admin": [],
1137-
"gists": [],
11381136
"git": [
11391137
{
11401138
"slug": "create-a-blob",
@@ -1545,7 +1543,6 @@
15451543
"requestPath": "/repos/{owner}/{repo}/stats/punch_card"
15461544
}
15471545
],
1548-
"migrations": [],
15491546
"orgs": [
15501547
{
15511548
"slug": "list-organizations",

src/github-apps/data/ghec.2022-11-28/server-to-server-rest.json renamed to src/github-apps/data/ghec-2022-11-28/server-to-server-rest.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1845,8 +1845,6 @@
18451845
"requestPath": "/emojis"
18461846
}
18471847
],
1848-
"enterprise-admin": [],
1849-
"gists": [],
18501848
"git": [
18511849
{
18521850
"slug": "create-a-blob",
@@ -2677,7 +2675,6 @@
26772675
"requestPath": "/users/{username}/orgs"
26782676
}
26792677
],
2680-
"packages": [],
26812678
"pages": [
26822679
{
26832680
"slug": "get-a-github-enterprise-cloud-pages-site",

src/github-apps/data/ghes-3.4/server-to-server-rest.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,7 +1439,6 @@
14391439
"requestPath": "/repos/{owner}/{repo}/pre-receive-hooks/{pre_receive_hook_id}"
14401440
}
14411441
],
1442-
"gists": [],
14431442
"git": [
14441443
{
14451444
"slug": "create-a-blob",
@@ -1850,8 +1849,6 @@
18501849
"requestPath": "/repos/{owner}/{repo}/stats/punch_card"
18511850
}
18521851
],
1853-
"migrations": [],
1854-
"oauth-authorizations": [],
18551852
"orgs": [
18561853
{
18571854
"slug": "list-organizations",

src/github-apps/data/ghes-3.5/server-to-server-rest.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,7 +1547,6 @@
15471547
"requestPath": "/repos/{owner}/{repo}/pre-receive-hooks/{pre_receive_hook_id}"
15481548
}
15491549
],
1550-
"gists": [],
15511550
"git": [
15521551
{
15531552
"slug": "create-a-blob",
@@ -1958,8 +1957,6 @@
19581957
"requestPath": "/repos/{owner}/{repo}/stats/punch_card"
19591958
}
19601959
],
1961-
"migrations": [],
1962-
"oauth-authorizations": [],
19631960
"orgs": [
19641961
{
19651962
"slug": "list-organizations",

src/github-apps/data/ghes-3.6/server-to-server-rest.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,7 +1389,6 @@
13891389
"requestPath": "/repos/{owner}/{repo}/dependabot/secrets/{secret_name}"
13901390
}
13911391
],
1392-
"dependency-graph": [],
13931392
"deploy-keys": [
13941393
{
13951394
"slug": "list-deploy-keys",
@@ -1644,7 +1643,6 @@
16441643
"requestPath": "/scim/v2/Users/{scim_user_id}"
16451644
}
16461645
],
1647-
"gists": [],
16481646
"git": [
16491647
{
16501648
"slug": "create-a-blob",
@@ -2055,8 +2053,6 @@
20552053
"requestPath": "/repos/{owner}/{repo}/stats/punch_card"
20562054
}
20572055
],
2058-
"migrations": [],
2059-
"oauth-authorizations": [],
20602056
"orgs": [
20612057
{
20622058
"slug": "list-organizations",

src/github-apps/data/ghes-3.7/server-to-server-rest.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1693,7 +1693,6 @@
16931693
"requestPath": "/scim/v2/Users/{scim_user_id}"
16941694
}
16951695
],
1696-
"gists": [],
16971696
"git": [
16981697
{
16991698
"slug": "create-a-blob",
@@ -2104,8 +2103,6 @@
21042103
"requestPath": "/repos/{owner}/{repo}/stats/punch_card"
21052104
}
21062105
],
2107-
"migrations": [],
2108-
"oauth-authorizations": [],
21092106
"orgs": [
21102107
{
21112108
"slug": "list-organizations",

src/github-apps/data/ghes-3.8/server-to-server-rest.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1923,7 +1923,6 @@
19231923
"requestPath": "/scim/v2/Users/{scim_user_id}"
19241924
}
19251925
],
1926-
"gists": [],
19271926
"git": [
19281927
{
19291928
"slug": "create-a-blob",
@@ -2334,8 +2333,6 @@
23342333
"requestPath": "/repos/{owner}/{repo}/stats/punch_card"
23352334
}
23362335
],
2337-
"migrations": [],
2338-
"oauth-authorizations": [],
23392336
"orgs": [
23402337
{
23412338
"slug": "list-organizations",

src/github-apps/data/meta.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"api-versions": {
3+
"fpt": [
4+
"2022-11-28"
5+
],
6+
"ghec": [
7+
"2022-11-28"
8+
]
9+
}
10+
}

src/github-apps/lib/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import path from 'path'
2+
13
import { readCompressedJsonFileFallback } from '../../../lib/read-json-file.js'
24
import { getOpenApiVersion } from '../../../lib/all-versions.js'
35

@@ -7,13 +9,12 @@ export const ENABLED_APPS_FILENAME = 'server-to-server-rest.json'
79
const enabledForApps = new Map()
810

911
export async function getEnabledForApps(docsVersion, apiVersion) {
10-
const openApiVersion = getOpenApiVersion(docsVersion) + (apiVersion ? `.${apiVersion}` : '')
12+
const openApiVersion = getOpenApiVersion(docsVersion) + (apiVersion ? `-${apiVersion}` : '')
1113
if (!enabledForApps.has(openApiVersion)) {
1214
// The `readCompressedJsonFileFallback()` function
1315
// will check for both a .br and .json extension.
14-
const data = readCompressedJsonFileFallback(
15-
`${ENABLED_APPS_DIR}/${openApiVersion}/${ENABLED_APPS_FILENAME}`
16-
)
16+
const appDataPath = path.join(ENABLED_APPS_DIR, openApiVersion, ENABLED_APPS_FILENAME)
17+
const data = readCompressedJsonFileFallback(appDataPath)
1718
enabledForApps.set(openApiVersion, data)
1819
}
1920

src/github-apps/scripts/sync.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/env node
2+
3+
import { existsSync } from 'fs'
4+
import mkdirp from 'mkdirp'
5+
import { readFile, writeFile } from 'fs/promises'
6+
import path from 'path'
7+
import { slug } from 'github-slugger'
8+
9+
import { getOverrideCategory } from '../../rest/scripts/utils/operation.js'
10+
import { ENABLED_APPS_DIR, ENABLED_APPS_FILENAME } from '../lib/index.js'
11+
12+
// Creates the src/github-apps/data files used for
13+
// https://docs.github.com/en/rest/overview/endpoints-available-for-github-apps
14+
export async function syncGitHubAppsData(sourceDirectory, sourceSchemas) {
15+
for (const schemaName of sourceSchemas) {
16+
const data = JSON.parse(await readFile(path.join(sourceDirectory, schemaName), 'utf8'))
17+
18+
// Because the information used on the apps page doesn't require any
19+
// rendered content we can parse the dereferenced files directly
20+
const enabledForApps = {}
21+
for (const [requestPath, operationsAtPath] of Object.entries(data.paths)) {
22+
for (const [verb, operation] of Object.entries(operationsAtPath)) {
23+
// We only want to process operations that are
24+
// server-to-server GitHub App enabled
25+
if (!operation['x-github'].enabledForGitHubApps) continue
26+
27+
const schemaCategory = operation['x-github'].category
28+
const schemaSubcategory = operation['x-github'].subcategory
29+
const { category, subcategory } = getOverrideCategory(
30+
operation.operationId,
31+
schemaCategory,
32+
schemaSubcategory
33+
)
34+
35+
if (!enabledForApps[category]) {
36+
enabledForApps[category] = []
37+
}
38+
enabledForApps[category].push({
39+
slug: slug(operation.summary),
40+
subcategory,
41+
verb,
42+
requestPath,
43+
})
44+
}
45+
}
46+
47+
if (Object.keys(enabledForApps).length === 0) {
48+
throw new Error(
49+
`Generating GitHub Apps data failed for ${sourceDirectory}/${schemaName}. The generated data file was empty.`
50+
)
51+
}
52+
53+
// Sort the operations by category for readability
54+
const sortedOperations = Object.keys(enabledForApps)
55+
.sort()
56+
.reduce((acc, key) => {
57+
acc[key] = enabledForApps[key]
58+
return acc
59+
}, {})
60+
61+
const versionName = path.basename(schemaName, '.json')
62+
const targetDirectory = path.join(ENABLED_APPS_DIR, versionName)
63+
64+
// When a new version is added, we need to create the directory for it
65+
if (!existsSync(targetDirectory)) {
66+
await mkdirp(targetDirectory)
67+
}
68+
69+
const targetPath = path.join(targetDirectory, ENABLED_APPS_FILENAME)
70+
await writeFile(targetPath, JSON.stringify(sortedOperations, null, 2))
71+
console.log(`✅ Wrote ${targetPath}`)
72+
}
73+
}

0 commit comments

Comments
 (0)