nexo-plugin-discovery 0.1.0

Plugin catalogue discovery — fetches public plugin metadata from crates.io / GitHub topic / curated index (Phase 98).
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
//! crates.io REST source.
//!
//! Hits `GET <endpoint>/api/v1/crates?q=<q>&per_page=100&page=N`
//! for each of two queries (`nexo-plugin` + `nexo-poller`),
//! follows pagination until exhausted, filters out crates without a
//! `max_stable_version` (means every version is yanked), and
//! constructs one `DiscoveredPlugin` per entry.

use std::time::Duration;

use serde::Deserialize;
use tracing::warn;

use crate::sources::{source_error, Source};
use crate::types::{
    CompatStatus, DiscoveredPlugin, PluginCategory, PluginSource, SourceError, TrustTier,
};
use nexo_tool_meta::admin::plugin_install::{InstallSource, PluginsInstallParams};

use async_trait::async_trait;

/// Source name for `partial_failures` + telemetry.
pub const SOURCE_NAME: &str = "crates_io";

/// Hard cap on pages walked per query — defensive bound in case
/// crates.io ever returns inconsistent pagination metadata. With
/// `per_page = 100` that's 1000 results per query, comfortably
/// above the realistic catalogue size for years.
const MAX_PAGES: u32 = 10;

/// Per-query keyword. The discovery design uses two — `nexo-plugin`
/// covers channel/tool plugins, `nexo-poller` covers the poller-v2
/// subprocess flavor (Phase 96). Future shapes can be added without
/// touching the source layer.
const QUERIES: &[&str] = &["nexo-plugin", "nexo-poller"];

/// crates.io REST source. Construct once per discovery run +
/// reused across both queries.
pub struct CratesIoSource {
    http: reqwest::Client,
    endpoint: String,
}

impl CratesIoSource {
    /// Build a fresh source. Pass the configured endpoint
    /// (production = `https://crates.io`, tests = wiremock URL).
    /// `http_timeout` flows from `DiscoveryConfig.http_timeout`.
    pub fn new(endpoint: impl Into<String>, http_timeout: Duration) -> Self {
        let http = reqwest::Client::builder()
            .user_agent(format!(
                "nexo-plugin-discovery/{} (+https://github.com/lordmacu/nexo-rs)",
                env!("CARGO_PKG_VERSION")
            ))
            .timeout(http_timeout)
            .build()
            // `reqwest::Client::builder().build()` only fails when
            // the underlying TLS impl can't initialise — pure-rustls
            // never does, so this unwrap is documented-safe.
            .expect("reqwest client build (rustls) failed");
        Self {
            http,
            endpoint: endpoint.into(),
        }
    }
}

#[async_trait]
impl Source for CratesIoSource {
    fn name(&self) -> &'static str {
        SOURCE_NAME
    }

    async fn fetch(&self) -> Result<Vec<DiscoveredPlugin>, SourceError> {
        let mut acc: Vec<DiscoveredPlugin> = Vec::new();
        for query in QUERIES {
            let mut page: u32 = 1;
            loop {
                let url = format!(
                    "{}/api/v1/crates?q={}&per_page=100&page={}",
                    self.endpoint.trim_end_matches('/'),
                    query,
                    page
                );
                let resp = self
                    .http
                    .get(&url)
                    .send()
                    .await
                    .map_err(|e| source_error(SOURCE_NAME, format!("GET {url}: {e}")))?;
                if !resp.status().is_success() {
                    return Err(source_error(
                        SOURCE_NAME,
                        format!("GET {url}: status {}", resp.status()),
                    ));
                }
                let parsed: CratesIoSearchPage = resp
                    .json()
                    .await
                    .map_err(|e| source_error(SOURCE_NAME, format!("parse {url}: {e}")))?;
                let page_len = parsed.crates.len();
                for raw in parsed.crates.into_iter() {
                    if let Some(plugin) = map_crate(raw) {
                        acc.push(plugin);
                    }
                }
                if page_len < 100 || page >= MAX_PAGES {
                    break;
                }
                page += 1;
            }
        }
        Ok(acc)
    }
}

// ── wire shapes (private — not part of the public API) ───────────

#[derive(Debug, Deserialize)]
struct CratesIoSearchPage {
    crates: Vec<CratesIoCrate>,
}

#[derive(Debug, Deserialize)]
struct CratesIoCrate {
    name: String,
    #[serde(default)]
    description: Option<String>,
    #[serde(default)]
    repository: Option<String>,
    #[serde(default)]
    homepage: Option<String>,
    #[serde(default)]
    max_stable_version: Option<String>,
    #[serde(default)]
    keywords: Option<Vec<String>>,
}

