ci-bill 0.1.1

Rank GitHub Actions cost per workflow from the billing usage CSV and point to likely causes
Documentation
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
use std::process::Command;

const HEADER: &str =
    "date,product,sku,quantity,unit_type,applied_cost_per_quantity,gross_amount,discount_amount,net_amount,username,organization,repository,workflow_path";

fn run(args: &[&str]) -> std::process::Output {
    Command::new(env!("CARGO_BIN_EXE_ci-bill"))
        .current_dir(env!("CARGO_MANIFEST_DIR"))
        .args(args)
        .output()
        .expect("run ci-bill")
}

fn stdout(output: &std::process::Output) -> String {
    String::from_utf8(output.stdout.clone()).expect("utf-8 stdout")
}

fn stderr(output: &std::process::Output) -> String {
    String::from_utf8(output.stderr.clone()).expect("utf-8 stderr")
}

fn write_usage(name: &str, body: &str) -> String {
    let path = std::path::PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join(name);
    std::fs::write(&path, body).unwrap();
    path.into_os_string().into_string().unwrap()
}

fn parse_json(output: &std::process::Output) -> serde_json::Value {
    let text = stdout(output);
    serde_json::from_str(text.trim_end()).unwrap_or_else(|err| panic!("{err}\n{text}"))
}

fn assert_exit(output: &std::process::Output, code: i32) {
    assert_eq!(
        output.status.code(),
        Some(code),
        "stdout:\n{}\nstderr:\n{}",
        stdout(output),
        stderr(output)
    );
}

fn money_micros(text: &str) -> i64 {
    let negative = text.starts_with('-');
    let text = text.trim_start_matches('-');
    let (whole, frac) = text.split_once('.').unwrap_or_else(|| panic!("{text}"));
    assert_eq!(frac.len(), 6, "{text}");
    let value = whole.parse::<i64>().unwrap() * 1_000_000 + frac.parse::<i64>().unwrap();
    if negative {
        -value
    } else {
        value
    }
}

/// Two repositories, one shared workflow path, mixed case, CRLF, and a shuffled header.
fn two_repo_csv(line_end: &str) -> String {
    let shuffled = [
        "Repository",
        "WORKFLOW_PATH",
        "Net_Amount",
        "Gross_Amount",
        "Discount_Amount",
        "Applied_Cost_Per_Quantity",
        "Quantity",
        "UNIT_TYPE",
        "Sku",
        "Product",
        "DATE",
        "Username",
        "Organization",
    ]
    .join(",");
    let mut body = String::new();
    body.push_str(&shuffled);
    body.push_str(line_end);
    // repository, path, net, gross, discount, price, quantity, unit, sku, product, date, user, org
    for (repo, product, date, net) in [
        ("example-org/zeta", "actions", "2026-09-30", "4"),
        ("example-org/alpha", "ACTIONS", "2026-09-01", "4"),
    ] {
        body.push_str(&format!(
            "{repo},.github/workflows/ci.yml,{net},{net},0,0.4,10,minutes,actions_linux,{product},{date},ada,example-org{line_end}"
        ));
    }
    body
}

#[test]
fn sample_text_shows_period_and_repository() {
    let output = run(&["report", "--usage", "samples/usage-sample.csv"]);
    assert_exit(&output, 0);
    let text = stdout(&output);
    assert!(
        text.contains("Period: 2026-09-01 .. 2026-09-30\n"),
        "{text}"
    );
    assert!(
        text.contains("Repositories: example-org/sample-app\n"),
        "{text}"
    );
    assert!(text.contains("Total net: 284.21\n"), "{text}");
    assert!(text.contains("Other products: 0.00\n"), "{text}");
    assert!(!text.contains("0000000001"), "{text}");
    assert!(!text.contains("9999999"), "{text}");
}

#[test]
fn sample_json_money_is_exact_decimal_text() {
    let output = run(&["report", "--usage", "samples/usage-sample.csv", "--json"]);
    assert_exit(&output, 0);
    let text = stdout(&output);
    assert!(!text.contains("0000000001"), "{text}");
    assert!(!text.contains("9999999"), "{text}");
    let value = parse_json(&output);
    assert_eq!(value["schema_version"], 2);
    assert_eq!(value["money_unit"], "USD");
    assert_eq!(value["total_net"], "284.208000");
    assert_eq!(value["other_products_net"], "0.000000");
    assert_eq!(value["period"]["from"], "2026-09-01");
    assert_eq!(value["period"]["to"], "2026-09-30");
    let ci = value["workflows"]
        .as_array()
        .unwrap()
        .iter()
        .find(|item| item["path"] == ".github/workflows/ci.yml")
        .unwrap();
    assert_eq!(ci["net"], "44.232000");
    assert_eq!(ci["repository"], "example-org/sample-app");
    assert!(ci["net"].is_string());
    assert!(value["total_minutes"].is_number());
}

