Skip to main content

rung_github/
traits.rs

1//! Trait abstractions for GitHub API operations.
2//!
3//! This module defines the `GitHubApi` trait which abstracts GitHub API operations,
4//! enabling dependency injection and testability.
5
6use std::collections::HashMap;
7
8use crate::{
9    CheckRun, CreateComment, CreatePullRequest, IssueComment, MergePullRequest, MergeResult,
10    PullRequest, Result, UpdateComment, UpdatePullRequest,
11};
12
13/// Trait for GitHub API operations.
14///
15/// This trait abstracts GitHub API calls, allowing for:
16/// - Dependency injection in commands/services
17/// - Mock implementations for testing
18/// - Alternative implementations (e.g., offline mode, caching)
19///
20/// All methods take `owner` and `repo` as parameters to support
21/// operations across different repositories.
22pub trait GitHubApi: Send + Sync {
23    // === PR Operations ===
24
25    /// Get a pull request by number.
26    fn get_pr(
27        &self,
28        owner: &str,
29        repo: &str,
30        number: u64,
31    ) -> impl std::future::Future<Output = Result<PullRequest>> + Send;
32
33    /// Get multiple pull requests by number (batch operation).
34    ///
35    /// Returns a map of PR number to PR data. Missing PRs are omitted.
36    fn get_prs_batch(
37        &self,
38        owner: &str,
39        repo: &str,
40        numbers: &[u64],
41    ) -> impl std::future::Future<Output = Result<HashMap<u64, PullRequest>>> + Send;
42
43    /// Find a PR for a branch.
44    ///
45    /// Returns `None` if no open PR exists for the branch.
46    fn find_pr_for_branch(
47        &self,
48        owner: &str,
49        repo: &str,
50        branch: &str,
51    ) -> impl std::future::Future<Output = Result<Option<PullRequest>>> + Send;
52
53    /// Create a pull request.
54    fn create_pr(
55        &self,
56        owner: &str,
57        repo: &str,
58        pr: CreatePullRequest,
59    ) -> impl std::future::Future<Output = Result<PullRequest>> + Send;
60
61    /// Update a pull request.
62    fn update_pr(
63        &self,
64        owner: &str,
65        repo: &str,
66        number: u64,
67        update: UpdatePullRequest,
68    ) -> impl std::future::Future<Output = Result<PullRequest>> + Send;
69
70    // === Check Runs ===
71
72    /// Get check runs for a commit.
73    fn get_check_runs(
74        &self,
75        owner: &str,
76        repo: &str,
77        commit_sha: &str,
78    ) -> impl std::future::Future<Output = Result<Vec<CheckRun>>> + Send;
79
80    // === Merge Operations ===
81
82    /// Merge a pull request.
83    fn merge_pr(
84        &self,
85        owner: &str,
86        repo: &str,
87        number: u64,
88        merge: MergePullRequest,
89    ) -> impl std::future::Future<Output = Result<MergeResult>> + Send;
90
91    // === Ref Operations ===
92
93    /// Delete a git reference (branch).
94    fn delete_ref(
95        &self,
96        owner: &str,
97        repo: &str,
98        ref_name: &str,
99    ) -> impl std::future::Future<Output = Result<()>> + Send;
100
101    // === Repository Operations ===
102
103    /// Get the repository's default branch name.
104    fn get_default_branch(
105        &self,
106        owner: &str,
107        repo: &str,
108    ) -> impl std::future::Future<Output = Result<String>> + Send;
109
110    // === Comment Operations ===
111
112    /// List comments on a pull request.
113    fn list_pr_comments(
114        &self,
115        owner: &str,
116        repo: &str,
117        pr_number: u64,
118    ) -> impl std::future::Future<Output = Result<Vec<IssueComment>>> + Send;
119
120    /// Create a comment on a pull request.
121    fn create_pr_comment(
122        &self,
123        owner: &str,
124        repo: &str,
125        pr_number: u64,
126        comment: CreateComment,
127    ) -> impl std::future::Future<Output = Result<IssueComment>> + Send;
128
129    /// Update a comment on a pull request.
130    fn update_pr_comment(
131        &self,
132        owner: &str,
133        repo: &str,
134        comment_id: u64,
135        comment: UpdateComment,
136    ) -> impl std::future::Future<Output = Result<IssueComment>> + Send;
137}