devboy-cli 0.28.0

Command-line interface for devboy-tools — `devboy` binary. Primary distribution is npm (@devboy-tools/cli); `cargo install devboy-cli` is the secondary channel.
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
428
429
430
431
432
433
434
435
436
437
mod checks;
mod output;

use self::checks::config::{ActiveContextCheck, ConfigExistsCheck, ConfigValidTomlCheck};
use self::checks::credentials::{
    ClickUpTokenCheck, ConfluenceTokenCheck, GitHubTokenCheck, GitLabTokenCheck, JiraTokenCheck,
    SlackTokenCheck,
};
use self::checks::environment::{ConfigDirCheck, CredentialStoreCheck, OsSupportCheck};
use self::checks::mcp::McpToolsCheck;
use self::checks::providers::{
    ClickUpApiCheck, ConfluenceApiCheck, GitHubApiCheck, GitLabApiCheck, JiraApiCheck,
    SlackApiCheck,
};
use self::checks::proxy::ProxyServersCheck;
use self::output::console::{print_check_list, print_report, summarize};
use self::output::json::print_json_report;
use crate::get_credential_store;
use crate::update_check::resolve_version_status;
use anyhow::Result;
use async_trait::async_trait;
use devboy_core::Config;
use devboy_storage::CredentialStore;
use serde::Serialize;
use serde_json::Value;
use std::collections::BTreeSet;
use std::path::PathBuf;
use std::sync::Arc;

#[derive(Debug, Clone, Serialize)]
pub struct CheckResult {
    pub id: String,
    pub category: String,
    pub name: String,
    pub status: CheckStatus,
    pub message: String,
    pub details: Option<Value>,
    pub fix_command: Option<String>,
    pub fix_url: Option<String>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum CheckStatus {
    Pass,
    Warning,
    Error,
    Skipped,
}

#[derive(Debug, Clone, Serialize)]
pub struct CheckSummary {
    pub passed: usize,
    pub warnings: usize,
    pub errors: usize,
    pub skipped: usize,
}

#[derive(Debug, Clone, Serialize)]
pub struct CheckDescriptor {
    pub id: String,
    pub category: String,
    pub name: String,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OutputFormat {
    Console,
    Json,
}

#[derive(Debug, Clone, Default)]
pub struct DoctorOptions {
    pub verbose: bool,
    pub output_format: Option<OutputFormat>,
    pub list_checks: bool,
    pub checks: Vec<String>,
}

#[async_trait]
pub trait DiagnosticCheck: Send + Sync {
    fn id(&self) -> &'static str;
    fn name(&self) -> &'static str;
    fn category(&self) -> &'static str;
    async fn run(&self, ctx: &DiagnosticContext) -> CheckResult;
}

pub struct DiagnosticContext {
    pub config: Option<Config>,
    pub config_path: Option<PathBuf>,
    pub config_exists: bool,
    pub config_source: &'static str,
    pub config_path_error: Option<String>,
    pub config_load_error: Option<String>,
    pub credential_store: Arc<dyn CredentialStore>,
    pub verbose: bool,
}

impl DiagnosticContext {
    pub fn load(verbose: bool) -> Self {
        let local_path = PathBuf::from(".devboy.toml");
        let mut config_source = "global";
        let (config_path, config_path_error) = if local_path.exists() {
            config_source = "local";
            (Some(local_path), None)
        } else {
            match Config::config_path() {
                Ok(path) => (Some(path), None),
                Err(error) => (None, Some(error.to_string())),
            }
        };

        let config_exists = config_path.as_ref().is_some_and(|path| path.exists());
        let (config, config_load_error) = if config_exists {
            match std::fs::read_to_string(config_path.as_ref().unwrap()) {
                Ok(contents) => match toml::from_str::<Config>(&contents) {
                    Ok(config) => (Some(config), None),
                    Err(error) => (None, Some(format!("Failed to parse config file: {error}"))),
                },
                Err(error) => (None, Some(format!("Failed to read config file: {error}"))),
            }
        } else {
            (None, None)
        };

        Self {
            config,
            config_path,
            config_exists,
            config_source,
            config_path_error,
            config_load_error,
            credential_store: Arc::from(get_credential_store()),
            verbose,
        }
    }
}

pub struct CheckRegistry {
    checks: Vec<Box<dyn DiagnosticCheck>>,
}

impl CheckRegistry {
    pub fn new() -> Self {
        let mut registry = Self { checks: Vec::new() };
        registry.register(Box::new(OsSupportCheck));
        registry.register(Box::new(ConfigDirCheck));
        registry.register(Box::new(CredentialStoreCheck));
        registry.register(Box::new(ConfigExistsCheck));
        registry.register(Box::new(ConfigValidTomlCheck));
        registry.register(Box::new(ActiveContextCheck));
        registry.register(Box::new(GitHubTokenCheck));
        registry.register(Box::new(GitLabTokenCheck));
        registry.register(Box::new(ClickUpTokenCheck));
        registry.register(Box::new(JiraTokenCheck));
        registry.register(Box::new(ConfluenceTokenCheck));
        registry.register(Box::new(SlackTokenCheck));
        registry.register(Box::new(GitHubApiCheck));
        registry.register(Box::new(GitLabApiCheck));
        registry.register(Box::new(ClickUpApiCheck));
        registry.register(Box::new(JiraApiCheck));
        registry.register(Box::new(ConfluenceApiCheck));
        registry.register(Box::new(SlackApiCheck));
        registry.register(Box::new(McpToolsCheck));
        registry.register(Box::new(ProxyServersCheck));
        registry
    }

