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
//! Integration tests for GitHub provider.
//!
//! These tests implement the Record & Replay pattern from ADR-003:
//! - With GITHUB_TOKEN: calls real API and updates fixtures
//! - Without GITHUB_TOKEN: uses saved fixtures
//!
//! # Running Tests
//!
//! ```bash
//! # Replay mode (no token needed, can run in parallel)
//! cargo test --test github_test
//!
//! # Record mode (updates fixtures, must run sequentially)
//! GITHUB_TOKEN=your_token GITHUB_OWNER=owner GITHUB_REPO=repo \
//!     cargo test --test github_test -- --test-threads=1
//! ```
//!
//! Note: Record mode requires `--test-threads=1` because some tests temporarily
//! modify environment variables to test Replay mode, which can cause race
//! conditions when running in parallel.

mod common;

use common::TestProvider;
use devboy_core::{
    CreateCommentInput, CreateIssueInput, IssueFilter, IssueProvider, MergeRequestProvider,
    MrFilter, Provider, UpdateIssueInput,
};

/// Test that we can detect the correct test mode.
#[tokio::test]
async fn test_mode_detection() {
    let provider = TestProvider::github();

    // Mode should be Replay unless GITHUB_TOKEN is set
    if std::env::var("GITHUB_TOKEN").is_ok() {
        assert!(
            provider.mode().is_record(),
            "Expected Record mode with token"
        );
    } else {
        assert!(
            provider.mode().is_replay(),
            "Expected Replay mode without token"
        );
    }
}

/// Test getting issues from GitHub.
#[tokio::test]
async fn test_get_issues() {
    let provider = TestProvider::github();

    let issues = provider
        .get_issues(IssueFilter::default())
        .await
        .unwrap()
        .items;

    assert!(!issues.is_empty(), "Should have at least one issue");

    // Verify issue structure
    let issue = &issues[0];
    assert!(
        issue.key.starts_with("gh#"),
        "Issue key should start with gh#"
    );
    assert!(!issue.title.is_empty(), "Issue should have a title");
    assert_eq!(issue.source, "github", "Source should be github");
}

/// Test getting issues with state filter.
#[tokio::test]
async fn test_get_issues_with_filter() {
    let provider = TestProvider::github();

    let filter = IssueFilter {
        state: Some("open".to_string()),
        limit: Some(5),
        ..Default::default()
    };

    let issues = provider.get_issues(filter).await.unwrap().items;

    // All issues should be open
    for issue in &issues {
        assert_eq!(issue.state, "open", "Issue should be open");
    }
}

/// Test getting a single issue.
#[tokio::test]
async fn test_get_issue() {
    let provider = TestProvider::github();

    // First get all issues
    let issues = provider
        .get_issues(IssueFilter::default())
        .await
        .unwrap()
        .items;
    assert!(!issues.is_empty());

    // Then get a specific issue
    let key = &issues[0].key;
    let issue = provider.get_issue(key).await.unwrap();

    assert_eq!(&issue.key, key);
}

/// Test getting pull requests.
#[tokio::test]
async fn test_get_pull_requests() {
    let provider = TestProvider::github();

    let prs = provider
        .get_merge_requests(MrFilter::default())
        .await
        .unwrap()
        .items;

    assert!(!prs.is_empty(), "Should have at least one PR");

    // Verify PR structure
    let pr = &prs[0];
    assert!(pr.key.starts_with("pr#"), "PR key should start with pr#");
    assert!(!pr.title.is_empty(), "PR should have a title");
    assert_eq!(pr.source, "github", "Source should be github");
    assert!(!pr.source_branch.is_empty(), "PR should have source branch");
    assert!(!pr.target_branch.is_empty(), "PR should have target branch");
}

/// Test getting pull requests with state filter.
#[tokio::test]
async fn test_get_pull_requests_with_filter() {
    let provider = TestProvider::github();

    let filter = MrFilter {
        state: Some("open".to_string()),
        limit: Some(5),
        ..Default::default()
    };

    let prs = provider.get_merge_requests(filter).await.unwrap().items;

    // All PRs should be open
    for pr in &prs {
        assert_eq!(pr.state, "open", "PR should be open");
    }
}

/// Test getting current user.
#[tokio::test]
async fn test_get_current_user() {
    let provider = TestProvider::github();

    let user = provider.get_current_user().await.unwrap();

    assert!(!user.username.is_empty(), "User should have a username");
    assert!(!user.id.is_empty(), "User should have an id");
}

#[tokio::test]
async fn test_provider_name() {
    let provider = TestProvider::github();

    assert_eq!(provider.name(), "github");
}

/// Test that issues have proper URL format.
#[tokio::test]
async fn test_issue_url_format() {
    let provider = TestProvider::github();

    let issues = provider
        .get_issues(IssueFilter::default())
        .await
        .unwrap()
        .items;
    assert!(!issues.is_empty());

    for issue in &issues {
        if let Some(url) = &issue.url {
            assert!(
                url.starts_with("https://github.com/"),
                "Issue URL should be a GitHub URL: {}",
                url
            );
        }
    }
}

