-
Notifications
You must be signed in to change notification settings - Fork 66.8k
Expand file tree
/
Copy pathsync.ts
More file actions
executable file
·267 lines (228 loc) · 8.81 KB
/
sync.ts
File metadata and controls
executable file
·267 lines (228 loc) · 8.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import fs from 'fs/promises'
import { appendFileSync } from 'fs'
import path from 'path'
import { mkdirp } from 'mkdirp'
import yaml from 'js-yaml'
import { execSync } from 'child_process'
import { getContents, hasMatchingRef } from '@/workflows/git-utils'
import { allVersions } from '@/versions/lib/all-versions'
import processPreviews from './utils/process-previews'
import processUpcomingChanges from './utils/process-upcoming-changes'
import processSchemas from './utils/process-schemas'
import {
prependDatedEntry,
createChangelogEntry,
getIgnoredChangesSummary,
} from './build-changelog'
// Type definitions
interface GitHubRepoOptions {
owner: string
repo: string
ref?: string
path?: string
}
interface IgnoredChange {
version: string
totalCount: number
types: Array<{ type: string }>
}
interface RawPreview {
title: string
description?: string
toggled_on: string[]
toggled_by: string
announcement?: unknown
updates?: unknown
owning_teams?: string[]
}
interface UpcomingChangeEntry {
location: string
date: string
description: string
[key: string]: unknown
}
interface UpcomingChangesDocument {
upcoming_changes: UpcomingChangeEntry[]
}
type SchemaPreview = Omit<RawPreview, 'toggled_by'> & { toggled_by: string[] }
const graphqlStaticDir = 'src/graphql/data'
const dataFilenames = JSON.parse(
await fs.readFile('src/graphql/scripts/utils/data-filenames.json', 'utf8'),
)
// check for required PAT
if (!process.env.GITHUB_TOKEN) {
throw new Error('Error! You must have a GITHUB_TOKEN set in an .env file to run this script.')
}
const versionsToBuild = Object.keys(allVersions)
main()
const allIgnoredChanges: IgnoredChange[] = []
async function main() {
for (const version of versionsToBuild) {
// Get the relevant GraphQL name for the current version
// For example, free-pro-team@latest corresponds to dotcom,
// enterprise-server@2.22 corresponds to ghes-2.22.
const graphqlVersion = allVersions[version].openApiVersionName
// 1. UPDATE PREVIEWS
const previewsPath = getDataFilepath('previews', graphqlVersion)
const rawPreviews = yaml.load(
await getRemoteRawContent(previewsPath, graphqlVersion),
) as RawPreview[]
const safeForPublicPreviews: RawPreview[] = Array.isArray(rawPreviews) ? rawPreviews : []
const previewsJson = processPreviews(safeForPublicPreviews)
await updateStaticFile(
previewsJson,
path.join(graphqlStaticDir, graphqlVersion, 'previews.json'),
)
// 2. UPDATE UPCOMING CHANGES
const upcomingChangesPath = getDataFilepath('upcomingChanges', graphqlVersion)
const previousUpcomingChanges = yaml.load(
await fs.readFile(upcomingChangesPath, 'utf8'),
) as UpcomingChangesDocument
const safeForPublicChanges = await getRemoteRawContent(upcomingChangesPath, graphqlVersion)
await updateFile(upcomingChangesPath, safeForPublicChanges)
const upcomingChangesJson = await processUpcomingChanges(safeForPublicChanges)
await updateStaticFile(
upcomingChangesJson,
path.join(graphqlStaticDir, graphqlVersion, 'upcoming-changes.json'),
)
// 3. UPDATE SCHEMAS
// note: schemas live in separate files per version
const previewFilePath = getDataFilepath('schemas', graphqlVersion)
const previousSchemaString = await fs.readFile(previewFilePath, 'utf8')
const latestSchema = await getRemoteRawContent(previewFilePath, graphqlVersion)
await updateFile(previewFilePath, latestSchema)
const previewsForSchema: SchemaPreview[] = safeForPublicPreviews.map((preview) => ({
...preview,
toggled_by: [preview.toggled_by].flat(),
}))
const schemaJsonPerVersion = await processSchemas(latestSchema, previewsForSchema) // This is slow!
await updateStaticFile(
schemaJsonPerVersion,
path.join(graphqlStaticDir, graphqlVersion, 'schema.json'),
)
// 4. UPDATE CHANGELOG
if (allVersions[version].nonEnterpriseDefault) {
// The changelog is only built for free-pro-team@latest
const changelogEntry = await createChangelogEntry(
previousSchemaString,
latestSchema,
safeForPublicPreviews,
previousUpcomingChanges.upcoming_changes,
(yaml.load(safeForPublicChanges) as UpcomingChangesDocument).upcoming_changes,
)
if (changelogEntry) {
prependDatedEntry(
changelogEntry,
path.join(graphqlStaticDir, graphqlVersion, 'changelog.json'),
)
}
// Capture ignored changes for potential workflow notifications
const ignoredSummary = getIgnoredChangesSummary()
if (ignoredSummary) {
allIgnoredChanges.push({
version: graphqlVersion,
...ignoredSummary,
})
}
}
}
// Ensure the YAML linter runs before checkinging in files
execSync('npx prettier -w "**/*.{yml,yaml}"')
// Output ignored changes for GitHub Actions
if (allIgnoredChanges.length > 0) {
const totalIgnored = allIgnoredChanges.reduce((sum, item) => sum + item.totalCount, 0)
const uniqueTypes = [
...new Set(allIgnoredChanges.flatMap((item) => item.types.map((t) => t.type))),
]
console.log(
'::notice title=GraphQL Ignored Changes::Found ignored change types that may need review',
)
// Write outputs to GitHub Actions output file
if (process.env.GITHUB_OUTPUT) {
appendFileSync(
process.env.GITHUB_OUTPUT,
`ignored-changes=${JSON.stringify(allIgnoredChanges)}\n`,
)
appendFileSync(process.env.GITHUB_OUTPUT, `ignored-count=${totalIgnored}\n`)
appendFileSync(process.env.GITHUB_OUTPUT, `ignored-types=${uniqueTypes.join(', ')}\n`)
}
}
}
// get latest from github/github
async function getRemoteRawContent(filepath: string, graphqlVersion: string) {
const options: GitHubRepoOptions = {
owner: 'github',
repo: 'github',
}
// find the relevant branch in github/github and set it as options.ref
let t0 = new Date().getTime()
options.ref = await getBranchAsRef(options, graphqlVersion)
let took = new Date().getTime() - t0
console.log(`Got ref (${options.ref}) for '${graphqlVersion}'. Took ${formatTime(took)}`)
// add the filepath to the options so we can get the contents of the file
options.path = `config/${path.basename(filepath)}`
t0 = new Date().getTime()
const contents = await getContents(options.owner, options.repo, options.ref, options.path)
took = new Date().getTime() - t0
console.log(`Got content for '${options.path}' (in ${options.ref}). Took ${formatTime(took)}`)
return contents
}
// find the relevant filepath in src/graphql/scripts/util/data-filenames.json
function getDataFilepath(id: string, graphqlVersion: string) {
const versionType = getVersionName(graphqlVersion)
// for example, dataFilenames['schema']['ghes'] = schema.docs-enterprise.graphql
const filename = dataFilenames[id][versionType]
return path.join(graphqlStaticDir, graphqlVersion, filename)
}
async function getBranchAsRef(
options: GitHubRepoOptions,
graphqlVersion: string,
branch: string | boolean = false,
): Promise<string> {
const versionType = getVersionName(graphqlVersion) as 'fpt' | 'ghec' | 'ghes'
const defaultBranch = 'master'
const branches: Record<string, string> = {
fpt: defaultBranch,
ghec: defaultBranch,
ghes: `enterprise-${graphqlVersion.replace('ghes-', '')}-release`,
}
// the first time this runs, it uses the branch found for the version above
if (!branch) branch = branches[versionType]
// set the branch as the ref
const ref = `heads/${branch}`
// check whether the branch can be found in github/github
const exists = await hasMatchingRef(options.owner, options.repo, ref)
// if ref is not found, the branch cannot be found, so try a fallback
if (!exists) {
const fallbackBranch = defaultBranch
return await getBranchAsRef(options, graphqlVersion, fallbackBranch)
}
return ref
}
// given a GraphQL version like `ghes-2.22`, return `ghes`;
// given a GraphQL version like `dotcom`, return as is
function getVersionName(graphqlVersion: string) {
return graphqlVersion.split('-')[0]
}
async function updateFile(filepath: string, content: string) {
console.log(`Updating file ${filepath}`)
await mkdirp(path.dirname(filepath))
return fs.writeFile(filepath, content, 'utf8')
}
// JSON data from GraphQL schema processing - complex nested structures
// Serialize unknown shapes because the structure varies (arrays, objects, nested schemas, etc.)
async function updateStaticFile(json: unknown, filepath: string) {
console.log(`Updating static file ${filepath}`)
const jsonString = JSON.stringify(json, null, 2)
return updateFile(filepath, jsonString)
}
function formatTime(ms: number) {
if (ms < 1000) {
return `${ms.toFixed(0)}ms`
}
const seconds = ms / 1000
if (seconds > 60) {
return `${Math.round(seconds / 60)}m${Math.round(seconds % 60)}s`
}
return `${seconds.toFixed(1)}s`
}