|
| 1 | +//! Pull request operations |
| 2 | +
|
| 3 | +use crate::client::GitHubClient; |
| 4 | +use anyhow::{Context, Result}; |
| 5 | +use serde::{Deserialize, Serialize}; |
| 6 | + |
| 7 | +#[derive(Serialize)] |
| 8 | +pub(crate) struct CreatePullRequestPayload<'a> { |
| 9 | + title: &'a str, |
| 10 | + head: &'a str, |
| 11 | + base: &'a str, |
| 12 | + body: &'a str, |
| 13 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 14 | + draft: Option<bool>, |
| 15 | +} |
| 16 | + |
| 17 | +#[derive(Deserialize, Debug)] |
| 18 | +pub struct PullRequest { |
| 19 | + pub html_url: String, |
| 20 | + pub number: u64, |
| 21 | + pub id: u64, |
| 22 | + pub title: String, |
| 23 | + pub state: String, |
| 24 | +} |
| 25 | + |
| 26 | +/// Parameters for creating a pull request |
| 27 | +#[derive(Debug, Clone)] |
| 28 | +pub struct PullRequestParams<'a> { |
| 29 | + pub owner: &'a str, |
| 30 | + pub repo: &'a str, |
| 31 | + pub title: &'a str, |
| 32 | + pub head: &'a str, |
| 33 | + pub base: &'a str, |
| 34 | + pub body: &'a str, |
| 35 | + pub draft: bool, |
| 36 | +} |
| 37 | + |
| 38 | +impl<'a> PullRequestParams<'a> { |
| 39 | + pub fn new( |
| 40 | + owner: &'a str, |
| 41 | + repo: &'a str, |
| 42 | + title: &'a str, |
| 43 | + head: &'a str, |
| 44 | + base: &'a str, |
| 45 | + body: &'a str, |
| 46 | + draft: bool, |
| 47 | + ) -> Self { |
| 48 | + Self { |
| 49 | + owner, |
| 50 | + repo, |
| 51 | + title, |
| 52 | + head, |
| 53 | + base, |
| 54 | + body, |
| 55 | + draft, |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +impl GitHubClient { |
| 61 | + /// Create a pull request on GitHub |
| 62 | + /// |
| 63 | + /// # Arguments |
| 64 | + /// * `params` - Pull request parameters including owner, repo, title, head, base, body, and draft status |
| 65 | + /// |
| 66 | + /// # Returns |
| 67 | + /// A PullRequest struct containing the created PR information |
| 68 | + /// |
| 69 | + /// # Errors |
| 70 | + /// Returns an error if: |
| 71 | + /// - No authentication token is configured |
| 72 | + /// - The API request fails |
| 73 | + /// - The response cannot be parsed |
| 74 | + pub async fn create_pull_request(&self, params: PullRequestParams<'_>) -> Result<PullRequest> { |
| 75 | + if self.token.is_none() { |
| 76 | + anyhow::bail!( |
| 77 | + "GitHub token is required for creating pull requests. Set GITHUB_TOKEN environment variable." |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + let url = format!( |
| 82 | + "https://api.github.com/repos/{}/{}/pulls", |
| 83 | + params.owner, params.repo |
| 84 | + ); |
| 85 | + |
| 86 | + let payload = CreatePullRequestPayload { |
| 87 | + title: params.title, |
| 88 | + head: params.head, |
| 89 | + base: params.base, |
| 90 | + body: params.body, |
| 91 | + draft: if params.draft { Some(true) } else { None }, |
| 92 | + }; |
| 93 | + |
| 94 | + let mut request = self.client.post(&url).header("User-Agent", "repos-cli"); |
| 95 | + |
| 96 | + if let Some(token) = &self.token { |
| 97 | + request = request.header("Authorization", format!("token {}", token)); |
| 98 | + } |
| 99 | + |
| 100 | + let response = request.json(&payload).send().await?; |
| 101 | + |
| 102 | + if !response.status().is_success() { |
| 103 | + let status = response.status(); |
| 104 | + let error_text = response |
| 105 | + .text() |
| 106 | + .await |
| 107 | + .unwrap_or_else(|_| "Unknown error".to_string()); |
| 108 | + return Err(anyhow::anyhow!( |
| 109 | + "Failed to create pull request ({} {}): {}", |
| 110 | + status.as_u16(), |
| 111 | + status.canonical_reason().unwrap_or("Unknown"), |
| 112 | + error_text |
| 113 | + )); |
| 114 | + } |
| 115 | + |
| 116 | + let pr: PullRequest = response |
| 117 | + .json() |
| 118 | + .await |
| 119 | + .context("Failed to parse PR creation response")?; |
| 120 | + Ok(pr) |
| 121 | + } |
| 122 | +} |
0 commit comments