forked from finos/git-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparsePush.ts
More file actions
275 lines (222 loc) · 7.17 KB
/
parsePush.ts
File metadata and controls
275 lines (222 loc) · 7.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import { Action, Step } from '../../actions';
import zlib from 'zlib';
import fs from 'fs';
import path from 'path';
import lod from 'lodash';
import { CommitContent } from '../types';
const BitMask = require('bit-mask') as any;
const dir = path.resolve(__dirname, './.tmp');
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
async function exec(req: any, action: Action): Promise<Action> {
const step = new Step('parsePackFile');
try {
if (!req.body || req.body.length === 0) {
throw new Error('No body found in request');
}
const messageParts = req.body.toString('utf8').split(' ');
action.branch = messageParts[2].trim().replace('\u0000', '');
action.setCommit(messageParts[0].substr(4), messageParts[1]);
const index = req.body.lastIndexOf('PACK');
const buf = req.body.slice(index);
const [meta, contentBuff] = getPackMeta(buf);
const contents = getContents(contentBuff as any, meta.entries as number);
action.commitData = getCommitData(contents as any);
if (action.commitFrom === '0000000000000000000000000000000000000000') {
action.commitFrom = action.commitData[action.commitData.length - 1].parent;
}
const { committer, committerEmail } = action.commitData[action.commitData.length - 1];
console.log(`Push Request received from user ${committer} with email ${committerEmail}`);
action.user = committer;
action.userEmail = committerEmail;
step.content = {
meta: meta,
};
} catch (e: any) {
step.setError(
`Unable to parse push. Please contact an administrator for support: ${e.toString('utf-8')}`,
);
} finally {
action.addStep(step);
}
return action;
}
const getCommitData = (contents: CommitContent[]) => {
console.log({ contents });
return lod
.chain(contents)
.filter({ type: 1 })
.map((x) => {
console.log({ x });
const formattedContent = x.content.split('\n');
console.log({ formattedContent });
const parts = formattedContent.filter((part) => part.length > 0);
console.log({ parts });
if (!parts || parts.length < 5) {
throw new Error('Invalid commit data');
}
const tree = parts
.find((t) => t.split(' ')[0] === 'tree')
?.replace('tree', '')
.trim();
console.log({ tree });
const parentValue = parts.find((t) => t.split(' ')[0] === 'parent');
console.log({ parentValue });
const parent = parentValue
? parentValue.replace('parent', '').trim()
: '0000000000000000000000000000000000000000';
console.log({ parent });
const author = parts
.find((t) => t.split(' ')[0] === 'author')
?.replace('author', '')
.trim();
console.log({ author });
const committer = parts
.find((t) => t.split(' ')[0] === 'committer')
?.replace('committer', '')
.trim();
console.log({ committer });
const indexOfMessages = formattedContent.indexOf('');
console.log({ indexOfMessages });
const message = formattedContent
.slice(indexOfMessages + 1)
.join(' ')
.trim();
console.log({ message });
const commitTimestamp = committer?.split(' ').reverse()[1];
console.log({ commitTimestamp });
const authorEmail = author?.split(' ').reverse()[2].slice(1, -1);
console.log({ authorEmail });
const committerEmail = committer?.split(' ').reverse()[2].slice(1, -1);
console.log({ committerEmail });
console.log({
tree,
parent,
author: author?.split('<')[0].trim(),
committer: committer?.split('<')[0].trim(),
commitTimestamp,
message,
authorEmail,
committerEmail,
});
if (
!tree ||
!parent ||
!author ||
!committer ||
!commitTimestamp ||
!message ||
!authorEmail ||
!committerEmail
) {
throw new Error('Invalid commit data');
}
return {
tree,
parent,
author: author.split('<')[0].trim(),
committer: committer.split('<')[0].trim(),
commitTimestamp,
message,
authorEmail,
committerEmail,
};
})
.value();
};
const getPackMeta = (buffer: Buffer) => {
const sig = buffer.slice(0, 4).toString('utf-8');
const version = buffer.readUIntBE(4, 4);
const entries = buffer.readUIntBE(8, 4);
const meta = {
sig: sig,
version: version,
entries: entries,
};
return [meta, buffer.slice(12)];
};
const getContents = (buffer: Buffer | CommitContent[], entries: number) => {
const contents = [];
for (let i = 0; i < entries; i++) {
try {
const [content, nextBuffer] = getContent(i, buffer as Buffer);
buffer = nextBuffer as Buffer;
contents.push(content);
} catch (e) {
console.log(e);
}
}
return contents;
};
const getInt = (bits: boolean[]) => {
let strBits = '';
// eslint-disable-next-line guard-for-in
for (const i in bits) {
strBits += bits[i] ? 1 : 0;
}
return parseInt(strBits, 2);
};
const getContent = (item: number, buffer: Buffer) => {
// FIRST byte contains the type and some of the size of the file
// a MORE flag -8th byte tells us if there is a subsequent byte
// which holds the file size
const byte = buffer.readUIntBE(0, 1);
const m = new BitMask(byte);
let more = m.getBit(3);
let size = [m.getBit(7), m.getBit(8), m.getBit(9), m.getBit(10)];
const type = getInt([m.getBit(4), m.getBit(5), m.getBit(6)]);
// Object IDs if this is a deltatfied blob
let objectRef: string | null = null;
// If we have a more flag get the next
// 8 bytes
while (more) {
buffer = buffer.slice(1);
const nextByte = buffer.readUIntBE(0, 1);
const nextM = new BitMask(nextByte);
const nextSize = [
nextM.getBit(4),
nextM.getBit(5),
nextM.getBit(6),
nextM.getBit(7),
nextM.getBit(8),
nextM.getBit(9),
nextM.getBit(10),
];
size = nextSize.concat(size);
more = nextM.getBit(3);
}
// NOTE Size is the unziped size, not the zipped size
const intSize = getInt(size);
// Deltafied objectives have a 20 byte identifer
if (type == 7 || type == 6) {
objectRef = buffer.slice(0, 20).toString('hex');
buffer = buffer.slice(20);
}
const contentBuffer = buffer.slice(1);
const [content, deflatedSize] = unpack(contentBuffer);
// NOTE Size is the unziped size, not the zipped size
// so it's kind of useless for us in terms of reading the stream
const result = {
item: item,
value: byte,
type: type,
size: intSize,
deflatedSize: deflatedSize,
objectRef: objectRef,
content: content,
};
// Move on by the zipped content size.
const nextBuffer = contentBuffer.slice(deflatedSize as number);
return [result, nextBuffer];
};
const unpack = (buf: Buffer) => {
// Unzip the content
const inflated = zlib.inflateSync(buf);
// We don't have IDX files here, so we need to know how
// big the zipped content was, to set the next read location
const deflated = zlib.deflateSync(inflated);
return [inflated.toString('utf8'), deflated.length];
};
exec.displayName = 'parsePush.exec';
export { exec, getPackMeta, unpack };