Skip to content

Commit 87abc97

Browse files
committed
fix: add GPG parsing and improve header checks
1 parent 1a76fd5 commit 87abc97

File tree

1 file changed

+42
-72
lines changed

1 file changed

+42
-72
lines changed

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

Lines changed: 42 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -158,92 +158,62 @@ const getCommitData = (contents: CommitContent[]) => {
158158
console.log({ x });
159159

160160
const allLines = x.content.split('\n');
161-
const trimmedLines = allLines.map((line) => line.trim());
162-
console.log({ trimmedLines });
163-
164-
const parts = trimmedLines.filter((part) => part.length > 0);
165-
console.log({ parts });
166-
167-
if (!parts || parts.length < 4) {
168-
throw new Error(
169-
`Invalid commit structure: Expected at least 4 non-empty lines (tree, author, committer, message), found ${parts.length}`
170-
);
161+
let headerEndIndex = -1;
162+
let inGpgSig = false;
163+
164+
// Find index that separates headers/message since GPG sig. may contain '\n\n'
165+
for (let i = 0; i < allLines.length; i++) {
166+
const line = allLines[i];
167+
168+
if (line.includes('gpgsig ') && line.includes('-----BEGIN PGP SIGNATURE-----')) {
169+
inGpgSig = true;
170+
continue;
171+
}
172+
if (inGpgSig) {
173+
if (line.includes('-----END PGP SIGNATURE-----')) {
174+
inGpgSig = false;
175+
}
176+
continue;
177+
}
178+
179+
// The first blank line NOT inside a GPG block is the real separator
180+
if (line === '') {
181+
headerEndIndex = i;
182+
break;
183+
}
171184
}
172185

173-
const indexOfMessages = trimmedLines.indexOf('');
174-
175-
if (indexOfMessages === -1) {
176-
throw new Error('Invalid commit data: Missing message separator');
186+
// Commit has no message body or may be malformed
187+
if (headerEndIndex === -1) {
188+
// Treat as commit with no message body, header format is checked later
189+
headerEndIndex = allLines.length;
177190
}
178191

179-
const tree = parts
180-
.find((t) => t.split(' ')[0] === 'tree')
181-
?.replace('tree', '')
182-
.trim();
183-
console.log({ tree });
184-
185-
const parentValue = parts.find((t) => t.split(' ')[0] === 'parent');
186-
console.log({ parentValue });
187-
188-
const parent = parentValue
189-
? parentValue.replace('parent', '').trim()
190-
: '0000000000000000000000000000000000000000';
191-
console.log({ parent });
192-
193-
const author = parts
194-
.find((t) => t.split(' ')[0] === 'author')
195-
?.replace('author', '')
196-
.trim();
197-
console.log({ author });
198-
199-
const committer = parts
200-
.find((t) => t.split(' ')[0] === 'committer')
201-
?.replace('committer', '')
202-
.trim();
203-
console.log({ committer });
204-
205-
const message = trimmedLines
206-
.slice(indexOfMessages + 1)
207-
.join('\n')
208-
.trim();
209-
console.log({ message });
210-
211-
const commitTimestamp = committer?.split(' ').reverse()[1];
212-
console.log({ commitTimestamp });
213-
214-
const authorEmail = author?.split(' ').reverse()[2].slice(1, -1);
215-
console.log({ authorEmail });
216-
217-
console.log({
218-
tree,
219-
parent,
220-
author: author?.split('<')[0].trim(),
221-
committer: committer?.split('<')[0].trim(),
222-
commitTimestamp,
223-
message,
224-
authorEmail,
225-
});
192+
const headerLines = allLines.slice(0, headerEndIndex);
193+
const message = allLines.slice(headerEndIndex + 1).join('\n').trim();
194+
console.log({ headerLines, message });
195+
196+
const { tree, parents, authorInfo, committerInfo } = getParsedData(headerLines);
197+
// No parent headers -> zero hash
198+
const parent = parents.length > 0 ? parents[0] : '0000000000000000000000000000000000000000';
226199

227-
if (!tree || !parent || !author || !committer || !commitTimestamp || !message || !authorEmail) {
200+
// Validation for required attributes
201+
if (!tree || !authorInfo || !committerInfo) {
228202
const missing = [];
229203
if (!tree) missing.push('tree');
230-
if (!parent) missing.push('parent');
231-
if (!author) missing.push('author');
232-
if (!committer) missing.push('committer');
233-
if (!commitTimestamp) missing.push('commitTimestamp');
234-
if (!message) missing.push('message');
235-
if (!authorEmail) missing.push('authorEmail');
204+
if (!authorInfo) missing.push('author');
205+
if (!committerInfo) missing.push('committer');
236206
throw new Error(`Invalid commit data: Missing ${missing.join(', ')}`);
237207
}
238208

239209
return {
240210
tree,
241211
parent,
242-
author: author.split('<')[0].trim(),
243-
committer: committer.split('<')[0].trim(),
244-
commitTimestamp,
212+
author: authorInfo.name,
213+
committer: committerInfo.name,
214+
authorEmail: authorInfo.email,
215+
commitTimestamp: committerInfo.timestamp,
245216
message,
246-
authorEmail: authorEmail,
247217
};
248218
})
249219
.value();

0 commit comments

Comments
 (0)