atlassian-cli 0.9.2

Unified CLI for Atlassian Cloud products
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
use anyhow::{bail, Context, Result};
use atlassian_cli_api::pagination::{fetch_paged, BitbucketPage, PageLimits};
use serde::{Deserialize, Serialize};

use super::utils::{encode_path_segment, encode_ref_path, BitbucketContext};
use crate::commands::common::confirm_destructive;

#[derive(Deserialize)]
struct Repository {
    slug: String,
    name: String,
    #[serde(default)]
    updated_on: Option<String>,
}

#[derive(Deserialize)]
struct Branch {
    name: String,
    #[serde(default)]
    target: Option<Target>,
}

#[derive(Deserialize)]
struct Target {
    #[serde(default)]
    date: Option<String>,
}

/// Decide whether a repository counts as stale.
///
/// Extracted so the threshold arithmetic is testable without HTTP or a clock.
/// A repository with no `updated_on` is never stale: absent data is not
/// evidence of disuse, and treating it as such would mutate repositories on the
/// strength of a missing field.
pub(crate) fn is_stale(
    updated_on: Option<&str>,
    now: chrono::DateTime<chrono::Utc>,
    days: i64,
) -> bool {
    let Some(updated) = updated_on else {
        return false;
    };
    let Ok(updated_date) = chrono::DateTime::parse_from_rfc3339(updated) else {
        // Not stale, but say so. A server-side date-format change would
        // otherwise turn this command into a permanent "No stale repositories
        // found" with nothing to explain why.
        tracing::warn!(
            updated_on = updated,
            "Ignoring repository: last-updated timestamp is not valid RFC 3339"
        );
        return false;
    };
    // `try_days` rather than `days`: the latter panics on a value large enough
    // to overflow, and `--days` comes from the command line.
    let Some(threshold) = chrono::Duration::try_days(days) else {
        tracing::warn!(
            days,
            "Staleness threshold is out of range; treating nothing as stale"
        );
        return false;
    };
    now.signed_duration_since(updated_date) > threshold
}