/// Map a crates.io result to `DiscoveredPlugin`. Returns `None`
/// when the crate has no non-yanked version (all yanked → omit
/// from catalogue rather than expose an un-installable entry).
fn map_crate(raw: CratesIoCrate) -> Option<DiscoveredPlugin> {
    let CratesIoCrate {
        name,
        description,
        repository,
        homepage,
        max_stable_version,
        keywords,
    } = raw;
    let Some(version) = max_stable_version else {
        warn!(
            target: "plugin_discovery::crates_io",
            crate_name = %name,
            "skipping — every version yanked (no max_stable_version)"
        );
        return None;
    };
    let owner = owner_from_repo(repository.as_deref()).unwrap_or_else(|| "unknown".to_string());
    let install_params = PluginsInstallParams {
        crate_name: name.clone(),
        version: Some(version.clone()),
        repo: repo_slug(repository.as_deref()),
        source: InstallSource::Release,
        force: false,
        require_signature: false,
        skip_signature_verify: false,
    };
    let install_cmd = format!("cargo install {name} --version {version}");
    Some(DiscoveredPlugin {
        name,
        version: Some(version),
        description,
        owner,
        sources: vec![PluginSource::CratesIo],
        repo_url: repository,
        homepage,
        tags: keywords.unwrap_or_default(),
        // Categories are derived from manifest sections in 98.8 —
        // the source layer can't see the TOML yet. Leave as
        // Unknown; merge step overwrites once the manifest fetch
        // completes.
        category: PluginCategory::Unknown,
        // Trust tier resolved at merge time too (needs the
        // `official_owners` allowlist + the curated-index name
        // set; both live above the source layer).
        trust_tier: TrustTier::Unverified,
        // Compat check needs the manifest; defer to merge.
        compat: CompatStatus::Unknown,
        manifest_url: None,
        install_cmd,
        install_params,
    })
}

/// Pull the GitHub org from a repo URL. crates.io stores user-
/// supplied URLs verbatim so we accept the common shapes:
///   - `https://github.com/<org>/<name>` (with or without `.git`)
///   - `https://github.com/<org>/<name>/tree/main`
/// Anything else → `None`.
fn owner_from_repo(url: Option<&str>) -> Option<String> {
    let u = url?;
    let prefix = "https://github.com/";
    let rest = u.strip_prefix(prefix)?;
    let org = rest.split('/').next()?;
    if org.is_empty() {
        None
    } else {
        Some(org.to_string())
    }
}

/// Build the `<org>/<name>` slug consumed by
/// `PluginsInstallParams.repo`. Returns `None` when we can't tell
/// — the install handler then falls back to its
/// `lordmacu/<crate>` default.
fn repo_slug(url: Option<&str>) -> Option<String> {
    let u = url?;
    let prefix = "https://github.com/";
    let rest = u.strip_prefix(prefix)?;
    let mut parts = rest.split('/');
    let org = parts.next()?;
    let name = parts.next()?;
    if org.is_empty() || name.is_empty() {
        return None;
    }
    // Trim `.git` suffix or any path fragment.
    let name = name.trim_end_matches(".git");
    Some(format!("{org}/{name}"))
}

#[cfg(test)]
mod tests {
    use super::*;
    use wiremock::matchers::{method, path, query_param};
    use wiremock::{Mock, MockServer, ResponseTemplate};

    fn page_body(items: &[(&str, &str, &str)]) -> serde_json::Value {
        // (name, version, repo)
        let crates: Vec<_> = items
            .iter()
            .map(|(name, version, repo)| {
                serde_json::json!({
                    "name": name,
                    "description": "demo",
                    "repository": repo,
                    "homepage": null,
                    "max_stable_version": version,
                    "keywords": ["nexo", "demo"],
                })
            })
            .collect();
        serde_json::json!({ "crates": crates })
    }

    /// Build a page response of EXACTLY 100 entries so the
    /// pagination loop continues to the next page.
    fn full_page_body(prefix: &str, version: &str, repo: &str) -> serde_json::Value {
        let crates: Vec<_> = (0..100)
            .map(|i| {
                serde_json::json!({
                    "name": format!("{prefix}-{i}"),
                    "description": "demo",
                    "repository": repo,
                    "homepage": null,
                    "max_stable_version": version,
                    "keywords": [],
                })
            })
            .collect();
        serde_json::json!({ "crates": crates })
    }

