gitai/features/commit/
types.rs

1use schemars::JsonSchema;
2use serde::{Deserialize, Serialize};
3use std::fmt::Write as _;
4use textwrap;
5
6/// Model for commit message generation results
7#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone)]
8pub struct GeneratedMessage {
9    /// Commit message title/subject line
10    pub title: String,
11    /// Detailed commit message body
12    pub message: String,
13}
14
15/// Model for pull request description generation results
16#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone)]
17pub struct GeneratedPullRequest {
18    /// Pull request title
19    pub title: String,
20    /// Brief summary of the changes
21    pub summary: String,
22    /// Detailed description of what was changed and why
23    pub description: String,
24    /// List of commit messages included in this PR
25    #[serde(skip_serializing_if = "Vec::is_empty")]
26    pub commits: Vec<String>,
27    /// Breaking changes if any
28    #[serde(skip_serializing_if = "Vec::is_empty")]
29    pub breaking_changes: Vec<String>,
30    /// Testing instructions for reviewers
31    #[serde(skip_serializing_if = "Option::is_none")]
32    pub testing_notes: Option<String>,
33    /// Additional notes or context
34    #[serde(skip_serializing_if = "Option::is_none")]
35    pub notes: Option<String>,
36}
37
38/// Formats a commit message from a `GeneratedMessage`
39pub fn format_commit_message(response: &GeneratedMessage) -> String {
40    let mut message = String::new();
41
42    message.push_str(&response.title);
43    message.push_str("\n\n");
44
45    for line in textwrap::wrap(&response.message, 78) {
46        message.push_str(&line);
47        message.push('\n');
48    }
49
50    message
51}
52
53/// Formats a pull request description from a `GeneratedPullRequest`
54pub fn format_pull_request(response: &GeneratedPullRequest) -> String {
55    let mut message = String::new();
56
57    // Summary - no word wrapping for web UI display
58    let _ = writeln!(&mut message, "## Summary");
59    let _ = writeln!(&mut message, "{}", response.summary);
60    message.push('\n');
61
62    // Description - no word wrapping for web UI display
63    let _ = writeln!(&mut message, "## Description");
64    let _ = writeln!(&mut message, "{}", response.description);
65    message.push('\n');
66
67    // Commits
68    if !response.commits.is_empty() {
69        let _ = writeln!(&mut message, "## Commits");
70        for commit in &response.commits {
71            let _ = writeln!(&mut message, "- {commit}");
72        }
73        message.push('\n');
74    }
75
76    // Breaking changes
77    if !response.breaking_changes.is_empty() {
78        let _ = writeln!(&mut message, "## Breaking Changes");
79        for change in &response.breaking_changes {
80            let _ = writeln!(&mut message, "- {change}");
81        }
82        message.push('\n');
83    }
84
85    // Testing notes - no word wrapping for web UI display
86    if let Some(testing) = &response.testing_notes {
87        let _ = writeln!(&mut message, "## Testing");
88        let _ = writeln!(&mut message, "{testing}");
89        message.push('\n');
90    }
91
92    // Additional notes - no word wrapping for web UI display
93    if let Some(notes) = &response.notes {
94        let _ = writeln!(&mut message, "## Notes");
95        let _ = writeln!(&mut message, "{notes}");
96    }
97
98    message
99}