Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions scripts/json-schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import * as path from 'node:path'
import { parseArgs } from 'node:util'

import type jsonSchemaToTypescriptModule from 'json-schema-to-typescript'
import { resolveConfig } from 'prettier'
import { format, resolveConfig } from 'prettier'

import { printLog, runMain, fetchHandlingError } from './lib/executionUtils.ts'
import { printLog, printWarning, runMain, fetchHandlingError } from './lib/executionUtils.ts'
import { command } from './lib/command.ts'
import { modifyFile } from './lib/filesUtils.ts'
import { SCHEMAS } from './lib/generatedSchemaTypes.ts'
Expand Down Expand Up @@ -67,8 +67,24 @@ async function update(branchOrCommit: string) {
printLog(`Using provided commit hash: ${commitHash}`)
} else {
printLog(`Resolving latest commit on ${branchOrCommit}...`)
// Unauthenticated GitHub API requests are limited to 60/hour per IP, which is easily exhausted
// behind a shared NAT. Authenticate with the user's `gh` CLI token to get the 5000/hour limit.
let token = ''
try {
token = command`gh auth token`.run().trim()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Pin gh token lookup to github.com

issue: If a developer has GH_HOST set to a GitHub Enterprise host, gh auth token can return that host's token—the GitHub CLI manual says gh auth token chooses a default host without --hostname (https://cli.github.com/manual/gh_auth_token) and GH_HOST supplies that host (https://cli.github.com/manual/gh_help_environment). This code then sends the token to the hard-coded api.github.com URL, so yarn json-schemas:sync can fail instead of falling back and may expose the wrong token; request the token with gh auth token --hostname github.com for this GitHub.com API call.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep it simple and not rely on env variables

} catch {
printWarning(
'Could not get a token from `gh auth token`; issuing unauthenticated GitHub requests (limited to 60/hour per IP).'
)
}
const response = await fetchHandlingError(
`https://api.github.com/repos/DataDog/rum-events-format/branches/${branchOrCommit}`
`https://api.github.com/repos/DataDog/rum-events-format/branches/${branchOrCommit}`,
{
headers: {
...(token ? { Authorization: `token ${token}` } : {}),
'X-GitHub-Api-Version': '2022-11-28',
},
}
)
const {
commit: { sha },
Expand Down Expand Up @@ -104,11 +120,15 @@ async function build() {
const compiledTypes = await compileFromFile(schemaPath, {
cwd: path.dirname(schemaPath),
bannerComment: '/**\n * DO NOT MODIFY IT BY HAND. Run `yarn json-schemas:sync` instead.\n*/',
style: prettierConfig || {},
// Skip json-schema-to-typescript's internal prettier pass: it resolves an ambient
// prettier that can differ between environments. Format with the repo's pinned prettier
// below instead, so the output is deterministic and matches `prettier --check .`.
format: false,
...options,
})
printLog(`Writing ${typesPath}...`)
fs.writeFileSync(absoluteTypesPath, compiledTypes)
const formattedTypes = await format(compiledTypes, { ...prettierConfig, parser: 'typescript' })
fs.writeFileSync(absoluteTypesPath, formattedTypes)
}

printLog('Done.')
Expand Down
Loading