From 378942a28e0b2cd07a9a3c53df6009cfb96708f3 Mon Sep 17 00:00:00 2001 From: tdurieux Date: Sat, 27 Apr 2024 17:05:39 +0100 Subject: [PATCH] fix: fix file list collection --- src/core/AnonymizedFile.ts | 3 +- src/core/Repository.ts | 61 +++++++++++++++++++------------------- src/server/index.ts | 7 ++++- 3 files changed, 39 insertions(+), 32 deletions(-) diff --git a/src/core/AnonymizedFile.ts b/src/core/AnonymizedFile.ts index 447206a..3fdfd76 100644 --- a/src/core/AnonymizedFile.ts +++ b/src/core/AnonymizedFile.ts @@ -13,6 +13,7 @@ import AnonymousError from "./AnonymousError"; import { handleError } from "../server/routes/route-utils"; import FileModel from "./model/files/files.model"; import { IFile } from "./model/files/files.types"; +import { FilterQuery } from "mongoose"; /** * Represent a file in a anonymized repository @@ -61,7 +62,7 @@ export default class AnonymizedFile { const filename = basename(this.anonymizedPath); if (!this.anonymizedPath.includes(config.ANONYMIZATION_MASK)) { - const query: Partial = { + const query: FilterQuery = { repoId: this.repository.repoId, path: fileDir, }; diff --git a/src/core/Repository.ts b/src/core/Repository.ts index fbe80d7..1e8d508 100644 --- a/src/core/Repository.ts +++ b/src/core/Repository.ts @@ -26,8 +26,8 @@ import { FILE_TYPE } from "./storage/Storage"; import config from "../config"; import FileModel from "./model/files/files.model"; import { IFile } from "./model/files/files.types"; -import { join } from "path"; import AnonymizedFile from "./AnonymizedFile"; +import { FilterQuery } from "mongoose"; function anonymizeTreeRecursive( tree: IFile[], terms: string[], @@ -179,11 +179,14 @@ export default class Repository { if (opt.recursive === false) { pathQuery = opt.path ? new RegExp(`^${opt.path}$`) : ""; } - - return await FileModel.find({ + + const query: FilterQuery = { repoId: this.repoId, - path: pathQuery, - }).exec(); + }; + if (pathQuery !== undefined) { + query.path = pathQuery; + } + return await FileModel.find(query).exec(); } finally { span.end(); } @@ -256,18 +259,12 @@ export default class Repository { async isReady() { if (this.status !== RepositoryStatus.READY) return false; if ( - this.source.type == "GitHubDownload" && - (await storage.exists(this.repoId)) == FILE_TYPE.NOT_FOUND + (this.source.type == "GitHubDownload" && + (await storage.exists(this.repoId)) == FILE_TYPE.NOT_FOUND) || + !(await FileModel.exists({ repoId: this.repoId }).exec()) ) { - await this.resetSate(RepositoryStatus.PREPARING); - - await downloadQueue.add(this.repoId, this, { - jobId: this.repoId, - attempts: 3, - }); - return false; - } - if (!(await FileModel.exists({ repoId: this.repoId }).exec())) { + this.model.status = RepositoryStatus.PREPARING; + await this.updateIfNeeded({ force: true }); return false; } return true; @@ -315,6 +312,23 @@ export default class Repository { repositoryID: this.model.source.repositoryId, 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 this.model.source.repositoryName = ghRepo.fullName; const branches = await ghRepo.branches({ @@ -365,20 +379,6 @@ export default class Repository { `[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 downloadQueue.add(this.repoId, this, { jobId: this.repoId, @@ -405,6 +405,7 @@ export default class Repository { const files = await this.files({ force: false, progress, + recursive: false, }); if (files.length === 0) { // create a dummy file when the repo is empty to avoid errors diff --git a/src/server/index.ts b/src/server/index.ts index 3aeb67b..a751582 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -142,7 +142,12 @@ export default async function start() { const start = Date.now(); res.on("finish", function () { 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(); });