Skip to content

Commit 654d535

Browse files
committed
refactor: replace getMissingData with checkEmptyBranch and move action order
1 parent 6bdf166 commit 654d535

File tree

6 files changed

+54
-82
lines changed

6 files changed

+54
-82
lines changed

src/proxy/chain.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { attemptAutoApproval, attemptAutoRejection } from './actions/autoActions
55

66
const pushActionChain: ((req: any, action: Action) => Promise<Action>)[] = [
77
proc.push.parsePush,
8+
proc.push.checkEmptyBranch,
89
proc.push.checkRepoInAuthorisedList,
910
proc.push.checkCommitMessages,
1011
proc.push.checkAuthorEmails,
@@ -13,7 +14,6 @@ const pushActionChain: ((req: any, action: Action) => Promise<Action>)[] = [
1314
proc.push.writePack,
1415
proc.push.checkHiddenCommits,
1516
proc.push.checkIfWaitingAuth,
16-
proc.push.getMissingData,
1717
proc.push.preReceive,
1818
proc.push.getDiff,
1919
// run before clear remote
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { Action, Step } from '../../actions';
2+
import simpleGit from 'simple-git';
3+
import { EMPTY_COMMIT_HASH } from '../constants';
4+
5+
const isEmptyBranch = async (action: Action) => {
6+
const git = simpleGit(`${action.proxyGitPath}/${action.repoName}`);
7+
8+
if (action.commitFrom === EMPTY_COMMIT_HASH) {
9+
try {
10+
const type = await git.raw(['cat-file', '-t', action.commitTo || '']);
11+
const known = type.trim() === 'commit';
12+
if (known) {
13+
return true;
14+
}
15+
} catch (err) {
16+
console.log(`Commit ${action.commitTo} not found: ${err}`);
17+
}
18+
}
19+
20+
return false;
21+
};
22+
23+
const exec = async (req: any, action: Action): Promise<Action> => {
24+
const step = new Step('checkEmptyBranch');
25+
26+
if (action.commitData && action.commitData.length > 0) {
27+
return action;
28+
}
29+
30+
if (await isEmptyBranch(action)) {
31+
step.setError('Push blocked: Empty branch. Please make a commit before pushing a new branch.');
32+
action.addStep(step);
33+
step.error = true;
34+
return action;
35+
} else {
36+
step.setError('Push blocked: Commit data not found. Please contact an administrator for support.');
37+
action.addStep(step);
38+
step.error = true;
39+
return action;
40+
}
41+
};
42+
43+
exec.displayName = 'checkEmptyBranch.exec';
44+
45+
export { exec };

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ const exec = async (req: any, action: Action): Promise<Action> => {
88
const user = action.user;
99

1010
if (!user) {
11-
console.log('Action has no user set. This may be due to a fast-forward ref update. Deferring to getMissingData action.');
11+
step.setError('Push blocked: User not found. Please contact an administrator for support.');
12+
action.addStep(step);
13+
step.error = true;
1214
return action;
1315
}
1416

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

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

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { exec as checkCommitMessages } from './checkCommitMessages';
1414
import { exec as checkAuthorEmails } from './checkAuthorEmails';
1515
import { exec as checkUserPushPermission } from './checkUserPushPermission';
1616
import { exec as clearBareClone } from './clearBareClone';
17-
import { exec as getMissingData } from './getMissingData';
17+
import { exec as checkEmptyBranch } from './checkEmptyBranch';
1818

1919
export {
2020
parsePush,
@@ -33,5 +33,5 @@ export {
3333
checkAuthorEmails,
3434
checkUserPushPermission,
3535
clearBareClone,
36-
getMissingData,
36+
checkEmptyBranch,
3737
};

test/processors/checkCommitMessages.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,9 @@ describe('checkCommitMessages', () => {
9797
});
9898

9999
it('should not error when commit data is empty', async () => {
100-
// Empty commit data is a valid scenario that happens when making a branch from an unapproved commit
101-
// This is remedied in the getMissingData.exec action
100+
// Empty commit data happens when making a branch from an unapproved commit
101+
// or when pushing an empty branch or deleting a branch
102+
// This is remedied in the checkEmptyBranch.exec action
102103
action.commitData = [];
103104
const result = await exec(req, action);
104105

0 commit comments

Comments
 (0)