dx-driven 0.1.0

Professional AI-assisted development orchestrator with binary-first architecture
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
//! # GitHub Integration
//!
//! GitHub CLI wrapper for issues, PRs, and workflows.
//!
//! ## Usage
//!
//! ```rust,ignore
//! use driven::integrations::github::{GitHubClient, GitHubConfig};
//!
//! let config = GitHubConfig::from_file("~/.dx/config/github.sr")?;
//! let client = GitHubClient::new(&config)?;
//!
//! // Create an issue
//! let issue = client.create_issue("repo", "Bug title", "Description").await?;
//!
//! // List PRs
//! let prs = client.list_prs("repo", "open").await?;
//! ```

use crate::error::{DrivenError, Result};
use serde::{Deserialize, Serialize};

/// GitHub configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GitHubConfig {
    /// Whether GitHub integration is enabled
    #[serde(default = "default_true")]
    pub enabled: bool,
    /// GitHub API token
    #[serde(default)]
    pub token: String,
    /// Default owner/org
    pub default_owner: Option<String>,
    /// Default repository
    pub default_repo: Option<String>,
    /// Use gh CLI instead of API
    #[serde(default = "default_true")]
    pub use_cli: bool,
}

fn default_true() -> bool {
    true
}

impl Default for GitHubConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            token: String::new(),
            default_owner: None,
            default_repo: None,
            use_cli: true,
        }
    }
}

impl GitHubConfig {
    /// Load from .sr config file
    pub fn from_file(path: impl AsRef<std::path::Path>) -> Result<Self> {
        let content = std::fs::read_to_string(path.as_ref())
            .map_err(|e| DrivenError::Io(e))?;
        Self::parse_sr(&content)
    }

    fn parse_sr(_content: &str) -> Result<Self> {
        Ok(Self::default())
    }

    /// Resolve environment variables
    pub fn resolve_env_vars(&mut self) {
        if self.token.is_empty() || self.token.starts_with('$') {
            self.token = std::env::var("GITHUB_TOKEN")
                .or_else(|_| std::env::var("GH_TOKEN"))
                .unwrap_or_default();
        }
    }
}

/// GitHub issue
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GitHubIssue {
    /// Issue number
    pub number: u64,
    /// Issue title
    pub title: String,
    /// Issue body
    pub body: Option<String>,
    /// Issue state
    pub state: IssueState,
    /// Issue URL
    pub url: String,
    /// Author
    pub author: String,
    /// Labels
    pub labels: Vec<String>,
    /// Assignees
    pub assignees: Vec<String>,
    /// Created at
    pub created_at: String,
    /// Updated at
    pub updated_at: String,
}

/// Issue/PR state
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum IssueState {
    Open,
    Closed,
}

/// GitHub pull request
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GitHubPR {
    /// PR number
    pub number: u64,
    /// PR title
    pub title: String,
    /// PR body
    pub body: Option<String>,
    /// PR state
    pub state: IssueState,
    /// PR URL
    pub url: String,
    /// Author
    pub author: String,
    /// Head branch
    pub head: String,
    /// Base branch
    pub base: String,
    /// Is draft
    pub draft: bool,
    /// Is merged
    pub merged: bool,
    /// Labels
    pub labels: Vec<String>,
    /// Reviewers
    pub reviewers: Vec<String>,
}

/// GitHub workflow run
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GitHubRun {
    /// Run ID
    pub id: u64,
    /// Workflow name
    pub workflow: String,
    /// Run status
    pub status: RunStatus,
    /// Conclusion
    pub conclusion: Option<String>,
    /// Branch
    pub branch: String,
    /// URL
    pub url: String,
    /// Created at
    pub created_at: String,
}

/// Workflow run status
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum RunStatus {
    Queued,
    InProgress,
    Completed,
}

/// GitHub client
pub struct GitHubClient {
    config: GitHubConfig,
    base_url: String,
}

impl GitHubClient {
    /// API base URL
    const API_BASE: &'static str = "https://api.github.com";

    /// Create a new GitHub client
    pub fn new(config: &GitHubConfig) -> Result<Self> {
        let mut config = config.clone();
        config.resolve_env_vars();

        Ok(Self {
            config,
            base_url: Self::API_BASE.to_string(),
        })
    }

