ci-bill 0.1.0

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
//! `--fix-diff` prints a unified diff for the highest-savings finding. These
//! tests copy `samples/`, tweak it so a chosen finding kind is the one with
//! the highest savings, and check that the printed diff applies cleanly with
//! `git apply --check`.

use std::fs;
use std::path::{Path, PathBuf};
use std::process::{Command, Output};

fn scratch(name: &str) -> PathBuf {
    let base = std::env::var_os("CARGO_TARGET_TMPDIR")
        .map(PathBuf::from)
        .unwrap_or_else(std::env::temp_dir);
    let path = base.join("ci-bill-fix-diff-cli").join(name);
    let _ = fs::remove_dir_all(&path);
    fs::create_dir_all(&path).expect("scratch dir");
    path
}

/// Copy `samples/usage-sample.csv` and `samples/.github/workflows/*` into `dir`.
fn copy_samples(dir: &Path) {
    let manifest = Path::new(env!("CARGO_MANIFEST_DIR"));
    fs::copy(
        manifest.join("samples/usage-sample.csv"),
        dir.join("usage-sample.csv"),
    )
    .expect("copy usage csv");
    let workflows = dir.join(".github/workflows");
    fs::create_dir_all(&workflows).expect("workflows dir");
    for entry in fs::read_dir(manifest.join("samples/.github/workflows")).expect("read samples") {
        let entry = entry.expect("dir entry");
        let path = entry.path();
        if path.is_file() {
            fs::copy(&path, workflows.join(path.file_name().unwrap())).expect("copy workflow");
        }
    }
}

fn replace_in_file(path: &Path, from: &str, to: &str) {
    let text = fs::read_to_string(path).expect("read workflow");
    assert!(text.contains(from), "{path:?} has no {from:?}:\n{text}");
    fs::write(path, text.replace(from, to)).expect("write workflow");
}

fn git(dir: &Path, args: &[&str]) -> Output {
    Command::new("git")
        .current_dir(dir)
        .args(args)
        .output()
        .expect("run git")
}

fn git_init_and_commit(dir: &Path) {
    let status = |output: Output| {
        assert!(
            output.status.success(),
            "git failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );
    };
    status(git(dir, &["init", "-q"]));
    status(git(dir, &["add", "-A"]));
    status(git(
        dir,
        &[
            "-c",
            "user.name=test",
            "-c",
            "user.email=test@example.com",
            "commit",
            "-q",
            "-m",
            "init",
        ],
    ));
}

fn run_fix_diff(dir: &Path) -> String {
    let output = Command::new(env!("CARGO_BIN_EXE_ci-bill"))
        .current_dir(dir)
        .args([
            "report",
            "--usage",
            "usage-sample.csv",
            "--workflows",
            ".github/workflows",
            "--fix-diff",
        ])
        .output()
        .expect("run ci-bill");
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        output.status.success(),
        "stderr:\n{stderr}\nstdout:\n{}",
        String::from_utf8_lossy(&output.stdout)
    );
    assert!(stderr.is_empty(), "{stderr}");
    String::from_utf8(output.stdout).expect("utf-8 stdout")
}

/// Apply `diff` to the working tree at `dir` with `git apply --check`, then
/// for real, and return the resulting file content so the caller can check
/// the fix actually lands as intended.
fn apply_diff(dir: &Path, diff: &str) -> String {
    let diff_path = dir.join("fix.diff");
    fs::write(&diff_path, diff).expect("write diff");
    let check = git(dir, &["apply", "--check", "fix.diff"]);
    assert!(
        check.status.success(),
        "git apply --check failed: {}\ndiff:\n{diff}",
        String::from_utf8_lossy(&check.stderr)
    );
    let apply = git(dir, &["apply", "fix.diff"]);
    assert!(
        apply.status.success(),
        "git apply failed: {}\ndiff:\n{diff}",
        String::from_utf8_lossy(&apply.stderr)
    );
    let touched = diff
        .lines()
        .find_map(|line| line.strip_prefix("--- a/"))
        .expect("diff names a file");
    fs::read_to_string(dir.join(touched)).expect("read patched file")
}

#[test]
fn fix_diff_moves_lint_off_macos() {
    let dir = scratch("f1");
    copy_samples(&dir);
    git_init_and_commit(&dir);

    let diff = run_fix_diff(&dir);
    assert!(
        diff.starts_with("--- a/.github/workflows/lint.yml\n"),
        "{diff}"
    );
    assert!(diff.contains("-    runs-on: macos-latest\n"), "{diff}");
    assert!(diff.contains("+    runs-on: ubuntu-latest\n"), "{diff}");

    let patched = apply_diff(&dir, &diff);
    assert!(patched.contains("runs-on: ubuntu-latest"), "{patched}");
    assert!(!patched.contains("macos-latest"), "{patched}");
}

