From 76dca8b5e65d60b926732fcc58520b82d6c1d430 Mon Sep 17 00:00:00 2001 From: dzdidi Date: Sun, 11 Feb 2024 17:00:16 +0000 Subject: [PATCH] rpc-git: fix function calls Signed-off-by: dzdidi --- src/rpc-handlers/git.js | 56 ++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/src/rpc-handlers/git.js b/src/rpc-handlers/git.js index 8470dc2..cbd6456 100644 --- a/src/rpc-handlers/git.js +++ b/src/rpc-handlers/git.js @@ -1,6 +1,7 @@ +const ACL = require('../acl') -async getReposHandler (publicKey, req) { - const { branch, url, userId } = await this.parseReq(publicKey, req) +async function getReposHandler (publicKey, req) { + const { branch, url, userId } = await parseReq.bind(this)(publicKey, req) const res = {} for (const repoName in this.repositories) { @@ -14,8 +15,8 @@ async getReposHandler (publicKey, req) { return Buffer.from(JSON.stringify(res)) } -async getRefsHandler (publicKey, req) { - const { repoName, branch, url, userId } = await this.parseReq(publicKey, req) +async function getRefsHandler (publicKey, req) { + const { repoName, branch, url, userId } = await parseReq.bind(this)(publicKey, req) const res = this.repositories[repoName] const isPublic = (ACL.getACL(repoName).visibility === 'public') @@ -26,8 +27,8 @@ async getRefsHandler (publicKey, req) { } } -async pushHandler (publicKey, req) { - const { url, repoName, branch, userId } = await this.parseReq(publicKey, req) +async function pushHandler (publicKey, req) { + const { url, repoName, branch, userId } = await parseReq.bind(this)(publicKey, req) const isContributor = ACL.getContributors(repoName).includes(userId) if (!isContributor) throw new Error('You are not allowed to push to this repo') @@ -51,8 +52,8 @@ async pushHandler (publicKey, req) { }) } -async forcePushHandler (publicKey, req) { - const { url, repoName, branch, userId } = await this.parseReq(publicKey, req) +async function forcePushHandler (publicKey, req) { + const { url, repoName, branch, userId } = await parseReq.bind(this)(publicKey, req) const isContributor = ACL.getContributors(repoName).includes(userId) if (!isContributor) throw new Error('You are not allowed to push to this repo') @@ -76,33 +77,32 @@ async forcePushHandler (publicKey, req) { }) } - async deleteBranchHandler (publicKey, req) { - const { url, repoName, branch, userId } = await this.parseReq(publicKey, req) - const isContributor = ACL.getContributors(repoName).includes(userId) +async function deleteBranchHandler (publicKey, req) { + const { url, repoName, branch, userId } = await parseReq.bind(this)(publicKey, req) + const isContributor = ACL.getContributors(repoName).includes(userId) - if (!isContributor) throw new Error('You are not allowed to push to this repo') + if (!isContributor) throw new Error('You are not allowed to push to this repo') - const isProtectedBranch = ACL.getACL(repoName).protectedBranches.includes(branch) - const isAdmin = ACL.getAdmins(repoName).includes(userId) + const isProtectedBranch = ACL.getACL(repoName).protectedBranches.includes(branch) + const isAdmin = ACL.getAdmins(repoName).includes(userId) - if (isProtectedBranch && !isAdmin) throw new Error('You are not allowed to push to this branch') + if (isProtectedBranch && !isAdmin) throw new Error('You are not allowed to push to this branch') - return await new Promise((resolve, reject) => { - const env = { ...process.env, GIT_DIR: home.getCodePath(repoName) } - const child = spawn('git', ['branch', '-D', branch], { env }) - let errBuffer = Buffer.from('') - child.stderr.on('data', data => { - errBuffer = Buffer.concat([errBuffer, data]) - }) - - child.on('close', code => { - return code === 0 ? resolve(errBuffer) : reject(errBuffer) - }) + return await new Promise((resolve, reject) => { + const env = { ...process.env, GIT_DIR: home.getCodePath(repoName) } + const child = spawn('git', ['branch', '-D', branch], { env }) + let errBuffer = Buffer.from('') + child.stderr.on('data', data => { + errBuffer = Buffer.concat([errBuffer, data]) }) - } + child.on('close', code => { + return code === 0 ? resolve(errBuffer) : reject(errBuffer) + }) + }) +} -async parseReq(publicKey, req) { +async function parseReq(publicKey, req) { if (!req) throw new Error('Request is empty') const request = JSON.parse(req.toString()) const parsed = {