    /// Check if client is configured
    pub fn is_configured(&self) -> bool {
        // gh CLI doesn't require token if already authenticated
        self.config.use_cli || !self.config.token.is_empty()
    }

    /// Get full repo path
    fn repo_path(&self, repo: &str) -> String {
        if repo.contains('/') {
            repo.to_string()
        } else if let Some(ref owner) = self.config.default_owner {
            format!("{}/{}", owner, repo)
        } else {
            repo.to_string()
        }
    }

    // Issue operations

    /// Create an issue
    pub async fn create_issue(&self, repo: &str, title: &str, body: &str) -> Result<GitHubIssue> {
        if self.config.use_cli {
            return self.cli_create_issue(repo, title, body).await;
        }

        let repo = self.repo_path(repo);
        let url = format!("{}/repos/{}/issues", self.base_url, repo);

        let client = reqwest::Client::new();
        let response = client
            .post(&url)
            .header("Authorization", format!("Bearer {}", self.config.token))
            .header("Accept", "application/vnd.github+json")
            .header("User-Agent", "dx-driven")
            .json(&serde_json::json!({
                "title": title,
                "body": body
            }))
            .send()
            .await
            .map_err(|e| DrivenError::Network(e.to_string()))?;

        if !response.status().is_success() {
            let error = response.text().await.unwrap_or_default();
            return Err(DrivenError::Api(format!("GitHub error: {}", error)));
        }

        let issue: serde_json::Value = response
            .json()
            .await
            .map_err(|e| DrivenError::Parse(e.to_string()))?;

        self.parse_issue(issue)
    }

    /// List issues
    pub async fn list_issues(&self, repo: &str, state: &str) -> Result<Vec<GitHubIssue>> {
        if self.config.use_cli {
            return self.cli_list_issues(repo, state).await;
        }

        let repo = self.repo_path(repo);
        let url = format!("{}/repos/{}/issues?state={}", self.base_url, repo, state);

        let client = reqwest::Client::new();
        let response = client
            .get(&url)
            .header("Authorization", format!("Bearer {}", self.config.token))
            .header("Accept", "application/vnd.github+json")
            .header("User-Agent", "dx-driven")
            .send()
            .await
            .map_err(|e| DrivenError::Network(e.to_string()))?;

        if !response.status().is_success() {
            return Err(DrivenError::Api("Failed to list issues".into()));
        }

        let issues: Vec<serde_json::Value> = response
            .json()
            .await
            .map_err(|e| DrivenError::Parse(e.to_string()))?;

        issues.into_iter().map(|i| self.parse_issue(i)).collect()
    }

    /// Close an issue
    pub async fn close_issue(&self, repo: &str, number: u64) -> Result<()> {
        if self.config.use_cli {
            return self.cli_close_issue(repo, number).await;
        }

        let repo = self.repo_path(repo);
        let url = format!("{}/repos/{}/issues/{}", self.base_url, repo, number);

        let client = reqwest::Client::new();
        let response = client
            .patch(&url)
            .header("Authorization", format!("Bearer {}", self.config.token))
            .header("Accept", "application/vnd.github+json")
            .header("User-Agent", "dx-driven")
            .json(&serde_json::json!({ "state": "closed" }))
            .send()
            .await
            .map_err(|e| DrivenError::Network(e.to_string()))?;

        if !response.status().is_success() {
            return Err(DrivenError::Api("Failed to close issue".into()));
        }

        Ok(())
    }

    // PR operations

    /// Create a pull request
    pub async fn create_pr(
        &self,
        repo: &str,
        title: &str,
        body: &str,
        head: &str,
        base: &str,
    ) -> Result<GitHubPR> {
        if self.config.use_cli {
            return self.cli_create_pr(repo, title, body, head, base).await;
        }

        let repo = self.repo_path(repo);
        let url = format!("{}/repos/{}/pulls", self.base_url, repo);

        let client = reqwest::Client::new();
        let response = client
            .post(&url)
            .header("Authorization", format!("Bearer {}", self.config.token))
            .header("Accept", "application/vnd.github+json")
            .header("User-Agent", "dx-driven")
            .json(&serde_json::json!({
                "title": title,
                "body": body,
                "head": head,
                "base": base
            }))
            .send()
            .await
            .map_err(|e| DrivenError::Network(e.to_string()))?;

        if !response.status().is_success() {
            let error = response.text().await.unwrap_or_default();
            return Err(DrivenError::Api(format!("GitHub error: {}", error)));
        }

        let pr: serde_json::Value = response
            .json()
            .await
            .map_err(|e| DrivenError::Parse(e.to_string()))?;

        self.parse_pr(pr)
    }