/// Disable the issue tracker and wiki on stale repositories.
///
/// This was called "archive stale repositories" and reported `archived`. It
/// does not archive anything: Bitbucket Cloud has no repository archive API,
/// and the `PUT` sets only `has_issues: false` and `has_wiki: false`. The
/// label described an operation that never happened, while the operation that
/// did happen — turning off two features, hiding any issues and wiki pages
/// filed in them — went unnamed.
///
/// As with `delete_branches`, the behaviour is kept and the description and
/// rails are what changed: listing is the default, `--execute` performs the
/// change, and `--execute` needs the workspace typed back or `--yes`.
///
/// The listing is followed to completion; a list that cannot be completed
/// within the request budget is an error rather than a silent truncation,
/// because it drives mutations.
pub async fn disable_features_on_stale_repos(
    ctx: &BitbucketContext<'_>,
    workspace: &str,
    days_threshold: i64,
    execute: bool,
    assume_yes: bool,
) -> Result<()> {
    let path = format!("/2.0/repositories/{workspace}?pagelen=100");
    let (repositories, page) =
        fetch_paged::<BitbucketPage<Repository>>(&ctx.client, &path, PageLimits::new(None))
            .await
            .with_context(|| format!("Failed to list repositories in workspace {workspace}"))?;

    // Same reasoning as delete_branches: this list drives mutations.
    if page.truncated {
        bail!(
            "Refusing to continue: only {} repositories could be listed before the request \
             budget ran out, so the set shown would be incomplete.",
            repositories.len()
        );
    }

    let now = chrono::Utc::now();

    #[derive(Serialize)]
    struct StaleRepo<'a> {
        slug: &'a str,
        name: &'a str,
        last_updated: &'a str,
        action: &'a str,
    }

    let targets: Vec<&Repository> = repositories
        .iter()
        .filter(|repo| is_stale(repo.updated_on.as_deref(), now, days_threshold))
        .collect();

    if execute && !assume_yes && !targets.is_empty() {
        confirm_destructive(
            workspace,
            &format!(
                "About to disable the issue tracker and wiki on {} repositor(ies) in {workspace}.\n\
                 This is not archiving: existing issues and wiki pages become inaccessible.\n\
                 Re-run without --execute to list them first.",
                targets.len()
            ),
        )?;
    }

    let mut rows = Vec::with_capacity(targets.len());
    let mut failure: Option<anyhow::Error> = None;

    for repo in targets {
        if execute {
            let update_path = format!("/2.0/repositories/{workspace}/{}", repo.slug);
            let payload = serde_json::json!({
                "has_issues": false,
                "has_wiki": false,
            });

            let result: Result<serde_json::Value> = ctx
                .client
                .put(&update_path, &payload)
                .await
                .with_context(|| format!("Failed to disable features on repository {}", repo.slug));

            // Same reasoning as delete_branches: report what already changed
            // rather than aborting with only the failing name.
            if let Err(err) = result {
                failure = Some(err);
                break;
            }

            tracing::info!(
                repo_slug = repo.slug.as_str(),
                workspace,
                "Issue tracker and wiki disabled"
            );
        }

        rows.push(StaleRepo {
            slug: repo.slug.as_str(),
            name: repo.name.as_str(),
            last_updated: repo.updated_on.as_deref().unwrap_or(""),
            action: if execute {
                "issues and wiki disabled"
            } else {
                "would disable issues and wiki"
            },
        });
    }

    if let Some(err) = failure {
        if !rows.is_empty() {
            eprintln!(
                "Stopped after an error. {} repositor(ies) were already changed:",
                rows.len()
            );
            ctx.renderer.render_list(&rows)?;
        }
        return Err(err);
    }

    if !execute && !rows.is_empty() {
        eprintln!(
            "Listing only. {} repositor(ies) match. Pass --execute to disable \
             their issue tracker and wiki.",
            rows.len()
        );
    }

    ctx.renderer.render_list_or_empty(
        &rows,
        // Was a plain string containing a literal `{days_threshold}`, which
        // printed verbatim because it was never a format string.
        &format!("No stale repositories found (threshold: {days_threshold} days)"),
    )
}

/// Branch names never deleted, whatever the filter says.
const PROTECTED_BRANCHES: [&str; 4] = ["main", "master", "develop", "development"];

/// Decide whether a branch is shielded from deletion.
///
/// Split out so the selection rule is testable without HTTP. Note what it does
/// *not* consult: merge status. See `delete_branches` for why.
pub(crate) fn is_protected(name: &str, exclude_patterns: &[String]) -> bool {
    PROTECTED_BRANCHES.contains(&name)
        || exclude_patterns
            .iter()
            .any(|pattern| name.contains(pattern))
}

