koji-diff 0.8.0

Koji build log differ — compare buildroot and build logs between two Koji builds
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
// SPDX-License-Identifier: MPL-2.0

use std::io::IsTerminal;
use std::process::ExitCode;

use clap::Parser;

use koji_diff::diff::{self, PackageDiff};
use koji_diff::koji::{self, KojiClient, TaskInfo};
use koji_diff::parse::{self, KojiRef, RefType};
use koji_diff::xmlrpc;

#[derive(Parser)]
#[command(
    version,
    about,
    long_about = None,
    before_help = concat!(env!("CARGO_PKG_NAME"), " ", env!("CARGO_PKG_VERSION"))
)]
struct Cli {
    /// First build reference (Koji URL, build:<ID>, or task:<ID>).
    ref1: String,

    /// Second build reference (Koji URL, build:<ID>, or task:<ID>).
    ref2: String,

    /// Koji instance hostname (required for bare IDs).
    #[arg(long, value_name = "HOST")]
    instance: Option<String>,

    /// Architecture to compare.
    #[arg(long, default_value = "x86_64")]
    arch: String,

    /// build.log tail lines on failure.
    #[arg(long = "log-lines", default_value = "50", value_name = "N")]
    build_log_lines: usize,

    /// Output as JSON.
    #[arg(long)]
    json: bool,

    /// Show debug info (log snippets, file listings).
    #[arg(long)]
    debug: bool,
}

#[derive(serde::Serialize)]
struct JsonOutput {
    instance: String,
    arch: String,
    ref1: JsonRef,
    ref2: JsonRef,
    package_diff: PackageDiff,
    #[serde(skip_serializing_if = "Option::is_none")]
    build_failure: Option<JsonBuildFailure>,
}

#[derive(serde::Serialize)]
struct JsonRef {
    input: String,
    ref_type: String,
    id: i64,
    build_arch_task_id: i64,
    state: String,
}

#[derive(serde::Serialize)]
struct JsonBuildFailure {
    task_id: i64,
    logs: Vec<FailureLog>,
}

#[derive(serde::Serialize)]
struct FailureLog {
    name: String,
    tail: String,
}

fn main() -> ExitCode {
    let cli = Cli::parse();

    if let Err(e) = sandogasa_cli::require_tool("koji", "sudo dnf install koji") {
        eprintln!("error: {e}");
        return ExitCode::FAILURE;
    }

    match run(&cli) {
        Ok(()) => ExitCode::SUCCESS,
        Err(e) => {
            eprintln!("Error: {e}");
            ExitCode::FAILURE
        }
    }
}