    /// List pull requests
    pub async fn list_prs(&self, repo: &str, state: &str) -> Result<Vec<GitHubPR>> {
        if self.config.use_cli {
            return self.cli_list_prs(repo, state).await;
        }

        let repo = self.repo_path(repo);
        let url = format!("{}/repos/{}/pulls?state={}", self.base_url, repo, state);

        let client = reqwest::Client::new();
        let response = client
            .get(&url)
            .header("Authorization", format!("Bearer {}", self.config.token))
            .header("Accept", "application/vnd.github+json")
            .header("User-Agent", "dx-driven")
            .send()
            .await
            .map_err(|e| DrivenError::Network(e.to_string()))?;

        if !response.status().is_success() {
            return Err(DrivenError::Api("Failed to list PRs".into()));
        }

        let prs: Vec<serde_json::Value> = response
            .json()
            .await
            .map_err(|e| DrivenError::Parse(e.to_string()))?;

        prs.into_iter().map(|p| self.parse_pr(p)).collect()
    }

    /// Merge a pull request
    pub async fn merge_pr(&self, repo: &str, number: u64) -> Result<()> {
        if self.config.use_cli {
            return self.cli_merge_pr(repo, number).await;
        }

        let repo = self.repo_path(repo);
        let url = format!("{}/repos/{}/pulls/{}/merge", self.base_url, repo, number);

        let client = reqwest::Client::new();
        let response = client
            .put(&url)
            .header("Authorization", format!("Bearer {}", self.config.token))
            .header("Accept", "application/vnd.github+json")
            .header("User-Agent", "dx-driven")
            .send()
            .await
            .map_err(|e| DrivenError::Network(e.to_string()))?;

        if !response.status().is_success() {
            return Err(DrivenError::Api("Failed to merge PR".into()));
        }

        Ok(())
    }

    // Workflow operations

    /// List workflow runs
    pub async fn list_runs(&self, repo: &str) -> Result<Vec<GitHubRun>> {
        if self.config.use_cli {
            return self.cli_list_runs(repo).await;
        }

        let repo = self.repo_path(repo);
        let url = format!("{}/repos/{}/actions/runs", self.base_url, repo);

        let client = reqwest::Client::new();
        let response = client
            .get(&url)
            .header("Authorization", format!("Bearer {}", self.config.token))
            .header("Accept", "application/vnd.github+json")
            .header("User-Agent", "dx-driven")
            .send()
            .await
            .map_err(|e| DrivenError::Network(e.to_string()))?;

        if !response.status().is_success() {
            return Err(DrivenError::Api("Failed to list runs".into()));
        }

        let result: serde_json::Value = response
            .json()
            .await
            .map_err(|e| DrivenError::Parse(e.to_string()))?;

        result["workflow_runs"]
            .as_array()
            .ok_or_else(|| DrivenError::Parse("Invalid response".into()))?
            .iter()
            .map(|r| self.parse_run(r.clone()))
            .collect()
    }

    /// Re-run a workflow
    pub async fn rerun_workflow(&self, repo: &str, run_id: u64) -> Result<()> {
        if self.config.use_cli {
            return self.cli_rerun_workflow(repo, run_id).await;
        }

        let repo = self.repo_path(repo);
        let url = format!("{}/repos/{}/actions/runs/{}/rerun", self.base_url, repo, run_id);

        let client = reqwest::Client::new();
        let response = client
            .post(&url)
            .header("Authorization", format!("Bearer {}", self.config.token))
            .header("Accept", "application/vnd.github+json")
            .header("User-Agent", "dx-driven")
            .send()
            .await
            .map_err(|e| DrivenError::Network(e.to_string()))?;

        if !response.status().is_success() {
            return Err(DrivenError::Api("Failed to rerun workflow".into()));
        }

        Ok(())
    }

    // CLI methods

