forked from sensu/web
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Neal Granger <[email protected]>
- Loading branch information
Showing
10 changed files
with
370 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
/build | ||
/stats.json | ||
/dashboard.tgz | ||
/.schema | ||
|
||
# misc | ||
.DS_Store | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import fs from "fs"; | ||
import https from "https"; | ||
import path from "path"; | ||
|
||
import fsPromise from "./fsPromise"; | ||
import api from "./githubApi"; | ||
|
||
import pkg from "../package.json"; | ||
|
||
const MAX_PARALLEL_REQUESTS = 5; | ||
|
||
const schemaDir = path.resolve(".schema"); | ||
const lockFilePath = path.join(schemaDir, "lock.json"); | ||
|
||
const readLockFile = () => | ||
fsPromise.readFile(lockFilePath, "utf-8").then( | ||
contents => JSON.parse(contents), | ||
error => { | ||
if (error.code === "ENOENT") { | ||
return []; | ||
} | ||
throw error; | ||
}, | ||
); | ||
|
||
const updateLockFile = contents => | ||
fsPromise.writeFile(lockFilePath, JSON.stringify(contents, null, 2), "utf-8"); | ||
|
||
const fetchSchemaContents = (ref = "master") => { | ||
console.log(`fetching graphql schema from sensu-go/${ref}`); | ||
return api.repos | ||
.getContents({ | ||
owner: "sensu", | ||
repo: "sensu-go", | ||
path: "backend/apid/graphql/schema", | ||
ref, | ||
}) | ||
.then(result => result.data.filter(item => /\.graphql$/.test(item.name))); | ||
}; | ||
|
||
const fetch = (url, filePath) => | ||
new Promise((resolve, reject) => { | ||
const done = (error, ...args) => { | ||
if (error) { | ||
reject(error); | ||
} else { | ||
resolve(args); | ||
} | ||
}; | ||
|
||
const fileStream = fs.createWriteStream(filePath); | ||
|
||
const request = https.get(url, response => { | ||
response.pipe(fileStream); | ||
fileStream.on("finish", () => { | ||
fileStream.close(done); | ||
}); | ||
}); | ||
|
||
request.on("error", done); | ||
}); | ||
|
||
const lockFilePromise = readLockFile(); | ||
|
||
const exists = filePath => | ||
fsPromise.stat(filePath).then( | ||
() => true, | ||
error => { | ||
if (error.code === "ENOENT") { | ||
return false; | ||
} | ||
throw error; | ||
}, | ||
); | ||
|
||
const updateLocal = remoteItem => { | ||
const filePath = path.join(schemaDir, remoteItem.name); | ||
|
||
return lockFilePromise | ||
.then(localContents => | ||
localContents.find( | ||
localItem => | ||
localItem.name === remoteItem.name && | ||
localItem.sha === remoteItem.sha, | ||
), | ||
) | ||
.then(currentItem => !!currentItem && exists(filePath)) | ||
.then(localFileExists => { | ||
if (!localFileExists) { | ||
console.log("updating local", filePath); | ||
return fetch(remoteItem.download_url, filePath); | ||
} | ||
return Promise.resolve(); | ||
}); | ||
}; | ||
|
||
const mapPromiseParallel = (items, max, callback) => | ||
new Promise((resolve, reject) => { | ||
const remaining = [...items]; | ||
const result = []; | ||
|
||
let count = 0; | ||
|
||
const run = () => { | ||
if (remaining.length === 0 && count === 0) { | ||
resolve(result); | ||
} | ||
|
||
if (remaining.length && count < max) { | ||
count += 1; | ||
callback(remaining.pop()).then(() => { | ||
count -= 1; | ||
run(); | ||
}, reject); | ||
run(); | ||
} | ||
}; | ||
|
||
run(); | ||
}); | ||
|
||
fsPromise | ||
.mkdir(schemaDir) | ||
.catch(error => { | ||
if (error.code !== "EEXIST") { | ||
throw error; | ||
} | ||
}) | ||
.then(() => fetchSchemaContents(pkg.graphQLSchemaRef)) | ||
.then(remoteContents => | ||
mapPromiseParallel(remoteContents, MAX_PARALLEL_REQUESTS, updateLocal) | ||
.then(() => remoteContents.map(({ name, sha }) => ({ name, sha }))) | ||
.then(items => updateLockFile(items)), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import fs from "fs"; | ||
|
||
const callback = (resolve, reject) => (error, ...args) => { | ||
if (error) { | ||
reject(error); | ||
} else { | ||
resolve(args); | ||
} | ||
}; | ||
|
||
export default Object.keys(fs).reduce((result, key) => { | ||
// eslint-disable-next-line no-param-reassign | ||
result[key] = (...args) => | ||
new Promise((resolve, reject) => { | ||
fs[key](...args, callback(resolve, reject)); | ||
}); | ||
return result; | ||
}, {}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import OctokitRest from "@octokit/rest"; | ||
import OctokitPluginThrottling from "@octokit/plugin-throttling"; | ||
|
||
OctokitRest.plugin(OctokitPluginThrottling); | ||
|
||
export default new OctokitRest({ | ||
throttle: { | ||
onRateLimit: (retryAfter, options) => { | ||
console.warn( | ||
`Request quota exhausted for request ${options.method} ${options.url}`, | ||
); | ||
|
||
if (options.request.retryCount === 0) { | ||
// only retries once | ||
console.log(`Retrying after ${retryAfter} seconds!`); | ||
return true; | ||
} | ||
|
||
return false; | ||
}, | ||
onAbuseLimit: (retryAfter, options) => { | ||
// does not retry, only logs a warning | ||
console.warn( | ||
`Abuse detected for request ${options.method} ${options.url}`, | ||
); | ||
}, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.