forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add-ghec-to-schema.js
executable file
·90 lines (78 loc) · 2.72 KB
/
add-ghec-to-schema.js
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
#!/usr/bin/env node
// [start-readme]
//
// A one-time use script to add GHEC to the REST schema on github/github.
//
// [end-readme]
import { readFileSync, writeFileSync } from 'fs'
import { join } from 'path'
import walk from 'walk-sync'
import yaml from 'js-yaml'
import { program } from 'commander'
let fullDir
let fileCount = 0
const updatedFilesAll = []
const updatedFilesGHECOnly = []
const notUpdated = []
const logFile = process.cwd() + '/log.txt'
program
.description('One time script to add a GHEC property to the REST API schema on github/github.')
.argument('<schema-directory>', 'Path to the schema files to update from github/github.')
.action((schemaDirectory) => {
fullDir = join(process.cwd(), schemaDirectory)
})
program.parse()
// use the operations from github/github
walk
.entries(fullDir, {
ignore: ['.keep'],
directories: false,
})
// only work on yaml files
.filter((entry) => entry.relativePath.endsWith('yaml'))
.forEach((file) => {
fileCount++
const fullPath = join(file.basePath, file.relativePath)
let fileContent = readFileSync(fullPath, 'utf8')
const data = yaml.load(fileContent)
if (data['x-github-releases'] && data['x-github-releases'].includes('api.github.com')) {
// always:
// remove githubCloudOnly from x-github
// add ghec to x-github-releases
fileContent = fileContent.replace(/x-github-releases:\n/g, 'x-github-releases:\n - ghec\n')
fileContent = fileContent.replace(/\n\s{2}githubCloudOnly:.*\n/g, '\n')
updatedFilesAll.push(fullPath)
// if githubCloudOnly in x-github is true
// also remove api.github.com from x-github-releases
if (data['x-github'] && data['x-github'].githubCloudOnly) {
fileContent = fileContent.replace(/\n\s{2}- api.github.com\n/g, '\n')
updatedFilesGHECOnly.push(fullPath)
}
writeFileSync(fullPath, fileContent)
} else {
// doesn't have an api.github.com endpoint
// log it, ignore it
notUpdated.push(fullPath)
}
writeFileSync(
logFile,
`${fileCount} Total operations processed
${notUpdated.length} Operations not updated
${updatedFilesAll.length} Operations updated
${updatedFilesGHECOnly.length} Operations with api.github.com removed
---
Operations not updated:
${notUpdated.join('\r\n')}
All updated operations:
${updatedFilesAll.join('\r\n')}
Updated operations with api.github.com removed (GHEC only):
${updatedFilesGHECOnly.join('\r\n')}`
)
})
console.log(
`${fileCount} Total operations processed
${notUpdated.length} Operations not updated
${updatedFilesAll.length} Operations updated
${updatedFilesGHECOnly.length} Operations with api.github.com removed (GHEC only)
Log file at ${logFile}`
)