/// Delete branches selected purely by name.
///
/// This was called `delete_merged_branches` and advertised as "Delete merged
/// branches". It never checked merge status, and still does not: Bitbucket's
/// `/refs/branches` response carries no merge information, and establishing it
/// costs either a pull request cross-reference or a request per branch. The
/// command deletes every branch that is not in `PROTECTED_BRANCHES` and not
/// matched by `--exclude`, which includes live feature and release branches.
///
/// The listing is now followed to completion rather than capped at the first
/// page. That cap was previously the only bound on the blast radius, which is
/// why the safety gate landed first: removing it before the gate existed would
/// have widened the damage rather than narrowing it. A list that cannot be
/// completed within the request budget is an error here, not a truncation.
///
/// Rather than quietly narrow a command people may already depend on, the
/// behaviour is unchanged and the safety rails are what changed:
///
/// - the name and help text now say what it actually does,
/// - listing is the default and deleting requires `--execute`,
/// - `--execute` requires typing the repository slug, or `--yes`.
///
/// The old `--dry-run` flag is still accepted and now redundant, so existing
/// invocations that passed it keep working and mean the same thing. Existing
/// invocations that omitted it used to delete and now list instead, which is
/// the intended change.
pub async fn delete_branches(
    ctx: &BitbucketContext<'_>,
    workspace: &str,
    repo_slug: &str,
    exclude_patterns: Vec<String>,
    execute: bool,
    assume_yes: bool,
) -> Result<()> {
    let path = format!("/2.0/repositories/{workspace}/{repo_slug}/refs/branches?pagelen=100");
    let (branches, page) =
        fetch_paged::<BitbucketPage<Branch>>(&ctx.client, &path, PageLimits::new(None))
            .await
            .with_context(|| format!("Failed to list branches for {workspace}/{repo_slug}"))?;

    // Everywhere else a truncated list is labelled and rendered. Here it is an
    // error: an incomplete branch list feeds deletions, and "these are the
    // branches, probably" is not something to confirm against.
    if page.truncated {
        bail!(
            "Refusing to continue: only {} branches could be listed before the request \
             budget ran out, so the set shown would be incomplete.",
            branches.len()
        );
    }

    #[derive(Serialize)]
    struct DeletableBranch<'a> {
        name: &'a str,
        last_commit: &'a str,
        action: &'a str,
    }

    let targets: Vec<&Branch> = branches
        .iter()
        .filter(|branch| !is_protected(&branch.name, &exclude_patterns))
        .collect();

    // Confirm before the first delete, not after some of them have happened.
    if execute && !assume_yes && !targets.is_empty() {
        confirm_destructive(
            repo_slug,
            &format!(
                "About to delete {} branch(es) from {workspace}/{repo_slug}.\n\
                 Merge status is NOT checked: unmerged branches will be deleted.\n\
                 Re-run without --execute to list them first.",
                targets.len()
            ),
        )?;
    }

    let mut rows = Vec::with_capacity(targets.len());
    let mut failure: Option<anyhow::Error> = None;

    for branch in targets {
        if execute {
            // Not `?`: a rejected name this far into the loop means earlier
            // branches are already deleted, and returning here would discard
            // that record -- the exact thing the failure handling below exists
            // to prevent.
            let encoded = match encode_ref_path(&branch.name) {
                Ok(encoded) => encoded,
                Err(err) => {
                    failure = Some(err);
                    break;
                }
            };
            // The workspace and slug are encoded too, not only the branch name.
            // With `--yes` the confirmation above is skipped entirely, and a
            // slug of `r?x` would truncate every iteration of this loop onto
            // the repository itself.
            let delete_path = format!(
                "/2.0/repositories/{}/{}/refs/branches/{encoded}",
                encode_path_segment(workspace)?,
                encode_path_segment(repo_slug)?
            );
            let result: Result<serde_json::Value> =
                ctx.client.delete(&delete_path).await.with_context(|| {
                    format!(
                        "Failed to delete branch {} from {workspace}/{repo_slug}",
                        branch.name
                    )
                });

            // Stop on the first failure, but do not discard the record of what
            // was already deleted. Returning `?` here would abort before
            // rendering, leaving the user with an error naming one branch and
            // no way to learn which others are already gone.
            if let Err(err) = result {
                failure = Some(err);
                break;
            }

            tracing::info!(
                branch_name = branch.name.as_str(),
                workspace,
                repo_slug,
                "Branch deleted"
            );
        }

        rows.push(DeletableBranch {
            name: branch.name.as_str(),
            last_commit: branch
                .target
                .as_ref()
                .and_then(|t| t.date.as_deref())
                .unwrap_or(""),
            action: if execute { "deleted" } else { "would delete" },
        });
    }

    if let Some(err) = failure {
        if !rows.is_empty() {
            eprintln!(
                "Stopped after an error. {} branch(es) were already deleted:",
                rows.len()
            );
            ctx.renderer.render_list(&rows)?;
        }
        return Err(err);
    }

    if !execute && !rows.is_empty() {
        // stderr, so a piped `-f json` listing stays machine-readable.
        eprintln!(
            "Listing only. {} branch(es) match; merge status was not checked. \
             Pass --execute to delete them.",
            rows.len()
        );
    }

    ctx.renderer
        .render_list_or_empty(&rows, "No branches matched (excluding protected patterns)")
}

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

    fn patterns(values: &[&str]) -> Vec<String> {
        values.iter().map(|v| v.to_string()).collect()
    }

    #[test]
    fn protected_branches_are_never_selected() {
        for name in PROTECTED_BRANCHES {
            assert!(is_protected(name, &[]), "{name} should be protected");
        }
    }

    #[test]
    fn exclude_patterns_match_as_substrings() {
        let excludes = patterns(&["release/"]);
        assert!(is_protected("release/2026-09", &excludes));
        assert!(!is_protected("feature/login", &excludes));
    }

    /// The defect this command was rewritten for: an unmerged feature branch is
    /// selected for deletion, because nothing here consults merge status. The
    /// test pins the behaviour so the rename cannot be mistaken for a fix.
    #[test]
    fn unmerged_feature_branches_are_still_selected() {
        assert!(!is_protected("feature/work-in-progress", &[]));
        assert!(!is_protected("hotfix/urgent", &patterns(&["release/"])));
    }

    use atlassian_cli_api::ApiClient;
    use atlassian_cli_output::{OutputFormat, OutputRenderer};
    use wiremock::matchers::{method, path_regex};
    use wiremock::{Mock, MockServer, ResponseTemplate};

    /// Serve a branch listing containing `names`, and accept any DELETE.
    async fn server_with_branches(names: &[&str]) -> MockServer {
        let server = MockServer::start().await;
        let values: Vec<_> = names
            .iter()
            .map(|n| serde_json::json!({ "name": n, "target": { "date": "2026-01-01" } }))
            .collect();

        Mock::given(method("GET"))
            .and(path_regex(r".*/refs/branches$"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "values": values
            })))
            .mount(&server)
            .await;

        Mock::given(method("DELETE"))
            .respond_with(ResponseTemplate::new(204))
            .mount(&server)
            .await;

        server
    }

    fn ctx_for(server: &MockServer, renderer: &OutputRenderer) -> BitbucketContext<'static> {
        // `renderer` outlives every call below; the transmute-free way to say so
        // is to leak it, which is acceptable in a test process.
        let renderer: &'static OutputRenderer =
            Box::leak(Box::new(OutputRenderer::new(renderer.format())));
        BitbucketContext {
            client: ApiClient::new(server.uri()).unwrap(),
            renderer,
            is_bearer: false,
        }
    }

    async fn delete_paths(server: &MockServer) -> Vec<String> {
        server
            .received_requests()
            .await
            .unwrap_or_default()
            .into_iter()
            .filter(|r| r.method == wiremock::http::Method::DELETE)
            .map(|r| r.url.path().to_string())
            .collect()
    }

    /// A repository with no `updated_on` must never be treated as stale:
    /// acting on a missing field would mutate repositories on no evidence.
    #[test]
    fn missing_updated_on_is_never_stale() {
        let now = chrono::Utc::now();
        assert!(!is_stale(None, now, 180));
        assert!(!is_stale(Some("not-a-date"), now, 180));
    }

    /// Exactly at the threshold is not stale: the comparison is strict.
    #[test]
    fn the_threshold_boundary_is_exclusive() {
        let now = chrono::Utc::now();
        let exactly = (now - chrono::Duration::days(180)).to_rfc3339();
        assert!(!is_stale(Some(&exactly), now, 180));

        let a_moment_older =
            (now - chrono::Duration::days(180) - chrono::Duration::seconds(1)).to_rfc3339();
        assert!(is_stale(Some(&a_moment_older), now, 180));
    }

    /// `chrono::Duration::days` panics on an overflowing value, and `--days`
    /// is user input.
    #[test]
    fn an_absurd_threshold_does_not_panic() {
        let now = chrono::Utc::now();
        let old = (now - chrono::Duration::days(500)).to_rfc3339();
        assert!(!is_stale(Some(&old), now, i64::MAX));
    }

    #[test]
    fn staleness_compares_against_the_threshold() {
        let now = chrono::Utc::now();
        let old = (now - chrono::Duration::days(200)).to_rfc3339();
        let recent = (now - chrono::Duration::days(10)).to_rfc3339();
        assert!(is_stale(Some(&old), now, 180));
        assert!(!is_stale(Some(&recent), now, 180));
    }

    async fn server_with_repos(slugs_and_dates: &[(&str, String)]) -> MockServer {
        let server = MockServer::start().await;
        let values: Vec<_> = slugs_and_dates
            .iter()
            .map(|(slug, date)| {
                serde_json::json!({ "slug": slug, "name": slug, "updated_on": date })
            })
            .collect();

        Mock::given(method("GET"))
            .and(path_regex(r"^/2\.0/repositories/[^/]+$"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "values": values
            })))
            .mount(&server)
            .await;

        Mock::given(method("PUT"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({})))
            .mount(&server)
            .await;

        server
    }

    async fn put_count(server: &MockServer) -> usize {
        server
            .received_requests()
            .await
            .unwrap_or_default()
            .into_iter()
            .filter(|r| r.method == wiremock::http::Method::PUT)
            .count()
    }

    /// The archive-repos equivalent of the gate test below: without --execute
    /// nothing is mutated.
    #[tokio::test]
    async fn stale_repo_listing_mutates_nothing() {
        let old = (chrono::Utc::now() - chrono::Duration::days(400)).to_rfc3339();
        let server = server_with_repos(&[("dusty", old)]).await;
        let renderer = OutputRenderer::new(OutputFormat::Json);
        let ctx = ctx_for(&server, &renderer);

        disable_features_on_stale_repos(&ctx, "ws", 180, false, false)
            .await
            .unwrap();

        assert_eq!(put_count(&server).await, 0, "listing must not PUT");
    }

    #[tokio::test]
    async fn execute_disables_features_only_on_stale_repos() {
        let now = chrono::Utc::now();
        let old = (now - chrono::Duration::days(400)).to_rfc3339();
        let fresh = (now - chrono::Duration::days(3)).to_rfc3339();
        let server = server_with_repos(&[("dusty", old), ("busy", fresh)]).await;
        let renderer = OutputRenderer::new(OutputFormat::Json);
        let ctx = ctx_for(&server, &renderer);

        disable_features_on_stale_repos(&ctx, "ws", 180, true, true)
            .await
            .unwrap();

        assert_eq!(put_count(&server).await, 1, "only the stale repository");
    }

    /// The truncation defect this branch set out to fix, on the destructive
    /// path: a second page of branches must be seen, not silently dropped.
    #[tokio::test]
    async fn every_page_of_branches_is_listed() {
        let server = MockServer::start().await;
        let page_two = format!(
            "{}/2.0/repositories/ws/repo/refs/branches?page=2",
            server.uri()
        );

        Mock::given(method("GET"))
            .and(path_regex(r".*/refs/branches$"))
            .and(wiremock::matchers::query_param("page", "2"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "values": [{"name": "second-page", "target": {"date": "2026-01-01"}}]
            })))
            .mount(&server)
            .await;

        Mock::given(method("GET"))
            .and(path_regex(r".*/refs/branches$"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "values": [{"name": "first-page", "target": {"date": "2026-01-01"}}],
                "next": page_two
            })))
            .mount(&server)
            .await;

        Mock::given(method("DELETE"))
            .respond_with(ResponseTemplate::new(204))
            .mount(&server)
            .await;

        let renderer = OutputRenderer::new(OutputFormat::Json);
        let ctx = ctx_for(&server, &renderer);

        delete_branches(&ctx, "ws", "repo", vec![], true, true)
            .await
            .unwrap();

        let deleted = delete_paths(&server).await;
        assert_eq!(deleted.len(), 2, "both pages must be acted on: {deleted:?}");
        assert!(deleted.iter().any(|p| p.ends_with("/second-page")));
    }

    /// A list that cannot be completed must abort rather than confirm against a
    /// partial set. Everywhere else truncation is a label; here it is an error.
    #[tokio::test]
    async fn an_incompletable_branch_list_refuses_to_delete() {
        let server = MockServer::start().await;
        let forever = format!(
            "{}/2.0/repositories/ws/repo/refs/branches?page=next",
            server.uri()
        );

        Mock::given(method("GET"))
            .and(path_regex(r".*/refs/branches$"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "values": [{"name": "a", "target": {"date": "2026-01-01"}}],
                "next": forever
            })))
            .mount(&server)
            .await;

        Mock::given(method("DELETE"))
            .respond_with(ResponseTemplate::new(204))
            .mount(&server)
            .await;

        let renderer = OutputRenderer::new(OutputFormat::Json);
        let ctx = ctx_for(&server, &renderer);

        let err = delete_branches(&ctx, "ws", "repo", vec![], true, true)
            .await
            .expect_err("an incomplete listing must not proceed to deletion");

        assert!(
            format!("{err:#}").contains("Refusing to continue"),
            "unexpected error: {err:#}"
        );
        assert!(
            delete_paths(&server).await.is_empty(),
            "nothing may be deleted from an incomplete list"
        );
    }

    /// The single most important test in this file. If someone makes deletion
    /// the default again, or drops the `execute` guard, this fails.
    #[tokio::test]
    async fn listing_is_the_default_and_deletes_nothing() {
        let server = server_with_branches(&["main", "feature/a", "feature/b"]).await;
        let renderer = OutputRenderer::new(OutputFormat::Json);
        let ctx = ctx_for(&server, &renderer);

        delete_branches(&ctx, "ws", "repo", vec![], false, false)
            .await
            .unwrap();

        assert!(
            delete_paths(&server).await.is_empty(),
            "listing must not issue any DELETE"
        );
    }

    #[tokio::test]
    async fn execute_with_yes_deletes_only_unprotected_branches() {
        let server = server_with_branches(&["main", "develop", "feature/a"]).await;
        let renderer = OutputRenderer::new(OutputFormat::Json);
        let ctx = ctx_for(&server, &renderer);

        delete_branches(&ctx, "ws", "repo", vec![], true, true)
            .await
            .unwrap();

        let deleted = delete_paths(&server).await;
        assert_eq!(deleted.len(), 1, "only the unprotected branch: {deleted:?}");
        assert!(deleted[0].ends_with("/refs/branches/feature/a"));
    }

    /// A branch named `main#old` must not resolve to a DELETE on `main`.
    /// Without percent-encoding, `#` starts a URL fragment and the request
    /// lands on the protected ref instead.
    #[tokio::test]
    async fn a_hash_in_a_branch_name_cannot_delete_the_protected_ref() {
        let server = server_with_branches(&["main", "main#old"]).await;
        let renderer = OutputRenderer::new(OutputFormat::Json);
        let ctx = ctx_for(&server, &renderer);

        delete_branches(&ctx, "ws", "repo", vec![], true, true)
            .await
            .unwrap();

        let deleted = delete_paths(&server).await;
        assert_eq!(deleted.len(), 1, "exactly one delete: {deleted:?}");
        assert!(
            !deleted[0].ends_with("/refs/branches/main"),
            "must not have deleted the protected ref: {deleted:?}"
        );
        assert!(
            deleted[0].contains("main%23old"),
            "expected the encoded ref, got: {deleted:?}"
        );
    }

    /// End-to-end guard for the traversal hole: a hostile listing entry must
    /// abort the run, not issue a DELETE against the retargeted path.
    #[tokio::test]
    async fn a_traversal_branch_name_aborts_without_deleting() {
        let server = server_with_branches(&["a/../../../../../../repositories/w2/r2"]).await;
        let renderer = OutputRenderer::new(OutputFormat::Json);
        let ctx = ctx_for(&server, &renderer);

        let err = delete_branches(&ctx, "ws", "repo", vec![], true, true)
            .await
            .expect_err("a dot-segment ref name must be refused");

        assert!(
            format!("{err:#}").contains("would change which resource"),
            "unexpected error: {err:#}"
        );
        assert!(
            delete_paths(&server).await.is_empty(),
            "nothing may be deleted when a name is refused"
        );
    }

    /// A hostile name in a *later* slot must not discard the record of the
    /// deletions that already happened. The earlier test only covered a
    /// rejected name in the first position, where there is nothing to lose.
    #[tokio::test]
    async fn a_late_traversal_name_still_reports_earlier_deletions() {
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path_regex(r".*/refs/branches$"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "values": [
                    {"name": "aaa", "target": {"date": "2026-01-01"}},
                    {"name": "x/../../../../../../repositories/w2/r2",
                     "target": {"date": "2026-01-01"}}
                ]
            })))
            .mount(&server)
            .await;
        Mock::given(method("DELETE"))
            .respond_with(ResponseTemplate::new(204))
            .mount(&server)
            .await;

        let renderer = OutputRenderer::new(OutputFormat::Json);
        let ctx = ctx_for(&server, &renderer);

        let err = delete_branches(&ctx, "ws", "repo", vec![], true, true)
            .await
            .expect_err("the hostile name must abort the run");
        assert!(
            format!("{err:#}").contains("would change which resource"),
            "unexpected error: {err:#}"
        );

        let deleted = delete_paths(&server).await;
        assert_eq!(deleted.len(), 1, "only the safe branch: {deleted:?}");
        assert!(deleted[0].ends_with("/aaa"));
        assert!(
            !deleted.iter().any(|p| p.contains("/repositories/w2/r2")),
            "the retargeted path must never be requested: {deleted:?}"
        );
    }

    /// A failure part-way through must still report what was already deleted,
    /// rather than aborting with only the failing name.
    #[tokio::test]
    async fn a_partial_failure_reports_what_was_deleted() {
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path_regex(r".*/refs/branches$"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "values": [
                    {"name": "aaa", "target": {"date": "2026-01-01"}},
                    {"name": "bbb", "target": {"date": "2026-01-01"}}
                ]
            })))
            .mount(&server)
            .await;
        // First branch succeeds, second is refused by the server.
        Mock::given(method("DELETE"))
            .and(path_regex(r".*/refs/branches/aaa$"))
            .respond_with(ResponseTemplate::new(204))
            .mount(&server)
            .await;
        Mock::given(method("DELETE"))
            .and(path_regex(r".*/refs/branches/bbb$"))
            .respond_with(ResponseTemplate::new(500))
            .mount(&server)
            .await;

        let renderer = OutputRenderer::new(OutputFormat::Json);
        let ctx = ctx_for(&server, &renderer);

        delete_branches(&ctx, "ws", "repo", vec![], true, true)
            .await
            .expect_err("the second delete fails");

        let deleted = delete_paths(&server).await;
        assert!(
            deleted.iter().any(|p| p.ends_with("/aaa")),
            "the first delete really happened: {deleted:?}"
        );
        assert!(
            deleted.iter().any(|p| p.ends_with("/bbb")),
            "the failing delete was attempted: {deleted:?}"
        );
        // Count distinct branches, not requests: a 500 is retryable, so the
        // client legitimately asks for `bbb` more than once.
        let mut distinct: Vec<&str> = deleted
            .iter()
            .filter_map(|p| p.rsplit('/').next())
            .collect();
        distinct.sort_unstable();
        distinct.dedup();
        assert_eq!(
            distinct,
            vec!["aaa", "bbb"],
            "must stop at the failure rather than continuing: {deleted:?}"
        );
    }

    #[tokio::test]
    async fn exclude_patterns_are_honoured_under_execute() {
        let server = server_with_branches(&["release/1", "feature/a"]).await;
        let renderer = OutputRenderer::new(OutputFormat::Json);
        let ctx = ctx_for(&server, &renderer);

        delete_branches(&ctx, "ws", "repo", vec!["release/".to_string()], true, true)
            .await
            .unwrap();

        let deleted = delete_paths(&server).await;
        assert_eq!(deleted.len(), 1, "{deleted:?}");
        assert!(deleted[0].ends_with("/refs/branches/feature/a"));
    }

    #[test]
    fn protection_is_exact_for_names_and_substring_for_patterns() {
        // "main" is protected, but "maintenance" is not a protected name.
        assert!(is_protected("main", &[]));
        assert!(!is_protected("maintenance", &[]));
        // As a pattern, though, "main" would shield it.
        assert!(is_protected("maintenance", &patterns(&["main"])));
    }
}