Skip to content

Commit 5c3ec06

Browse files
committed
Merge branch 'typescript-setup' of https://github.com/fabiovincenzi/git-proxy into pr/fabiovincenzi/1142
2 parents 5e76a44 + 8f18d23 commit 5c3ec06

40 files changed

+1299
-1157
lines changed

.github/workflows/sample-publish.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ permissions:
99
contents: read
1010

1111
jobs:
12+
build-and-publish:
1213
build-and-publish:
1314
runs-on: ubuntu-latest
1415
steps:
@@ -21,7 +22,7 @@ jobs:
2122
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
2223

2324
- name: Setup Node.js # Setup .npmrc file to publish to npm
24-
uses: actions/setup-node@cdca7365b2dadb8aad0a33bc7601856ffabcc48e # v4
25+
uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4
2526
with:
2627
node-version: '22.x'
2728
registry-url: 'https://registry.npmjs.org'

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
"source.fixAll.eslint": "explicit"
99
},
1010
"editor.defaultFormatter": "esbenp.prettier-vscode",
11-
"editor.formatOnSave": true
11+
"editor.formatOnSave": true,
12+
"cSpell.words": ["Deltafied"]
1213
}

package-lock.json

Lines changed: 45 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@
145145
"eslint-plugin-react": "^7.37.5",
146146
"eslint-plugin-standard": "^5.0.0",
147147
"eslint-plugin-typescript": "^0.14.0",
148+
"fast-check": "^4.2.0",
148149
"husky": "^9.1.7",
149150
"mocha": "^10.8.2",
150151
"nyc": "^17.1.0",

