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

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

Comments

Loading...