fn run(cli: &Cli) -> Result<(), Box<dyn std::error::Error>> {
    let ref1 = parse::parse_ref(&cli.ref1, cli.instance.as_deref())?;
    let ref2 = parse::parse_ref(&cli.ref2, cli.instance.as_deref())?;

    if ref1.instance != ref2.instance {
        return Err(format!(
            "both references must be from the same Koji instance\n\
             ref1: {}\n  ref2: {}",
            ref1.instance, ref2.instance
        )
        .into());
    }

    let koji_client = KojiClient::new(&ref1.instance);

    if !cli.json {
        eprintln!("koji-diff: comparing builds on {}", koji_client.instance());
    }

    // Resolve build IDs to task IDs (and BuildInfo when available).
    let (task_id1, build1) = resolve_ref(&koji_client, &ref1)?;
    let (task_id2, build2) = resolve_ref(&koji_client, &ref2)?;

    // Resolve to buildArch tasks.
    let arch_task1 = koji_client.resolve_build_arch_task(task_id1, &cli.arch)?;
    let arch_task2 = koji_client.resolve_build_arch_task(task_id2, &cli.arch)?;

    if !cli.json {
        eprintln!(
            "  ref1: {}",
            format_resolution(&ref1, task_id1, &arch_task1, &cli.arch)
        );
        eprintln!(
            "  ref2: {}",
            format_resolution(&ref2, task_id2, &arch_task2, &cli.arch)
        );
        eprintln!();
    }

    // Download root.log from both builds/tasks.
    // Prefer build-based HTTP download (persists after task GC),
    // fall back to koji download-logs (task-based).
    let tmpdir1 = tempfile::tempdir()?;
    let tmpdir2 = tempfile::tempdir()?;

    let root_log1 = download_root_log(
        &koji_client,
        build1.as_ref(),
        &arch_task1,
        &cli.arch,
        tmpdir1.path(),
        cli.json,
    )?;
    let root_log2 = download_root_log(
        &koji_client,
        build2.as_ref(),
        &arch_task2,
        &cli.arch,
        tmpdir2.path(),
        cli.json,
    )?;

    if cli.debug {
        debug_log_info("ref1", &root_log1, tmpdir1.path());
        debug_log_info("ref2", &root_log2, tmpdir2.path());
    }

    // Parse and diff packages.
    let pkgs1 = diff::parse_installed_packages(&root_log1);
    let pkgs2 = diff::parse_installed_packages(&root_log2);

    if !cli.json {
        eprintln!(
            "Parsed {} packages from ref1, {} from ref2\n",
            pkgs1.len(),
            pkgs2.len()
        );
    }

    let pkg_diff = diff::diff_packages(&pkgs1, &pkgs2);

    // Check for build failure and fetch build.log tail.
    // Reuse tmpdir if the failed task already had its logs downloaded,
    // otherwise create a new one.
    let resolved1 = ResolvedRef {
        arch_task: &arch_task1,
        build: build1.as_ref(),
        tmpdir: tmpdir1.path(),
    };
    let resolved2 = ResolvedRef {
        arch_task: &arch_task2,
        build: build2.as_ref(),
        tmpdir: tmpdir2.path(),
    };
    let build_failure = fetch_build_failure(
        &koji_client,
        &resolved1,
        &resolved2,
        &cli.arch,
        cli.build_log_lines,
        cli.json,
    );

    if cli.json {
        let output = JsonOutput {
            instance: ref1.instance.clone(),
            arch: cli.arch.clone(),
            ref1: JsonRef {
                input: cli.ref1.clone(),
                ref_type: ref1.ref_type.to_string(),
                id: ref1.id,
                build_arch_task_id: arch_task1.id,
                state: arch_task1.state_name().to_string(),
            },
            ref2: JsonRef {
                input: cli.ref2.clone(),
                ref_type: ref2.ref_type.to_string(),
                id: ref2.id,
                build_arch_task_id: arch_task2.id,
                state: arch_task2.state_name().to_string(),
            },
            package_diff: pkg_diff,
            build_failure,
        };
        println!("{}", serde_json::to_string_pretty(&output)?);
    } else {
        let color = std::io::stdout().is_terminal();
        let label1 = format!("task {}", arch_task1.id);
        let label2 = format!("task {}", arch_task2.id);
        diff::print_diff(&pkg_diff, &label1, &label2, color);

        if let Some(failure) = &build_failure {
            for log in &failure.logs {
                println!(
                    "\n--- Build failure in task {} ({} tail) ---",
                    failure.task_id, log.name
                );
                println!("{}", log.tail);
            }
        }
    }

    Ok(())
}

fn tail_log(name: &str, content: &str, lines: usize) -> FailureLog {
    let all_lines: Vec<_> = content.lines().collect();
    let start = all_lines.len().saturating_sub(lines);
    FailureLog {
        name: name.to_string(),
        tail: all_lines[start..].join("\n"),
    }
}

/// Try to read a file by name from a directory, searching recursively.
fn find_and_read(dir: &std::path::Path, name: &str) -> Option<String> {
    let direct = dir.join(name);
    std::fs::read_to_string(&direct).ok().or_else(|| {
        koji_diff::koji::find_file_in(dir, name).and_then(|p| std::fs::read_to_string(p).ok())
    })
}

fn debug_log_info(label: &str, log_content: &str, tmpdir: &std::path::Path) {
    eprintln!("--- DEBUG {label} ---");

    // List files in tmpdir.
    eprintln!("  files in tmpdir:");
    if let Ok(entries) = std::fs::read_dir(tmpdir) {
        for entry in entries.flatten() {
            let meta = entry.metadata().ok();
            let size = meta.map(|m| m.len()).unwrap_or(0);
            eprintln!("    {} ({size} bytes)", entry.file_name().to_string_lossy());
        }
    }

    // Show root.log stats and lines containing "Installed" or "nstall".
    let lines: Vec<_> = log_content.lines().collect();
    eprintln!(
        "  root.log: {} bytes, {} lines",
        log_content.len(),
        lines.len()
    );
    // Show a sample of parsed packages.
    let pkgs = diff::parse_installed_packages(log_content);
    eprintln!("  parsed {} packages (first 5):", pkgs.len());
    for pkg in pkgs.iter().take(5) {
        eprintln!("    {}", pkg.full);
    }
    eprintln!("---");
}