#[test]
fn two_repositories_with_the_same_workflow_stay_two_rows() {
    for (name, line_end) in [("two-repos-lf.csv", "\n"), ("two-repos-crlf.csv", "\r\n")] {
        let path = write_usage(name, &two_repo_csv(line_end));
        let text_out = run(&["report", "--usage", &path]);
        assert_exit(&text_out, 0);
        let text = stdout(&text_out);
        assert_eq!(
            text.matches(".github/workflows/ci.yml").count(),
            2,
            "{text}"
        );
        assert!(
            text.contains("example-org/alpha .github/workflows/ci.yml"),
            "{text}"
        );
        assert!(
            text.contains("example-org/zeta .github/workflows/ci.yml"),
            "{text}"
        );
        assert!(
            text.contains("Period: 2026-09-01 .. 2026-09-30\n"),
            "{text}"
        );
        assert!(
            text.contains("Repositories: example-org/alpha, example-org/zeta\n"),
            "{text}"
        );
        let alpha = text.find("example-org/alpha").unwrap();
        let zeta = text.find("example-org/zeta").unwrap();
        assert!(alpha < zeta, "equal net sorts by repository\n{text}");

        let json_out = run(&["report", "--usage", &path, "--json"]);
        assert_exit(&json_out, 0);
        let raw = stdout(&json_out);
        assert!(!raw.contains("0000000001"), "{raw}");
        assert!(!raw.contains("9999999"), "{raw}");
        let value = parse_json(&json_out);
        let workflows = value["workflows"].as_array().unwrap();
        assert_eq!(workflows.len(), 2);
        assert_eq!(workflows[0]["repository"], "example-org/alpha");
        assert_eq!(workflows[1]["repository"], "example-org/zeta");
        assert!(workflows
            .iter()
            .all(|item| item["path"] == ".github/workflows/ci.yml"));
        assert_eq!(workflows[0]["net"], "4.000000");
        assert_eq!(workflows[1]["net"], "4.000000");
        assert_eq!(value["total_net"], "8.000000");
        assert_eq!(
            money_micros(value["total_net"].as_str().unwrap()),
            8_000_000
        );
    }
}

#[test]
fn copilot_rows_stay_out_of_the_actions_total_and_budget() {
    for product in ["copilot", "Copilot", "COPILOT"] {
        let body = format!(
            "{HEADER}\n\
2026-09-01,actions,actions_linux,10,minutes,0.4,4,0,4,ada,example-org,example-org/app,.github/workflows/ci.yml\n\
2026-09-15,{product},copilot_premium_request,2,requests,1.5,3,0,3,ada,example-org,example-org/app,.github/workflows/ci.yml\n\
2026-09-20,Actions,actions_macos,1,MINUTES,1,1,0,1,ada,example-org,example-org/other,.github/workflows/ci.yml\n"
        );
        let path = write_usage(&format!("copilot-{product}.csv"), &body);
        let text_out = run(&["report", "--usage", &path]);
        assert_exit(&text_out, 0);
        let text = stdout(&text_out);
        assert!(text.contains("Total net: 5.00\n"), "{product}\n{text}");
        assert!(text.contains("Other products: 3.00\n"), "{product}\n{text}");
        assert!(text.contains("Total minutes: 11\n"), "{product}\n{text}");
        assert!(
            !text.contains("copilot_premium_request"),
            "{product}\n{text}"
        );
        assert_eq!(
            text.matches(".github/workflows/ci.yml").count(),
            2,
            "{product}\n{text}"
        );

        let inside = run(&["report", "--usage", &path, "--budget", "5"]);
        assert_exit(&inside, 0);
        assert!(!stdout(&inside).contains("Budget exceeded"), "{product}");

        let above_actions_only = run(&["report", "--usage", &path, "--budget", "7"]);
        assert_exit(&above_actions_only, 0);
        assert!(
            !stdout(&above_actions_only).contains("Budget exceeded"),
            "{product}"
        );

        let over = run(&["report", "--usage", &path, "--budget", "4", "--json"]);
        assert_exit(&over, 1);
        assert_eq!(stderr(&over), "Budget exceeded: 5.00 > 4.00\n");
        let value = parse_json(&over);
        assert_eq!(value["budget_exceeded"], true);
        assert_eq!(value["total_net"], "5.000000");
        assert_eq!(value["other_products_net"], "3.000000");
        assert_eq!(value["budget"], "4.000000");
        let actions: i64 = value["workflows"]
            .as_array()
            .unwrap()
            .iter()
            .map(|item| money_micros(item["net"].as_str().unwrap()))
            .sum();
        assert_eq!(actions, money_micros(value["total_net"].as_str().unwrap()));
        assert_ne!(
            actions + money_micros(value["other_products_net"].as_str().unwrap()),
            actions
        );
    }
}

