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
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
use crate::doctor::checks::{
    DEFAULT_CONTEXT_NAME, resolve_active_provider_context, resolve_secret,
};
use crate::doctor::{CheckResult, CheckStatus, DiagnosticCheck, DiagnosticContext};
use async_trait::async_trait;
use devboy_core::ContextConfig;
use secrecy::ExposeSecret;
use serde_json::json;

pub struct GitHubTokenCheck;
pub struct GitLabTokenCheck;
pub struct ClickUpTokenCheck;
pub struct JiraTokenCheck;
pub struct ConfluenceTokenCheck;
pub struct SlackTokenCheck;

struct CredentialSpec {
    provider: &'static str,
    label: &'static str,
    help_key: &'static str,
    format_hint: &'static str,
    validator: fn(&str) -> TokenFormat,
}

enum TokenFormat {
    Recognized,
    Suspicious(&'static str),
}

impl CredentialSpec {
    fn build_result(
        &self,
        check: &dyn DiagnosticCheck,
        ctx: &DiagnosticContext,
        configured: bool,
    ) -> CheckResult {
        let Some(config) = &ctx.config else {
            return skipped(check, "Skipped because config could not be loaded");
        };

        let Some(active) = resolve_active_provider_context(config) else {
            return skipped(check, "Skipped because no active context could be resolved");
        };

        if !configured {
            return skipped(
                check,
                &format!(
                    "Skipped because {} is not configured in context '{}'",
                    self.label, active.name
                ),
            );
        }

        match resolve_secret(ctx, Some(&active.name), self.provider) {
            Ok(Some(secret)) => {
                let format = (self.validator)(secret.value.expose_secret());
                let status = match format {
                    TokenFormat::Recognized => CheckStatus::Pass,
                    TokenFormat::Suspicious(_) => CheckStatus::Warning,
                };
                let message = match format {
                    TokenFormat::Recognized => format!("{} token found", self.label),
                    TokenFormat::Suspicious(reason) => {
                        format!(
                            "{} token found but format looks unusual ({reason})",
                            self.label
                        )
                    }
                };

                CheckResult {
                    id: check.id().to_string(),
                    category: check.category().to_string(),
                    name: check.name().to_string(),
                    status,
                    message,
                    details: ctx.verbose.then(|| {
                        json!({
                            "provider": self.provider,
                            "context": active.name,
                            "token_key": secret.key,
                            "token_source": secret.source,
                            "format_hint": self.format_hint,
                        })
                    }),
                    fix_command: None,
                    fix_url: None,
                }
            }
            Ok(None) => {
                let secret_key = if active.name == DEFAULT_CONTEXT_NAME {
                    self.help_key.to_string()
                } else {
                    format!("contexts.{}.{}", active.name, self.help_key)
                };

                CheckResult {
                    id: check.id().to_string(),
                    category: check.category().to_string(),
                    name: check.name().to_string(),
                    status: CheckStatus::Error,
                    message: format!("{} token missing", self.label),
                    details: ctx.verbose.then(|| {
                        json!({
                            "provider": self.provider,
                            "context": active.name,
                            "expected_secret_key": secret_key,
                            "token_source_order": ["context", "global"],
                        })
                    }),
                    fix_command: Some(format!("devboy config set-secret {secret_key} <TOKEN>")),
                    fix_url: None,
                }
            }
            Err(error) => CheckResult {
                id: check.id().to_string(),
                category: check.category().to_string(),
                name: check.name().to_string(),
                status: CheckStatus::Error,
                message: format!("Could not read {} token: {error}", self.label),
                details: ctx.verbose.then(|| {
                    json!({
                        "provider": self.provider,
                        "context": active.name,
                        "error": error,
                    })
                }),
                fix_command: None,
                fix_url: None,
            },
        }
    }
}

fn skipped(check: &dyn DiagnosticCheck, message: &str) -> CheckResult {
    CheckResult {
        id: check.id().to_string(),
        category: check.category().to_string(),
        name: check.name().to_string(),
        status: CheckStatus::Skipped,
        message: message.to_string(),
        details: None,
        fix_command: None,
        fix_url: None,
    }
}

fn github_configured(context: &ContextConfig) -> bool {
    context.github.is_some()
}

fn gitlab_configured(context: &ContextConfig) -> bool {
    context.gitlab.is_some()
}

fn clickup_configured(context: &ContextConfig) -> bool {
    context.clickup.is_some()
}

fn jira_configured(context: &ContextConfig) -> bool {
    context.jira.is_some()
}

fn slack_configured(context: &ContextConfig) -> bool {
    context.slack.is_some()
}

fn confluence_configured(context: &ContextConfig) -> bool {
    context.confluence.is_some()
}

fn validate_github_token(token: &str) -> TokenFormat {
    let token = token.trim();
    if token.starts_with("ghp_")
        || token.starts_with("github_pat_")
        || token.starts_with("gho_")
        || token.starts_with("ghu_")
        || token.starts_with("ghs_")
        || token.starts_with("ghr_")
    {
        TokenFormat::Recognized
    } else if token.len() >= 20 {
        TokenFormat::Suspicious("expected a GitHub PAT prefix like ghp_ or github_pat_")
    } else {
        TokenFormat::Suspicious("token is shorter than a typical GitHub PAT")
    }
}

fn validate_gitlab_token(token: &str) -> TokenFormat {
    let token = token.trim();
    if token.starts_with("glpat-") {
        TokenFormat::Recognized
    } else if token.len() >= 20 {
        TokenFormat::Suspicious("expected a GitLab personal access token prefix like glpat-")
    } else {
        TokenFormat::Suspicious("token is shorter than a typical GitLab personal access token")
    }
}

fn validate_clickup_token(token: &str) -> TokenFormat {
    let token = token.trim();
    if token.len() >= 24 {
        TokenFormat::Recognized
    } else {
        TokenFormat::Suspicious("token is shorter than a typical ClickUp API token")
    }
}

fn validate_jira_token(token: &str) -> TokenFormat {
    let token = token.trim();
    if token.contains(':') || token.len() >= 16 {
        TokenFormat::Recognized
    } else {
        TokenFormat::Suspicious("token is shorter than a typical Jira API token or credential pair")
    }
}

fn validate_slack_token(token: &str) -> TokenFormat {
    let token = token.trim();
    if token.starts_with("xoxb-") {
        TokenFormat::Recognized
    } else if token.starts_with("xox") {
        TokenFormat::Suspicious("expected a Slack bot token prefix like xoxb-")
    } else if token.len() >= 20 {
        TokenFormat::Suspicious("token does not look like a Slack OAuth token")
    } else {
        TokenFormat::Suspicious("token is shorter than a typical Slack bot token")
    }
}

fn validate_confluence_credential(token: &str) -> TokenFormat {
    let token = token.trim();
    if token.len() >= 12 {
        TokenFormat::Recognized
    } else {
        TokenFormat::Suspicious("credential is shorter than a typical Confluence PAT or password")
    }
}

#[async_trait]
impl DiagnosticCheck for GitHubTokenCheck {
    fn id(&self) -> &'static str {
        "credentials.github"
    }

