Skip to content

Commit 468bd12

Browse files
authored
Merge pull request finos#1393 from finos/1386-remote-folder-cleanup
fix: improve handling of repository clones in the .remote folder
2 parents 956d5e2 + aaeda4f commit 468bd12

File tree

13 files changed

+215
-196
lines changed

13 files changed

+215
-196
lines changed

src/proxy/actions/Action.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ class Action {
9494
}
9595

9696
/**
97-
* Set the commit range for the action.
97+
* Set the commit range for the action. Changes the action.id to be based on
98+
* the commit details.
9899
* @param {string} commitFrom the starting commit
99100
* @param {string} commitTo the ending commit
100101
*/

src/proxy/chain.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,13 @@ const pushActionChain: ((req: any, action: Action) => Promise<Action>)[] = [
1010
proc.push.checkCommitMessages,
1111
proc.push.checkAuthorEmails,
1212
proc.push.checkUserPushPermission,
13-
proc.push.pullRemote,
13+
proc.push.pullRemote, // cleanup is handled after chain execution if successful
1414
proc.push.writePack,
1515
proc.push.checkHiddenCommits,
1616
proc.push.checkIfWaitingAuth,
1717
proc.push.preReceive,
1818
proc.push.getDiff,
19-
// run before clear remote
2019
proc.push.gitleaks,
21-
proc.push.clearBareClone,
2220
proc.push.scanDiff,
2321
proc.push.blockForAuth,
2422
];
@@ -35,6 +33,7 @@ let pluginsInserted = false;
3533

3634
export const executeChain = async (req: any, res: any): Promise<Action> => {
3735
let action: Action = {} as Action;
36+
let checkoutCleanUpRequired = false;
3837

3938
try {
4039
action = await proc.pre.parseAction(req);
@@ -44,18 +43,28 @@ export const executeChain = async (req: any, res: any): Promise<Action> => {
4443
action = await fn(req, action);
4544
if (!action.continue() || action.allowPush) {
4645
break;
46+
} else if (fn === proc.push.pullRemote) {
47+
//if the pull was successful then record the fact we need to clean it up again
48+
// pullRemote should cleanup unsuccessful clones itself
49+
checkoutCleanUpRequired = true;
4750
}
4851
}
4952
} catch (e) {
5053
action.error = true;
5154
action.errorMessage = `An error occurred when executing the chain: ${e}`;
5255
console.error(action.errorMessage);
5356
} finally {
54-
await proc.push.audit(req, action);
57+
//clean up the clone created
58+
if (checkoutCleanUpRequired) {
59+
action = await proc.post.clearBareClone(req, action);
60+
}
61+
62+
action = await proc.post.audit(req, action);
63+
5564
if (action.autoApproved) {
56-
attemptAutoApproval(action);
65+
await attemptAutoApproval(action);
5766
} else if (action.autoRejected) {
58-
attemptAutoRejection(action);
67+
await attemptAutoRejection(action);
5968
}
6069
}
6170

src/proxy/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ export class Proxy {
4343
constructor() {}
4444

4545
private async proxyPreparations() {
46+
// Clean-up the .remote folder in case anything was in-progress when we shut down
47+
const remoteDir = './.remote';
48+
if (fs.existsSync(remoteDir)) {
49+
console.log('Cleaning up the existing .remote dir...');
50+
// Recursively remove the contents of ./.remote and ignore exceptions
51+
fs.rmSync(remoteDir, { recursive: true, force: true, retryDelay: 100 });
52+
}
53+
console.log('Creating the .remote dir...');
54+
fs.mkdirSync(remoteDir);
55+
4656
const plugins = getPlugins();
4757
const pluginLoader = new PluginLoader(plugins);
4858
await pluginLoader.load();

src/proxy/processors/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as pre from './pre-processor';
22
import * as push from './push-action';
3+
import * as post from './post-processor';
34

4-
export { pre, push };
5+
export { pre, push, post };
File renamed without changes.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Action, Step } from '../../actions';
2+
import fs from 'fs';
3+
4+
const exec = async (req: any, action: Action): Promise<Action> => {
5+
const step = new Step('clearBareClone');
6+
7+
// Recursively remove the contents of ./.remote and ignore exceptions
8+
if (action.proxyGitPath) {
9+
fs.rmSync(action.proxyGitPath, { recursive: true, force: true });
10+
step.log(`.remote is deleted!`);
11+
} else {
12+
// This action should not be called unless a clone was made successfully as pullRemote cleans up after itself on failures
13+
// Log an error as we couldn't delete the clone
14+
step.setError(`action.proxyGitPath was not set and cannot be removed`);
15+
}
16+
action.addStep(step);
17+
18+
return action;
19+
};
20+
21+
exec.displayName = 'clearBareClone.exec';
22+
23+
export { exec };
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { exec as audit } from '../post-processor/audit';
2+
import { exec as clearBareClone } from '../post-processor/clearBareClone';
3+
4+
export { audit, clearBareClone };

src/proxy/processors/push-action/clearBareClone.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/proxy/processors/push-action/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { exec as parsePush } from './parsePush';
22
import { exec as preReceive } from './preReceive';
33
import { exec as checkRepoInAuthorisedList } from './checkRepoInAuthorisedList';
4-
import { exec as audit } from './audit';
4+
55
import { exec as pullRemote } from './pullRemote';
66
import { exec as writePack } from './writePack';
77
import { exec as getDiff } from './getDiff';
@@ -13,14 +13,13 @@ import { exec as checkIfWaitingAuth } from './checkIfWaitingAuth';
1313
import { exec as checkCommitMessages } from './checkCommitMessages';
1414
import { exec as checkAuthorEmails } from './checkAuthorEmails';
1515
import { exec as checkUserPushPermission } from './checkUserPushPermission';
16-
import { exec as clearBareClone } from './clearBareClone';
16+
1717
import { exec as checkEmptyBranch } from './checkEmptyBranch';
1818

1919
export {
2020
parsePush,
2121
preReceive,
2222
checkRepoInAuthorisedList,
23-
audit,
2423
pullRemote,
2524
writePack,
2625
getDiff,
@@ -32,6 +31,5 @@ export {
3231
checkCommitMessages,
3332
checkAuthorEmails,
3433
checkUserPushPermission,
35-
clearBareClone,
3634
checkEmptyBranch,
3735
};

src/proxy/processors/push-action/parsePush.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ async function exec(req: any, action: Action): Promise<Action> {
6363
// Strip everything after NUL, which is cap-list from
6464
// https://git-scm.com/docs/http-protocol#_smart_server_response
6565
action.branch = ref.replace(/\0.*/, '').trim();
66+
67+
// Note this will change the action.id to be based on the commits
6668
action.setCommit(oldCommit, newCommit);
6769

6870
// Check if the offset is valid and if there's data after it

0 commit comments

Comments
 (0)