/// Test that PRs have proper URL format.
#[tokio::test]
async fn test_pr_url_format() {
    let provider = TestProvider::github();

    let prs = provider
        .get_merge_requests(MrFilter::default())
        .await
        .unwrap()
        .items;
    assert!(!prs.is_empty());

    for pr in &prs {
        if let Some(url) = &pr.url {
            assert!(
                url.starts_with("https://github.com/"),
                "PR URL should be a GitHub URL: {}",
                url
            );
        }
    }
}

/// Test getting comments for an issue.
#[tokio::test]
async fn test_get_issue_comments() {
    let provider = TestProvider::github();

    // First get all issues
    let issues = provider
        .get_issues(IssueFilter::default())
        .await
        .unwrap()
        .items;
    assert!(!issues.is_empty());

    // Get comments for the first issue
    let key = &issues[0].key;
    let comments = provider.get_comments(key).await.unwrap().items;

    // Comments should be a vector (may be empty)
    for comment in &comments {
        assert!(!comment.id.is_empty(), "Comment should have an ID");
        assert!(!comment.body.is_empty(), "Comment should have a body");
    }
}

/// Test getting a single pull request.
#[tokio::test]
async fn test_get_pull_request() {
    let provider = TestProvider::github();

    // First get all PRs
    let prs = provider
        .get_merge_requests(MrFilter::default())
        .await
        .unwrap()
        .items;
    assert!(!prs.is_empty());

    // Get a specific PR
    let key = &prs[0].key;
    let pr = provider.get_merge_request(key).await.unwrap();

    assert_eq!(&pr.key, key);
    assert!(!pr.title.is_empty(), "PR should have a title");
}

/// Test getting discussions for a pull request.
#[tokio::test]
async fn test_get_pull_request_discussions() {
    let provider = TestProvider::github();

    // First get all PRs
    let prs = provider
        .get_merge_requests(MrFilter::default())
        .await
        .unwrap()
        .items;
    assert!(!prs.is_empty());

    // Get discussions for the first PR
    let key = &prs[0].key;
    let discussions = provider.get_discussions(key).await.unwrap().items;

    // Discussions should be a vector (may be empty)
    for discussion in &discussions {
        assert!(!discussion.id.is_empty(), "Discussion should have an ID");
    }
}

/// Test getting diffs for a pull request.
#[tokio::test]
async fn test_get_pull_request_diffs() {
    let provider = TestProvider::github();

    // First get all PRs
    let prs = provider
        .get_merge_requests(MrFilter::default())
        .await
        .unwrap()
        .items;
    assert!(!prs.is_empty());

    // Get diffs for the first PR
    let key = &prs[0].key;
    let diffs = provider.get_diffs(key).await.unwrap().items;

    // Diffs should be a vector (may be empty for PRs without changes)
    for diff in &diffs {
        assert!(!diff.file_path.is_empty(), "Diff should have a file path");
    }
}

/// Test that adding comment to PR returns error in test mode.
/// Note: In real implementation this would check if PR exists,
/// but TestProvider always returns "not supported" for write operations.
#[tokio::test]
async fn test_add_comment_to_pr_not_supported() {
    use devboy_core::{CreateCommentInput, MergeRequestProvider};

    let provider = TestProvider::github();

    let input = CreateCommentInput {
        body: "Test comment".to_string(),
        position: None,
        discussion_id: None,
    };

    // Write operations are not supported in TestProvider
    let result = MergeRequestProvider::add_comment(&provider, "pr#1", input).await;

    assert!(result.is_err(), "Adding comment should fail in test mode");
    let err_msg = result.unwrap_err().to_string();
    assert!(
        err_msg.contains("not supported"),
        "Error should indicate operation not supported: {}",
        err_msg
    );
}

/// Test that PR and Issue with same number are distinguished.
#[tokio::test]
async fn test_pr_issue_distinction() {
    let provider = TestProvider::github();

    // Get issues and PRs
    let issues = provider
        .get_issues(IssueFilter::default())
        .await
        .unwrap()
        .items;
    let prs = provider
        .get_merge_requests(MrFilter::default())
        .await
        .unwrap()
        .items;

    // Extract numbers from keys
    let issue_numbers: Vec<u64> = issues
        .iter()
        .map(|i| i.key.strip_prefix("gh#").unwrap().parse().unwrap())
        .collect();
    let pr_numbers: Vec<u64> = prs
        .iter()
        .map(|p| p.key.strip_prefix("pr#").unwrap().parse().unwrap())
        .collect();

    // In GitHub, PRs are also issues, so there may be overlap in numbering
    // But get_issues should filter out PRs
    for issue in &issues {
        let num: u64 = issue.key.strip_prefix("gh#").unwrap().parse().unwrap();
        // Issues returned should not be PRs
        assert!(
            !pr_numbers.contains(&num) || issue_numbers.contains(&num),
            "Issue {} should not be a PR",
            issue.key
        );
    }
}

