Skip to content

Commit 95d1d90

Browse files
committed
fix: refactor problematic checks (undefined commiter, user)
1 parent 3758d80 commit 95d1d90

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
lines changed

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,29 @@ import { getUsers, isUserPushAllowed } from '../../../db';
44
// Execute if the repo is approved
55
const exec = async (req: any, action: Action): Promise<Action> => {
66
const step = new Step('checkUserPushPermission');
7+
const user = action.user;
78

9+
if (!user) {
10+
step.log('Action has no user set. This may be due to a fast-forward ref update. Deferring to getMissingData action.');
11+
action.addStep(step);
12+
return action;
13+
}
14+
15+
return await validateUser(user, action, step);
16+
};
17+
18+
/**
19+
* Helper that validates the user's push permission.
20+
* This can be used by other actions that need it. For example, when the user is missing from the commit data,
21+
* validation is deferred to getMissingData, but the logic is the same.
22+
* @param {string} user The user to validate
23+
* @param {Action} action The action object
24+
* @param {Step} step The step object
25+
* @return {Promise<Action>} The action object
26+
*/
27+
const validateUser = async (user: string, action: Action, step: Step): Promise<Action> => {
828
const repoName = action.repo.split('/')[1].replace('.git', '');
929
let isUserAllowed = false;
10-
let user = action.user;
1130

1231
// Find the user associated with this Git Account
1332
const list = await getUsers({ gitAccount: action.user });
@@ -44,4 +63,4 @@ const exec = async (req: any, action: Action): Promise<Action> => {
4463

4564
exec.displayName = 'checkUserPushPermission.exec';
4665

47-
export { exec };
66+
export { exec, validateUser };

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

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
import { Action, Step } from '../../actions';
22
import zlib from 'zlib';
3-
import fs from 'fs';
4-
import path from 'path';
53
import lod from 'lodash';
64
import { CommitContent } from '../types';
75
const BitMask = require('bit-mask') as any;
86

9-
const dir = path.resolve(__dirname, './.tmp');
10-
11-
if (!fs.existsSync(dir)) {
12-
fs.mkdirSync(dir, { recursive: true });
13-
}
14-
157
/**
168
* Executes the parsing of a push request.
179
* @param {*} req - The request object containing the push data.
@@ -29,7 +21,6 @@ async function exec(req: any, action: Action): Promise<Action> {
2921
return action;
3022
}
3123
const [packetLines, packDataOffset] = parsePacketLines(req.body);
32-
3324
const refUpdates = packetLines.filter((line) => line.includes('refs/heads/'));
3425

3526
if (refUpdates.length !== 1) {
@@ -68,15 +59,16 @@ async function exec(req: any, action: Action): Promise<Action> {
6859
const contents = getContents(contentBuff as any, meta.entries as number);
6960

7061
action.commitData = getCommitData(contents as any);
71-
72-
if (action.commitFrom === '0000000000000000000000000000000000000000') {
73-
action.commitFrom = action.commitData[action.commitData.length - 1].parent;
62+
if (action.commitData.length === 0) {
63+
step.log('No commit data found when parsing push.')
64+
} else {
65+
if (action.commitFrom === '0000000000000000000000000000000000000000') {
66+
action.commitFrom = action.commitData[action.commitData.length - 1].parent;
67+
}
68+
const user = action.commitData[action.commitData.length - 1].committer;
69+
action.user = user;
7470
}
7571

76-
const user = action.commitData[action.commitData.length - 1].committer;
77-
console.log(`Push Request received from user ${user}`);
78-
action.user = user;
79-
8072
step.content = {
8173
meta: meta,
8274
};
@@ -235,6 +227,7 @@ const getContents = (buffer: Buffer | CommitContent[], entries: number) => {
235227
for (let i = 0; i < entries; i++) {
236228
try {
237229
const [content, nextBuffer] = getContent(i, buffer as Buffer);
230+
console.log({ content, nextBuffer });
238231
buffer = nextBuffer as Buffer;
239232
contents.push(content);
240233
} catch (e) {

0 commit comments

Comments
 (0)