    fn register(&mut self, check: Box<dyn DiagnosticCheck>) {
        self.checks.push(check);
    }

    pub fn list(&self) -> Vec<CheckDescriptor> {
        self.checks
            .iter()
            .map(|check| CheckDescriptor {
                id: check.id().to_string(),
                category: check.category().to_string(),
                name: check.name().to_string(),
            })
            .collect()
    }

    pub fn validate_filter<'a>(&self, requested: impl IntoIterator<Item = &'a str>) -> Vec<String> {
        let available: BTreeSet<_> = self.checks.iter().map(|check| check.id()).collect();
        requested
            .into_iter()
            .filter(|id| !available.contains(*id))
            .map(ToString::to_string)
            .collect()
    }

    pub async fn run_filtered(
        &self,
        ctx: &DiagnosticContext,
        selected_ids: &[String],
    ) -> Vec<CheckResult> {
        if selected_ids.is_empty() {
            return self.run_all(ctx).await;
        }

        let selected: BTreeSet<_> = selected_ids.iter().map(String::as_str).collect();
        let mut results = Vec::with_capacity(selected.len());
        for check in &self.checks {
            if selected.contains(check.id()) {
                results.push(check.run(ctx).await);
            }
        }
        results
    }

    pub async fn run_all(&self, ctx: &DiagnosticContext) -> Vec<CheckResult> {
        let mut results = Vec::with_capacity(self.checks.len());
        for check in &self.checks {
            results.push(check.run(ctx).await);
        }
        results
    }
}

pub fn summarize_results(results: &[CheckResult]) -> CheckSummary {
    let summary = summarize(results);
    CheckSummary {
        passed: summary.passed,
        warnings: summary.warnings,
        errors: summary.errors,
        skipped: summary.skipped,
    }
}

pub fn exit_code_for_summary(summary: &CheckSummary) -> i32 {
    if summary.errors > 0 {
        2
    } else if summary.warnings > 0 {
        1
    } else {
        0
    }
}

pub async fn handle_doctor_command(options: DoctorOptions) -> Result<i32> {
    let registry = CheckRegistry::new();

    if options.list_checks {
        let checks = registry.list();
        if matches!(options.output_format, Some(OutputFormat::Json)) {
            print_json_report(&checks)?;
        } else {
            print_check_list(&checks);
        }
        return Ok(0);
    }

    let unknown_checks = registry.validate_filter(options.checks.iter().map(String::as_str));
    if !unknown_checks.is_empty() {
        anyhow::bail!(
            "Unknown doctor check(s): {}. Use `devboy doctor --list-checks` to see available IDs.",
            unknown_checks.join(", ")
        );
    }

    let ctx = DiagnosticContext::load(options.verbose);
    let results = registry.run_filtered(&ctx, &options.checks).await;
    let summary = summarize_results(&results);
    let version = resolve_version_status().await;

    if matches!(options.output_format, Some(OutputFormat::Json)) {
        print_json_report(
            &(serde_json::json!({
                "version": version,
                "results": results,
                "summary": summary,
            })),
        )?;
    } else {
        print_report(&version, &results, options.verbose);
    }

    Ok(exit_code_for_summary(&summary))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::doctor::checks::config::{
        ActiveContextCheck, ConfigExistsCheck, ConfigValidTomlCheck,
    };
    use crate::doctor::checks::environment::CredentialStoreCheck;
    use devboy_core::{ContextConfig, GitHubConfig};
    use devboy_storage::MemoryStore;
    use std::collections::BTreeMap;
    use tempfile::tempdir;

    fn test_context(
        config: Option<Config>,
        config_path: Option<PathBuf>,
        config_exists: bool,
        config_load_error: Option<&str>,
    ) -> DiagnosticContext {
        DiagnosticContext {
            config,
            config_path,
            config_exists,
            config_source: "test",
            config_path_error: None,
            config_load_error: config_load_error.map(ToString::to_string),
            credential_store: Arc::new(MemoryStore::new()),
            verbose: false,
        }
    }

    #[tokio::test]
    async fn config_exists_check_warns_when_missing() {
        let ctx = test_context(None, Some(PathBuf::from("missing.toml")), false, None);
        let result = ConfigExistsCheck.run(&ctx).await;
        assert_eq!(result.status, CheckStatus::Warning);
        assert_eq!(result.fix_command.as_deref(), Some("devboy init"));
    }

    #[tokio::test]
    async fn config_valid_toml_check_errors_on_parse_failure() {
        let ctx = test_context(
            None,
            Some(PathBuf::from("bad.toml")),
            true,
            Some("Failed to parse config file"),
        );
        let result = ConfigValidTomlCheck.run(&ctx).await;
        assert_eq!(result.status, CheckStatus::Error);
        assert!(result.message.contains("invalid"));
    }

    #[tokio::test]
    async fn active_context_check_passes_when_context_resolves() {
        let mut contexts = BTreeMap::new();
        contexts.insert(
            "workspace".to_string(),
            ContextConfig {
                github: Some(GitHubConfig {
                    owner: "owner".to_string(),
                    repo: "repo".to_string(),
                    base_url: None,
                }),
                ..Default::default()
            },
        );

        let config = Config {
            contexts,
            active_context: Some("workspace".to_string()),
            ..Default::default()
        };

        let ctx = test_context(Some(config), Some(PathBuf::from("config.toml")), true, None);
        let result = ActiveContextCheck.run(&ctx).await;
        assert_eq!(result.status, CheckStatus::Pass);
        assert!(result.message.contains("workspace"));
    }

    #[tokio::test]
    async fn credential_store_check_uses_store_probe() {
        let ctx = test_context(None, None, false, None);
        let result = CredentialStoreCheck.run(&ctx).await;
        assert_eq!(result.status, CheckStatus::Pass);
    }

    #[test]
    fn exit_code_prefers_errors_over_warnings() {
        let summary = CheckSummary {
            passed: 1,
            warnings: 2,
            errors: 1,
            skipped: 0,
        };

        assert_eq!(exit_code_for_summary(&summary), 2);
    }

    #[test]
    fn exit_code_returns_warning_when_no_errors_exist() {
        let summary = CheckSummary {
            passed: 3,
            warnings: 1,
            errors: 0,
            skipped: 0,
        };

        assert_eq!(exit_code_for_summary(&summary), 1);
    }

    #[test]
    fn registry_can_list_and_validate_checks() {
        let registry = CheckRegistry::new();
        let checks = registry.list();

        assert!(!checks.is_empty());
        assert!(checks.iter().any(|check| check.id == "config.exists"));
        assert_eq!(
            registry.validate_filter(["config.exists", "missing.check"]),
            vec!["missing.check".to_string()]
        );
    }

    #[tokio::test]
    async fn registry_runs_only_selected_checks() {
        let registry = CheckRegistry::new();
        let ctx = test_context(None, Some(PathBuf::from("missing.toml")), false, None);

        let results = registry
            .run_filtered(&ctx, &["config.exists".to_string()])
            .await;

        assert_eq!(results.len(), 1);
        assert_eq!(results[0].id, "config.exists");
    }

    #[test]
    fn diagnostic_context_prefers_local_config() {
        let dir = tempdir().unwrap();
        std::fs::write(
            dir.path().join(".devboy.toml"),
            "[github]\nowner='o'\nrepo='r'\n",
        )
        .unwrap();

        let previous = std::env::current_dir().unwrap();
        std::env::set_current_dir(dir.path()).unwrap();

        let ctx = DiagnosticContext::load(false);

        std::env::set_current_dir(previous).unwrap();

        assert_eq!(ctx.config_source, "local");
        assert!(ctx.config_exists);
        assert!(ctx.config.is_some());
    }
}