This commit is contained in:
tdurieux
2021-03-16 11:23:16 +01:00
parent 141d016aae
commit a2d0f0b212
475 changed files with 23855 additions and 3869 deletions

59
utils/github.js Normal file
View File

@@ -0,0 +1,59 @@
const ofs = require("fs");
const db = require("./database");
const repoUtils = require("./repository");
const fileUtils = require("./file");
const config = require("../config");
module.exports.getToken = async (repoConfig) => {
if (repoConfig.owner) {
const user = await db
.get()
.collection("users")
.findOne(
{ username: repoConfig.owner },
{ projection: { accessToken: 1 } }
);
if (user && user.accessToken) {
return user.accessToken;
}
}
if (repoConfig.token) {
return repoConfig.token;
}
return config.GITHUB_TOKEN;
};
module.exports.downloadRepoAndAnonymize = async (repoConfig) => {
const cachePath = repoUtils.getAnonymizedPath(repoConfig.repoId);
const originalPath = repoUtils.getOriginalPath(repoConfig.repoId);
if (ofs.existsSync(cachePath) || ofs.existsSync(originalPath)) {
return true;
}
if (repoConfig.options.mode == "download") {
// if cache folder does not exist download and anonumize it
const originalPath = repoUtils.getOriginalPath(repoConfig.repoId);
await repoUtils.updateStatus(repoConfig, "downloading");
await repoUtils.downloadOriginalRepo(repoConfig, originalPath);
await repoUtils.updateStatus(repoConfig, "ready");
// anonymize all the files
// await repoUtils.updateStatus(repoConfig, "anonymize");
// await anonymizeUtils.anonymizeFolder(originalPath, cachePath, repoConfig);
// await repoUtils.updateStatus(repoConfig, "anonymized");
// clean up
// await fs.rm(originalPath, { recursive: true, force: true });
return true;
} else if (repoConfig.options.mode == "stream") {
// in stream mode only download the list of file from github
await fileUtils.getFileList({ repoConfig });
await repoUtils.updateStatus(repoConfig, "ready");
return true;
}
return false;
};