fix claim

This commit is contained in:
tdurieux
2021-04-22 09:01:30 +02:00
parent aaf7e49bb4
commit 06b3d0e8f1

View File

@@ -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({