gitflow-github 2.0.0

gf 的 GitHub 适配器,基于 gh CLI 提供 Issue/PR/Release/Review 等能力。GitHub adapter for gf via gh CLI.
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
//! GitHub Review 提供者实现。
//!
//! 通过 `gh` CLI 实现 [`ReviewProvider`] trait,支持 PR 审查的评论、
//! 批准、要求修改及提交审查。
//! 所有方法通过 `tokio::process::Command` 调用 `gh`,捕获 stdout 并解析 JSON。

use async_trait::async_trait;
use gitflow_core::{
    CoreError, Result,
    review::{ReviewData, ReviewProvider, ReviewState},
};
use tracing::debug;

use crate::{datetime::parse_api_datetime, error::parse_gh_error, issue::GitHubUser};

/// GitHub Review 提供者,通过 `gh` CLI 操作。
///
/// 该结构体通过调用 `gh` CLI 实现 [`ReviewProvider`] trait 的所有方法,
/// 使上层命令能够以统一的方式操作 GitHub PR 审查。
///
/// # Examples
///
/// ```no_run
/// use gitflow_github::GitHubReviewProvider;
///
/// let provider = GitHubReviewProvider::new("octocat/hello-world");
/// ```
#[derive(Debug, Clone)]
pub struct GitHubReviewProvider {
    /// GitHub `owner/repo`,如 `"byx-darwin/gitflow-cli"`。
    repo: String,
}

impl GitHubReviewProvider {
    /// 创建新的 GitHub Review 提供者。
    ///
    /// `repo` 格式为 `owner/repo`。
    #[must_use]
    pub fn new(repo: impl Into<String>) -> Self {
        Self { repo: repo.into() }
    }

    /// Create a new provider from a shared [`Session`].
    ///
    /// This enables state reuse across multiple operations in workflow chains.
    #[must_use]
    pub fn with_session(session: &gitflow_core::Session) -> Self {
        Self {
            repo: session.repo.clone(),
        }
    }
}

#[async_trait]
impl ReviewProvider for GitHubReviewProvider {
    async fn comment(&self, pr_number: u64, body: &str) -> Result<ReviewData> {
        debug!(repo = %self.repo, number = pr_number, "spawning `gh pr review --comment`");

        // 1. 执行 gh pr review --comment(不返回 JSON)
        let number_str = pr_number.to_string();
        let output = tokio::process::Command::new("gh")
            .args(["pr", "review"])
            .arg(&number_str)
            .arg("--comment")
            .arg("--body")
            .arg(body)
            .arg("--repo")
            .arg(&self.repo)
            .output()
            .await
            .map_err(|e| CoreError::Platform(format!("Failed to spawn gh: {e}")))?;

        if !output.status.success() {
            return Err(parse_gh_error(&output.stderr).into());
        }

        // 2. 使用 gh api 获取该 PR 的最新 review
        self.fetch_latest_review(pr_number).await
    }

    async fn approve(&self, pr_number: u64, body: Option<&str>) -> Result<ReviewData> {
        debug!(repo = %self.repo, number = pr_number, "spawning `gh pr review --approve`");

        let mut cmd = tokio::process::Command::new("gh");
        cmd.args(["pr", "review"])
            .arg(pr_number.to_string())
            .arg("--approve")
            .arg("--repo")
            .arg(&self.repo);

        if let Some(b) = body {
            cmd.arg("--body").arg(b);
        }

        let output = cmd
            .output()
            .await
            .map_err(|e| CoreError::Platform(format!("Failed to spawn gh: {e}")))?;

        if !output.status.success() {
            let gh_err = parse_gh_error(&output.stderr);
            // 检测 "approve your own pull request" 错误
            if gh_err
                .user_message
                .contains("approve your own pull request")
            {
                return Err(CoreError::Platform(
                    "GitHub 不允许批准自己的 PR。可以请求其他维护者审查。".to_string(),
                ));
            }
            return Err(gh_err.into());
        }

        // 使用 gh api 获取该 PR 的最新 review
        self.fetch_latest_review(pr_number).await
    }

