diff --git a/src/proxy/chain.js b/src/proxy/chain.js index 68982eef..c389a989 100644 --- a/src/proxy/chain.js +++ b/src/proxy/chain.js @@ -11,6 +11,7 @@ const pushActionChain = [ proc.push.pullRemote, proc.push.writePack, proc.push.getDiff, + proc.push.clearBareClone, proc.push.scanDiff, proc.push.blockForAuth, ]; diff --git a/src/proxy/processors/push-action/clearBareClone.js b/src/proxy/processors/push-action/clearBareClone.js new file mode 100644 index 00000000..dfda43eb --- /dev/null +++ b/src/proxy/processors/push-action/clearBareClone.js @@ -0,0 +1,20 @@ +const Step = require('../../actions').Step; +const fs = require('node:fs'); + +const exec = async (req, action) => { + const step = new Step('clearBareClone'); + + // Recursively remove the contents of ./.remote and ignore exceptions + fs.rm('./.remote', { recursive: true, force: true }, (err) => { + if (err) { + throw err; + } + console.log(`.remote is deleted!`); + }); + + action.addStep(step); + return action; +}; + +exec.displayName = 'clearBareClone.exec'; +exports.exec = exec; diff --git a/src/proxy/processors/push-action/index.js b/src/proxy/processors/push-action/index.js index 309b82cd..72a97b33 100644 --- a/src/proxy/processors/push-action/index.js +++ b/src/proxy/processors/push-action/index.js @@ -10,3 +10,4 @@ exports.checkIfWaitingAuth = require('./checkIfWaitingAuth').exec; exports.checkCommitMessages = require('./checkCommitMessages').exec; exports.checkAuthorEmails = require('./checkAuthorEmails').exec; exports.checkUserPushPermission = require('./checkUserPushPermission').exec; +exports.clearBareClone = require('./clearBareClone').exec; diff --git a/test/testClearBareClone.test.js b/test/testClearBareClone.test.js new file mode 100644 index 00000000..c600610c --- /dev/null +++ b/test/testClearBareClone.test.js @@ -0,0 +1,26 @@ +const fs = require('fs'); +const chai = require('chai'); +const clearBareClone = require('../src/proxy/processors/push-action/clearBareClone').exec; +const pullRemote = require('../src/proxy/processors/push-action/pullRemote').exec; +const { Action } = require('../src/proxy/actions/Action'); +chai.should(); + +const expect = chai.expect; +const timestamp = Date.now(); + +describe('clear bare and local clones', async () => { + it('pull remote generates a local .remote folder', async () => { + const action = new Action('123', 'type', 'get', timestamp, 'finos/git-proxy'); + action.url = 'https://github.com/finos/git-proxy'; + await pullRemote({}, action); + + expect(fs.existsSync(`./.remote/${timestamp}`)).to.be.true; + }); + + it('clear bare clone function purges .remote folder and specific clone folder', async () => { + const action = new Action('123', 'type', 'get', timestamp, 'finos/git-proxy'); + await clearBareClone(null, action); + expect(fs.existsSync(`./.remote`)).to.throw; + expect(fs.existsSync(`./.remote/${timestamp}`)).to.throw; + }); +});