From 06b3d0e8f175d8deb1ed03052ee9663e0d8d1e3e Mon Sep 17 00:00:00 2001 From: tdurieux Date: Thu, 22 Apr 2021 09:01:30 +0200 Subject: [PATCH] fix claim --- routes/repository.js | 70 ++++++++++++++++++++++---------------------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/routes/repository.js b/routes/repository.js index 2d071da..04ea0f8 100644 --- a/routes/repository.js +++ b/routes/repository.js @@ -16,6 +16,41 @@ const router = express.Router(); // user needs to be connected for all user API router.use(connection.ensureAuthenticated); +// claim a repository +router.post("/claim", async (req, res) => { + try { + if (!req.body.repoId) { + return res.status(500).json({ error: "repoId_not_defined" }); + } + if (!req.body.repoUrl) { + return res.status(500).json({ error: "repoUrl_not_defined" }); + } + + const repoConfig = await repoUtils.getConfig(req.body.repoId); + if (repoConfig == null) { + return res.status(500).json({ error: "repo_not_found" }); + } + + const repo = gh(req.body.repoUrl); + if (repoConfig.fullName != repo.repository) { + return res.status(500).json({ error: "repo_not_found" }); + } + + console.log(`${req.user.username} claims ${repoConfig.fullName}.`); + + await db + .get("anonymized_repositories") + .updateOne( + { repoId: repoConfig.repoId }, + { $set: { owner: req.user.username } } + ); + return res.send("Ok"); + } catch (error) { + console.error(req.path, error); + return res.status(500).json({ error }); + } +}); + router.get("/:repoId/", async (req, res) => { try { const repository = await repoUtils.getAnonymizedRepoDetails( @@ -173,41 +208,6 @@ router.delete("/:repoId/", async (req, res) => { } }); -// claim a repository -router.post("/claim", async (req, res) => { - try { - if (!req.body.repoId) { - return res.status(500).json({ error: "repoId_not_defined" }); - } - if (!req.body.repoUrl) { - return res.status(500).json({ error: "repoUrl_not_defined" }); - } - - const repoConfig = await repoUtils.getConfig(req.body.repoId); - if (repoConfig == null) { - return res.status(500).json({ error: "repo_not_found" }); - } - - const repo = gh(req.body.repoUrl); - if (repoConfig.fullName != repo.repository) { - return res.status(500).json({ error: "repo_not_found" }); - } - - console.log(`${req.user.username} claims ${repoConfig.fullName}.`); - - await db - .get("anonymized_repositories") - .updateOne( - { repoId: repoConfig.repoId }, - { $set: { owner: req.user.username } } - ); - return res.send("Ok"); - } catch (error) { - console.error(req.path, error); - return res.status(500).json({ error }); - } -}); - router.get("/:owner/:repo/", async (req, res) => { try { const repository = await repoUtils.getRepoDetails({