    async fn request_changes(&self, pr_number: u64, body: &str) -> Result<ReviewData> {
        debug!(repo = %self.repo, number = pr_number, "spawning `gh pr review --request-changes`");

        let output = tokio::process::Command::new("gh")
            .args(["pr", "review"])
            .arg(pr_number.to_string())
            .arg("--request-changes")
            .arg("--body")
            .arg(body)
            .arg("--repo")
            .arg(&self.repo)
            .output()
            .await
            .map_err(|e| CoreError::Platform(format!("Failed to spawn gh: {e}")))?;

        if !output.status.success() {
            return Err(parse_gh_error(&output.stderr).into());
        }

        // 使用 gh api 获取该 PR 的最新 review
        self.fetch_latest_review(pr_number).await
    }

    async fn submit_review(
        &self,
        pr_number: u64,
        event: ReviewState,
        body: Option<&str>,
    ) -> Result<ReviewData> {
        debug!(repo = %self.repo, number = pr_number, ?event, "spawning `gh pr review`");

        let mut cmd = tokio::process::Command::new("gh");
        cmd.args(["pr", "review"])
            .arg(pr_number.to_string())
            .arg("--repo")
            .arg(&self.repo);

        match event {
            ReviewState::Approved => {
                cmd.arg("--approve");
            }
            ReviewState::ChangesRequested => {
                cmd.arg("--request-changes");
            }
            ReviewState::Commented => {
                cmd.arg("--comment");
            }
        }

        if let Some(b) = body {
            cmd.arg("--body").arg(b);
        }

        let output = cmd
            .output()
            .await
            .map_err(|e| CoreError::Platform(format!("Failed to spawn gh: {e}")))?;

        if !output.status.success() {
            let gh_err = parse_gh_error(&output.stderr);
            if gh_err
                .user_message
                .contains("approve your own pull request")
            {
                return Err(CoreError::Platform(
                    "GitHub 不允许批准自己的 PR。可以请求其他维护者审查。".to_string(),
                ));
            }
            return Err(gh_err.into());
        }

        // 使用 gh api 获取该 PR 的最新 review
        self.fetch_latest_review(pr_number).await
    }
}

impl GitHubReviewProvider {
    /// 使用 gh api 获取指定 PR 的最新 review。
    ///
    /// 在提交 review 后调用,以获取完整的 review 数据(ID、状态、时间戳等)。
    async fn fetch_latest_review(&self, pr_number: u64) -> Result<ReviewData> {
        let api_path = format!(
            "repos/{repo}/pulls/{number}/reviews?per_page=1",
            repo = self.repo,
            number = pr_number
        );

        let output = tokio::process::Command::new("gh")
            .args(["api", &api_path])
            .output()
            .await
            .map_err(|e| CoreError::Platform(format!("Failed to spawn gh api: {e}")))?;

        if !output.status.success() {
            let gh_err = String::from_utf8_lossy(&output.stderr);
            return Err(CoreError::Platform(format!(
                "Failed to fetch review via gh api: {gh_err}"
            )));
        }

        let reviews: Vec<GitHubReviewApiResponse> =
            serde_json::from_slice(&output.stdout).map_err(CoreError::Serialization)?;

        let review = reviews
            .into_iter()
            .next()
            .ok_or_else(|| CoreError::Platform("No review returned from gh api".to_string()))?;

        Ok(review.into())
    }
}

/// GitHub API Review 响应结构。
///
/// 用于解析 `gh api repos/{owner}/{repo}/pulls/{number}/reviews` 的返回数据。
#[derive(Debug, Clone, serde::Deserialize)]
pub struct GitHubReviewApiResponse {
    /// Review 的 numeric ID。
    pub id: u64,
    /// Review 的状态(`APPROVED`, `CHANGES_REQUESTED`, `COMMENTED` 等)。
    pub state: String,
    /// Review 正文(Markdown,可选)。
    #[serde(default)]
    pub body: Option<String>,
    /// 审查人。
    pub user: GitHubUser,
    /// 提交时间(UTC,ISO 8601 格式)。
    pub submitted_at: String,
}