#[test]
fn fix_diff_adds_timeout_minutes() {
    let dir = scratch("f2");
    copy_samples(&dir);
    // Take F1 off the table so a missing-timeout finding is the highest-savings one.
    replace_in_file(
        &dir.join(".github/workflows/lint.yml"),
        "runs-on: macos-latest",
        "runs-on: ubuntu-latest",
    );
    git_init_and_commit(&dir);

    let diff = run_fix_diff(&dir);
    assert!(diff.contains("+    timeout-minutes: 30\n"), "{diff}");
    assert!(
        !diff
            .lines()
            .any(|line| line.starts_with('-') && !line.starts_with("---")),
        "a timeout fix only adds a line: {diff}"
    );

    let patched = apply_diff(&dir, &diff);
    assert!(patched.contains("timeout-minutes: 30"), "{patched}");
}

/// Write a single custom workflow file plus a usage row that covers it, for
/// flow-style YAML shapes the `samples/` fixtures do not exercise.
fn write_flow_workflow(dir: &Path, yaml: &str) -> PathBuf {
    let workflows = dir.join(".github/workflows");
    fs::create_dir_all(&workflows).expect("workflows dir");
    let path = workflows.join("lint.yml");
    fs::write(&path, yaml).expect("write workflow");
    fs::write(
        dir.join("usage-sample.csv"),
        "date,product,sku,quantity,unit_type,applied_cost_per_quantity,gross_amount,discount_amount,net_amount,username,organization,repository,workflow_path\n\
2026-09-01,actions,actions_macos,100,minutes,0.062,6.2,0,6.2,ada,example-org,example-org/app,.github/workflows/lint.yml\n",
    )
    .expect("write usage");
    path
}

