|
| 1 | +use anyhow::Result; |
| 2 | +use std::sync::Arc; |
| 3 | + |
| 4 | +use crate::config; |
| 5 | +use crate::output::OutputFormat; |
| 6 | +use crate::server::pr_readiness::{get_pr_readiness_snapshot, PrReadinessSnapshot}; |
| 7 | +use crate::server::state::AppState; |
| 8 | + |
| 9 | +use super::gh::{fetch_pr_metadata, resolve_pr_number}; |
| 10 | + |
| 11 | +struct PrReadinessTarget { |
| 12 | + repo: String, |
| 13 | + pr_number: u32, |
| 14 | + current_head_sha: Option<String>, |
| 15 | +} |
| 16 | + |
| 17 | +pub(super) async fn run_pr_readiness_flow( |
| 18 | + number: Option<u32>, |
| 19 | + repo: Option<&str>, |
| 20 | + config: config::Config, |
| 21 | + format: OutputFormat, |
| 22 | +) -> Result<()> { |
| 23 | + let target = resolve_pr_readiness_target(number, repo)?; |
| 24 | + let state = Arc::new(AppState::new(config).await?); |
| 25 | + let snapshot = get_pr_readiness_snapshot( |
| 26 | + &state, |
| 27 | + &target.repo, |
| 28 | + target.pr_number, |
| 29 | + target.current_head_sha.as_deref(), |
| 30 | + ) |
| 31 | + .await; |
| 32 | + |
| 33 | + match format { |
| 34 | + OutputFormat::Json => println!("{}", serde_json::to_string_pretty(&snapshot)?), |
| 35 | + OutputFormat::Markdown | OutputFormat::Patch => { |
| 36 | + println!("{}", format_pr_readiness_markdown(&snapshot)) |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + Ok(()) |
| 41 | +} |
| 42 | + |
| 43 | +fn resolve_pr_readiness_target( |
| 44 | + number: Option<u32>, |
| 45 | + repo: Option<&str>, |
| 46 | +) -> Result<PrReadinessTarget> { |
| 47 | + match (number, repo) { |
| 48 | + (Some(pr_number), Some(repo)) => { |
| 49 | + let current_head_sha = fetch_pr_metadata(&pr_number.to_string(), Some(repo)) |
| 50 | + .ok() |
| 51 | + .map(|metadata| metadata.head_ref_oid); |
| 52 | + Ok(PrReadinessTarget { |
| 53 | + repo: repo.to_string(), |
| 54 | + pr_number, |
| 55 | + current_head_sha, |
| 56 | + }) |
| 57 | + } |
| 58 | + _ => { |
| 59 | + let pr_number = resolve_pr_number(number, repo)?; |
| 60 | + let metadata = fetch_pr_metadata(&pr_number, repo)?; |
| 61 | + Ok(PrReadinessTarget { |
| 62 | + repo: repo |
| 63 | + .map(str::to_string) |
| 64 | + .unwrap_or(metadata.base_repository.name_with_owner), |
| 65 | + pr_number: metadata.number, |
| 66 | + current_head_sha: Some(metadata.head_ref_oid), |
| 67 | + }) |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +fn format_pr_readiness_markdown(snapshot: &PrReadinessSnapshot) -> String { |
| 73 | + let mut output = String::new(); |
| 74 | + output.push_str("# PR Readiness\n\n"); |
| 75 | + output.push_str(&format!( |
| 76 | + "- PR: `{}#{}`\n", |
| 77 | + snapshot.repo, snapshot.pr_number |
| 78 | + )); |
| 79 | + if let Some(current_head_sha) = snapshot.current_head_sha.as_deref() { |
| 80 | + output.push_str(&format!( |
| 81 | + "- Current head: `{}`\n", |
| 82 | + short_sha(current_head_sha) |
| 83 | + )); |
| 84 | + } |
| 85 | + |
| 86 | + match &snapshot.latest_review { |
| 87 | + Some(review) => { |
| 88 | + output.push_str(&format!( |
| 89 | + "- Latest DiffScope review: `{}` ({:?})\n", |
| 90 | + review.id, review.status |
| 91 | + )); |
| 92 | + if let Some(reviewed_head_sha) = review.reviewed_head_sha.as_deref() { |
| 93 | + output.push_str(&format!( |
| 94 | + "- Reviewed head: `{}`\n", |
| 95 | + short_sha(reviewed_head_sha) |
| 96 | + )); |
| 97 | + } |
| 98 | + if let Some(summary) = review.summary.as_ref() { |
| 99 | + output.push_str(&format!("- Merge readiness: {}\n", summary.merge_readiness)); |
| 100 | + output.push_str(&format!("- Open blockers: {}\n", summary.open_blockers)); |
| 101 | + output.push_str(&format!( |
| 102 | + "- Lifecycle: {} open · {} resolved · {} dismissed\n", |
| 103 | + summary.open_comments, summary.resolved_comments, summary.dismissed_comments |
| 104 | + )); |
| 105 | + output.push_str(&format!("- Verification: {}\n", summary.verification.state)); |
| 106 | + if !summary.readiness_reasons.is_empty() { |
| 107 | + output.push_str("- Readiness reasons:\n"); |
| 108 | + for reason in &summary.readiness_reasons { |
| 109 | + output.push_str(&format!(" - {}\n", reason)); |
| 110 | + } |
| 111 | + } |
| 112 | + } else { |
| 113 | + output.push_str("- State: readiness summary is not available yet\n"); |
| 114 | + } |
| 115 | + } |
| 116 | + None => { |
| 117 | + output.push_str("- Latest DiffScope review: none\n"); |
| 118 | + output.push_str("- State: no stored PR readiness summary found\n"); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + output |
| 123 | +} |
| 124 | + |
| 125 | +fn short_sha(sha: &str) -> &str { |
| 126 | + sha.get(..12).unwrap_or(sha) |
| 127 | +} |
| 128 | + |
| 129 | +#[cfg(test)] |
| 130 | +mod tests { |
| 131 | + use super::*; |
| 132 | + use crate::core::comment::{MergeReadiness, ReviewVerificationState}; |
| 133 | + |
| 134 | + #[test] |
| 135 | + fn markdown_output_includes_summary_fields() { |
| 136 | + let mut summary = crate::core::CommentSynthesizer::generate_summary(&[]); |
| 137 | + summary.merge_readiness = MergeReadiness::NeedsAttention; |
| 138 | + summary.open_blockers = 2; |
| 139 | + summary.open_comments = 3; |
| 140 | + summary.resolved_comments = 1; |
| 141 | + summary.dismissed_comments = 1; |
| 142 | + summary.verification.state = ReviewVerificationState::Inconclusive; |
| 143 | + summary.readiness_reasons = vec!["new commits landed after this review".to_string()]; |
| 144 | + let snapshot = PrReadinessSnapshot { |
| 145 | + repo: "owner/repo".to_string(), |
| 146 | + pr_number: 42, |
| 147 | + diff_source: "pr:owner/repo#42".to_string(), |
| 148 | + current_head_sha: Some("0123456789abcdef".to_string()), |
| 149 | + latest_review: Some(crate::server::pr_readiness::PrReadinessReview { |
| 150 | + id: "review-1".to_string(), |
| 151 | + status: crate::server::state::ReviewStatus::Complete, |
| 152 | + started_at: 10, |
| 153 | + completed_at: Some(11), |
| 154 | + reviewed_head_sha: Some("fedcba9876543210".to_string()), |
| 155 | + summary: Some(summary), |
| 156 | + files_reviewed: 2, |
| 157 | + comment_count: 4, |
| 158 | + error: None, |
| 159 | + }), |
| 160 | + }; |
| 161 | + |
| 162 | + let output = format_pr_readiness_markdown(&snapshot); |
| 163 | + assert!(output.contains("# PR Readiness")); |
| 164 | + assert!(output.contains("PR: `owner/repo#42`")); |
| 165 | + assert!(output.contains("Current head: `0123456789ab`")); |
| 166 | + assert!(output.contains("Merge readiness: Needs attention")); |
| 167 | + assert!(output.contains("Open blockers: 2")); |
| 168 | + assert!(output.contains("new commits landed after this review")); |
| 169 | + } |
| 170 | + |
| 171 | + #[test] |
| 172 | + fn markdown_output_handles_missing_reviews() { |
| 173 | + let snapshot = PrReadinessSnapshot { |
| 174 | + repo: "owner/repo".to_string(), |
| 175 | + pr_number: 42, |
| 176 | + diff_source: "pr:owner/repo#42".to_string(), |
| 177 | + current_head_sha: None, |
| 178 | + latest_review: None, |
| 179 | + }; |
| 180 | + |
| 181 | + let output = format_pr_readiness_markdown(&snapshot); |
| 182 | + assert!(output.contains("Latest DiffScope review: none")); |
| 183 | + assert!(output.contains("no stored PR readiness summary found")); |
| 184 | + } |
| 185 | +} |
0 commit comments