impl From<GitHubReviewApiResponse> for ReviewData {
    fn from(api: GitHubReviewApiResponse) -> Self {
        Self {
            id: api.id,
            state: match api.state.as_str() {
                "APPROVED" => ReviewState::Approved,
                "CHANGES_REQUESTED" => ReviewState::ChangesRequested,
                _ => ReviewState::Commented,
            },
            body: api.body,
            author: gitflow_core::types::UserSummary {
                login: api.user.login,
                id: api.user.id.to_string(),
            },
            submitted_at: Some(parse_api_datetime(&api.submitted_at)),
        }
    }
}

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

    #[test]
    fn test_should_construct_github_review_provider() {
        let provider = GitHubReviewProvider::new("octocat/hello-world");
        assert_eq!(provider.repo, "octocat/hello-world");
    }

    #[test]
    fn test_should_construct_github_review_provider_from_string() {
        let repo = String::from("octocat/hello-world");
        let provider = GitHubReviewProvider::new(repo);
        assert_eq!(provider.repo, "octocat/hello-world");
    }

    #[test]
    fn test_should_deserialize_review_data_from_gh_output() {
        // 模拟 `gh pr review --json ...` 的实际输出
        let gh_json = br#"{
            "id": 2001,
            "state": "approved",
            "body": "Looks great, LGTM!",
            "author": {"login": "reviewer", "id": "42"},
            "submittedAt": "2026-05-20T14:30:00Z"
        }"#;

        let review: ReviewData = serde_json::from_slice(gh_json).expect("valid ReviewData JSON");
        assert_eq!(review.id, 2001);
        assert_eq!(review.state, ReviewState::Approved);
        assert_eq!(review.body.as_deref(), Some("Looks great, LGTM!"));
        assert_eq!(review.author.login, "reviewer");
        assert_eq!(review.author.id, "42");
    }

    #[test]
    fn test_should_deserialize_changes_requested_review_from_gh_output() {
        let gh_json = br#"{
            "id": 2002,
            "state": "changes_requested",
            "body": "Please fix the error handling",
            "author": {"login": "senior-dev", "id": "7"},
            "submittedAt": "2026-05-21T09:00:00Z"
        }"#;

        let review: ReviewData = serde_json::from_slice(gh_json).expect("valid ReviewData");
        assert_eq!(review.state, ReviewState::ChangesRequested);
        assert_eq!(
            review.body.as_deref(),
            Some("Please fix the error handling")
        );
    }

    #[test]
    fn test_should_deserialize_commented_review_from_gh_output() {
        let gh_json = br#"{
            "id": 2003,
            "state": "commented",
            "body": null,
            "author": {"login": "observer", "id": "15"},
            "submittedAt": "2026-05-22T11:00:00Z"
        }"#;

        let review: ReviewData = serde_json::from_slice(gh_json).expect("valid ReviewData");
        assert_eq!(review.state, ReviewState::Commented);
        assert!(review.body.is_none());
    }

    #[test]
    fn test_should_debug_format_provider() {
        let provider = GitHubReviewProvider::new("octocat/hello-world");
        let debug = format!("{provider:?}");
        assert!(debug.contains("GitHubReviewProvider"));
        assert!(debug.contains("octocat/hello-world"));
    }

    #[test]
    fn test_should_create_provider_with_different_repos() {
        let r1 = GitHubReviewProvider::new("org/repo-a");
        let r2 = GitHubReviewProvider::new("org/repo-b");
        assert_eq!(r1.repo, "org/repo-a");
        assert_eq!(r2.repo, "org/repo-b");
    }

    #[test]
    fn test_should_clone_github_review_provider() {
        let original = GitHubReviewProvider::new("owner/repo");
        let cloned = original.clone();
        assert_eq!(original.repo, cloned.repo);
    }

    #[test]
    fn test_should_deserialize_github_review_api_response() {
        let json = r#"{
            "id": 12345,
            "state": "APPROVED",
            "body": "LGTM",
            "user": {"login": "octocat", "id": 1},
            "submitted_at": "2026-08-03T10:00:00Z"
        }"#;

        let response: GitHubReviewApiResponse = serde_json::from_str(json).expect("valid response");

        assert_eq!(response.id, 12345);
        assert_eq!(response.state, "APPROVED");
        assert_eq!(response.body, Some("LGTM".to_string()));
        assert_eq!(response.user.login, "octocat");
        assert_eq!(response.user.id, 1);
        assert_eq!(response.submitted_at, "2026-08-03T10:00:00Z");
    }

    #[test]
    fn test_should_convert_github_review_api_response_to_review_data() {
        let api_response = GitHubReviewApiResponse {
            id: 12345,
            state: "APPROVED".to_string(),
            body: Some("LGTM".to_string()),
            user: GitHubUser {
                login: "octocat".to_string(),
                id: 1,
            },
            submitted_at: "2026-08-03T10:00:00Z".to_string(),
        };

        let review_data: ReviewData = api_response.into();

        assert_eq!(review_data.id, 12345);
        assert_eq!(review_data.state, ReviewState::Approved);
        assert_eq!(review_data.body, Some("LGTM".to_string()));
        assert_eq!(review_data.author.login, "octocat");
        assert_eq!(review_data.author.id, "1");
    }

    #[test]
    fn test_should_deserialize_review_with_present_submitted_at_as_some() {
        let api_response = GitHubReviewApiResponse {
            id: 1,
            state: "APPROVED".to_string(),
            body: None,
            user: GitHubUser {
                login: "user".to_string(),
                id: 1,
            },
            submitted_at: "2026-08-03T10:00:00Z".to_string(),
        };
        let review_data: ReviewData = api_response.into();
        assert!(review_data.submitted_at.is_some());
    }

    #[test]
    fn test_should_use_epoch_for_malformed_review_timestamp() {
        let api_response = GitHubReviewApiResponse {
            id: 1,
            state: "APPROVED".to_string(),
            body: None,
            user: GitHubUser {
                login: "user".to_string(),
                id: 1,
            },
            submitted_at: "not-a-timestamp".to_string(),
        };

        let review_data: ReviewData = api_response.into();
        assert_eq!(review_data.submitted_at, Some(chrono::DateTime::UNIX_EPOCH));
    }

    #[test]
    fn test_should_convert_all_review_states() {
        let states = vec![
            ("APPROVED", ReviewState::Approved),
            ("CHANGES_REQUESTED", ReviewState::ChangesRequested),
            ("COMMENTED", ReviewState::Commented),
        ];

        for (state_str, expected_state) in states {
            let api_response = GitHubReviewApiResponse {
                id: 1,
                state: state_str.to_string(),
                body: None,
                user: GitHubUser {
                    login: "test".to_string(),
                    id: 1,
                },
                submitted_at: "2026-08-03T10:00:00Z".to_string(),
            };

            let review_data: ReviewData = api_response.into();
            assert_eq!(review_data.state, expected_state);
        }
    }

    #[test]
    fn test_should_map_unknown_review_state_to_commented() {
        let api_response = GitHubReviewApiResponse {
            id: 99,
            state: "PENDING".to_string(),
            body: None,
            user: GitHubUser {
                login: "bot".to_string(),
                id: 0,
            },
            submitted_at: "2026-08-03T10:00:00Z".to_string(),
        };

        let review_data: ReviewData = api_response.into();
        assert_eq!(review_data.state, ReviewState::Commented);
    }

    #[test]
    fn test_should_map_dismissed_review_state_to_commented() {
        let api_response = GitHubReviewApiResponse {
            id: 100,
            state: "DISMISSED".to_string(),
            body: Some("Outdated review".to_string()),
            user: GitHubUser {
                login: "admin".to_string(),
                id: 1,
            },
            submitted_at: "2026-08-03T10:00:00Z".to_string(),
        };

        let review_data: ReviewData = api_response.into();
        assert_eq!(review_data.state, ReviewState::Commented);
        assert_eq!(review_data.id, 100);
    }
}