poi-tracker 0.12.0

Package-of-interest tracker for Fedora, EPEL, and CentOS SIGs
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
// SPDX-License-Identifier: Apache-2.0 OR MIT

//! `triage-retired` subcommand.
//!
//! For each package in the inventory, check whether it's retired
//! on the configured dist-git branch (a `dead.package` file
//! present on that branch). Retired packages have no live spec
//! to update, so any open release-monitoring bug filed against
//! that branch can be closed as `CANTFIX`.

use std::collections::BTreeMap;

use sandogasa_bugzilla::BzClient;
use sandogasa_bugzilla::models::Bug;
use sandogasa_distgit::DistGitClient;
use sandogasa_inventory::Inventory;

/// Anitya / the-new-hotness reporter address.
pub const RELEASE_MONITORING_REPORTER: &str = "upstream-release-monitoring@fedoraproject.org";

/// One planned bug close.
#[derive(Debug, Clone)]
pub struct BugClose {
    pub bug_id: u64,
    pub component: String,
    pub summary: String,
    pub current_status: String,
}

/// Per-package outcome from planning.
#[derive(Debug)]
pub enum PackageOutcome {
    /// The package is still live on this branch — nothing to do.
    NotRetired,
    /// Retired, but no open release-monitoring bugs for this
    /// branch.
    RetiredNoBugs,
    /// One or more bugs queued for closure.
    RetiredClose(Vec<BugClose>),
}

/// Decide what to do for one package: which (if any) open bugs
/// to close. Pure function over the dist-git check + fetched
/// bug list so it's easy to unit-test.
pub fn plan_package(package: &str, retired: bool, bugs: &[Bug]) -> PackageOutcome {
    if !retired {
        return PackageOutcome::NotRetired;
    }
    let opens: Vec<BugClose> = bugs
        .iter()
        .filter(|b| b.status != "CLOSED")
        .map(|b| BugClose {
            bug_id: b.id,
            component: package.to_string(),
            summary: b.summary.clone(),
            current_status: b.status.clone(),
        })
        .collect();
    if opens.is_empty() {
        PackageOutcome::RetiredNoBugs
    } else {
        PackageOutcome::RetiredClose(opens)
    }
}

/// Build the Bugzilla product + version pair to scope the search
/// to bugs filed against the dist-git branch being retired.
///
/// Conventions: `rawhide` maps to product `Fedora` / version
/// `rawhide`; anything starting with `epel` maps to `Fedora EPEL`
/// / `<branch>`; everything else maps to `Fedora` / `<branch>`
/// (so an `f43` branch retirement still narrows correctly).
pub fn product_version_for_branch(branch: &str) -> (&'static str, String) {
    if branch.starts_with("epel") {
        ("Fedora EPEL", branch.to_string())
    } else {
        ("Fedora", branch.to_string())
    }
}

/// Build the Bugzilla search query for retired-package triage:
/// the component's open release-monitoring bugs against the
/// retirement branch's product/version pair.
pub fn bug_search_query(component: &str, branch: &str) -> String {
    let (product, version) = product_version_for_branch(branch);
    [
        format!("component={}", urlencode(component)),
        format!("product={}", urlencode(product)),
        format!("version={}", urlencode(&version)),
        format!("reporter={}", urlencode(RELEASE_MONITORING_REPORTER)),
        "bug_status=__open__".to_string(),
    ]
    .join("&")
}

fn urlencode(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    for b in s.bytes() {
        match b {
            b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => {
                out.push(b as char);
            }
            _ => out.push_str(&format!("%{b:02X}")),
        }
    }
    out
}

/// Print one package's planned closures as soon as they're
/// known, so a long inventory run gives live feedback instead of
/// accumulating everything to a final block.
pub fn print_package_closes(component: &str, closes: &[BugClose]) {
    println!("{component} ({} bug(s)):", closes.len());
    for c in closes {
        println!("  bug {} [{}]: {}", c.bug_id, c.current_status, c.summary);
    }
}