    async fn cli_create_issue(&self, repo: &str, title: &str, body: &str) -> Result<GitHubIssue> {
        let output = self.run_gh(&[
            "issue", "create",
            "-R", &self.repo_path(repo),
            "-t", title,
            "-b", body,
            "--json", "number,title,body,state,url,author,labels,assignees,createdAt,updatedAt"
        ]).await?;

        let issue: serde_json::Value = serde_json::from_str(&output)
            .map_err(|e| DrivenError::Parse(e.to_string()))?;
        
        self.parse_issue(issue)
    }

    async fn cli_list_issues(&self, repo: &str, state: &str) -> Result<Vec<GitHubIssue>> {
        let output = self.run_gh(&[
            "issue", "list",
            "-R", &self.repo_path(repo),
            "-s", state,
            "--json", "number,title,body,state,url,author,labels,assignees,createdAt,updatedAt"
        ]).await?;

        let issues: Vec<serde_json::Value> = serde_json::from_str(&output)
            .map_err(|e| DrivenError::Parse(e.to_string()))?;

        issues.into_iter().map(|i| self.parse_issue(i)).collect()
    }

    async fn cli_close_issue(&self, repo: &str, number: u64) -> Result<()> {
        self.run_gh(&[
            "issue", "close",
            "-R", &self.repo_path(repo),
            &number.to_string()
        ]).await?;
        Ok(())
    }

    async fn cli_create_pr(&self, repo: &str, title: &str, body: &str, head: &str, base: &str) -> Result<GitHubPR> {
        let output = self.run_gh(&[
            "pr", "create",
            "-R", &self.repo_path(repo),
            "-t", title,
            "-b", body,
            "-H", head,
            "-B", base,
            "--json", "number,title,body,state,url,author,headRefName,baseRefName,isDraft,merged,labels,reviewRequests"
        ]).await?;

        let pr: serde_json::Value = serde_json::from_str(&output)
            .map_err(|e| DrivenError::Parse(e.to_string()))?;

        self.parse_pr(pr)
    }

    async fn cli_list_prs(&self, repo: &str, state: &str) -> Result<Vec<GitHubPR>> {
        let output = self.run_gh(&[
            "pr", "list",
            "-R", &self.repo_path(repo),
            "-s", state,
            "--json", "number,title,body,state,url,author,headRefName,baseRefName,isDraft,merged,labels,reviewRequests"
        ]).await?;

        let prs: Vec<serde_json::Value> = serde_json::from_str(&output)
            .map_err(|e| DrivenError::Parse(e.to_string()))?;

        prs.into_iter().map(|p| self.parse_pr(p)).collect()
    }

    async fn cli_merge_pr(&self, repo: &str, number: u64) -> Result<()> {
        self.run_gh(&[
            "pr", "merge",
            "-R", &self.repo_path(repo),
            &number.to_string(),
            "--merge"
        ]).await?;
        Ok(())
    }

    async fn cli_list_runs(&self, repo: &str) -> Result<Vec<GitHubRun>> {
        let output = self.run_gh(&[
            "run", "list",
            "-R", &self.repo_path(repo),
            "--json", "databaseId,workflowName,status,conclusion,headBranch,url,createdAt"
        ]).await?;

        let runs: Vec<serde_json::Value> = serde_json::from_str(&output)
            .map_err(|e| DrivenError::Parse(e.to_string()))?;

        runs.into_iter().map(|r| self.parse_run(r)).collect()
    }

    async fn cli_rerun_workflow(&self, repo: &str, run_id: u64) -> Result<()> {
        self.run_gh(&[
            "run", "rerun",
            "-R", &self.repo_path(repo),
            &run_id.to_string()
        ]).await?;
        Ok(())
    }

    async fn run_gh(&self, args: &[&str]) -> Result<String> {
        use tokio::process::Command;

        let output = Command::new("gh")
            .args(args)
            .output()
            .await
            .map_err(|e| DrivenError::Process(format!("Failed to run gh: {}", e)))?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            return Err(DrivenError::Process(format!("gh command failed: {}", stderr)));
        }

