fix: fix file list collection

This commit is contained in:
tdurieux
2024-04-27 17:05:39 +01:00
parent 2a145730b7
commit 378942a28e
3 changed files with 39 additions and 32 deletions
+2 -1
View File
@@ -13,6 +13,7 @@ import AnonymousError from "./AnonymousError";
import { handleError } from "../server/routes/route-utils"; import { handleError } from "../server/routes/route-utils";
import FileModel from "./model/files/files.model"; import FileModel from "./model/files/files.model";
import { IFile } from "./model/files/files.types"; import { IFile } from "./model/files/files.types";
import { FilterQuery } from "mongoose";
/** /**
* Represent a file in a anonymized repository * Represent a file in a anonymized repository
@@ -61,7 +62,7 @@ export default class AnonymizedFile {
const filename = basename(this.anonymizedPath); const filename = basename(this.anonymizedPath);
if (!this.anonymizedPath.includes(config.ANONYMIZATION_MASK)) { if (!this.anonymizedPath.includes(config.ANONYMIZATION_MASK)) {
const query: Partial<IFile> = { const query: FilterQuery<IFile> = {
repoId: this.repository.repoId, repoId: this.repository.repoId,
path: fileDir, path: fileDir,
}; };
+31 -30
View File
@@ -26,8 +26,8 @@ import { FILE_TYPE } from "./storage/Storage";
import config from "../config"; import config from "../config";
import FileModel from "./model/files/files.model"; import FileModel from "./model/files/files.model";
import { IFile } from "./model/files/files.types"; import { IFile } from "./model/files/files.types";
import { join } from "path";
import AnonymizedFile from "./AnonymizedFile"; import AnonymizedFile from "./AnonymizedFile";
import { FilterQuery } from "mongoose";
function anonymizeTreeRecursive( function anonymizeTreeRecursive(
tree: IFile[], tree: IFile[],
terms: string[], terms: string[],
@@ -179,11 +179,14 @@ export default class Repository {
if (opt.recursive === false) { if (opt.recursive === false) {
pathQuery = opt.path ? new RegExp(`^${opt.path}$`) : ""; pathQuery = opt.path ? new RegExp(`^${opt.path}$`) : "";
} }
return await FileModel.find({ const query: FilterQuery<IFile> = {
repoId: this.repoId, repoId: this.repoId,
path: pathQuery, };
}).exec(); if (pathQuery !== undefined) {
query.path = pathQuery;
}
return await FileModel.find(query).exec();
} finally { } finally {
span.end(); span.end();
} }
@@ -256,18 +259,12 @@ export default class Repository {
async isReady() { async isReady() {
if (this.status !== RepositoryStatus.READY) return false; if (this.status !== RepositoryStatus.READY) return false;
if ( if (
this.source.type == "GitHubDownload" && (this.source.type == "GitHubDownload" &&
(await storage.exists(this.repoId)) == FILE_TYPE.NOT_FOUND (await storage.exists(this.repoId)) == FILE_TYPE.NOT_FOUND) ||
!(await FileModel.exists({ repoId: this.repoId }).exec())
) { ) {
await this.resetSate(RepositoryStatus.PREPARING); this.model.status = RepositoryStatus.PREPARING;
await this.updateIfNeeded({ force: true });
await downloadQueue.add(this.repoId, this, {
jobId: this.repoId,
attempts: 3,
});
return false;
}
if (!(await FileModel.exists({ repoId: this.repoId }).exec())) {
return false; return false;
} }
return true; return true;
@@ -315,6 +312,23 @@ export default class Repository {
repositoryID: this.model.source.repositoryId, repositoryID: this.model.source.repositoryId,
force: true, force: true,
}); });
if (ghRepo.size) {
if (
ghRepo.size > config.AUTO_DOWNLOAD_REPO_SIZE &&
this.model.source.type == "GitHubDownload"
) {
this.model.source.type = "GitHubStream";
await this.model.save();
} else if (
ghRepo.size < config.AUTO_DOWNLOAD_REPO_SIZE &&
this.model.source.type == "GitHubStream"
) {
this.model.source.type = "GitHubDownload";
await this.model.save();
}
}
// update the repository name if it has changed // update the repository name if it has changed
this.model.source.repositoryName = ghRepo.fullName; this.model.source.repositoryName = ghRepo.fullName;
const branches = await ghRepo.branches({ const branches = await ghRepo.branches({
@@ -365,20 +379,6 @@ export default class Repository {
`[UPDATE] ${this._model.repoId} will be updated to ${newCommit}` `[UPDATE] ${this._model.repoId} will be updated to ${newCommit}`
); );
if (ghRepo.size) {
if (
ghRepo.size > config.AUTO_DOWNLOAD_REPO_SIZE &&
this.model.source.type == "GitHubDownload"
) {
this.model.source.type = "GitHubStream";
} else if (
ghRepo.size < config.AUTO_DOWNLOAD_REPO_SIZE &&
this.model.source.type == "GitHubStream"
) {
this.model.source.type = "GitHubDownload";
}
}
await this.resetSate(RepositoryStatus.PREPARING); await this.resetSate(RepositoryStatus.PREPARING);
await downloadQueue.add(this.repoId, this, { await downloadQueue.add(this.repoId, this, {
jobId: this.repoId, jobId: this.repoId,
@@ -405,6 +405,7 @@ export default class Repository {
const files = await this.files({ const files = await this.files({
force: false, force: false,
progress, progress,
recursive: false,
}); });
if (files.length === 0) { if (files.length === 0) {
// create a dummy file when the repo is empty to avoid errors // create a dummy file when the repo is empty to avoid errors
+6 -1
View File
@@ -142,7 +142,12 @@ export default async function start() {
const start = Date.now(); const start = Date.now();
res.on("finish", function () { res.on("finish", function () {
const time = Date.now() - start; const time = Date.now() - start;
console.log(`${req.method} ${join(req.baseUrl, req.url)} ${time}ms`); console.log(
`${req.method} ${res.statusCode} ${join(
req.baseUrl,
req.url
)} ${time}ms`
);
}); });
next(); next();
}); });