/// Download root.log using `koji download-logs` (task-based), falling
/// back to HTTP download from the build's persistent storage when task
/// logs have been garbage collected.
fn download_root_log(
    koji_client: &KojiClient,
    build: Option<&koji::BuildInfo>,
    arch_task: &TaskInfo,
    arch: &str,
    tmpdir: &std::path::Path,
    quiet: bool,
) -> Result<String, Box<dyn std::error::Error>> {
    if !quiet {
        eprintln!("Downloading logs from task {}...", arch_task.id);
    }
    match koji_client.download_log(arch_task.id, "root.log", tmpdir) {
        Ok(content) => return Ok(content),
        Err(e) => {
            if build.is_none() {
                return Err(e.into());
            }
            if !quiet {
                eprintln!("  task log download failed, trying build storage: {e}");
            }
        }
    }

    let build = build.unwrap();
    if !quiet {
        eprintln!("Downloading logs for {} (build {})...", build.nvr, build.id);
    }
    Ok(koji_client.download_build_log(build, arch, "root.log")?)
}

/// Resolve a reference to (task_id, Option<BuildInfo>).
///
/// Build refs return the BuildInfo so we can download logs from the
/// build's persistent storage when task logs have been garbage collected.
fn resolve_ref(
    koji_client: &KojiClient,
    koji_ref: &KojiRef,
) -> Result<(i64, Option<koji::BuildInfo>), xmlrpc::Error> {
    match koji_ref.ref_type {
        RefType::Task => Ok((koji_ref.id, None)),
        RefType::Build => {
            let build = koji_client.get_build(koji_ref.id)?;
            let task_id = build.task_id;
            Ok((task_id, Some(build)))
        }
    }
}

/// Format the resolution chain for display.
///
/// Shows how an input reference was resolved to a buildArch task:
/// - `build 2970832 (task 143889060) -> buildArch 143889177 (x86_64, CLOSED)`
/// - `task 143927217 -> buildArch 143927280 (x86_64, FAILED)`
/// - `task 143927280 (x86_64, FAILED)` (already a buildArch)
fn format_resolution(
    koji_ref: &KojiRef,
    parent_task_id: i64,
    arch_task: &TaskInfo,
    arch: &str,
) -> String {
    let arch_suffix = format!(
        "buildArch {} ({}, {})",
        arch_task.id,
        arch,
        arch_task.state_name()
    );

    // If the arch task IS the input, no chain needed.
    if koji_ref.ref_type == RefType::Task && koji_ref.id == arch_task.id {
        return format!(
            "task {} ({}, {})",
            arch_task.id,
            arch,
            arch_task.state_name()
        );
    }

    let input = match koji_ref.ref_type {
        RefType::Build => format!("build {} (task {})", koji_ref.id, parent_task_id),
        RefType::Task => {
            if parent_task_id == arch_task.id {
                // Task was already the buildArch task.
                return format!(
                    "task {} ({}, {})",
                    arch_task.id,
                    arch,
                    arch_task.state_name()
                );
            }
            format!("task {}", koji_ref.id)
        }
    };

    format!("{input} -> {arch_suffix}")
}

/// Resolved state for one side of the comparison.
struct ResolvedRef<'a> {
    arch_task: &'a TaskInfo,
    build: Option<&'a koji::BuildInfo>,
    tmpdir: &'a std::path::Path,
}

fn fetch_build_failure(
    koji_client: &KojiClient,
    ref1: &ResolvedRef<'_>,
    ref2: &ResolvedRef<'_>,
    arch: &str,
    lines: usize,
    quiet: bool,
) -> Option<JsonBuildFailure> {
    let failed = if ref1.arch_task.state == koji::TASK_FAILED {
        ref1
    } else if ref2.arch_task.state == koji::TASK_FAILED {
        ref2
    } else {
        return None;
    };

    // Try to read failure logs from the tmpdir first (already downloaded
    // by koji download-logs if that path was used), then fall back to
    // build-based HTTP download.
    let read_log = |name: &str| -> Option<String> {
        find_and_read(failed.tmpdir, name).or_else(|| {
            failed
                .build
                .and_then(|b| koji_client.download_build_log(b, arch, name).ok())
        })
    };

    // Always show mock_output.log if present (it has the mock/dep error).
    // Also show build.log unless mock_output.log already shows a dep
    // resolution failure ("Problem: nothing provides"), in which case
    // build.log adds no useful info.
    let mut logs = Vec::new();

    let mock_output = read_log("mock_output.log");
    let has_dep_error = mock_output
        .as_ref()
        .is_some_and(|t| t.contains("Problem: nothing provides"));

    if let Some(text) = mock_output {
        logs.push(tail_log("mock_output.log", &text, lines));
    }

    if !has_dep_error && let Some(text) = read_log("build.log") {
        logs.push(tail_log("build.log", &text, lines));
    }

    if logs.is_empty() {
        if !quiet {
            eprintln!("(no failure logs found for task {})", failed.arch_task.id);
        }
        return None;
    }

    Some(JsonBuildFailure {
        task_id: failed.arch_task.id,
        logs,
    })
}