        Ok(String::from_utf8_lossy(&output.stdout).to_string())
    }

    fn parse_issue(&self, v: serde_json::Value) -> Result<GitHubIssue> {
        Ok(GitHubIssue {
            number: v["number"].as_u64().unwrap_or_default(),
            title: v["title"].as_str().unwrap_or_default().to_string(),
            body: v["body"].as_str().map(String::from),
            state: if v["state"].as_str().unwrap_or("open") == "open" {
                IssueState::Open
            } else {
                IssueState::Closed
            },
            url: v["url"].as_str().or(v["html_url"].as_str()).unwrap_or_default().to_string(),
            author: v["author"]["login"].as_str()
                .or(v["user"]["login"].as_str())
                .unwrap_or_default().to_string(),
            labels: v["labels"].as_array()
                .map(|arr| arr.iter()
                    .filter_map(|l| l["name"].as_str().or(l.as_str()).map(String::from))
                    .collect())
                .unwrap_or_default(),
            assignees: v["assignees"].as_array()
                .map(|arr| arr.iter()
                    .filter_map(|a| a["login"].as_str().map(String::from))
                    .collect())
                .unwrap_or_default(),
            created_at: v["createdAt"].as_str().or(v["created_at"].as_str()).unwrap_or_default().to_string(),
            updated_at: v["updatedAt"].as_str().or(v["updated_at"].as_str()).unwrap_or_default().to_string(),
        })
    }

    fn parse_pr(&self, v: serde_json::Value) -> Result<GitHubPR> {
        Ok(GitHubPR {
            number: v["number"].as_u64().unwrap_or_default(),
            title: v["title"].as_str().unwrap_or_default().to_string(),
            body: v["body"].as_str().map(String::from),
            state: if v["state"].as_str().unwrap_or("open") == "open" {
                IssueState::Open
            } else {
                IssueState::Closed
            },
            url: v["url"].as_str().or(v["html_url"].as_str()).unwrap_or_default().to_string(),
            author: v["author"]["login"].as_str()
                .or(v["user"]["login"].as_str())
                .unwrap_or_default().to_string(),
            head: v["headRefName"].as_str().or(v["head"]["ref"].as_str()).unwrap_or_default().to_string(),
            base: v["baseRefName"].as_str().or(v["base"]["ref"].as_str()).unwrap_or_default().to_string(),
            draft: v["isDraft"].as_bool().or(v["draft"].as_bool()).unwrap_or(false),
            merged: v["merged"].as_bool().unwrap_or(false),
            labels: v["labels"].as_array()
                .map(|arr| arr.iter()
                    .filter_map(|l| l["name"].as_str().or(l.as_str()).map(String::from))
                    .collect())
                .unwrap_or_default(),
            reviewers: v["reviewRequests"].as_array()
                .or(v["requested_reviewers"].as_array())
                .map(|arr| arr.iter()
                    .filter_map(|r| r["login"].as_str().map(String::from))
                    .collect())
                .unwrap_or_default(),
        })
    }

    fn parse_run(&self, v: serde_json::Value) -> Result<GitHubRun> {
        let status_str = v["status"].as_str().unwrap_or("completed");
        let status = match status_str {
            "queued" => RunStatus::Queued,
            "in_progress" => RunStatus::InProgress,
            _ => RunStatus::Completed,
        };

        Ok(GitHubRun {
            id: v["databaseId"].as_u64().or(v["id"].as_u64()).unwrap_or_default(),
            workflow: v["workflowName"].as_str().or(v["name"].as_str()).unwrap_or_default().to_string(),
            status,
            conclusion: v["conclusion"].as_str().map(String::from),
            branch: v["headBranch"].as_str().or(v["head_branch"].as_str()).unwrap_or_default().to_string(),
            url: v["url"].as_str().or(v["html_url"].as_str()).unwrap_or_default().to_string(),
            created_at: v["createdAt"].as_str().or(v["created_at"].as_str()).unwrap_or_default().to_string(),
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_default_config() {
        let config = GitHubConfig::default();
        assert!(config.enabled);
        assert!(config.use_cli);
    }

    #[test]
    fn test_repo_path() {
        let mut config = GitHubConfig::default();
        config.default_owner = Some("owner".to_string());
        let client = GitHubClient::new(&config).unwrap();

        assert_eq!(client.repo_path("repo"), "owner/repo");
        assert_eq!(client.repo_path("other/repo"), "other/repo");
    }
}