/// Whether a package should be included in this run given the
/// optional `--package` (only that name), `--start-from` (skip
/// earlier names in iteration order), and `--end-with` (stop
/// after this name, inclusive) filters. `--package` is mutually
/// exclusive with the range flags at the CLI layer;
/// `--start-from` and `--end-with` compose to bound a sub-range.
pub fn should_include(
    name: &str,
    only: Option<&str>,
    start_from: Option<&str>,
    end_with: Option<&str>,
) -> bool {
    if let Some(o) = only {
        return name == o;
    }
    if let Some(s) = start_from
        && name < s
    {
        return false;
    }
    if let Some(e) = end_with
        && name > e
    {
        return false;
    }
    true
}

/// Retry an async fallible operation a few times, sleeping a
/// little longer between each attempt. Used for transient
/// network failures from Pagure / Bugzilla — the failure
/// message includes the operation label so users can see what's
/// being retried.
pub async fn retry<F, Fut, T, E>(
    label: &str,
    attempts: usize,
    mut f: F,
    verbose: bool,
) -> Result<T, E>
where
    F: FnMut() -> Fut,
    Fut: std::future::Future<Output = Result<T, E>>,
    E: std::fmt::Display,
{
    let mut last: Option<E> = None;
    for attempt in 1..=attempts {
        match f().await {
            Ok(v) => return Ok(v),
            Err(e) => {
                if attempt < attempts {
                    let backoff = 1u64 << (attempt - 1).min(4); // 1, 2, 4, 8s
                    if verbose {
                        eprintln!(
                            "[poi-tracker] {label} attempt {attempt}/{attempts} failed: {e}; \
                             retrying in {backoff}s"
                        );
                    }
                    tokio::time::sleep(std::time::Duration::from_secs(backoff)).await;
                }
                last = Some(e);
            }
        }
    }
    Err(last.expect("loop ran at least once"))
}

/// Default number of attempts for transient-network retries.
pub const RETRY_ATTEMPTS: usize = 3;

/// The comment body added when closing a retired-package bug.
pub fn close_comment(package: &str, branch: &str) -> String {
    format!(
        "Package `{package}` is retired on the `{branch}` dist-git \
         branch (the `dead.package` marker is present); closing as \
         CANTFIX since there's no live package to update."
    )
}

/// Summary returned from `run` so the caller can pick an exit
/// code without re-counting.
#[derive(Debug, Default)]
pub struct RunReport {
    pub packages_checked: usize,
    pub packages_retired: usize,
    pub closes_planned: usize,
    pub closes_applied: usize,
    pub failures: usize,
}

