git_commit_helper_cli/
github.rs1use anyhow::Result;
2use reqwest;
3use serde::Deserialize;
4use log::debug;
5#[derive(Debug, Deserialize)]
6struct Commit {
7 commit: CommitDetails,
8}
9
10#[derive(Debug, Deserialize)]
11struct CommitDetails {
12 message: String,
13}
14
15#[derive(Debug, Deserialize)]
16struct PullRequest {
17 diff_url: String,
18 title: String,
19 body: Option<String>,
20}
21
22use crate::terminal_format::print_progress;
23
24pub async fn get_pr_info(pr_url: &str) -> Result<String> {
25 debug!("从GitHub获取PR信息: {}", pr_url);
26
27 let parts: Vec<&str> = pr_url.split('/').collect();
30 if parts.len() < 7 {
31 return Err(anyhow::anyhow!("无效的GitHub PR URL"));
32 }
33
34 let owner = parts[3];
35 let repo = parts[4];
36 let pr_number = parts[6];
37
38 let api_url = format!(
40 "https://api.github.com/repos/{}/{}/pulls/{}",
41 owner, repo, pr_number
42 );
43
44 print_progress("正在请求 github.com 获取PR内容", None);
46
47 let client = reqwest::Client::new();
49 let pr: PullRequest = client
50 .get(&api_url)
51 .header("User-Agent", "git-commit-helper")
52 .header("Accept", "application/vnd.github.v3+json")
53 .send()
54 .await?
55 .json()
56 .await?;
57
58 print_progress("正在请求 github.com 获取PR内容", Some(100));
59
60 let mut info = format!("标题:{}\n", pr.title);
61 if let Some(body) = pr.body {
62 if !body.trim().is_empty() {
63 info.push_str(&format!("\n描述:\n{}", body));
64 }
65 }
66
67 Ok(info)
68}
69
70pub async fn get_commit_info(commit_url: &str) -> Result<String> {
71 debug!("从GitHub获取commit信息: {}", commit_url);
72
73 let parts: Vec<&str> = commit_url.split('/').collect();
76 if parts.len() < 7 {
77 return Err(anyhow::anyhow!("无效的GitHub commit URL"));
78 }
79
80 let owner = parts[3];
81 let repo = parts[4];
82 let commit_hash = parts[6];
83
84 let api_url = format!(
86 "https://api.github.com/repos/{}/{}/commits/{}",
87 owner, repo, commit_hash
88 );
89
90 let client = reqwest::Client::new();
92 let commit: Commit = client
93 .get(&api_url)
94 .header("User-Agent", "git-commit-helper")
95 .header("Accept", "application/vnd.github.v3+json")
96 .send()
97 .await?
98 .json()
99 .await?;
100
101 Ok(commit.commit.message)
102}
103
104pub async fn get_pr_diff(pr_url: &str) -> Result<String> {
105 debug!("从GitHub获取PR差异内容: {}", pr_url);
106
107 let parts: Vec<&str> = pr_url.split('/').collect();
110 if parts.len() < 7 {
111 return Err(anyhow::anyhow!("无效的GitHub PR URL"));
112 }
113
114 let owner = parts[3];
115 let repo = parts[4];
116 let pr_number = parts[6];
117
118 let api_url = format!(
120 "https://api.github.com/repos/{}/{}/pulls/{}",
121 owner, repo, pr_number
122 );
123
124 print_progress("正在请求 github.com 获取PR内容", None);
126
127 let client = reqwest::Client::new();
129 let pr: PullRequest = client
130 .get(&api_url)
131 .header("User-Agent", "git-commit-helper")
132 .header("Accept", "application/vnd.github.v3+json")
133 .send()
134 .await?
135 .json()
136 .await?;
137
138 print_progress("正在请求 github.com 获取PR内容", Some(60));
139
140 print_progress("正在请求 github.com 获取PR差异内容", None);
142 let diff = client
143 .get(&pr.diff_url)
144 .header("User-Agent", "git-commit-helper")
145 .send()
146 .await?
147 .text()
148 .await?;
149
150 print_progress("正在请求 github.com 获取PR差异内容", Some(100));
151
152 Ok(diff)
153}
154
155pub async fn get_commit_diff(commit_url: &str) -> Result<String> {
156 debug!("从GitHub获取commit差异内容: {}", commit_url);
157
158 let parts: Vec<&str> = commit_url.split('/').collect();
161 if parts.len() < 7 {
162 return Err(anyhow::anyhow!("无效的GitHub commit URL"));
163 }
164
165 let owner = parts[3];
166 let repo = parts[4];
167 let commit_hash = parts[6];
168
169 let api_url = format!(
171 "https://api.github.com/repos/{}/{}/commits/{}",
172 owner, repo, commit_hash
173 );
174
175 let client = reqwest::Client::new();
177 let diff = client
178 .get(&api_url)
179 .header("User-Agent", "git-commit-helper")
180 .header("Accept", "application/vnd.github.v3.diff")
181 .send()
182 .await?
183 .text()
184 .await?;
185
186 Ok(diff)
187}