    fn name(&self) -> &'static str {
        "GitHub token available"
    }

    fn category(&self) -> &'static str {
        "Credentials"
    }

    async fn run(&self, ctx: &DiagnosticContext) -> CheckResult {
        let spec = CredentialSpec {
            provider: "github",
            label: "GitHub",
            help_key: "github.token",
            format_hint: "GitHub personal access token",
            validator: validate_github_token,
        };

        let configured = ctx
            .config
            .as_ref()
            .and_then(resolve_active_provider_context)
            .is_some_and(|active| github_configured(&active.config));

        spec.build_result(self, ctx, configured)
    }
}

#[async_trait]
impl DiagnosticCheck for GitLabTokenCheck {
    fn id(&self) -> &'static str {
        "credentials.gitlab"
    }

    fn name(&self) -> &'static str {
        "GitLab token available"
    }

    fn category(&self) -> &'static str {
        "Credentials"
    }

    async fn run(&self, ctx: &DiagnosticContext) -> CheckResult {
        let spec = CredentialSpec {
            provider: "gitlab",
            label: "GitLab",
            help_key: "gitlab.token",
            format_hint: "GitLab personal access token",
            validator: validate_gitlab_token,
        };

        let configured = ctx
            .config
            .as_ref()
            .and_then(resolve_active_provider_context)
            .is_some_and(|active| gitlab_configured(&active.config));

        spec.build_result(self, ctx, configured)
    }
}

