Skip to main content

anodizer_core/
publisher.rs

1//! Publisher trait + preflight result type.
2//!
3//! Defines the polymorphic interface that every publisher (cargo, homebrew,
4//! scoop, chocolatey, nix, AUR, krew, winget, snapcraft, blob, release, ...)
5//! implements. Lives in `anodizer-core` rather than `stage-publish` so that
6//! `stage-blob`, `stage-release`, and `stage-snapcraft` can implement
7//! `Publisher` without taking a circular dependency on `stage-publish`.
8
9use crate::context::Context;
10use crate::{PublishEvidence, PublisherGroup};
11
12/// Outcome of a publisher's pre-flight self-check.
13///
14/// Each variant signals a different release-pipeline reaction:
15///
16/// * `Pass` — no concern detected; publishing may proceed.
17/// * `Warning(msg)` — surface the message to the operator (and review log)
18///   but do not block the publish. Use for soft signals like "remote
19///   already has a tag at this version but contents match".
20/// * `Blocker(msg)` — abort before the publish stage runs. Use for hard
21///   prerequisites the publisher knows it cannot satisfy at runtime, e.g.
22///   "homebrew tap repo not reachable", "winget-pkgs fork not configured".
23///
24/// Named `Pass` (not `Clean`) to avoid nominal collision with
25/// [`crate::preflight::PublisherState::Clean`], which describes the
26/// already-published state of a publisher rather than a self-check result.
27#[derive(Debug, Clone, PartialEq, Eq)]
28pub enum PreflightCheck {
29    /// Publisher's pre-flight checks completed with no concerns.
30    Pass,
31    /// Publisher detected a non-blocking concern; surface it but continue.
32    Warning(String),
33    /// Publisher detected a blocking concern; abort before the publish stage.
34    Blocker(String),
35}
36
37/// Publisher contract — one implementer per upstream registry / channel.
38///
39/// Required methods describe the publisher's identity, behavior, and how
40/// it participates in [`PublisherGroup`]-based scheduling:
41///
42/// * [`Publisher::name`] — stable identifier used in logs, evidence, and
43///   review findings (e.g. `"cargo"`, `"homebrew"`, `"winget"`).
44/// * [`Publisher::run`] — perform the actual publish and emit a
45///   [`PublishEvidence`] record describing what was sent upstream.
46/// * [`Publisher::group`] — which [`PublisherGroup`] this publisher belongs
47///   to; used by the publish stage to order and parallelize work.
48/// * [`Publisher::required`] — whether a failure in this publisher should
49///   fail the overall release.
50///
51/// Default-implemented hooks describe optional behavior:
52///
53/// * [`Publisher::rollback`] — best-effort undo of a successful publish.
54///   The default is a no-op so publishers that target irreversible
55///   registries (most of them) do not need to override.
56/// * [`Publisher::preflight`] — fast self-check executed before any
57///   publisher in the pipeline runs. Defaults to [`PreflightCheck::Pass`].
58/// * [`Publisher::rollback_scope_needed`] — declare an opt-in OAuth /
59///   token scope that rollback would require (e.g. `"delete_repo"` for
60///   GitHub-fork-based publishers). Defaults to `None`. Surfaced by
61///   the CLI when explaining why a rollback path is unavailable.
62///
63/// Implementations must be `Send + Sync` so the publish stage can fan out
64/// across publisher groups in parallel. Wrap non-`Send` clients (Rc-based,
65/// thread-local channels) behind an `Arc<Mutex<_>>` or move them inside
66/// `run()`'s scope rather than holding them on `self`.
67pub trait Publisher: Send + Sync {
68    /// Stable, lowercase identifier for this publisher (e.g. `"cargo"`).
69    fn name(&self) -> &str;
70
71    /// Execute the publish and emit evidence describing what was sent.
72    fn run(&self, ctx: &mut Context) -> anyhow::Result<PublishEvidence>;
73
74    /// Scheduling group — controls ordering and parallelism in the publish stage.
75    fn group(&self) -> PublisherGroup;
76
77    /// Whether a failure here should fail the overall release.
78    fn required(&self) -> bool;
79
80    /// Best-effort rollback of a successful publish, given its evidence.
81    ///
82    /// Default is a no-op: most upstream registries are append-only or
83    /// require human moderation to revoke, so the publisher opts in by
84    /// overriding only when it actually has a rollback path.
85    fn rollback(&self, _ctx: &mut Context, _evidence: &PublishEvidence) -> anyhow::Result<()> {
86        Ok(())
87    }
88
89    /// Fast self-check executed before any publisher runs.
90    ///
91    /// Default returns [`PreflightCheck::Pass`]. Override to surface
92    /// publisher-specific blockers (missing tap, missing fork, network
93    /// unreachable) or warnings (duplicate-but-matching upload).
94    fn preflight(&self, _ctx: &Context) -> anyhow::Result<PreflightCheck> {
95        Ok(PreflightCheck::Pass)
96    }
97
98    /// Opt-in OAuth / token scope rollback would require, if any.
99    ///
100    /// Default is `None`. Used by the CLI to explain why a `--rollback`
101    /// invocation cannot recover a given publisher without elevating the
102    /// release token's permissions.
103    fn rollback_scope_needed(&self) -> Option<&'static str> {
104        None
105    }
106}
107
108/// The exact warn message a publisher emits when `rollback()` is invoked
109/// with no evidence to act on (empty `artifact_paths`, no `primary_ref`).
110/// Each publisher's empty-evidence branch calls this helper; tests can
111/// assert on the returned string without having to intercept stderr
112/// (`eprintln!` cannot be portably captured from the same process).
113///
114/// Lives in `anodizer_core` because the rollback shape is shared across
115/// publishers spread between `stage-publish` and `stage-blob` (and any
116/// future stage crate that implements `Publisher`).
117pub fn rollback_empty_warning_msg(publisher: &str, target_label: &str) -> String {
118    format!(
119        "{}: no {} recorded in evidence; verify {} state manually",
120        publisher, target_label, publisher
121    )
122}
123
124#[cfg(test)]
125mod tests {
126    use super::*;
127
128    struct MinimalPublisher;
129    impl Publisher for MinimalPublisher {
130        fn name(&self) -> &str {
131            "minimal"
132        }
133        fn run(&self, _ctx: &mut Context) -> anyhow::Result<PublishEvidence> {
134            Ok(PublishEvidence::new("minimal"))
135        }
136        fn group(&self) -> PublisherGroup {
137            PublisherGroup::Manager
138        }
139        fn required(&self) -> bool {
140            false
141        }
142    }
143
144    #[test]
145    fn rollback_default_is_noop_ok() {
146        let p = MinimalPublisher;
147        let mut ctx = Context::test_fixture();
148        let evidence = PublishEvidence::new("minimal");
149        assert!(p.rollback(&mut ctx, &evidence).is_ok());
150    }
151
152    #[test]
153    fn preflight_default_is_pass() {
154        let p = MinimalPublisher;
155        let ctx = Context::test_fixture();
156        assert!(matches!(p.preflight(&ctx).unwrap(), PreflightCheck::Pass));
157    }
158
159    #[test]
160    fn rollback_scope_needed_default_is_none() {
161        let p = MinimalPublisher;
162        assert!(p.rollback_scope_needed().is_none());
163    }
164
165    #[test]
166    fn pending_outcome_round_trips_through_context() {
167        // The slot is single-shot: write once, drain once, then empty.
168        // Without single-shot semantics, a chocolatey moderation skip
169        // would bleed into the next publisher's row at dispatch time.
170        let mut ctx = Context::test_fixture();
171        assert!(ctx.take_pending_outcome().is_none());
172
173        ctx.record_publisher_outcome(crate::PublisherOutcome::PendingModeration);
174        assert!(matches!(
175            ctx.take_pending_outcome(),
176            Some(crate::PublisherOutcome::PendingModeration)
177        ));
178        assert!(
179            ctx.take_pending_outcome().is_none(),
180            "slot must be empty after take"
181        );
182
183        // Overwrite semantics: last writer wins (no implicit accumulation).
184        ctx.record_publisher_outcome(crate::PublisherOutcome::PendingModeration);
185        ctx.record_publisher_outcome(crate::PublisherOutcome::PendingValidation);
186        assert!(matches!(
187            ctx.take_pending_outcome(),
188            Some(crate::PublisherOutcome::PendingValidation)
189        ));
190    }
191}