From 4da3272bf7893760f6710c9a1ec7de02358136e6 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sat, 5 Jun 2021 19:37:52 +0200 Subject: Reorganized into a monorepo, refactored the frontend again, goodbye Parcel --- packages/server/src/api/util.js | 45 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 packages/server/src/api/util.js (limited to 'packages/server/src/api/util.js') diff --git a/packages/server/src/api/util.js b/packages/server/src/api/util.js new file mode 100644 index 0000000..aa31296 --- /dev/null +++ b/packages/server/src/api/util.js @@ -0,0 +1,45 @@ +const fs = require("fs"); +const git = require("./git"); + +function verifyRepoName(dirty, base_dir) +{ + return new Promise((resolve) => + { + const is_valid_repo_name = /^[a-zA-Z0-9\\.\-_]+$/.test(dirty); + if(!is_valid_repo_name) { + resolve("ERR_REPO_REGEX"); + } + + fs.readdir(base_dir, (err, dir_content) => + { + if(err) { + resolve("ERR_REPO_NOT_FOUND"); + } + + dir_content = dir_content.filter(repo => repo.endsWith(".git")); + if(!dir_content.includes(dirty + ".git")) { + resolve("ERR_REPO_NOT_FOUND"); + } + + resolve(true); + }); + }); +} + +async function verifyCommitID(base_dir, repo, dirty) +{ + if(!/^[a-fA-F0-9]+$/.test(dirty)) { + return "ERR_COMMIT_REGEX"; + } + + const commit_exists = await git.doesCommitExist(base_dir, repo, dirty); + + if(!commit_exists) { + return "ERR_COMMIT_NOT_FOUND"; + } + + return true; +} + +module.exports.verifyRepoName = verifyRepoName; +module.exports.verifyCommitID = verifyCommitID; \ No newline at end of file -- cgit v1.2.3-18-g5258