#[async_trait]
impl DiagnosticCheck for ClickUpTokenCheck {
    fn id(&self) -> &'static str {
        "credentials.clickup"
    }

    fn name(&self) -> &'static str {
        "ClickUp token available"
    }

    fn category(&self) -> &'static str {
        "Credentials"
    }

    async fn run(&self, ctx: &DiagnosticContext) -> CheckResult {
        let spec = CredentialSpec {
            provider: "clickup",
            label: "ClickUp",
            help_key: "clickup.token",
            format_hint: "ClickUp API token",
            validator: validate_clickup_token,
        };

        let configured = ctx
            .config
            .as_ref()
            .and_then(resolve_active_provider_context)
            .is_some_and(|active| clickup_configured(&active.config));

        spec.build_result(self, ctx, configured)
    }
}

#[async_trait]
impl DiagnosticCheck for JiraTokenCheck {
    fn id(&self) -> &'static str {
        "credentials.jira"
    }

    fn name(&self) -> &'static str {
        "Jira token available"
    }

    fn category(&self) -> &'static str {
        "Credentials"
    }

    async fn run(&self, ctx: &DiagnosticContext) -> CheckResult {
        let spec = CredentialSpec {
            provider: "jira",
            label: "Jira",
            help_key: "jira.token",
            format_hint: "Jira API token or self-hosted user:password pair",
            validator: validate_jira_token,
        };

        let configured = ctx
            .config
            .as_ref()
            .and_then(resolve_active_provider_context)
            .is_some_and(|active| jira_configured(&active.config));

        spec.build_result(self, ctx, configured)
    }
}

#[async_trait]
impl DiagnosticCheck for SlackTokenCheck {
    fn id(&self) -> &'static str {
        "credentials.slack"
    }

    fn name(&self) -> &'static str {
        "Slack bot token available"
    }

    fn category(&self) -> &'static str {
        "Credentials"
    }

    async fn run(&self, ctx: &DiagnosticContext) -> CheckResult {
        let spec = CredentialSpec {
            provider: "slack",
            label: "Slack",
            help_key: "slack.token",
            format_hint: "Slack bot token (xoxb-...)",
            validator: validate_slack_token,
        };

        let configured = ctx
            .config
            .as_ref()
            .and_then(resolve_active_provider_context)
            .is_some_and(|active| slack_configured(&active.config));

        spec.build_result(self, ctx, configured)
    }
}