packages/git-proxy-cli/index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ async function authoriseGitPush(id) {
176176
if (error.response) {
177177
switch (error.response.status) {
178178
case 401:
179-
errorMessage = 'Error: Authorise: Authentication required';
179+
errorMessage =
180+
'Error: Authorise: Authentication required (401): ' + error?.response?.data?.message;
180181
process.exitCode = 3;
181182
break;
182183
case 404:
@@ -223,7 +224,8 @@ async function rejectGitPush(id) {
223224
if (error.response) {
224225
switch (error.response.status) {
225226
case 401:
226-
errorMessage = 'Error: Reject: Authentication required';
227+
errorMessage =
228+
'Error: Reject: Authentication required (401): ' + error?.response?.data?.message;
227229
process.exitCode = 3;
228230
break;
229231
case 404:
@@ -270,7 +272,8 @@ async function cancelGitPush(id) {
270272
if (error.response) {
271273
switch (error.response.status) {
272274
case 401:
273-
errorMessage = 'Error: Cancel: Authentication required';
275+
errorMessage =
276+
'Error: Cancel: Authentication required (401): ' + error?.response?.data?.message;
274277
process.exitCode = 3;
275278
break;
276279
case 404:

packages/git-proxy-cli/test/testCli.test.js

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ const TEST_REPO_CONFIG = {
2222
url: 'https://github.com/finos/git-proxy-test.git',
2323
};
2424
const TEST_REPO = 'finos/git-proxy-test.git';
25+
// user for test cases
26+
const TEST_USER = 'testuser';
27+
const TEST_PASSWORD = 'testpassword';
28+
const TEST_EMAIL = '[email protected]';
29+
const TEST_GIT_ACCOUNT = 'testGitAccount';
2530

2631
describe('test git-proxy-cli', function () {
2732
// *** help ***
@@ -87,16 +92,12 @@ describe('test git-proxy-cli', function () {
8792
// *** login ***
8893

8994
describe('test git-proxy-cli :: login', function () {
90-
const testUser = 'testuser';
91-
const testPassword = 'testpassword';
92-
const testEmail = '[email protected]';
93-
9495
before(async function () {
95-
await helper.addUserToDb(testUser, testPassword, testEmail, 'testGitAccount');
96+
await helper.addUserToDb(TEST_USER, TEST_PASSWORD, TEST_EMAIL, TEST_GIT_ACCOUNT);
9697
});
9798

9899
after(async function () {
99-
await helper.removeUserFromDb(testUser);
100+
await helper.removeUserFromDb(TEST_USER);
100101
});
101102

102103
it('login should fail when server is down', async function () {
@@ -140,9 +141,9 @@ describe('test git-proxy-cli', function () {
140141
});
141142

142143
it('login shoud be successful with valid credentials (non-admin)', async function () {
143-
const cli = `npx -- @finos/git-proxy-cli login --username ${testUser} --password ${testPassword}`;
144+
const cli = `npx -- @finos/git-proxy-cli login --username ${TEST_USER} --password ${TEST_PASSWORD}`;
144145
const expectedExitCode = 0;
145-
const expectedMessages = [`Login "${testUser}" <${testEmail}>: OK`];
146+
const expectedMessages = [`Login "${TEST_USER}" <${TEST_EMAIL}>: OK`];
146147
const expectedErrorMessages = null;
147148
try {
148149
await helper.startServer(service);
@@ -219,11 +220,13 @@ describe('test git-proxy-cli', function () {
219220

220221
before(async function () {
221222
await helper.addRepoToDb(TEST_REPO_CONFIG);
222-
await helper.addGitPushToDb(pushId, TEST_REPO);
223+
await helper.addUserToDb(TEST_USER, TEST_PASSWORD, TEST_EMAIL, TEST_GIT_ACCOUNT);
224+
await helper.addGitPushToDb(pushId, TEST_USER, TEST_EMAIL, TEST_REPO);
223225
});
224226

225227
after(async function () {
226228
await helper.removeGitPushFromDb(pushId);
229+
await helper.removeUserFromDb(TEST_USER);
227230
await helper.removeRepoFromDb(TEST_REPO_CONFIG.name);
228231
});
229232

@@ -294,11 +297,13 @@ describe('test git-proxy-cli', function () {
294297

295298
before(async function () {
296299
await helper.addRepoToDb(TEST_REPO_CONFIG);
297-
await helper.addGitPushToDb(pushId, TEST_REPO);
300+
await helper.addUserToDb(TEST_USER, TEST_PASSWORD, TEST_EMAIL, TEST_GIT_ACCOUNT);
301+
await helper.addGitPushToDb(pushId, TEST_USER, TEST_EMAIL, TEST_REPO);
298302
});
299303

300304
after(async function () {
301305
await helper.removeGitPushFromDb(pushId);
306+
await helper.removeUserFromDb(TEST_USER);
302307
await helper.removeRepoFromDb(TEST_REPO_CONFIG.name);
303308
});
304309

@@ -415,11 +420,13 @@ describe('test git-proxy-cli', function () {
415420

416421
before(async function () {
417422
await helper.addRepoToDb(TEST_REPO_CONFIG);
418-
await helper.addGitPushToDb(pushId, TEST_REPO);
423+
await helper.addUserToDb(TEST_USER, TEST_PASSWORD, TEST_EMAIL, TEST_GIT_ACCOUNT);
424+
await helper.addGitPushToDb(pushId, TEST_USER, TEST_EMAIL, TEST_REPO);
419425
});
420426

421427
after(async function () {
422428
await helper.removeGitPushFromDb(pushId);
429+
await helper.removeUserFromDb(TEST_USER);
423430
await helper.removeRepoFromDb(TEST_REPO_CONFIG.name);
424431
});
425432

@@ -487,12 +494,17 @@ describe('test git-proxy-cli', function () {
487494

488495
describe('test git-proxy-cli :: git push administration', function () {
489496
const pushId = `0000000000000000000000000000000000000000__${Date.now()}`;
490-
const gitAccount = 'testGitAccount1';
491497

492498
before(async function () {
493499
await helper.addRepoToDb(TEST_REPO_CONFIG);
494-
await helper.addUserToDb('testuser1', 'testpassword', '[email protected]', gitAccount);
495-
await helper.addGitPushToDb(pushId, TEST_REPO, gitAccount);
500+
await helper.addUserToDb(TEST_USER, TEST_PASSWORD, TEST_EMAIL, TEST_GIT_ACCOUNT);
501+
await helper.addGitPushToDb(pushId, TEST_REPO, TEST_USER, TEST_EMAIL);
502+
});
503+
504+
after(async function () {
505+
await helper.removeGitPushFromDb(pushId);
506+
await helper.removeUserFromDb(TEST_USER);
507+
await helper.removeRepoFromDb(TEST_REPO_CONFIG.name);
496508
});
497509

498510
after(async function () {

packages/git-proxy-cli/test/testCliUtils.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,10 @@ async function removeRepoFromDb(repoName) {
177177
* @param {string} id The ID of the git push.
178178
* @param {string} repo The repository of the git push.
179179
* @param {string} user The user who pushed the git push.
180+
* @param {string} userEmail The email of the user who pushed the git push.
180181
* @param {boolean} debug Flag to enable logging for debugging.
181182
*/
182-
async function addGitPushToDb(id, repo, user = null, debug = false) {
183+
async function addGitPushToDb(id, repo, user = null, userEmail = null, debug = false) {
183184
const action = new actions.Action(
184185
id,
185186
'push', // type
@@ -188,6 +189,7 @@ async function addGitPushToDb(id, repo, user = null, debug = false) {
188189
repo,
189190
);
190191
action.user = user;
192+
action.userEmail = userEmail;
191193
const step = new steps.Step(
192194
'authBlock', // stepName
193195
false, // error

src/proxy/actions/Action.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Step } from './Step';
77
export interface Commit {
88
message: string;
99
committer: string;
10+
committerEmail: string;
1011
tree: string;
1112
parent: string;
1213
author: string;
@@ -45,6 +46,7 @@ class Action {
4546
message?: string;
4647
author?: string;
4748
user?: string;
49+
userEmail?: string;
4850
attestation?: string;
4951
lastStep?: Step;
5052
proxyGitPath?: string;

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: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
if (action.commitFrom === EMPTY_COMMIT_HASH) {
7+
try {
8+
const git = simpleGit(`${action.proxyGitPath}/${action.repoName}`);
9+
10+
const type = await git.raw(['cat-file', '-t', action.commitTo || '']);
11+
return type.trim() === 'commit';
12+
} catch (err) {
13+
console.log(`Commit ${action.commitTo} not found: ${err}`);
14+
}
15+
}
16+
17+
return false;
18+
};
19+
20+
const exec = async (req: any, action: Action): Promise<Action> => {
21+
const step = new Step('checkEmptyBranch');
22+
23+
if (action.commitData && action.commitData.length > 0) {
24+
return action;
25+
}
26+
27+
if (await isEmptyBranch(action)) {
28+
step.setError('Push blocked: Empty branch. Please make a commit before pushing a new branch.');
29+
action.addStep(step);
30+
step.error = true;
31+
return action;
32+
} else {
33+
step.setError('Push blocked: Commit data not found. Please contact an administrator for support.');
34+
action.addStep(step);
35+
step.error = true;
36+
return action;
37+
}
38+
};
39+
40+
exec.displayName = 'checkEmptyBranch.exec';
41+
42+
export { exec };

0 commit comments

Comments
 (0)