/// Run the whole `triage-retired` flow.
#[allow(clippy::too_many_arguments)]
pub async fn run(
    inventory: &Inventory,
    bz: &BzClient,
    dg: &DistGitClient,
    branch: &str,
    only_package: Option<&str>,
    start_from: Option<&str>,
    end_with: Option<&str>,
    claim: bool,
    claim_email: Option<&str>,
    dry_run: bool,
    yes: bool,
    verbose: bool,
) -> Result<RunReport, String> {
    let mut all_closes: Vec<BugClose> = Vec::new();
    let mut packages_checked = 0usize;
    let mut packages_retired = 0usize;

    for pkg in &inventory.package {
        if !should_include(&pkg.name, only_package, start_from, end_with) {
            continue;
        }
        packages_checked += 1;
        if verbose {
            eprintln!(
                "[poi-tracker] {}: checking retirement on {branch}",
                pkg.name
            );
        }
        let retired = retry(
            &format!("is_retired({}, {branch})", pkg.name),
            RETRY_ATTEMPTS,
            || dg.is_retired(&pkg.name, branch),
            verbose,
        )
        .await
        .map_err(|e| format!("dist-git is_retired for {}: {e}", pkg.name))?;
        if !retired {
            continue;
        }
        packages_retired += 1;

        if verbose {
            eprintln!(
                "[poi-tracker] {}: retired on {branch}, searching open bugs",
                pkg.name
            );
        }
        let query = bug_search_query(&pkg.name, branch);
        let bugs = retry(
            &format!("bug search for {}", pkg.name),
            RETRY_ATTEMPTS,
            || bz.search(&query, 0),
            verbose,
        )
        .await
        .map_err(|e| format!("Bugzilla search for {}: {e}", pkg.name))?;
        match plan_package(&pkg.name, true, &bugs) {
            PackageOutcome::NotRetired => unreachable!("retired check passed above"),
            PackageOutcome::RetiredNoBugs => {
                if verbose {
                    eprintln!(
                        "[poi-tracker] {}: retired but no open bugs to close",
                        pkg.name
                    );
                }
            }
            PackageOutcome::RetiredClose(closes) => {
                print_package_closes(&pkg.name, &closes);
                all_closes.extend(closes);
            }
        }
    }

    if all_closes.is_empty() {
        println!("No retired packages with open release-monitoring bugs.");
    } else {
        print_tally(&all_closes);
    }

    let mut report = RunReport {
        packages_checked,
        packages_retired,
        closes_planned: all_closes.len(),
        closes_applied: 0,
        failures: 0,
    };

    if all_closes.is_empty() {
        return Ok(report);
    }
    if dry_run {
        eprintln!("\n(dry-run: not applying)");
        return Ok(report);
    }
    // Offer to claim ownership before the main confirm so the
    // user sees one prompt-then-confirm flow. With `--claim`,
    // skip the prompt. With `-y` and no `--claim`, don't claim.
    // Without a configured email there's nothing to assign to,
    // so just skip silently.
    let want_claim = if claim {
        claim_email.is_some()
    } else if yes || claim_email.is_none() {
        false
    } else {
        confirm(&format!(
            "Also claim ownership (assigned_to = {})?",
            claim_email.unwrap()
        ))?
    };
    let active_claim_email = if want_claim {
        let e = claim_email.unwrap();
        eprintln!("claiming ownership as {e}");
        Some(e.to_string())
    } else {
        None
    };

    if !yes && !confirm(&format!("\nClose {} bug(s) as CANTFIX?", all_closes.len()))? {
        eprintln!("aborted.");
        return Ok(report);
    }

    for c in &all_closes {
        let mut body = serde_json::json!({
            "status": "CLOSED",
            "resolution": "CANTFIX",
            "comment": { "body": close_comment(&c.component, branch) },
        });
        if let Some(ref email) = active_claim_email {
            body["assigned_to"] = serde_json::json!(email);
        }
        match bz.update(c.bug_id, &body).await {
            Ok(()) => {
                report.closes_applied += 1;
                eprintln!(
                    "closed bug {} ({}): {} -> CLOSED/CANTFIX",
                    c.bug_id, c.component, c.current_status
                );
            }
            Err(e) => {
                report.failures += 1;
                eprintln!("error: bug {} ({}): {e}", c.bug_id, c.component);
            }
        }
    }
    Ok(report)
}

/// One-line-per-package recap printed after the loop, so the
/// reader can scan everything that's about to be closed (or that
/// was just closed) without scrolling back through the live
/// per-package blocks.
fn print_tally(closes: &[BugClose]) {
    let mut by_pkg: BTreeMap<&str, Vec<&BugClose>> = BTreeMap::new();
    for c in closes {
        by_pkg.entry(c.component.as_str()).or_default().push(c);
    }
    println!(
        "\nTotal: {} closure(s) across {} package(s):",
        closes.len(),
        by_pkg.len()
    );
    for (pkg, bugs) in &by_pkg {
        let ids: Vec<String> = bugs.iter().map(|b| format!("rhbz#{}", b.bug_id)).collect();
        println!("  {pkg}: {}", ids.join(", "));
    }
}

fn confirm(prompt: &str) -> Result<bool, String> {
    use std::io::{BufRead, Write};
    eprint!("{prompt} [y/N]: ");
    std::io::stderr().flush().map_err(|e| e.to_string())?;
    let mut line = String::new();
    std::io::stdin()
        .lock()
        .read_line(&mut line)
        .map_err(|e| e.to_string())?;
    Ok(line.trim().eq_ignore_ascii_case("y"))
}

#[cfg(test)]
mod tests {
    use super::*;

    fn make_bug(id: u64, status: &str, summary: &str) -> Bug {
        serde_json::from_value(serde_json::json!({
            "id": id,
            "summary": summary,
            "status": status,
            "resolution": "",
            "product": "Fedora",
            "component": ["foo"],
            "severity": "unspecified",
            "priority": "unspecified",
            "assigned_to": "nobody@fedoraproject.org",
            "creator": RELEASE_MONITORING_REPORTER,
            "creation_time": "2026-01-01T00:00:00Z",
            "last_change_time": "2026-01-01T00:00:00Z",
        }))
        .unwrap()
    }