    #[tokio::test]
    async fn happy_path_single_page_returns_items() {
        let server = MockServer::start().await;
        let body = page_body(&[
            (
                "nexo-plugin-telegram",
                "0.3.0",
                "https://github.com/lordmacu/nexo-rs-plugin-telegram",
            ),
            (
                "nexo-plugin-whatsapp",
                "0.1.3",
                "https://github.com/lordmacu/nexo-rs-plugin-whatsapp",
            ),
        ]);
        // Both queries return the same 2 entries; the merge step
        // dedups by name (verified in 98.8). Source layer just
        // emits whatever crates.io says.
        Mock::given(method("GET"))
            .and(path("/api/v1/crates"))
            .respond_with(ResponseTemplate::new(200).set_body_json(body))
            .mount(&server)
            .await;
        let src = CratesIoSource::new(server.uri(), Duration::from_secs(5));
        let items = src.fetch().await.expect("fetch ok");
        // 2 entries × 2 queries (no dedup at source layer).
        assert_eq!(items.len(), 4);
        let tele = items
            .iter()
            .find(|p| p.name == "nexo-plugin-telegram")
            .expect("telegram present");
        assert_eq!(tele.owner, "lordmacu");
        assert_eq!(tele.version.as_deref(), Some("0.3.0"));
        assert_eq!(tele.sources, vec![PluginSource::CratesIo]);
        assert_eq!(
            tele.install_cmd,
            "cargo install nexo-plugin-telegram --version 0.3.0"
        );
        // `install_params.repo` must be the slug, not the full URL.
        assert_eq!(
            tele.install_params.repo.as_deref(),
            Some("lordmacu/nexo-rs-plugin-telegram")
        );
    }

    #[tokio::test]
    async fn yanked_only_crate_is_filtered_out() {
        let server = MockServer::start().await;
        // Crate with `max_stable_version: null` → every version
        // yanked. Must NOT appear in results.
        let body = serde_json::json!({
            "crates": [
                {
                    "name": "nexo-plugin-broken",
                    "description": "all yanked",
                    "repository": "https://github.com/lordmacu/broken",
                    "max_stable_version": serde_json::Value::Null,
                    "keywords": []
                }
            ]
        });
        Mock::given(method("GET"))
            .and(path("/api/v1/crates"))
            .respond_with(ResponseTemplate::new(200).set_body_json(body))
            .mount(&server)
            .await;
        let src = CratesIoSource::new(server.uri(), Duration::from_secs(5));
        let items = src.fetch().await.expect("fetch ok");
        assert!(
            items.is_empty(),
            "yanked-only crate must be filtered out, got {items:?}"
        );
    }

    #[tokio::test]
    async fn paginates_until_short_page() {
        let server = MockServer::start().await;
        // Page 1 full (100 entries) → pagination MUST request
        // page 2; page 2 short → loop exits. Verified per query —
        // each query independently paginates.
        Mock::given(method("GET"))
            .and(path("/api/v1/crates"))
            .and(query_param("page", "1"))
            .respond_with(ResponseTemplate::new(200).set_body_json(full_page_body(
                "nexo-plugin",
                "0.1.0",
                "https://github.com/x/y",
            )))
            .mount(&server)
            .await;
        Mock::given(method("GET"))
            .and(path("/api/v1/crates"))
            .and(query_param("page", "2"))
            .respond_with(ResponseTemplate::new(200).set_body_json(page_body(&[(
                "nexo-plugin-final",
                "0.2.0",
                "https://github.com/x/y",
            )])))
            .mount(&server)
            .await;
        let src = CratesIoSource::new(server.uri(), Duration::from_secs(5));
        let items = src.fetch().await.expect("fetch ok");
        // 100 (page 1) + 1 (page 2) = 101 entries per query × 2
        // queries = 202 total. The "nexo-plugin-final" name appears
        // once per query (so twice total).
        assert_eq!(items.len(), 202);
        assert!(items.iter().any(|p| p.name == "nexo-plugin-final"));
    }

    #[tokio::test]
    async fn http_500_surfaces_as_source_error() {
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/api/v1/crates"))
            .respond_with(ResponseTemplate::new(500))
            .mount(&server)
            .await;
        let src = CratesIoSource::new(server.uri(), Duration::from_secs(5));
        let err = src.fetch().await.expect_err("5xx must surface");
        assert_eq!(err.source, SOURCE_NAME);
        assert!(err.message.contains("status 500"), "{}", err.message);
    }

    #[test]
    fn owner_from_repo_handles_common_shapes() {
        assert_eq!(
            owner_from_repo(Some("https://github.com/lordmacu/foo")),
            Some("lordmacu".into())
        );
        assert_eq!(
            owner_from_repo(Some("https://github.com/lordmacu/foo.git")),
            Some("lordmacu".into())
        );
        assert_eq!(
            owner_from_repo(Some("https://github.com/lordmacu/foo/tree/main")),
            Some("lordmacu".into())
        );
        assert_eq!(owner_from_repo(Some("https://gitlab.com/foo/bar")), None);
        assert_eq!(owner_from_repo(None), None);
    }

    #[test]
    fn repo_slug_trims_git_suffix() {
        assert_eq!(
            repo_slug(Some("https://github.com/lordmacu/foo.git")).as_deref(),
            Some("lordmacu/foo")
        );
        assert_eq!(
            repo_slug(Some("https://github.com/lordmacu/foo/tree/main")).as_deref(),
            Some("lordmacu/foo")
        );
        assert!(repo_slug(Some("https://github.com/")).is_none());
        assert!(repo_slug(None).is_none());
    }
}