/// Test that adding a comment to an issue returns error in test mode.
#[tokio::test]
async fn test_add_issue_comment_not_supported() {
    let provider = TestProvider::github();

    // First get all issues
    let issues = provider
        .get_issues(IssueFilter::default())
        .await
        .unwrap()
        .items;
    assert!(!issues.is_empty(), "Should have at least one issue");

    // Add a comment to the first issue
    let key = &issues[0].key;
    let comment_body = "Test comment";

    let result = IssueProvider::add_comment(&provider, key, comment_body).await;

    // Write operations are not supported in TestProvider
    assert!(
        result.is_err(),
        "Add comment should fail in test mode with proper error"
    );
    let err_msg = result.unwrap_err().to_string();
    assert!(
        err_msg.contains("not supported"),
        "Error should indicate operation not supported: {}",
        err_msg
    );
}

/// Test that adding a comment to a PR returns error in test mode.
#[tokio::test]
async fn test_add_pr_comment_not_supported() {
    let provider = TestProvider::github();

    // First get all PRs
    let prs = provider
        .get_merge_requests(MrFilter::default())
        .await
        .unwrap()
        .items;
    assert!(!prs.is_empty(), "Should have at least one PR");

    // Add a comment to the first PR
    let key = &prs[0].key;
    let input = CreateCommentInput {
        body: "Test PR comment".to_string(),
        position: None,
        discussion_id: None,
    };

    let result = MergeRequestProvider::add_comment(&provider, key, input).await;

    // Write operations are not supported in TestProvider
    assert!(
        result.is_err(),
        "Add PR comment should fail in test mode with proper error"
    );
    let err_msg = result.unwrap_err().to_string();
    assert!(
        err_msg.contains("not supported"),
        "Error should indicate operation not supported: {}",
        err_msg
    );
}

/// Test that adding an inline comment returns error in test mode.
#[tokio::test]
async fn test_add_pr_inline_comment_not_supported() {
    use devboy_core::CodePosition;

    let provider = TestProvider::github();

    // First get all PRs
    let prs = provider
        .get_merge_requests(MrFilter::default())
        .await
        .unwrap()
        .items;
    assert!(!prs.is_empty(), "Should have at least one PR");

    let key = &prs[0].key;

    // Try to add an inline comment (position provided)
    let input = CreateCommentInput {
        body: "Test inline comment".to_string(),
        position: Some(CodePosition {
            file_path: "src/main.rs".to_string(),
            line: 1,
            line_type: "new".to_string(),
            commit_sha: None,
        }),
        discussion_id: None,
    };

    let result = MergeRequestProvider::add_comment(&provider, key, input).await;

    // Write operations are not supported in TestProvider
    assert!(
        result.is_err(),
        "Add inline comment should fail in test mode"
    );
    let err_msg = result.unwrap_err().to_string();
    assert!(
        err_msg.contains("not supported"),
        "Error should indicate operation not supported: {}",
        err_msg
    );
}

/// Test that creating a new issue returns error in test mode.
#[tokio::test]
async fn test_create_issue_not_supported() {
    let provider = TestProvider::github();

    let input = CreateIssueInput {
        title: "Test Issue".to_string(),
        description: Some("Test description".to_string()),
        labels: vec!["test".to_string()],
        ..Default::default()
    };

    let result = provider.create_issue(input).await;

    // Write operations are not supported in TestProvider
    assert!(
        result.is_err(),
        "Create issue should fail in test mode with proper error"
    );
    let err_msg = result.unwrap_err().to_string();
    assert!(
        err_msg.contains("not supported"),
        "Error should indicate operation not supported: {}",
        err_msg
    );
}

/// Test that updating an issue returns error in test mode.
#[tokio::test]
async fn test_update_issue_not_supported() {
    let provider = TestProvider::github();

    // First get all issues
    let issues = provider
        .get_issues(IssueFilter::default())
        .await
        .unwrap()
        .items;
    assert!(
        !issues.is_empty(),
        "Should have at least one issue to update"
    );

    let key = &issues[0].key;
    let input = UpdateIssueInput {
        title: Some("Updated Title".to_string()),
        description: Some("Updated description".to_string()),
        labels: Some(vec!["test".to_string()]),
        ..Default::default()
    };

    let result = provider.update_issue(key, input).await;

    // Write operations are not supported in TestProvider
    assert!(
        result.is_err(),
        "Update issue should fail in test mode with proper error"
    );
    let err_msg = result.unwrap_err().to_string();
    assert!(
        err_msg.contains("not supported"),
        "Error should indicate operation not supported: {}",
        err_msg
    );
}

/// Test that adding comment via PR interface returns error in test mode.
/// Note: In real implementation this would validate that the key is a PR,
/// but TestProvider always returns "not supported" for write operations.
#[tokio::test]
async fn test_add_comment_via_pr_key_not_supported() {
    let provider = TestProvider::github();

    // Write operations are not supported in TestProvider regardless of key format
    let input = CreateCommentInput {
        body: "This should fail".to_string(),
        position: None,
        discussion_id: None,
    };

    let result = MergeRequestProvider::add_comment(&provider, "pr#1", input).await;

    assert!(result.is_err(), "Adding comment should fail in test mode");
    let err_msg = result.unwrap_err().to_string();
    assert!(
        err_msg.contains("not supported"),
        "Error should indicate operation not supported: {}",
        err_msg
    );
}