#[async_trait]
impl DiagnosticCheck for ConfluenceTokenCheck {
    fn id(&self) -> &'static str {
        "credentials.confluence"
    }

    fn name(&self) -> &'static str {
        "Confluence credential available"
    }

    fn category(&self) -> &'static str {
        "Credentials"
    }

    async fn run(&self, ctx: &DiagnosticContext) -> CheckResult {
        let spec = CredentialSpec {
            provider: "confluence",
            label: "Confluence",
            help_key: "confluence.token",
            format_hint: "Confluence personal access token or password",
            validator: validate_confluence_credential,
        };

        let configured = ctx
            .config
            .as_ref()
            .and_then(resolve_active_provider_context)
            .is_some_and(|active| confluence_configured(&active.config));

        spec.build_result(self, ctx, configured)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::doctor::DiagnosticContext;
    use devboy_core::{
        ClickUpConfig, Config, ConfluenceConfig, ContextConfig, Error, GitHubConfig, GitLabConfig,
        JiraConfig,
    };
    use devboy_storage::{CredentialStore, MemoryStore};
    use secrecy::SecretString;
    use std::collections::BTreeMap;
    use std::path::PathBuf;
    use std::sync::Arc;

    #[derive(Debug)]
    struct FailingStore;

    impl CredentialStore for FailingStore {
        fn store(&self, _key: &str, _value: &SecretString) -> devboy_core::Result<()> {
            Err(Error::Storage("store failed".to_string()))
        }

        fn get(&self, _key: &str) -> devboy_core::Result<Option<SecretString>> {
            Err(Error::Storage("credential backend unavailable".to_string()))
        }

        fn delete(&self, _key: &str) -> devboy_core::Result<()> {
            Err(Error::Storage("delete failed".to_string()))
        }
    }

    fn context_with_store(
        store: Arc<dyn CredentialStore>,
        context: ContextConfig,
    ) -> DiagnosticContext {
        let mut contexts = BTreeMap::new();
        contexts.insert("workspace".to_string(), context);

        DiagnosticContext {
            config: Some(Config {
                contexts,
                active_context: Some("workspace".to_string()),
                ..Default::default()
            }),
            config_path: Some(PathBuf::from("config.toml")),
            config_exists: true,
            config_source: "test",
            config_path_error: None,
            config_load_error: None,
            credential_store: store,
            verbose: true,
        }
    }

    fn github_context() -> ContextConfig {
        ContextConfig {
            github: Some(GitHubConfig {
                owner: "owner".to_string(),
                repo: "repo".to_string(),
                base_url: None,
            }),
            ..Default::default()
        }
    }

    fn confluence_context() -> ContextConfig {
        ContextConfig {
            confluence: Some(ConfluenceConfig {
                base_url: "https://wiki.example.com".to_string(),
                api_version: Some("v1".to_string()),
                username: Some("dev@example.com".to_string()),
                space_key: Some("ENG".to_string()),
            }),
            ..Default::default()
        }
    }

    #[tokio::test]
    async fn github_token_check_prefers_context_secret() {
        let store = Arc::new(MemoryStore::with_credentials([
            (
                "contexts.workspace.github.token".to_string(),
                "ghp_context_token_1234567890".to_string(),
            ),
            (
                "github.token".to_string(),
                "ghp_global_token_1234567890".to_string(),
            ),
        ]));
        let ctx = context_with_store(store, github_context());

        let result = GitHubTokenCheck.run(&ctx).await;

        assert_eq!(result.status, CheckStatus::Pass);
        let details = result.details.unwrap();
        assert_eq!(details["token_source"], "context");
        assert_eq!(details["token_key"], "contexts.workspace.github.token");
    }

    #[tokio::test]
    async fn github_token_check_errors_when_missing() {
        let ctx = context_with_store(Arc::new(MemoryStore::new()), github_context());

        let result = GitHubTokenCheck.run(&ctx).await;

        assert_eq!(result.status, CheckStatus::Error);
        assert_eq!(
            result.fix_command.as_deref(),
            Some("devboy config set-secret contexts.workspace.github.token <TOKEN>")
        );
    }

    #[tokio::test]
    async fn github_token_check_warns_for_suspicious_global_token() {
        let store = Arc::new(MemoryStore::with_credentials([(
            "github.token".to_string(),
            "token-without-known-prefix-but-long".to_string(),
        )]));
        let ctx = context_with_store(store, github_context());

        let result = GitHubTokenCheck.run(&ctx).await;

        assert_eq!(result.status, CheckStatus::Warning);
        assert_eq!(result.details.unwrap()["token_source"], "global");
    }

    #[tokio::test]
    async fn gitlab_token_check_passes_for_recognized_token() {
        let store = Arc::new(MemoryStore::with_credentials([(
            "contexts.workspace.gitlab.token".to_string(),
            "glpat-12345678901234567890".to_string(),
        )]));
        let ctx = context_with_store(
            store,
            ContextConfig {
                gitlab: Some(GitLabConfig {
                    url: "https://gitlab.example.com".to_string(),
                    project_id: "group/project".to_string(),
                }),
                ..Default::default()
            },
        );

        let result = GitLabTokenCheck.run(&ctx).await;

        assert_eq!(result.status, CheckStatus::Pass);
        assert_eq!(result.details.unwrap()["provider"], "gitlab");
    }

    #[tokio::test]
    async fn clickup_token_check_warns_for_short_token() {
        let store = Arc::new(MemoryStore::with_credentials([(
            "contexts.workspace.clickup.token".to_string(),
            "short-token".to_string(),
        )]));
        let ctx = context_with_store(
            store,
            ContextConfig {
                clickup: Some(ClickUpConfig {
                    list_id: "123".to_string(),
                    team_id: None,
                }),
                ..Default::default()
            },
        );

        let result = ClickUpTokenCheck.run(&ctx).await;

        assert_eq!(result.status, CheckStatus::Warning);
        assert!(result.message.contains("format looks unusual"));
    }

    #[tokio::test]
    async fn jira_token_check_passes_for_credential_pair() {
        let store = Arc::new(MemoryStore::with_credentials([(
            "contexts.workspace.jira.token".to_string(),
            "user:token".to_string(),
        )]));
        let ctx = context_with_store(
            store,
            ContextConfig {
                jira: Some(JiraConfig {
                    url: "https://jira.example.com".to_string(),
                    project_key: "PROJ".to_string(),
                    email: "jira@example.com".to_string(),
                }),
                ..Default::default()
            },
        );

        let result = JiraTokenCheck.run(&ctx).await;

        assert_eq!(result.status, CheckStatus::Pass);
        assert_eq!(result.details.unwrap()["provider"], "jira");
    }

    #[tokio::test]
    async fn provider_token_checks_skip_when_provider_not_configured() {
        let ctx = context_with_store(Arc::new(MemoryStore::new()), ContextConfig::default());

        let result = GitLabTokenCheck.run(&ctx).await;

        assert_eq!(result.status, CheckStatus::Skipped);
        assert!(result.message.contains("not configured"));
    }

    #[tokio::test]
    async fn provider_token_checks_skip_when_active_context_is_missing() {
        let ctx = DiagnosticContext {
            config: Some(Config::default()),
            config_path: Some(PathBuf::from("config.toml")),
            config_exists: true,
            config_source: "test",
            config_path_error: None,
            config_load_error: None,
            credential_store: Arc::new(MemoryStore::new()),
            verbose: true,
        };

        let result = GitHubTokenCheck.run(&ctx).await;

        assert_eq!(result.status, CheckStatus::Skipped);
        assert!(result.message.contains("no active context"));
    }

    #[tokio::test]
    async fn provider_token_checks_error_when_store_fails() {
        let ctx = context_with_store(Arc::new(FailingStore), github_context());

        let result = GitHubTokenCheck.run(&ctx).await;

        assert_eq!(result.status, CheckStatus::Error);
        assert!(result.message.contains("Could not read GitHub token"));
        assert_eq!(
            result.details.unwrap()["error"],
            "Storage error: credential backend unavailable"
        );
    }

    #[tokio::test]
    async fn confluence_token_check_passes_for_present_credential() {
        let store = Arc::new(MemoryStore::with_credentials([(
            "contexts.workspace.confluence.token".to_string(),
            "pat_confluence_secret".to_string(),
        )]));
        let ctx = context_with_store(store, confluence_context());

        let result = ConfluenceTokenCheck.run(&ctx).await;

        assert_eq!(result.status, CheckStatus::Pass);
        assert_eq!(result.details.unwrap()["provider"], "confluence");
    }

    #[tokio::test]
    async fn confluence_token_check_skips_when_not_configured() {
        let ctx = context_with_store(Arc::new(MemoryStore::new()), ContextConfig::default());

        let result = ConfluenceTokenCheck.run(&ctx).await;

        assert_eq!(result.status, CheckStatus::Skipped);
        assert!(result.message.contains("not configured"));
    }

    #[test]
    fn confluence_credential_validator_flags_short_values() {
        assert!(matches!(
            validate_confluence_credential("short"),
            TokenFormat::Suspicious(_)
        ));
        assert!(matches!(
            validate_confluence_credential("pat_confluence_secret"),
            TokenFormat::Recognized
        ));
    }

    #[test]
    fn token_format_validators_detect_suspicious_values() {
        assert!(matches!(
            validate_github_token("short"),
            TokenFormat::Suspicious(_)
        ));
        assert!(matches!(
            validate_gitlab_token("token-without-prefix-but-long-enough"),
            TokenFormat::Suspicious(_)
        ));
        assert!(matches!(
            validate_clickup_token("123"),
            TokenFormat::Suspicious(_)
        ));
        assert!(matches!(
            validate_jira_token("token"),
            TokenFormat::Suspicious(_)
        ));
        assert!(matches!(
            validate_github_token("ghp_12345678901234567890"),
            TokenFormat::Recognized
        ));
        assert!(matches!(
            validate_gitlab_token("glpat-12345678901234567890"),
            TokenFormat::Recognized
        ));
        assert!(matches!(
            validate_clickup_token("123456789012345678901234"),
            TokenFormat::Recognized
        ));
        assert!(matches!(
            validate_jira_token("1234567890123456"),
            TokenFormat::Recognized
        ));
    }
}