Skip to main content

ag_forge/
client.rs

1//! Public review-request trait boundary and production client wiring.
2
3use std::sync::Arc;
4
5use super::{
6    CreateReviewRequestInput, ForgeCommandRunner, ForgeFuture, ForgeKind, ForgeRemote,
7    GitHubReviewRequestAdapter, GitLabReviewRequestAdapter, RealForgeCommandRunner,
8    ReviewCommentSnapshot, ReviewRequestAttention, ReviewRequestError, ReviewRequestMetadata,
9    ReviewRequestSummary, UpdateReviewRequestInput, detect_remote,
10};
11
12/// Async boundary used by app orchestration for forge review requests.
13///
14/// The app layer depends on this narrow contract so provider-specific request
15/// formats remain isolated inside concrete adapters.
16#[cfg_attr(any(test, feature = "test-utils"), mockall::automock)]
17pub trait ReviewRequestClient: Send + Sync {
18    /// Detects whether `repo_url` belongs to one supported forge.
19    ///
20    /// # Errors
21    /// Returns [`ReviewRequestError::UnsupportedRemote`] when the remote does
22    /// not map to a supported forge.
23    fn detect_remote(&self, repo_url: String) -> Result<ForgeRemote, ReviewRequestError>;
24
25    /// Finds an existing review request for `source_branch`.
26    ///
27    /// # Errors
28    /// Returns a provider-specific review-request error when the forge lookup
29    /// cannot be completed.
30    fn find_by_source_branch(
31        &self,
32        remote: ForgeRemote,
33        source_branch: String,
34    ) -> ForgeFuture<Result<Option<ReviewRequestSummary>, ReviewRequestError>>;
35
36    /// Creates a new review request from `input`.
37    ///
38    /// # Errors
39    /// Returns a provider-specific review-request error when creation fails.
40    fn create_review_request(
41        &self,
42        remote: ForgeRemote,
43        input: CreateReviewRequestInput,
44    ) -> ForgeFuture<Result<ReviewRequestSummary, ReviewRequestError>>;
45
46    /// Refreshes one existing review request by provider display id.
47    ///
48    /// # Errors
49    /// Returns a provider-specific review-request error when refresh fails.
50    fn refresh_review_request(
51        &self,
52        remote: ForgeRemote,
53        display_id: String,
54    ) -> ForgeFuture<Result<ReviewRequestSummary, ReviewRequestError>>;
55
56    /// Loads the current title and body of one existing review request.
57    ///
58    /// # Errors
59    /// Returns a provider-specific review-request error when metadata lookup
60    /// fails.
61    fn review_request_metadata(
62        &self,
63        remote: ForgeRemote,
64        display_id: String,
65    ) -> ForgeFuture<Result<ReviewRequestMetadata, ReviewRequestError>>;
66
67    /// Best-effort syncs reconciled metadata after rechecking that the remote
68    /// fields match the values used during evaluation.
69    ///
70    /// The provider CLI update is not atomic with the recheck, so a later
71    /// concurrent manual edit can still be overwritten.
72    ///
73    /// # Errors
74    /// Returns a provider-specific review-request error when metadata lookup,
75    /// update, or refresh fails.
76    fn sync_review_request_metadata(
77        &self,
78        remote: ForgeRemote,
79        display_id: String,
80        input: UpdateReviewRequestInput,
81    ) -> ForgeFuture<Result<ReviewRequestSummary, ReviewRequestError>>;
82
83    /// Returns the browser-openable URL for one review request.
84    ///
85    /// # Errors
86    /// Returns [`ReviewRequestError::OperationFailed`] when the summary does
87    /// not carry a web URL.
88    fn review_request_web_url(
89        &self,
90        review_request: &ReviewRequestSummary,
91    ) -> Result<String, ReviewRequestError>;
92
93    /// Fetches the review-comment snapshot for one open review request.
94    ///
95    /// Returns both inline threads and review-request-wide comments. Threads
96    /// are grouped by `path` and sorted by `(path, line)` by callers; adapters
97    /// return what the forge reports without enforcing an ordering.
98    ///
99    /// # Errors
100    /// Returns a provider-specific review-request error when the snapshot fetch
101    /// cannot be completed (including authentication and host failures).
102    fn fetch_review_comment_snapshot(
103        &self,
104        remote: ForgeRemote,
105        display_id: String,
106    ) -> ForgeFuture<Result<ReviewCommentSnapshot, ReviewRequestError>>;
107
108    /// Fetches the conditions on one open review request that may need
109    /// follow-up work: failed and pending checks for its head commit, merge
110    /// conflicts with its target branch, and unresolved review threads
111    /// awaiting a response from trusted authors.
112    ///
113    /// # Errors
114    /// Returns a provider-specific review-request error when any forge lookup
115    /// cannot be completed, including the author-trust lookups, so callers
116    /// never act on partial facts.
117    fn fetch_review_request_attention(
118        &self,
119        remote: ForgeRemote,
120        display_id: String,
121    ) -> ForgeFuture<Result<ReviewRequestAttention, ReviewRequestError>>;
122
123    /// Adds one reply to an existing review thread.
124    ///
125    /// # Errors
126    /// Returns a provider-specific review-request error when the reply cannot
127    /// be posted.
128    fn reply_to_thread(
129        &self,
130        remote: ForgeRemote,
131        display_id: String,
132        thread_id: String,
133        body: String,
134    ) -> ForgeFuture<Result<(), ReviewRequestError>>;
135
136    /// Marks one existing review thread resolved.
137    ///
138    /// # Errors
139    /// Returns a provider-specific review-request error when the thread cannot
140    /// be resolved.
141    fn resolve_thread(
142        &self,
143        remote: ForgeRemote,
144        display_id: String,
145        thread_id: String,
146    ) -> ForgeFuture<Result<(), ReviewRequestError>>;
147}
148
149/// Production [`ReviewRequestClient`] that routes to forge-specific adapters.
150pub struct RealReviewRequestClient {
151    command_runner: Arc<dyn ForgeCommandRunner>,
152}
153
154impl RealReviewRequestClient {
155    /// Builds one review-request client from a forge command runner.
156    pub(crate) fn new(command_runner: Arc<dyn ForgeCommandRunner>) -> Self {
157        Self { command_runner }
158    }
159
160    /// Runs `call` on an authenticated adapter selected for `remote`.
161    fn call_with_authenticated_adapter<T>(
162        &self,
163        remote: ForgeRemote,
164        call: impl FnOnce(
165            Arc<dyn ReviewRequestAdapter>,
166            ForgeRemote,
167        ) -> ForgeFuture<Result<T, ReviewRequestError>>
168        + Send
169        + 'static,
170    ) -> ForgeFuture<Result<T, ReviewRequestError>>
171    where
172        T: Send + 'static,
173    {
174        let adapter = self.adapter_for(remote.forge_kind);
175
176        Box::pin(async move {
177            adapter.ensure_authenticated(&remote).await?;
178
179            call(adapter, remote).await
180        })
181    }
182
183    /// Returns one adapter implementation for `forge_kind`.
184    fn adapter_for(&self, forge_kind: ForgeKind) -> Arc<dyn ReviewRequestAdapter> {
185        match forge_kind {
186            ForgeKind::GitHub => Arc::new(GitHubReviewRequestAdapter::new(Arc::clone(
187                &self.command_runner,
188            ))),
189            ForgeKind::GitLab => Arc::new(GitLabReviewRequestAdapter::new(Arc::clone(
190                &self.command_runner,
191            ))),
192        }
193    }
194}
195
196impl Default for RealReviewRequestClient {
197    fn default() -> Self {
198        Self::new(Arc::new(RealForgeCommandRunner))
199    }
200}
201
202impl ReviewRequestClient for RealReviewRequestClient {
203    fn detect_remote(&self, repo_url: String) -> Result<ForgeRemote, ReviewRequestError> {
204        detect_remote(&repo_url)
205    }
206
207    fn find_by_source_branch(
208        &self,
209        remote: ForgeRemote,
210        source_branch: String,
211    ) -> ForgeFuture<Result<Option<ReviewRequestSummary>, ReviewRequestError>> {
212        self.call_with_authenticated_adapter(remote, move |adapter, remote| {
213            adapter.find_authenticated_by_source_branch(remote, source_branch)
214        })
215    }
216
217    fn create_review_request(
218        &self,
219        remote: ForgeRemote,
220        input: CreateReviewRequestInput,
221    ) -> ForgeFuture<Result<ReviewRequestSummary, ReviewRequestError>> {
222        self.call_with_authenticated_adapter(remote, move |adapter, remote| {
223            adapter.create_authenticated_review_request(remote, input)
224        })
225    }
226
227    fn refresh_review_request(
228        &self,
229        remote: ForgeRemote,
230        display_id: String,
231    ) -> ForgeFuture<Result<ReviewRequestSummary, ReviewRequestError>> {
232        self.call_with_authenticated_adapter(remote, move |adapter, remote| {
233            adapter.refresh_authenticated_review_request(remote, display_id)
234        })
235    }
236
237    fn review_request_metadata(
238        &self,
239        remote: ForgeRemote,
240        display_id: String,
241    ) -> ForgeFuture<Result<ReviewRequestMetadata, ReviewRequestError>> {
242        self.call_with_authenticated_adapter(remote, move |adapter, remote| {
243            adapter.authenticated_review_request_metadata(remote, display_id)
244        })
245    }
246
247    fn sync_review_request_metadata(
248        &self,
249        remote: ForgeRemote,
250        display_id: String,
251        input: UpdateReviewRequestInput,
252    ) -> ForgeFuture<Result<ReviewRequestSummary, ReviewRequestError>> {
253        self.call_with_authenticated_adapter(remote, move |adapter, remote| {
254            adapter.sync_authenticated_review_request_metadata(remote, display_id, input)
255        })
256    }
257
258    fn review_request_web_url(
259        &self,
260        review_request: &ReviewRequestSummary,
261    ) -> Result<String, ReviewRequestError> {
262        if review_request.web_url.trim().is_empty() {
263            return Err(ReviewRequestError::OperationFailed {
264                forge_kind: review_request.forge_kind,
265                message: "review request summary is missing a web URL".to_string(),
266            });
267        }
268
269        Ok(review_request.web_url.clone())
270    }
271
272    fn fetch_review_comment_snapshot(
273        &self,
274        remote: ForgeRemote,
275        display_id: String,
276    ) -> ForgeFuture<Result<ReviewCommentSnapshot, ReviewRequestError>> {
277        self.call_with_authenticated_adapter(remote, move |adapter, remote| {
278            adapter.fetch_authenticated_review_comment_snapshot(remote, display_id)
279        })
280    }
281
282    fn fetch_review_request_attention(
283        &self,
284        remote: ForgeRemote,
285        display_id: String,
286    ) -> ForgeFuture<Result<ReviewRequestAttention, ReviewRequestError>> {
287        self.call_with_authenticated_adapter(remote, move |adapter, remote| {
288            adapter.fetch_authenticated_review_request_attention(remote, display_id)
289        })
290    }
291
292    fn reply_to_thread(
293        &self,
294        remote: ForgeRemote,
295        display_id: String,
296        thread_id: String,
297        body: String,
298    ) -> ForgeFuture<Result<(), ReviewRequestError>> {
299        self.call_with_authenticated_adapter(remote, move |adapter, remote| {
300            adapter.reply_to_authenticated_thread(remote, display_id, thread_id, body)
301        })
302    }
303
304    fn resolve_thread(
305        &self,
306        remote: ForgeRemote,
307        display_id: String,
308        thread_id: String,
309    ) -> ForgeFuture<Result<(), ReviewRequestError>> {
310        self.call_with_authenticated_adapter(remote, move |adapter, remote| {
311            adapter.resolve_authenticated_thread(remote, display_id, thread_id)
312        })
313    }
314}
315
316/// Provider-specific operation boundary used after client-level authentication.
317///
318/// The production client selects one implementation, calls
319/// [`ReviewRequestAdapter::ensure_authenticated`] once, and then invokes the
320/// requested operation without provider-specific dispatch in each public
321/// method.
322pub(crate) trait ReviewRequestAdapter: Send + Sync {
323    /// Verifies that CLI authentication succeeds for `remote`.
324    ///
325    /// # Errors
326    /// Returns a provider-specific review-request error when the forge CLI is
327    /// unavailable, unauthenticated, or cannot resolve the target host.
328    fn ensure_authenticated(
329        &self,
330        remote: &ForgeRemote,
331    ) -> ForgeFuture<Result<(), ReviewRequestError>>;
332
333    /// Finds one review request after the production client has authenticated.
334    fn find_authenticated_by_source_branch(
335        &self,
336        remote: ForgeRemote,
337        source_branch: String,
338    ) -> ForgeFuture<Result<Option<ReviewRequestSummary>, ReviewRequestError>>;
339
340    /// Creates one review request after the production client has
341    /// authenticated.
342    fn create_authenticated_review_request(
343        &self,
344        remote: ForgeRemote,
345        input: CreateReviewRequestInput,
346    ) -> ForgeFuture<Result<ReviewRequestSummary, ReviewRequestError>>;
347
348    /// Refreshes one existing review request after authentication.
349    fn refresh_authenticated_review_request(
350        &self,
351        remote: ForgeRemote,
352        display_id: String,
353    ) -> ForgeFuture<Result<ReviewRequestSummary, ReviewRequestError>>;
354
355    /// Loads current review-request metadata after authentication.
356    fn authenticated_review_request_metadata(
357        &self,
358        remote: ForgeRemote,
359        display_id: String,
360    ) -> ForgeFuture<Result<ReviewRequestMetadata, ReviewRequestError>>;
361
362    /// Synchronizes review-request metadata after authentication.
363    fn sync_authenticated_review_request_metadata(
364        &self,
365        remote: ForgeRemote,
366        display_id: String,
367        input: UpdateReviewRequestInput,
368    ) -> ForgeFuture<Result<ReviewRequestSummary, ReviewRequestError>>;
369
370    /// Fetches a review-comment snapshot after authentication.
371    fn fetch_authenticated_review_comment_snapshot(
372        &self,
373        remote: ForgeRemote,
374        display_id: String,
375    ) -> ForgeFuture<Result<ReviewCommentSnapshot, ReviewRequestError>>;
376
377    /// Fetches review-request attention facts after authentication.
378    fn fetch_authenticated_review_request_attention(
379        &self,
380        remote: ForgeRemote,
381        display_id: String,
382    ) -> ForgeFuture<Result<ReviewRequestAttention, ReviewRequestError>>;
383
384    /// Adds one reply after authentication.
385    fn reply_to_authenticated_thread(
386        &self,
387        remote: ForgeRemote,
388        display_id: String,
389        thread_id: String,
390        body: String,
391    ) -> ForgeFuture<Result<(), ReviewRequestError>>;
392
393    /// Resolves one review thread after authentication.
394    fn resolve_authenticated_thread(
395        &self,
396        remote: ForgeRemote,
397        display_id: String,
398        thread_id: String,
399    ) -> ForgeFuture<Result<(), ReviewRequestError>>;
400}
401
402#[cfg(test)]
403#[path = "client_test.rs"]
404mod tests;