Skip to content

Commit

Permalink
Convert more files to TypeScript sync.js & find-page.js (#51775)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ebonsignori authored Jul 29, 2024
1 parent a54bfeb commit 5038ef5
Show file tree
Hide file tree
Showing 21 changed files with 241 additions and 148 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/delete-orphan-translation-files.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ jobs:
changes=$(git diff --name-only | wc -l)
untracked=$(git status --untracked-files --short | wc -l)
if [[ $changes -eq 0 ]] && [[ $untracked -eq 0 ]]; then
echo "There are no changes to commit after running src/rest/scripts/update-files.js. Exiting..."
echo "There are no changes to commit or untracked files. Exiting."
exit 0
fi
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/sync-audit-logs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ jobs:
changes=$(git diff --name-only | wc -l)
untracked=$(git status --untracked-files --short | wc -l)
if [[ $changes -eq 0 ]] && [[ $untracked -eq 0 ]]; then
echo "There are no changes to commit after running src/rest/scripts/update-files.js. Exiting..."
echo "There are no changes to commit or untracked files. Exiting..."
exit 0
fi
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/sync-openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
# Needed for gh
GITHUB_TOKEN: ${{ secrets.DOCS_BOT_PAT_WRITEORG_PROJECT }}
run: |
src/rest/scripts/update-files.js --source-repo rest-api-description --output rest github-apps webhooks rest-redirects
npm run sync-rest -- --source-repo rest-api-description --output rest github-apps webhooks rest-redirects
git status
echo "Deleting the cloned github/rest-api-description repo..."
rm -rf rest-api-description
Expand All @@ -73,7 +73,7 @@ jobs:
# If nothing to commit, exit now. It's fine. No orphans.
changes=$(git diff --name-only | wc -l)
if [[ $changes -eq 0 ]]; then
echo "There are no changes to commit after running src/rest/scripts/update-files.js. Exiting..."
echo "There are no changes to commit after running `npm run sync-rest` Exiting..."
exit 0
fi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ Repository admins can add any topics they'd like to a repository. Helpful topics
You can search for repositories that are associated with a particular topic. For more information, see "[AUTOTITLE](/search-github/searching-on-github/searching-for-repositories#search-by-topic)." You can also search for a list of topics on {% data variables.product.product_name %}. For more information, see "[AUTOTITLE](/search-github/searching-on-github/searching-topics)."

When creating a topic:
* use lowercase letters, numbers, and hyphens.
* use 50 characters or less.
* add no more than 20 topics.
* Use lowercase letters, numbers, and hyphens.
* Use 50 characters or less.
* Add no more than 20 topics.

## Adding topics to your repository

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"start-all-languages": "cross-env NODE_ENV=development tsx src/frame/server.ts",
"start-for-playwright": "cross-env ROOT=src/fixtures/fixtures TRANSLATIONS_FIXTURE_ROOT=src/fixtures/fixtures/translations ENABLED_LANGUAGES=en,ja NODE_ENV=test tsx src/frame/server.ts",
"symlink-from-local-repo": "node src/early-access/scripts/symlink-from-local-repo.js",
"sync-rest": "node src/rest/scripts/update-files.js",
"sync-rest": "tsx src/rest/scripts/update-files.ts",
"sync-search": "cross-env NODE_OPTIONS='--max_old_space_size=8192' start-server-and-test sync-search-server 4002 sync-search-indices",
"sync-search-ghes-release": "cross-env GHES_RELEASE=1 start-server-and-test sync-search-server 4002 sync-search-indices",
"sync-search-indices": "node src/search/scripts/sync-search-indices.js",
Expand Down
4 changes: 4 additions & 0 deletions src/automated-pipelines/lib/update-markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,10 @@ async function getIndexFileVersions(directory, files) {
`File ${filepath} does not exist while assembling directory index.md files to create parent version.`,
)
}
// If not a markdown(x) file, skip it
if (!file.endsWith('.md') && !file.endsWith('.mdx')) {
return
}
const { data } = matter(await readFile(filepath, 'utf-8'))
if (!data || !data.versions) {
throw new Error(`Frontmatter in ${filepath} does not contain versions.`)
Expand Down
6 changes: 3 additions & 3 deletions src/codeql-cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ To run the CodeQL CLI pipeline locally:

## About this directory

- `src/rest/lib/config.json` - A configuration file used to specify metadata about the REST pipeline.
- `src/rest/scripts` - The scripts and source code used run the CodeQL CLI pipeline.
- `src/rest/scripts/sync.js` - The entrypoint script that runs the CodeQL CLI pipeline.
- `src/codeql-cli/lib/config.json` - A configuration file used to specify metadata about the CodeQL CLI pipeline.
- `src/codeql-cli/scripts` - The scripts and source code used run the CodeQL CLI pipeline.
- `src/codeql-cli/scripts/sync.js` - The entrypoint script that runs the CodeQL CLI pipeline.

## Content team

Expand Down
2 changes: 1 addition & 1 deletion src/ghes-releases/lib/release-templates/release-steps-5.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ The benefit of the first method is that you don't need to deal with merging two
- [ ] To update the OpenAPI data, run the following command.

```shell
src/rest/scripts/update-files.js --source-repo rest-api-description --output rest github-apps webhooks rest-redirects
npm run sync-rest -- --source-repo rest-api-description --output rest github-apps webhooks rest-redirects
```

You may see an error that indicates that "...you must have the GITHUB_TOKEN environment variable set to access the programmatic access and resource files via the GitHub REST API." You can ignore this error.
Expand Down
3 changes: 2 additions & 1 deletion src/github-apps/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ If the OpenAPI has changed, you will need to first wait for the OpenAPI to be me
To run the GitHub Apps pipeline locally:

1. Clone the [`github/rest-api-description`](https://github.com/github/rest-api-description) repository inside your local `docs-internal` repository.
1. Run `src/rest/scripts/update-files.js -s rest-api-description -o github-apps`.
1. Set a `GITHUB_TOKEN` in your `.env` with (classic) `repo` scopes & enable SSO for the github org.
1. Run `npm run sync-rest -- -s rest-api-description -o github-apps`.

## About this directory

Expand Down
7 changes: 4 additions & 3 deletions src/rest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ A [workflow](.github/workflows/sync-openapi.yml) is used to trigger the automati
- REST
- Webhooks

The workflow automatically creates a pull request with the changes (for all three pipelines) and the label `github-openapi-bot`. The workflow runs the `src/rest/scripts/update-files.js` script, which creates, deletes, or updates Markdown files in the `content/rest` directory.
The workflow automatically creates a pull request with the changes (for all three pipelines) and the label `github-openapi-bot`. The workflow runs the `npm run sync-rest` script, which creates, deletes, or updates Markdown files in the `content/rest` directory.

### Triggering the workflow sooner than the scheduled time

Expand All @@ -35,7 +35,8 @@ Then, you can manually sync the data used by the REST, Webhooks, and GitHub App
To run the REST pipeline locally:

1. Clone the [`github/rest-api-description`](https://github.com/github/rest-api-description) repository inside your local `docs-internal` repository.
1. Run `src/rest/scripts/update-files.js -s rest-api-description -o rest`. Note, by default `-o rest` is specified, so you can omit it.
1. Set a `GITHUB_TOKEN` in your `.env` with (classic) `repo` scopes & enable SSO for the github org.
1. Run `npm run sync-rest -- -s rest-api-description -o rest`. Note, by default `-o rest` is specified, so you can omit it.

## About this directory

Expand All @@ -45,7 +46,7 @@ To run the REST pipeline locally:
- `src/rest/lib` - The source code used in production for the automated documentation generated by the REST pipeline and configuration files edited by content and engineering team members.
- `src/rest/lib/config.json` - A configuration file used to specify metadata about the REST pipeline.
- `src/rest/scripts` - The scripts and source code used run the REST pipeline, which updates the `src/rest/data` directory.
- `src/rest/scripts/update-files.js` - The entrypoint script that runs the REST pipeline.
- `src/rest/scripts/update-files.ts` - The entrypoint script that runs the REST pipeline.
- `src/rest/tests` - The tests used to verify the REST pipeline.

## Configuring the pipeline
Expand Down
8 changes: 4 additions & 4 deletions src/rest/scripts/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# REST scripts

Writers run the [update-files.js](./update-files.js) script to get the latest dereferenced OpenAPI schema files.
Writers run the [update-files.ts](./update-files.ts) script to get the latest dereferenced OpenAPI schema files.
```
src/rest/scripts/update-files.js
npm run sync-rest
```
These scripts update the dereferenced OpenAPI files to create [the decorated files](../../src/rest/data) used to
render REST docs. See the [`src/rest/README`](../../src/rest/README.md)
Expand All @@ -20,12 +20,12 @@ Writers and developers depend on this script to preview OpenAPI changes in the d

## Production `--decorate-only` option

When changes to the OpenAPI are merged to the default branch of the `github/github` repository, a pull request is automatically opened with the updated dereferenced OpenAPI files. When pull requests are authored by `github-openapi-bot`, a CI test runs the `src/rest/scripts/update-files.js` script with the `--decorate-only` option. The `--decorate-only` option only decorates the dereferenced OpenAPI files, using the existing dereferenced OpenAPI schema files, and checks those changes in to the existing branch. The `--decorate-only` option is only used by a 🤖 and is only used on production dereferenced OpenAPI schema files.
When changes to the OpenAPI are merged to the default branch of the `github/github` repository, a pull request is automatically opened with the updated dereferenced OpenAPI files. When pull requests are authored by `github-openapi-bot`, a CI test runs the `npm run sync-rest` script with the `--decorate-only` option. The `--decorate-only` option only decorates the dereferenced OpenAPI files, using the existing dereferenced OpenAPI schema files, and checks those changes in to the existing branch. The `--decorate-only` option is only used by a 🤖 and is only used on production dereferenced OpenAPI schema files.

The `.github/workflows/openapi-schema-check.yml` CI test checks that the dereferenced and decorated schema files match. If the files don't match, potential causes could be:
- something went wrong when the schema changes (created by `github-openapi-bot`) were merged into another branch
- the workflow that generates the decorated files didn't run or failed

⚠️ Only do this if you know exactly what the `--decorate-only` option does. ⚠️

If you know that the dereferenced schema files are correct, you can run the `src/rest/scripts/update-files.js --decorate-only` command on the branch locally to update the decorated files in your branch.
If you know that the dereferenced schema files are correct, you can run the `npm run sync-rest -- --decorate-only` command on the branch locally to update the decorated files in your branch.
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,22 @@ import { fileURLToPath } from 'url'
import walk from 'walk-sync'
import { existsSync } from 'fs'

import { syncRestData, getOpenApiSchemaFiles } from './utils/sync.js'
import { validateVersionsOptions } from './utils/get-openapi-schemas.js'
import { allVersions } from '#src/versions/lib/all-versions.js'
import { syncWebhookData } from '../../webhooks/scripts/sync.js'
import { syncGitHubAppsData } from '../../github-apps/scripts/sync.js'
import { syncRestRedirects } from './utils/get-redirects.js'
import { syncRestData, getOpenApiSchemaFiles } from './utils/sync'
import { validateVersionsOptions } from './utils/get-openapi-schemas'
import { allVersions } from '@/versions/lib/all-versions'
import { syncWebhookData } from '../../webhooks/scripts/sync'
import { syncGitHubAppsData } from '../../github-apps/scripts/sync'
import { syncRestRedirects } from './utils/get-redirects'

const __dirname = path.dirname(fileURLToPath(import.meta.url))
const TEMP_OPENAPI_DIR = path.join(__dirname, '../../../rest-api-description/openApiTemp')
const TEMP_BUNDLED_OPENAPI_DIR = path.join(TEMP_OPENAPI_DIR, 'bundled')
const GITHUB_REP_DIR = '../github'
const REST_API_DESCRIPTION_ROOT = 'rest-api-description'
const REST_DESCRIPTION_DIR = path.join(REST_API_DESCRIPTION_ROOT, 'descriptions-next')
const VERSION_NAMES = JSON.parse(await readFile('src/rest/lib/config.json', 'utf8')).versionMapping
const VERSION_NAMES: Record<string, string> = JSON.parse(
await readFile('src/rest/lib/config.json', 'utf8'),
).versionMapping
const noConfig = ['rest-redirects']

program
Expand Down Expand Up @@ -105,7 +107,10 @@ async function main() {
// so that we don't spend time generating data files for them.
if (sourceRepo === REST_API_DESCRIPTION_ROOT) {
const derefDir = await readdir(TEMP_OPENAPI_DIR)
const currentOpenApiVersions = Object.values(allVersions).map((elem) => elem.openApiVersionName)
// TODO: After migrating all-version.js to TypeScript, we can remove the type assertion
const currentOpenApiVersions = Object.values(allVersions).map(
(elem) => (elem as any).openApiVersionName,
)

for (const schema of derefDir) {
// if the schema does not start with a current version name, delete it
Expand Down Expand Up @@ -162,7 +167,7 @@ async function main() {
)
}

async function getBundledFiles() {
async function getBundledFiles(): Promise<void> {
// Get the github/github repo branch name and pull latest
const githubBranch = execSync('git rev-parse --abbrev-ref HEAD', { cwd: GITHUB_REP_DIR })
.toString()
Expand Down Expand Up @@ -197,7 +202,7 @@ async function getBundledFiles() {
}
}

async function getBundlerOptions() {
async function getBundlerOptions(): Promise<string> {
let includeParams = ['--generate_dref_json_only']

if (versions) {
Expand All @@ -213,7 +218,7 @@ async function getBundlerOptions() {
return includeParams.join(' ')
}

async function validateInputParameters() {
async function validateInputParameters(): Promise<void> {
// The `--versions` option cannot be used
// with the `--include-deprecated` option
if (includeDeprecated && versions) {
Expand Down Expand Up @@ -248,7 +253,7 @@ async function validateInputParameters() {
// the short name of the version defined in lib/allVersions.js.
// This function also translates calendar-date format from .2022-11-28 to
// -2022-11-28
export async function normalizeDataVersionNames(sourceDirectory) {
export async function normalizeDataVersionNames(sourceDirectory: string): Promise<void> {
const schemas = await readdir(sourceDirectory)

for (const schema of schemas) {
Expand All @@ -259,8 +264,8 @@ export async function normalizeDataVersionNames(sourceDirectory) {
// Update the version name to use docs convention, e.g.,
// api.github.com.2022-11-28 -> fpt.2022-11-28
const docsBaseName = baseName.replace(
matchingSourceVersion,
VERSION_NAMES[matchingSourceVersion],
matchingSourceVersion!,
VERSION_NAMES[matchingSourceVersion!],
)
// Match a calendar version if it exists, e.g., .2022-11-28
const regex = /.\d{4}-\d{2}-\d{2}/
Expand Down
Loading

0 comments on commit 5038ef5

Please sign in to comment.