Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

update-files.js 8.1 KB

You have to be logged in to leave a comment. Sign In
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
  1. #!/usr/bin/env node
  2. const fs = require('fs')
  3. const path = require('path')
  4. const mkdirp = require('mkdirp').sync
  5. const yaml = require('js-yaml')
  6. const { execSync } = require('child_process')
  7. const graphqlDataDir = path.join(process.cwd(), 'data/graphql')
  8. const graphqlStaticDir = path.join(process.cwd(), 'lib/graphql/static')
  9. const { getContents, listMatchingRefs } = require('../helpers/git-utils')
  10. const dataFilenames = require('./utils/data-filenames')
  11. const allVersions = require('../../lib/all-versions')
  12. const processPreviews = require('./utils/process-previews')
  13. const processUpcomingChanges = require('./utils/process-upcoming-changes')
  14. const processSchemas = require('./utils/process-schemas')
  15. const prerenderObjects = require('./utils/prerender-objects')
  16. const prerenderInputObjects = require('./utils/prerender-input-objects')
  17. const { prependDatedEntry, createChangelogEntry } = require('./build-changelog')
  18. const loadData = require('../../lib/site-data')
  19. // check for required PAT
  20. if (!process.env.GITHUB_TOKEN) {
  21. console.error('Error! You must have a GITHUB_TOKEN set in an .env file to run this script.')
  22. process.exit(1)
  23. }
  24. // check for required Ruby gems (see note below about why this is needed)
  25. try {
  26. execSync('gem which graphql')
  27. } catch (err) {
  28. console.error('\nYou need to run: bundle install')
  29. process.exit(1)
  30. }
  31. // TODO this step is only required as long as we support GHE versions *OLDER THAN* 2.21
  32. // as soon as 2.20 is deprecated on 2021-02-11, we can remove all graphql-ruby filtering
  33. const removeHiddenMembersScript = path.join(__dirname, './utils/remove-hidden-schema-members.rb')
  34. const versionsToBuild = Object.keys(allVersions)
  35. const currentLanguage = 'en'
  36. main()
  37. async function main () {
  38. const previewsJson = {}
  39. const upcomingChangesJson = {}
  40. const prerenderedObjects = {}
  41. const prerenderedInputObjects = {}
  42. const siteData = await loadData()
  43. // create a bare minimum context for rendering the graphql-object.html layout
  44. const context = {
  45. currentLanguage,
  46. site: siteData[currentLanguage].site
  47. }
  48. for (const version of versionsToBuild) {
  49. // Get the relevant GraphQL name for the current version
  50. // For example, free-pro-team@latest corresponds to dotcom,
  51. // enterprise-server@2.22 corresponds to ghes-2.22,
  52. // and github-ae@latest corresponds to ghae
  53. const graphqlVersion = allVersions[version].miscVersionName
  54. // 1. UPDATE PREVIEWS
  55. const previewsPath = getDataFilepath('previews', graphqlVersion)
  56. const safeForPublicPreviews = yaml.load(await getRemoteRawContent(previewsPath, graphqlVersion))
  57. updateFile(previewsPath, yaml.dump(safeForPublicPreviews))
  58. previewsJson[graphqlVersion] = processPreviews(safeForPublicPreviews)
  59. // 2. UPDATE UPCOMING CHANGES
  60. const upcomingChangesPath = getDataFilepath('upcomingChanges', graphqlVersion)
  61. const previousUpcomingChanges = yaml.load(fs.readFileSync(upcomingChangesPath, 'utf8'))
  62. const safeForPublicChanges = await getRemoteRawContent(upcomingChangesPath, graphqlVersion)
  63. updateFile(upcomingChangesPath, safeForPublicChanges)
  64. upcomingChangesJson[graphqlVersion] = await processUpcomingChanges(safeForPublicChanges)
  65. // 3. UPDATE SCHEMAS
  66. // note: schemas live in separate files per version
  67. const schemaPath = getDataFilepath('schemas', graphqlVersion)
  68. const previousSchemaString = fs.readFileSync(schemaPath, 'utf8')
  69. const latestSchema = await getRemoteRawContent(schemaPath, graphqlVersion)
  70. const safeForPublicSchema = removeHiddenMembers(schemaPath, latestSchema)
  71. updateFile(schemaPath, safeForPublicSchema)
  72. const schemaJsonPerVersion = await processSchemas(safeForPublicSchema, safeForPublicPreviews)
  73. updateStaticFile(schemaJsonPerVersion, path.join(graphqlStaticDir, `schema-${graphqlVersion}.json`))
  74. // Add some version specific data to the context
  75. context.graphql = { schemaForCurrentVersion: schemaJsonPerVersion }
  76. context.currentVersion = version
  77. // 4. PRERENDER OBJECTS HTML
  78. // because the objects page is too big to render on page load
  79. prerenderedObjects[graphqlVersion] = await prerenderObjects(context)
  80. // 5. PRERENDER INPUT OBJECTS HTML
  81. // because the objects page is too big to render on page load
  82. prerenderedInputObjects[graphqlVersion] = await prerenderInputObjects(context)
  83. // 6. UPDATE CHANGELOG
  84. if (allVersions[version].nonEnterpriseDefault) {
  85. // The Changelog is only build for free-pro-team@latest
  86. const changelogEntry = await createChangelogEntry(
  87. previousSchemaString,
  88. safeForPublicSchema,
  89. safeForPublicPreviews,
  90. previousUpcomingChanges.upcoming_changes,
  91. yaml.load(safeForPublicChanges).upcoming_changes
  92. )
  93. if (changelogEntry) {
  94. prependDatedEntry(changelogEntry, path.join(process.cwd(), 'lib/graphql/static/changelog.json'))
  95. }
  96. }
  97. }
  98. updateStaticFile(previewsJson, path.join(graphqlStaticDir, 'previews.json'))
  99. updateStaticFile(upcomingChangesJson, path.join(graphqlStaticDir, 'upcoming-changes.json'))
  100. updateStaticFile(prerenderedObjects, path.join(graphqlStaticDir, 'prerendered-objects.json'))
  101. updateStaticFile(prerenderedInputObjects, path.join(graphqlStaticDir, 'prerendered-input-objects.json'))
  102. // Ensure the YAML linter runs before checkinging in files
  103. execSync('npx prettier -w "**/*.{yml,yaml}"')
  104. }
  105. // get latest from github/github
  106. async function getRemoteRawContent (filepath, graphqlVersion) {
  107. const options = {
  108. owner: 'github',
  109. repo: 'github'
  110. }
  111. // find the relevant branch in github/github and set it as options.ref
  112. await setBranchAsRef(options, graphqlVersion)
  113. // add the filepath to the options so we can get the contents of the file
  114. options.path = `config/${path.basename(filepath)}`
  115. return getContents(...Object.values(options))
  116. }
  117. // find the relevant filepath in script/graphql/utils/data-filenames.json
  118. function getDataFilepath (id, graphqlVersion) {
  119. const versionType = getVersionType(graphqlVersion)
  120. // for example, dataFilenames['schema']['ghes'] = schema.docs-enterprise.graphql
  121. const filename = dataFilenames[id][versionType]
  122. // dotcom files live at the root of data/graphql
  123. // non-dotcom files live in data/graphql/<version_subdir>
  124. const dataSubdir = graphqlVersion === 'dotcom' ? '' : graphqlVersion
  125. return path.join(graphqlDataDir, dataSubdir, filename)
  126. }
  127. async function setBranchAsRef (options, graphqlVersion, branch = false) {
  128. const versionType = getVersionType(graphqlVersion)
  129. const defaultBranch = 'master'
  130. const branches = {
  131. dotcom: defaultBranch,
  132. ghes: `enterprise-${graphqlVersion.replace('ghes-', '')}-release`,
  133. // TODO confirm the below is accurate after the release branch is created
  134. ghae: 'github-ae-release'
  135. }
  136. // the first time this runs, it uses the branch found for the version above
  137. if (!branch) branch = branches[versionType]
  138. // set the branch as the ref
  139. options.ref = `heads/${branch}`
  140. // check whether the branch can be found in github/github
  141. const foundRefs = await listMatchingRefs(...Object.values(options))
  142. // if foundRefs array is empty, the branch cannot be found, so try a fallback
  143. if (!foundRefs.length) {
  144. const fallbackBranch = defaultBranch
  145. await setBranchAsRef(options, graphqlVersion, fallbackBranch)
  146. }
  147. }
  148. // given a GraphQL version like `ghes-2.22`, return `ghes`;
  149. // given a GraphQL version like `ghae` or `dotcom`, return as is
  150. function getVersionType (graphqlVersion) {
  151. return graphqlVersion.split('-')[0]
  152. }
  153. function updateFile (filepath, content) {
  154. console.log(`fetching latest data to ${filepath}`)
  155. mkdirp(path.dirname(filepath))
  156. fs.writeFileSync(filepath, content, 'utf8')
  157. }
  158. function updateStaticFile (json, filepath) {
  159. const jsonString = JSON.stringify(json, null, 2)
  160. updateFile(filepath, jsonString)
  161. }
  162. // run Ruby script to remove featureFlagged directives and other hidden members
  163. function removeHiddenMembers (schemaPath, latestSchema) {
  164. // have to write a temp file because the schema is too big to store in memory
  165. const tempSchemaFilePath = `${schemaPath}-TEMP`
  166. fs.writeFileSync(tempSchemaFilePath, latestSchema)
  167. const remoteClean = execSync(`${removeHiddenMembersScript} ${tempSchemaFilePath}`).toString()
  168. fs.unlinkSync(tempSchemaFilePath)
  169. return remoteClean
  170. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...