Skip to content

Commit cb06ce7

Browse files
committed
Add comprehensive tests for readGithubUsageReport to handle new 14-column format and edge cases
- Updated existing tests to check for a minimum number of parsed lines instead of a fixed count. - Introduced new tests for the 14-column format, including parsing, handling of undefined workflow names, and validation of various edge cases. - Added tests for scientific notation, invalid lines, empty values, zero and negative amounts, large numbers, different date formats, special characters, and mixed column formats. - Ensured robustness against whitespace in values and calculated date ranges correctly for single-day reports.
1 parent d42a731 commit cb06ce7

8 files changed

Lines changed: 10972 additions & 803828 deletions

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "github-usage-report",
3-
"version": "3.1.0",
3+
"version": "4.0.0",
44
"description": "Parse github usage report",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/node.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,15 @@ export const readGithubUsageReportFile = async (fileName: string, newLine?: (lin
2424
return;
2525
}
2626
if (line.length < 1) return;
27-
const data = readGithubUsageReportLine(line);
28-
usageReportLines.push(data);
29-
if (newLine) {
30-
newLine(data);
27+
try {
28+
const data = readGithubUsageReportLine(line);
29+
usageReportLines.push(data);
30+
if (newLine) {
31+
newLine(data);
32+
}
33+
} catch (err) {
34+
rl.close();
35+
reject(err);
3136
}
3237
});
3338

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ interface UsageReportLine {
1818
username: string;
1919
organization: string;
2020
repositoryName: string;
21-
workflowName: string;
21+
workflowName?: string;
2222
workflowPath: string;
2323
costCenterName: string;
2424
}

src/usage-report.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ import { UsageReport, UsageReportLine, ModelUsageReport, ModelUsageReportLine }
22

33
const readGithubUsageReportLine = (line: string): UsageReportLine => {
44
const csv = line.split(',').map(field => field.replace(/^"|"$/g, ''));
5-
if (csv.length < 15) {
6-
throw new Error(`Invalid line format: "${line}"`);
5+
if (csv.length < 14 || csv.length > 15) {
6+
throw new Error(`Invalid line format: expected 14 or 15 columns, got ${csv.length}`);
77
}
8+
9+
// Support both 14-column (new) and 15-column (legacy) formats
10+
const is14Column = csv.length === 14;
11+
812
return {
913
date: new Date(Date.parse(csv[0])),
1014
product: csv[1],
@@ -18,9 +22,9 @@ const readGithubUsageReportLine = (line: string): UsageReportLine => {
1822
username: csv[9],
1923
organization: csv[10],
2024
repositoryName: csv[11],
21-
workflowName: csv[12],
22-
workflowPath: csv[13],
23-
costCenterName: csv[14],
25+
workflowName: is14Column ? undefined : csv[12],
26+
workflowPath: is14Column ? csv[12] : csv[13],
27+
costCenterName: is14Column ? csv[13] : csv[14],
2428
};
2529
}
2630

@@ -31,8 +35,13 @@ const readGithubUsageReport = async (data: string): Promise<UsageReport> => {
3135
const lines = data.split(/\r?\n/);
3236
lines.forEach((line, index) => {
3337
if (index == 0 || line.length < 1) return;
34-
const data = readGithubUsageReportLine(line);
35-
usageReportLines.push(data);
38+
try {
39+
const data = readGithubUsageReportLine(line);
40+
usageReportLines.push(data);
41+
} catch (err) {
42+
// Skip lines that don't match expected format (e.g., rows with commas in quoted values)
43+
console.warn(`Skipping line ${index + 1}: ${err instanceof Error ? err.message : 'Unknown error'}`);
44+
}
3645
});
3746

3847
const startDate = usageReportLines[0].date;

0 commit comments

Comments
 (0)