/// Re-run `ci-bill report` on the patched tree: it must still exit 0 (the
/// YAML is still valid) and the named finding text must be gone.
fn assert_fixed(dir: &Path, gone_finding: &str) {
    let output = Command::new(env!("CARGO_BIN_EXE_ci-bill"))
        .current_dir(dir)
        .args([
            "report",
            "--usage",
            "usage-sample.csv",
            "--workflows",
            ".github/workflows",
        ])
        .output()
        .expect("run ci-bill");
    assert!(
        output.status.success(),
        "patched YAML did not parse, stderr:\n{}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8(output.stdout).expect("utf-8 stdout");
    assert!(
        !stdout.contains(gone_finding),
        "{gone_finding:?} still present:\n{stdout}"
    );
}

#[test]
fn fix_diff_pure_flow_lint_on_runner_edits_only_the_value() {
    let dir = scratch("flow-pure");
    write_flow_workflow(
        &dir,
        "jobs: { lint: { runs-on: macos-latest, timeout-minutes: 30, steps: [ { run: \"echo lint\" } ] } }\n",
    );
    git_init_and_commit(&dir);

    let diff = run_fix_diff(&dir);
    assert!(
        diff.contains("-jobs: { lint: { runs-on: macos-latest, timeout-minutes: 30, steps: [ { run: \"echo lint\" } ] } }\n"),
        "{diff}"
    );
    assert!(
        diff.contains("+jobs: { lint: { runs-on: ubuntu-latest, timeout-minutes: 30, steps: [ { run: \"echo lint\" } ] } }\n"),
        "{diff}"
    );

    let patched = apply_diff(&dir, &diff);
    assert!(patched.contains("runs-on: ubuntu-latest"), "{patched}");
    assert!(!patched.contains("macos-latest"), "{patched}");
    assert!(
        patched.contains("steps: [ { run: \"echo lint\" } ] } }"),
        "{patched}"
    );
    assert_fixed(&dir, "lint on macOS");
}

#[test]
fn fix_diff_pure_flow_missing_timeout_inserts_a_flow_entry() {
    let dir = scratch("flow-pure-timeout");
    write_flow_workflow(
        &dir,
        "jobs: { lint: { runs-on: ubuntu-latest, steps: [ { run: \"echo lint\" } ] } }\n",
    );
    git_init_and_commit(&dir);

    let diff = run_fix_diff(&dir);
    assert!(
        diff.contains("+jobs: { lint: { timeout-minutes: 30, runs-on: ubuntu-latest, steps: [ { run: \"echo lint\" } ] } }\n"),
        "{diff}"
    );

    let patched = apply_diff(&dir, &diff);
    assert!(patched.contains("timeout-minutes: 30"), "{patched}");
    assert!(
        patched.contains("steps: [ { run: \"echo lint\" } ] } }"),
        "{patched}"
    );
    assert_fixed(&dir, "default timeout");
}

#[test]
fn fix_diff_flow_job_inside_block_jobs_fixes_both_findings() {
    let dir = scratch("flow-job-in-block-jobs");
    write_flow_workflow(
        &dir,
        "jobs:\n  lint: { runs-on: macos-latest, steps: [ { run: \"echo lint\" } ] }\n",
    );
    git_init_and_commit(&dir);

    let first = run_fix_diff(&dir);
    apply_diff(&dir, &first);
    let second = run_fix_diff(&dir);
    let patched = apply_diff(&dir, &second);

    assert!(patched.contains("runs-on: ubuntu-latest"), "{patched}");
    assert!(!patched.contains("macos-latest"), "{patched}");
    assert!(patched.contains("timeout-minutes: 30"), "{patched}");
    assert!(
        patched.contains("steps: [ { run: \"echo lint\" } ]"),
        "{patched}"
    );
    assert_fixed(&dir, "lint on macOS");
    assert_fixed(&dir, "default timeout");
}

#[test]
fn fix_diff_block_job_inside_flow_jobs_fixes_both_findings() {
    let dir = scratch("block-job-in-flow-jobs");
    write_flow_workflow(
        &dir,
        "jobs: { lint: {\n    runs-on: macos-latest,\n    steps: [ { run: \"echo lint\" } ]\n  } }\n",
    );
    git_init_and_commit(&dir);

    let first = run_fix_diff(&dir);
    apply_diff(&dir, &first);
    let second = run_fix_diff(&dir);
    let patched = apply_diff(&dir, &second);

    assert!(patched.contains("runs-on: ubuntu-latest"), "{patched}");
    assert!(!patched.contains("macos-latest"), "{patched}");
    assert!(patched.contains("timeout-minutes: 30"), "{patched}");
    assert!(
        patched.contains("steps: [ { run: \"echo lint\" } ]"),
        "{patched}"
    );
    assert_fixed(&dir, "lint on macOS");
    assert_fixed(&dir, "default timeout");
}

#[test]
fn fix_diff_flow_quoted_key_and_value_edits_only_the_value() {
    let dir = scratch("flow-quoted");
    write_flow_workflow(
        &dir,
        "jobs: { lint: { \"runs-on\": \"macos-latest\", timeout-minutes: 30, steps: [ { run: \"echo lint\" } ] } }\n",
    );
    git_init_and_commit(&dir);

    let diff = run_fix_diff(&dir);
    let patched = apply_diff(&dir, &diff);
    assert!(patched.contains("\"runs-on\": ubuntu-latest"), "{patched}");
    assert!(!patched.contains("macos-latest"), "{patched}");
    assert_fixed(&dir, "lint on macOS");
}

#[test]
fn fix_diff_flow_comment_and_crlf_edit_only_the_value() {
    let dir = scratch("flow-comment-crlf");
    write_flow_workflow(
        &dir,
        "jobs:\r\n  lint:\r\n    runs-on: macos-latest # pick a runner\r\n    steps:\r\n      - run: echo lint\r\n",
    );
    git_init_and_commit(&dir);

    let first = run_fix_diff(&dir);
    apply_diff(&dir, &first);
    let second = run_fix_diff(&dir);
    let patched = apply_diff(&dir, &second);

    assert!(
        patched.contains("runs-on: ubuntu-latest # pick a runner\r\n"),
        "{patched}"
    );
    assert!(!patched.contains("macos-latest"), "{patched}");
    assert_fixed(&dir, "lint on macOS");
    assert_fixed(&dir, "default timeout");
}

#[test]
fn fix_diff_tab_after_colon_edits_only_the_value() {
    let dir = scratch("flow-tab");
    write_flow_workflow(
        &dir,
        "jobs: { lint: { runs-on: \tmacos-latest, timeout-minutes: 30, steps: [ { run: \"echo lint\" } ] } }\n",
    );
    git_init_and_commit(&dir);

    let diff = run_fix_diff(&dir);
    let patched = apply_diff(&dir, &diff);
    assert!(patched.contains("runs-on: \tubuntu-latest"), "{patched:?}");
    assert!(!patched.contains("macos-latest"), "{patched}");
    assert_fixed(&dir, "lint on macOS");
}

#[test]
fn fix_diff_adds_concurrency_cancel_in_progress() {
    let dir = scratch("f3");
    copy_samples(&dir);
    // Take F1 and both F2s off the table so the pull_request-without-cancel
    // finding on lint.yml is the only, and so the highest-savings, finding.
    let lint = dir.join(".github/workflows/lint.yml");
    let docs = dir.join(".github/workflows/docs.yml");
    replace_in_file(&lint, "runs-on: macos-latest", "runs-on: ubuntu-latest");
    replace_in_file(
        &lint,
        "runs-on: ubuntu-latest\n",
        "runs-on: ubuntu-latest\n    timeout-minutes: 20\n",
    );
    replace_in_file(
        &docs,
        "runs-on: ubuntu-latest\n",
        "runs-on: ubuntu-latest\n    timeout-minutes: 20\n",
    );
    git_init_and_commit(&dir);

    let diff = run_fix_diff(&dir);
    assert!(
        diff.starts_with("--- a/.github/workflows/lint.yml\n"),
        "{diff}"
    );
    assert!(diff.contains("+concurrency:\n"), "{diff}");
    assert!(
        diff.contains("+  group: ${{ github.workflow }}-${{ github.ref }}\n"),
        "{diff}"
    );
    assert!(diff.contains("+  cancel-in-progress: true\n"), "{diff}");

    let patched = apply_diff(&dir, &diff);
    assert!(patched.contains("cancel-in-progress: true"), "{patched}");
}