    #[test]
    fn plan_skips_live_packages() {
        let outcome = plan_package("foo", false, &[make_bug(1, "NEW", "x")]);
        assert!(matches!(outcome, PackageOutcome::NotRetired));
    }

    #[test]
    fn plan_no_bugs_when_retired_with_empty_search() {
        let outcome = plan_package("foo", true, &[]);
        assert!(matches!(outcome, PackageOutcome::RetiredNoBugs));
    }

    #[test]
    fn plan_closes_only_open_bugs_when_retired() {
        let bugs = vec![
            make_bug(1, "NEW", "foo 1.0 available"),
            make_bug(2, "ASSIGNED", "foo 0.9 available"),
            // The query filters to open already, but a defensive
            // check guards against a stray CLOSED slipping in.
            make_bug(3, "CLOSED", "foo 0.8 available"),
        ];
        let outcome = plan_package("foo", true, &bugs);
        match outcome {
            PackageOutcome::RetiredClose(closes) => {
                assert_eq!(closes.len(), 2);
                let ids: Vec<u64> = closes.iter().map(|c| c.bug_id).collect();
                assert_eq!(ids, vec![1, 2]);
            }
            other => panic!("expected RetiredClose, got {other:?}"),
        }
    }

    #[test]
    fn product_version_picks_epel_for_epel_branches() {
        assert_eq!(
            product_version_for_branch("epel10"),
            ("Fedora EPEL", "epel10".to_string())
        );
        assert_eq!(
            product_version_for_branch("epel9"),
            ("Fedora EPEL", "epel9".to_string())
        );
    }

    #[test]
    fn product_version_picks_fedora_for_rawhide_and_fnn() {
        assert_eq!(
            product_version_for_branch("rawhide"),
            ("Fedora", "rawhide".to_string())
        );
        assert_eq!(
            product_version_for_branch("f43"),
            ("Fedora", "f43".to_string())
        );
    }

    #[test]
    fn bug_search_query_scopes_to_branch() {
        let q = bug_search_query("python-django6", "epel10");
        assert!(q.contains("component=python-django6"));
        assert!(q.contains("product=Fedora%20EPEL"));
        assert!(q.contains("version=epel10"));
        assert!(q.contains("bug_status=__open__"));
        assert!(q.contains("reporter=upstream-release-monitoring%40fedoraproject.org"));

        let q = bug_search_query("foo", "rawhide");
        assert!(q.contains("product=Fedora&"));
        assert!(q.contains("version=rawhide"));
    }

    #[test]
    fn close_comment_mentions_package_and_branch() {
        let c = close_comment("python-django6", "epel10");
        assert!(c.contains("python-django6"));
        assert!(c.contains("epel10"));
        assert!(c.contains("CANTFIX"));
    }

    #[test]
    fn should_include_no_filters_keeps_everything() {
        assert!(should_include("foo", None, None, None));
        assert!(should_include("zzz", None, None, None));
    }

    #[test]
    fn should_include_only_matches_exact() {
        assert!(should_include("foo", Some("foo"), None, None));
        assert!(!should_include("foo-utils", Some("foo"), None, None));
        assert!(!should_include("foo", Some("bar"), None, None));
    }

    #[test]
    fn should_include_start_from_is_inclusive() {
        // Skip until name >= start_from; include start and onward.
        assert!(!should_include("apple", None, Some("mango"), None));
        assert!(should_include("mango", None, Some("mango"), None));
        assert!(should_include("zebra", None, Some("mango"), None));
    }

    #[test]
    fn should_include_end_with_is_inclusive() {
        // Include up to and including end_with; skip anything after.
        assert!(should_include("apple", None, None, Some("mango")));
        assert!(should_include("mango", None, None, Some("mango")));
        assert!(!should_include("zebra", None, None, Some("mango")));
    }

    #[test]
    fn should_include_range_bounds_both_inclusive() {
        // [start, end] inclusive sub-range — handy for "all rust-nu-*".
        let s = Some("rust-nu-cli");
        let e = Some("rust-nu-engine");
        assert!(!should_include("rust-itertools", None, s, e));
        assert!(should_include("rust-nu-cli", None, s, e));
        assert!(should_include("rust-nu-cmd-base", None, s, e));
        assert!(should_include("rust-nu-engine", None, s, e));
        assert!(!should_include("rust-nu-utils", None, s, e));
    }
}