From 28075889a6ecaf88d7fb4a87dbbbf2f2aee830fc Mon Sep 17 00:00:00 2001 From: tdurieux Date: Fri, 20 Aug 2021 15:58:32 +0200 Subject: [PATCH] fix: fix repository update --- public/partials/anonymize.htm | 6 ++-- public/script/app.js | 57 ++++++++++++++++++++------------ src/Repository.ts | 10 +++--- src/routes/repository-private.ts | 52 +++++++++++++++-------------- src/source/GitHubBase.ts | 3 +- 5 files changed, 73 insertions(+), 55 deletions(-) diff --git a/public/partials/anonymize.htm b/public/partials/anonymize.htm index 9b5fc83..8e2a4e2 100644 --- a/public/partials/anonymize.htm +++ b/public/partials/anonymize.htm @@ -97,7 +97,7 @@ class="form-control" id="branch" name="branch" - ng-model="branch" + ng-model="source.branch" > diff --git a/public/script/app.js b/public/script/app.js index 68c5216..af265b1 100644 --- a/public/script/app.js +++ b/public/script/app.js @@ -616,10 +616,12 @@ angular $scope.repoId = ""; $scope.terms = ""; $scope.defaultTerms = ""; - $scope.branch = ""; - $scope.branches = []; - + source = { + type: "GitHubDownload", + branch: "", + commit: "", + }; $scope.options = { expirationMode: "remove", expirationDate: new Date(), @@ -628,7 +630,6 @@ angular pdf: true, notebook: true, link: true, - mode: "GitHubDownload", }; $scope.options.expirationDate.setDate( $scope.options.expirationDate.getDate() + 90 @@ -662,8 +663,8 @@ angular async (res) => { $scope.repoUrl = "https://github.com/" + res.data.source.fullName; - $scope.terms = res.data.options.terms.join("\n"); - $scope.branch = res.data.source.branch.name; + $scope.terms = res.data.options.terms.filter((f) => f).join("\n"); + $scope.source = res.data.source; $scope.options = res.data.options; $scope.conference = res.data.conference; if (res.data.options.expirationDate) { @@ -712,9 +713,13 @@ angular $scope.getRepositories(); $scope.repoSelected = async () => { - $scope.terms = $scope.defaultTerms; + $scope.terms = $scope.defaultTerms.filter((f) => f); $scope.repoId = ""; - $scope.branch = ""; + $scope.source = { + type: "GitHubDownload", + branch: "", + commit: "", + }; $scope.anonymize_readme = ""; $scope.readme = ""; @@ -739,19 +744,19 @@ angular }; $('[data-toggle="tooltip"]').tooltip(); - $scope.$watch("branch", async (v) => { + $scope.$watch("source.branch", async (v) => { const selected = $scope.branches.filter( - (f) => f.name == $scope.branch + (f) => f.name == $scope.source.branch )[0]; if ($scope.details && $scope.details.hasPage) { $scope.anonymize.page.$$element[0].disabled = false; - if ($scope.details.pageSource.branch != $scope.branch) { + if ($scope.details.pageSource.branch != $scope.source.branch) { $scope.anonymize.page.$$element[0].disabled = true; } } if (selected) { - $scope.commit = selected.commit; + $scope.source.commit = selected.commit; $scope.readme = selected.readme; await getReadme(); anonymize(); @@ -786,12 +791,14 @@ angular { params: { force: force === true ? "1" : "0" } } ); $scope.branches = branches.data; - if (!$scope.branch) { - $scope.branch = $scope.details.defaultBranch; + if (!$scope.source.branch) { + $scope.source.branch = $scope.details.defaultBranch; } - const selected = $scope.branches.filter((b) => b.name == $scope.branch); + const selected = $scope.branches.filter( + (b) => b.name == $scope.source.branch + ); if (selected.length > 0) { - $scope.commit = selected[0].commit; + $scope.source.commit = selected[0].commit; $scope.readme = selected[0].readme; } $scope.$apply(); @@ -854,11 +861,17 @@ angular } content = content.replace( - new RegExp(`\\b${$scope.repoUrl}/blob/${$scope.branch}\\b`, "gi"), + new RegExp( + `\\b${$scope.repoUrl}/blob/${$scope.source.branch}\\b`, + "gi" + ), `https://anonymous.4open.science/r/${$scope.repoId}` ); content = content.replace( - new RegExp(`\\b${$scope.repoUrl}/tree/${$scope.branch}\\b`, "gi"), + new RegExp( + `\\b${$scope.repoUrl}/tree/${$scope.source.branch}\\b`, + "gi" + ), `https://anonymous.4open.science/r/${$scope.repoId}` ); content = content.replace( @@ -934,12 +947,14 @@ angular $scope.options.pageSource = $scope.details.pageSource; return { repoId: $scope.repoId, - terms: $scope.terms.trim().split("\n"), + terms: $scope.terms + .trim() + .split("\n") + .filter((f) => f), fullName: `${o.owner}/${o.repo}`, repository: $scope.repoUrl, options: $scope.options, - branch: $scope.branch, - commit: $scope.commit, + source: $scope.source, conference: $scope.conference, }; } diff --git a/src/Repository.ts b/src/Repository.ts index bfeaa15..db05a5f 100644 --- a/src/Repository.ts +++ b/src/Repository.ts @@ -170,7 +170,7 @@ export default class Repository { if (this._model.status == "ready") return; await this.updateStatus("preparing"); await this.files(); - await this.updateStatus("ready"); + return this.updateStatus("ready"); } /** @@ -179,7 +179,7 @@ export default class Repository { async countView() { this._model.lastView = new Date(); this._model.pageView = (this._model.pageView || 0) + 1; - await this._model.save(); + return this._model.save(); } /** @@ -190,21 +190,21 @@ export default class Repository { async updateStatus(status: RepositoryStatus, errorMessage?: string) { this._model.status = status; this._model.errorMessage = errorMessage; - await this._model.save(); + return this._model.save(); } /** * Expire the repository */ async expire() { - this.resetSate("expired"); + return this.resetSate("expired"); } /** * Remove the repository */ async remove() { - this.resetSate("removed"); + return this.resetSate("removed"); } /** diff --git a/src/routes/repository-private.ts b/src/routes/repository-private.ts index a83412c..f56932a 100644 --- a/src/routes/repository-private.ts +++ b/src/routes/repository-private.ts @@ -13,22 +13,6 @@ import Repository from "../Repository"; const router = express.Router(); -// get repository information -router.get("/:repoId/", async (req: express.Request, res: express.Response) => { - const repo = await getRepo(req, res, { nocheck: false }); - if (!repo) return; - - try { - const user = await getUser(req); - if (user.username != repo.model.owner) { - return res.status(401).send({ error: "not_authorized" }); - } - res.json((await db.getRepository(req.params.repoId)).toJSON()); - } catch (error) { - handleError(error, res); - } -}); - // user needs to be connected for all user API router.use(ensureAuthenticated); @@ -71,7 +55,7 @@ router.post("/claim", async (req: express.Request, res: express.Response) => { } }); -// refresh a repository +// refresh repository router.post( "/:repoId/refresh", async (req: express.Request, res: express.Response) => { @@ -183,23 +167,26 @@ function validateNewRepo(repoUpdate) { ) { throw new Error("invalid_repoId"); } - if (!repoUpdate.branch) { + if (!repoUpdate.source.branch) { throw new Error("branch_not_specified"); } + if (!repoUpdate.source.commit) { + throw new Error("commit_not_specified"); + } if (!repoUpdate.options) { throw new Error("options_not_provided"); } if (!Array.isArray(repoUpdate.terms)) { throw new Error("invalid_terms_format"); } - if (!/^[a-f0-9]+$/.test(repoUpdate.commit)) { + if (!/^[a-f0-9]+$/.test(repoUpdate.source.commit)) { throw new Error("invalid_commit_format"); } } function updateRepoModel(model: IAnonymizedRepositoryDocument, repoUpdate) { - model.source.commit = repoUpdate.commit; - model.source.branch = repoUpdate.branch; + model.source.commit = repoUpdate.source.commit; + model.source.branch = repoUpdate.source.branch; model.conference = repoUpdate.conference; model.options = { terms: repoUpdate.terms, @@ -216,6 +203,23 @@ function updateRepoModel(model: IAnonymizedRepositoryDocument, repoUpdate) { pageSource: repoUpdate.options.pageSource, }; } + +// get repository information +router.get("/:repoId/", async (req: express.Request, res: express.Response) => { + try { + const repo = await getRepo(req, res, { nocheck: true }); + if (!repo) throw new Error("repo_not_found"); + + const user = await getUser(req); + if (user.username != repo.model.owner) { + return res.status(401).send({ error: "not_authorized" }); + } + res.json((await db.getRepository(req.params.repoId)).toJSON()); + } catch (error) { + handleError(error, res); + } +}); + // update a repository router.post( "/:repoId/", @@ -233,17 +237,15 @@ router.post( validateNewRepo(repoUpdate); - if (repoUpdate.commit != repo.model.source.commit) { + if (repoUpdate.source.commit != repo.model.source.commit) { repo.model.anonymizeDate = new Date(); - repo.model.source.commit = repoUpdate.commit; + repo.model.source.commit = repoUpdate.source.commit; await repo.remove(); } updateRepoModel(repo.model, repoUpdate); await repo.updateStatus("preparing"); - - await repo.model.save(); res.send("ok"); new Repository(repo.model).anonymize(); } catch (error) { diff --git a/src/source/GitHubBase.ts b/src/source/GitHubBase.ts index 94c8683..e2da5dc 100644 --- a/src/source/GitHubBase.ts +++ b/src/source/GitHubBase.ts @@ -77,7 +77,8 @@ export default abstract class GitHubBase { return { type: this.type, fullName: this.githubRepository.fullName?.toString(), - branch: this.branch, + branch: this.branch.name, + commit: this.branch.commit, }; } }