1use async_trait::async_trait;
8use chrono::{DateTime, Utc};
9use serde::{Deserialize, Serialize};
10
11use crate::{
12 Result,
13 types::{
14 CommentData, MergeResult, MergeStrategy, State, UserSummary, deserialize_u64_or_string,
15 },
16};
17
18#[derive(Debug, Clone, Serialize, Deserialize)]
23#[serde(rename_all = "camelCase")]
24pub struct PrData {
25 #[serde(deserialize_with = "deserialize_u64_or_string")]
27 pub number: u64,
28 pub title: String,
30 #[serde(skip_serializing_if = "Option::is_none")]
32 pub body: Option<String>,
33 pub state: State,
35 #[serde(alias = "isDraft")]
37 pub draft: bool,
38 pub author: UserSummary,
40 #[serde(alias = "baseRefName")]
42 pub base_branch: String,
43 #[serde(alias = "headRefName")]
45 pub head_branch: String,
46 pub created_at: DateTime<Utc>,
48 pub updated_at: DateTime<Utc>,
50 pub url: String,
52}
53
54#[derive(Debug, Clone)]
56pub struct CreatePrArgs {
57 pub title: String,
59 pub body: Option<String>,
61 pub head: String,
63 pub base: String,
65 pub draft: bool,
67 pub repo: Option<String>,
69}
70
71#[derive(Debug, Clone, Default)]
75pub struct ListPrArgs {
76 pub state: Option<State>,
78 pub limit: Option<u32>,
80}
81
82#[async_trait]
93pub trait PrProvider: std::fmt::Debug + Send + Sync {
94 async fn create(&self, args: CreatePrArgs) -> Result<PrData>;
100
101 async fn list(&self, args: ListPrArgs) -> Result<Vec<PrData>>;
107
108 async fn view(&self, number: u64) -> Result<PrData>;
114
115 async fn close(&self, number: u64) -> Result<PrData>;
121
122 async fn reopen(&self, number: u64) -> Result<PrData>;
128
129 async fn comment(&self, number: u64, body: &str) -> Result<CommentData>;
135
136 async fn merge(&self, number: u64, strategy: Option<MergeStrategy>) -> Result<MergeResult>;
145
146 async fn checkout(&self, number: u64) -> Result<()>;
152
153 async fn mark_ready(&self, number: u64) -> Result<PrData>;
159
160 async fn mark_wip(&self, number: u64) -> Result<PrData>;
166
167 async fn sync_branch(&self, number: u64) -> Result<()>;
173
174 async fn diff(&self, number: u64) -> Result<String>;
184
185 async fn patch(&self, number: u64) -> Result<String>;
195}
196
197#[cfg(test)]
198mod tests {
199 use super::*;
200
201 fn sample_pr_json() -> &'static str {
203 r#"{
204 "number": 101,
205 "title": "Add feature X",
206 "body": "Implements X per spec.",
207 "state": "open",
208 "draft": false,
209 "author": {"login": "alice", "id": "7"},
210 "baseBranch": "main",
211 "headBranch": "feature/x",
212 "createdAt": "2026-02-10T08:00:00Z",
213 "updatedAt": "2026-02-11T12:30:00Z",
214 "url": "https://github.com/octocat/hello-world/pull/101"
215 }"#
216 }
217
218 #[test]
219 fn test_should_deserialize_pr_data_from_gh_cli_json() {
220 let json = sample_pr_json();
221 let pr: PrData = serde_json::from_str(json).expect("valid PrData JSON");
222
223 assert_eq!(pr.number, 101);
224 assert_eq!(pr.title, "Add feature X");
225 assert_eq!(pr.body.as_deref(), Some("Implements X per spec."));
226 assert_eq!(pr.state, State::Open);
227 assert!(!pr.draft);
228 assert_eq!(pr.author.login, "alice");
229 assert_eq!(pr.author.id, "7");
230 assert_eq!(pr.base_branch, "main");
231 assert_eq!(pr.head_branch, "feature/x");
232 assert_eq!(pr.url, "https://github.com/octocat/hello-world/pull/101");
233 }
234
235 #[test]
236 fn test_should_roundtrip_pr_data_via_serde() {
237 let json = sample_pr_json();
238 let pr: PrData = serde_json::from_str(json).expect("deserialize");
239 let re_serialized = serde_json::to_string(&pr).expect("serialize");
240 let round_tripped: PrData = serde_json::from_str(&re_serialized).expect("re-deserialize");
241
242 assert_eq!(round_tripped.number, pr.number);
243 assert_eq!(round_tripped.title, pr.title);
244 assert_eq!(round_tripped.body, pr.body);
245 assert_eq!(round_tripped.state, pr.state);
246 assert_eq!(round_tripped.draft, pr.draft);
247 assert_eq!(round_tripped.base_branch, pr.base_branch);
248 assert_eq!(round_tripped.head_branch, pr.head_branch);
249 assert_eq!(round_tripped.url, pr.url);
250 assert_eq!(round_tripped.created_at, pr.created_at);
251 assert_eq!(round_tripped.updated_at, pr.updated_at);
252 }
253
254 #[test]
255 fn test_should_deserialize_draft_pr_with_null_body() {
256 let json = r#"{
257 "number": 5,
258 "title": "WIP experiment",
259 "body": null,
260 "state": "open",
261 "draft": true,
262 "author": {"login": "bob", "id": "9"},
263 "baseBranch": "main",
264 "headBranch": "wip",
265 "createdAt": "2026-04-01T00:00:00Z",
266 "updatedAt": "2026-04-02T00:00:00Z",
267 "url": "https://example.com/pull/5"
268 }"#;
269 let pr: PrData = serde_json::from_str(json).expect("deserialize");
270 assert!(pr.draft);
271 assert!(pr.body.is_none());
272 assert_eq!(pr.state, State::Open);
273 }
274
275 #[test]
276 fn test_should_omit_none_body_on_serialize() {
277 let json = sample_pr_json();
278 let mut pr: PrData = serde_json::from_str(json).expect("deserialize");
279 pr.body = None;
280 let serialized = serde_json::to_string(&pr).expect("serialize");
281 assert!(!serialized.contains("\"body\":null"));
283 assert!(!serialized.contains("\"body\": null"));
284 }
285
286 #[test]
287 fn test_should_serialize_camel_case_fields() {
288 let json = sample_pr_json();
289 let pr: PrData = serde_json::from_str(json).expect("deserialize");
290 let serialized = serde_json::to_string(&pr).expect("serialize");
291 assert!(serialized.contains("\"baseBranch\""));
293 assert!(serialized.contains("\"headBranch\""));
294 assert!(serialized.contains("\"createdAt\""));
295 assert!(serialized.contains("\"updatedAt\""));
296 assert!(!serialized.contains("\"base_branch\""));
298 assert!(!serialized.contains("\"head_branch\""));
299 }
300
301 #[test]
302 fn test_list_pr_args_default_is_empty() {
303 let args = ListPrArgs::default();
304 assert!(args.state.is_none());
305 assert!(args.limit.is_none());
306 }
307
308 #[test]
312 fn test_should_have_diff_and_patch_methods_on_trait() {
313 use async_trait::async_trait;
314
315 use crate::{Result, pr::PrProvider};
316
317 #[derive(Debug)]
318 struct Check;
319
320 #[async_trait]
321 impl PrProvider for Check {
322 async fn create(&self, _args: crate::pr::CreatePrArgs) -> Result<crate::pr::PrData> {
323 unimplemented!()
324 }
325 async fn list(&self, _args: crate::pr::ListPrArgs) -> Result<Vec<crate::pr::PrData>> {
326 unimplemented!()
327 }
328 async fn view(&self, _number: u64) -> Result<crate::pr::PrData> {
329 unimplemented!()
330 }
331 async fn close(&self, _number: u64) -> Result<crate::pr::PrData> {
332 unimplemented!()
333 }
334 async fn reopen(&self, _number: u64) -> Result<crate::pr::PrData> {
335 unimplemented!()
336 }
337 async fn comment(
338 &self,
339 _number: u64,
340 _body: &str,
341 ) -> Result<crate::types::CommentData> {
342 unimplemented!()
343 }
344 async fn merge(
345 &self,
346 _number: u64,
347 _strategy: Option<crate::types::MergeStrategy>,
348 ) -> Result<crate::types::MergeResult> {
349 unimplemented!()
350 }
351 async fn checkout(&self, _number: u64) -> Result<()> {
352 unimplemented!()
353 }
354 async fn mark_ready(&self, _number: u64) -> Result<crate::pr::PrData> {
355 unimplemented!()
356 }
357 async fn mark_wip(&self, _number: u64) -> Result<crate::pr::PrData> {
358 unimplemented!()
359 }
360 async fn sync_branch(&self, _number: u64) -> Result<()> {
361 unimplemented!()
362 }
363 async fn diff(&self, _number: u64) -> Result<String> {
364 unimplemented!()
365 }
366 async fn patch(&self, _number: u64) -> Result<String> {
367 unimplemented!()
368 }
369 }
370
371 let check = Check;
373 let _ = format!("{check:?}");
374 }
375}