#[test]
fn seven_decimal_places_exit_2_without_the_cell() {
    let columns = [
        ("quantity", 3, "1.2345678"),
        ("applied_cost_per_quantity", 5, "0.0000001"),
        ("gross_amount", 6, "1e-7"),
        ("discount_amount", 7, "-0.0000001"),
        ("net_amount", 8, "12345678e-7"),
    ];
    for (column, index, token) in columns {
        for line_end in ["\n", "\r\n"] {
            let good = [
                "2026-09-01",
                "actions",
                "actions_linux",
                "1",
                "minutes",
                "0.000001",
                "1.234567",
                "0",
                "1.234567",
                "ada",
                "example-org",
                "example-org/app",
                "a.yml",
            ];
            let mut bad = good;
            bad[index] = token;
            let name = format!(
                "seven-{}-{}.csv",
                column,
                if line_end == "\n" { "lf" } else { "crlf" }
            );
            let body = format!(
                "{HEADER}{line_end}{}{line_end}{}{line_end}",
                good.join(","),
                bad.join(",")
            );
            let path = write_usage(&name, &body);
            let output = run(&["report", "--usage", &path]);
            assert_exit(&output, 2);
            let err = stderr(&output);
            assert!(
                err.contains(&format!("line 3: {column} has more than 6 decimal places")),
                "{column} {line_end:?}\n{err}"
            );
            assert!(!err.contains(token), "{column}\n{err}");
            assert!(!err.contains("1.2345678"), "{column}\n{err}");
            assert!(stdout(&output).is_empty(), "{column}\n{}", stdout(&output));
        }
    }
}

#[test]
fn non_numeric_money_exits_2_without_the_cell() {
    for (column, index, token) in [
        ("quantity", 3, "NOT_A_NUMBER"),
        ("applied_cost_per_quantity", 5, "NaN"),
        ("gross_amount", 6, "inf"),
        ("discount_amount", 7, "Infinity"),
        ("net_amount", 8, "12abc34"),
    ] {
        let mut fields = [
            "2026-09-01",
            "actions",
            "actions_linux",
            "1",
            "minutes",
            "0.006",
            "1",
            "0",
            "1",
            "ada",
            "example-org",
            "example-org/app",
            "a.yml",
        ];
        fields[index] = token;
        let path = write_usage(
            &format!("nan-{column}.csv"),
            &format!("{HEADER}\n{}\n", fields.join(",")),
        );
        let output = run(&["report", "--usage", &path]);
        assert_exit(&output, 2);
        let err = stderr(&output);
        assert!(
            err.contains(&format!("line 2: {column} is not a number")),
            "{column}\n{err}"
        );
        assert!(!err.contains(token), "{column}\n{err}");
        assert!(stdout(&output).is_empty(), "{}", stdout(&output));
    }
}

#[test]
fn missing_and_empty_fields() {
    for column in [
        "date",
        "product",
        "sku",
        "quantity",
        "unit_type",
        "applied_cost_per_quantity",
        "gross_amount",
        "discount_amount",
        "net_amount",
        "username",
        "organization",
        "repository",
        "workflow_path",
    ] {
        let headers = HEADER
            .split(',')
            .filter(|name| *name != column)
            .collect::<Vec<_>>()
            .join(",");
        let path = write_usage(
            &format!("missing-{column}.csv"),
            &format!("{headers}\nsecret-cell\n"),
        );
        let output = run(&["report", "--usage", &path]);
        assert_exit(&output, 2);
        let err = stderr(&output);
        assert!(
            err.contains(&format!("line 1: missing column {column}")),
            "{column}\n{err}"
        );
        assert!(!err.contains("secret-cell"), "{column}\n{err}");
    }

    let empty_numbers = format!(
        "{HEADER}\n\
2026-09-01,actions,actions_linux,,minutes,0.006,1,0,1,ada,example-org,example-org/app,a.yml\n"
    );
    let path = write_usage("empty-quantity.csv", &empty_numbers);
    let output = run(&["report", "--usage", &path]);
    assert_exit(&output, 2);
    assert!(stderr(&output).contains("line 2: quantity is not a number"));

    let blanks = format!(
        "{HEADER}\r\n\
,copilot,copilot_premium_request,1,requests,1,2,0,2,,, ,\r\n\
2026-09-08,ACTIONS,actions_linux,3,MINUTES,0.006,1,0,1, ,Example-Org,example-org/other, .github/workflows/ci.yml\r\n"
    );
    let path = write_usage("empty-text-crlf.csv", &blanks);
    let output = run(&["report", "--usage", &path, "--json"]);
    assert_exit(&output, 0);
    let value = parse_json(&output);
    assert_eq!(value["total_net"], "1.000000");
    assert_eq!(value["other_products_net"], "2.000000");
    assert_eq!(value["period"]["from"], "2026-09-08");
    assert_eq!(value["period"]["to"], "2026-09-08");
    assert_eq!(
        value["repositories"],
        serde_json::json!(["example-org/other"])
    );
    let workflows = value["workflows"].as_array().unwrap();
    assert_eq!(workflows.len(), 1);
    assert_eq!(workflows[0]["repository"], "example-org/other");
    assert_eq!(workflows[0]["path"], ".github/workflows/ci.yml");
    assert_eq!(workflows[0]["minutes"].as_i64(), Some(3));
    let text = run(&["report", "--usage", &path]);
    assert!(stdout(&text).contains("Other products: 2.00\n"));
    assert!(stdout(&text).contains("example-org/other .github/workflows/ci.yml"));
}