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
//! Pre-flight publisher-state types shared between `core` and `stage-publish`.
//!
//! The preflight check runs before any stage in the release pipeline to detect
//! one-way-door publishers (crates.io, Chocolatey, WinGet, AUR) that already
//! have the target version submitted or approved. Discovering this before the
//! build prevents an entire wasted release cycle.
//!
//! # State machine
//!
//! ```text
//! Clean → safe to publish
//! Published → idempotent skip (not a blocker)
//! InModeration { reason } → blocker (version submitted, moderation queue)
//! PRPending → blocker (PR already open for this version)
//! Unknown { reason } → warn-and-allow unless --strict-preflight
//! ```
use std::fmt;
// ---------------------------------------------------------------------------
// PublisherState
// ---------------------------------------------------------------------------
/// The state of a single publisher for the target version.
#[derive(Debug, Clone, PartialEq)]
pub enum PublisherState {
/// Version not present. Safe to publish.
Clean,
/// Version already published / approved. Idempotent skip (not a blocker).
Published,
/// Submitted but pending review / moderation. Blocker. `reason` is a
/// short human-readable explanation (e.g. "package in moderation queue").
InModeration { reason: String },
/// PR already open against the upstream registry. Blocker.
PRPending(String),
/// Couldn't determine state. Warn-and-allow unless `--strict-preflight`.
/// `reason` carries a short error description for diagnostics.
Unknown { reason: String },
}
impl PublisherState {
/// Returns `true` when this state blocks the release.
///
/// `InModeration` and `PRPending` are hard blockers.
/// `Unknown` only blocks when `strict` is `true`.
pub fn is_blocker(&self, strict: bool) -> bool {
match self {
PublisherState::InModeration { .. } | PublisherState::PRPending(_) => true,
PublisherState::Unknown { .. } => strict,
_ => false,
}
}
/// A short human-readable label for table output.
pub fn label(&self) -> &'static str {
match self {
PublisherState::Clean => "clean",
PublisherState::Published => "published",
PublisherState::InModeration { .. } => "in-moderation",
PublisherState::PRPending(_) => "pr-pending",
PublisherState::Unknown { .. } => "unknown",
}
}
}
impl fmt::Display for PublisherState {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
PublisherState::Clean => write!(f, "clean"),
PublisherState::Published => write!(f, "already published (idempotent skip)"),
PublisherState::InModeration { reason } => {
write!(f, "in moderation queue: {} — BLOCKER", reason)
}
PublisherState::PRPending(url) => write!(f, "PR already open: {} — BLOCKER", url),
PublisherState::Unknown { reason } => write!(f, "unknown ({})", reason),
}
}
}
// ---------------------------------------------------------------------------
// PreflightEntry
// ---------------------------------------------------------------------------
/// One publisher's result in the preflight report.
#[derive(Debug, Clone)]
pub struct PreflightEntry {
/// Short publisher name for display (e.g. "cargo", "chocolatey").
pub publisher: String,
/// Crate / package name being checked.
pub package: String,
/// Version that was queried.
pub version: String,
/// Result of the state query.
pub state: PublisherState,
}
// ---------------------------------------------------------------------------
// PreflightReport
// ---------------------------------------------------------------------------
/// Aggregated results for all one-way-door publishers.
///
/// `entries` carries one row per checked publisher (cargo / chocolatey /
/// winget / aur). `warnings` and `blockers` are free-form, publisher-agnostic
/// messages produced by the release-resilience preflight extension: rollback
/// token scope checks and per-publisher `Publisher::preflight()` hook
/// results. The two channels are kept separate from `entries` so that
/// the existing one-way-door consumers (state-machine queries like
/// `has_blockers` / `clean_count`) stay focused on publisher state, while the
/// CLI's operator-facing output can still surface every warning and blocker
/// the preflight pipeline produced.
#[derive(Debug, Default)]
pub struct PreflightReport {
pub entries: Vec<PreflightEntry>,
/// Non-blocking concerns surfaced during preflight (missing rollback
/// scope in default mode, `Publisher::preflight()` returning Warning).
pub warnings: Vec<String>,
/// Hard blockers surfaced during preflight (missing rollback scope in
/// `--strict` mode, `Publisher::preflight()` returning Blocker).
pub blockers: Vec<String>,
}
impl PreflightReport {
pub fn new() -> Self {
Self::default()
}
pub fn push(&mut self, entry: PreflightEntry) {
self.entries.push(entry);
}
/// Entries whose state is `Clean`.
pub fn clean_count(&self) -> usize {
self.entries
.iter()
.filter(|e| e.state == PublisherState::Clean)
.count()
}
/// Whether any entry is a blocker given the strict flag.
pub fn has_blockers(&self, strict: bool) -> bool {
self.entries.iter().any(|e| e.state.is_blocker(strict))
}
/// Entries that are blockers.
pub fn blockers(&self, strict: bool) -> Vec<&PreflightEntry> {
self.entries
.iter()
.filter(|e| e.state.is_blocker(strict))
.collect()
}
}
impl fmt::Display for PreflightReport {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "Pre-flight publisher check:")?;
for entry in &self.entries {
writeln!(
f,
" [{:>14}] {} {}@{}",
entry.state.label(),
entry.publisher,
entry.package,
entry.version
)?;
// Print extra detail for states that carry context.
match &entry.state {
PublisherState::PRPending(url) => {
writeln!(f, " PR: {}", url)?;
}
PublisherState::Unknown { reason } | PublisherState::InModeration { reason } => {
writeln!(f, " reason: {}", reason)?;
}
_ => {}
}
}
// Surface free-form warnings/blockers from the resilience extension
// (rollback-scope checks + `Publisher::preflight()` results) so they
// flow through the same Display channel the CLI prints. Suppressed
// when both are empty to preserve the existing one-line-per-entry
// cadence for clean reports.
for w in &self.warnings {
writeln!(f, " [ warning] {}", w)?;
}
for b in &self.blockers {
writeln!(f, " [ blocker] {}", b)?;
}
Ok(())
}
}
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
#[cfg(test)]
mod tests {
use super::*;
fn entry(publisher: &str, state: PublisherState) -> PreflightEntry {
PreflightEntry {
publisher: publisher.to_string(),
package: "mypkg".to_string(),
version: "1.2.3".to_string(),
state,
}
}
#[test]
fn report_aggregation_four_publishers() {
// Mock 4 publishers, one in each non-trivial state, assert categorisation.
let mut report = PreflightReport::new();
report.push(entry("cargo", PublisherState::Clean));
report.push(entry(
"chocolatey",
PublisherState::InModeration {
reason: "package in moderation queue".into(),
},
));
report.push(entry(
"winget",
PublisherState::PRPending("https://github.com/microsoft/winget-pkgs/pull/123".into()),
));
report.push(entry(
"aur",
PublisherState::Unknown {
reason: "AUR RPC returned 503".into(),
},
));
// clean_count
assert_eq!(report.clean_count(), 1);
// non-strict: Unknown is not a blocker
assert!(report.has_blockers(false));
let blockers = report.blockers(false);
assert_eq!(blockers.len(), 2);
assert!(blockers.iter().any(|e| e.publisher == "chocolatey"));
assert!(blockers.iter().any(|e| e.publisher == "winget"));
// strict: Unknown also blocks
assert!(report.has_blockers(true));
let strict_blockers = report.blockers(true);
assert_eq!(strict_blockers.len(), 3);
}
#[test]
fn report_all_clean_no_blockers() {
let mut report = PreflightReport::new();
report.push(entry("cargo", PublisherState::Clean));
report.push(entry("aur", PublisherState::Clean));
assert!(!report.has_blockers(false));
assert!(!report.has_blockers(true));
assert_eq!(report.clean_count(), 2);
}
#[test]
fn published_is_not_blocker() {
let mut report = PreflightReport::new();
report.push(entry("cargo", PublisherState::Published));
assert!(!report.has_blockers(false));
assert!(!report.has_blockers(true));
}
#[test]
fn unknown_only_blocks_when_strict() {
let mut report = PreflightReport::new();
report.push(entry(
"aur",
PublisherState::Unknown {
reason: "timeout".into(),
},
));
assert!(!report.has_blockers(false));
assert!(report.has_blockers(true));
}
#[test]
fn display_includes_blocker_label() {
let mut report = PreflightReport::new();
report.push(entry(
"chocolatey",
PublisherState::InModeration {
reason: "package in moderation queue".into(),
},
));
let s = report.to_string();
assert!(s.contains("in-moderation"), "display: {s}");
assert!(s.contains("chocolatey"), "display: {s}");
}
}