Skip to content

Commit 1a76fd5

Browse files
committed
chore: add parser helpers for getCommitData
1 parent b9657b1 commit 1a76fd5

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,60 @@ async function exec(req: any, action: Action): Promise<Action> {
9090
return action;
9191
};
9292

93+
/**
94+
* Parses the name, email, and timestamp from an author or committer line.
95+
*
96+
* Timestamp including timezone offset is required.
97+
* @param {string} line - The line to parse.
98+
* @return {Object} An object containing the name, email, and timestamp.
99+
*/
100+
const parsePersonLine = (line: string): { name: string; email: string; timestamp: string } | null => {
101+
const personRegex = /^(.*?) <(.*?)> (\d+) ([+-]\d+)$/;
102+
const match = line.match(personRegex);
103+
if (!match) {
104+
throw new Error(`Failed to parse person line: ${line}. Make sure to include a name, email, timestamp and timezone offset.`);
105+
}
106+
return { name: match[1].trim(), email: match[2], timestamp: match[3] };
107+
};
108+
109+
/**
110+
* Parses the header lines of a commit.
111+
* @param {string[]} headerLines - The header lines of a commit.
112+
* @return {Object} An object containing the parsed data.
113+
*/
114+
const getParsedData = (headerLines: string[]) => {
115+
const parsedData: {
116+
tree?: string;
117+
parents: string[];
118+
authorInfo?: ReturnType<typeof parsePersonLine>;
119+
committerInfo?: ReturnType<typeof parsePersonLine>;
120+
} = { parents: [] };
121+
122+
for (const line of headerLines) {
123+
const spaceIndex = line.indexOf(' ');
124+
if (spaceIndex === -1) continue;
125+
126+
const key = line.substring(0, spaceIndex);
127+
const value = line.substring(spaceIndex + 1);
128+
129+
switch (key) {
130+
case 'tree':
131+
parsedData.tree = value.trim();
132+
break;
133+
case 'parent':
134+
parsedData.parents.push(value.trim());
135+
break;
136+
case 'author':
137+
parsedData.authorInfo = parsePersonLine(value);
138+
break;
139+
case 'committer':
140+
parsedData.committerInfo = parsePersonLine(value);
141+
break;
142+
}
143+
}
144+
return parsedData;
145+
};
146+
93147
/**
94148
* Parses the commit data from the contents of a pack file.
95149
* @param {CommitContent[]} contents - The contents of the pack file.

0 commit comments

Comments
 (0)