1use std::str::FromStr;
2use std::sync::{Arc, OnceLock};
3
4use anyhow::{bail, Context, Result};
5use async_trait::async_trait;
6use futures::AsyncReadExt;
7use http_client::{AsyncBody, HttpClient, HttpRequestExt, Request};
8use regex::Regex;
9use serde::Deserialize;
10use url::Url;
11
12use git::{
13 BuildCommitPermalinkParams, BuildPermalinkParams, GitHostingProvider, Oid, ParsedGitRemote,
14 PullRequest, RemoteUrl,
15};
16
17fn pull_request_number_regex() -> &'static Regex {
18 static PULL_REQUEST_NUMBER_REGEX: OnceLock<Regex> = OnceLock::new();
19
20 PULL_REQUEST_NUMBER_REGEX.get_or_init(|| Regex::new(r"\(#(\d+)\)$").unwrap())
21}
22
23#[derive(Debug, Deserialize)]
24struct CommitDetails {
25 commit: Commit,
26 author: Option<User>,
27}
28
29#[derive(Debug, Deserialize)]
30struct Commit {
31 author: Author,
32}
33
34#[derive(Debug, Deserialize)]
35struct Author {
36 email: String,
37}
38
39#[derive(Debug, Deserialize)]
40struct User {
41 pub id: u64,
42 pub avatar_url: String,
43}
44
45pub struct Github;
46
47impl Github {
48 async fn fetch_github_commit_author(
49 &self,
50 repo_owner: &str,
51 repo: &str,
52 commit: &str,
53 client: &Arc<dyn HttpClient>,
54 ) -> Result<Option<User>> {
55 let url = format!("https://api.github.com/repos/{repo_owner}/{repo}/commits/{commit}");
56
57 let mut request = Request::get(&url)
58 .header("Content-Type", "application/json")
59 .follow_redirects(http_client::RedirectPolicy::FollowAll);
60
61 if let Ok(github_token) = std::env::var("GITHUB_TOKEN") {
62 request = request.header("Authorization", format!("Bearer {}", github_token));
63 }
64
65 let mut response = client
66 .send(request.body(AsyncBody::default())?)
67 .await
68 .with_context(|| format!("error fetching GitHub commit details at {:?}", url))?;
69
70 let mut body = Vec::new();
71 response.body_mut().read_to_end(&mut body).await?;
72
73 if response.status().is_client_error() {
74 let text = String::from_utf8_lossy(body.as_slice());
75 bail!(
76 "status error {}, response: {text:?}",
77 response.status().as_u16()
78 );
79 }
80
81 let body_str = std::str::from_utf8(&body)?;
82
83 serde_json::from_str::<CommitDetails>(body_str)
84 .map(|commit| commit.author)
85 .context("failed to deserialize GitHub commit details")
86 }
87}
88
89#[async_trait]
90impl GitHostingProvider for Github {
91 fn name(&self) -> String {
92 "GitHub".to_string()
93 }
94
95 fn base_url(&self) -> Url {
96 Url::parse("https://github.com").unwrap()
97 }
98
99 fn supports_avatars(&self) -> bool {
100 true
101 }
102
103 fn format_line_number(&self, line: u32) -> String {
104 format!("L{line}")
105 }
106
107 fn format_line_numbers(&self, start_line: u32, end_line: u32) -> String {
108 format!("L{start_line}-L{end_line}")
109 }
110
111 fn parse_remote_url(&self, url: &str) -> Option<ParsedGitRemote> {
112 let url = RemoteUrl::from_str(url).ok()?;
113
114 let host = url.host_str()?;
115 if host != "github.com" {
116 return None;
117 }
118
119 let mut path_segments = url.path_segments()?;
120 let owner = path_segments.next()?;
121 let repo = path_segments.next()?.trim_end_matches(".git");
122
123 Some(ParsedGitRemote {
124 owner: owner.into(),
125 repo: repo.into(),
126 })
127 }
128
129 fn build_commit_permalink(
130 &self,
131 remote: &ParsedGitRemote,
132 params: BuildCommitPermalinkParams,
133 ) -> Url {
134 let BuildCommitPermalinkParams { sha } = params;
135 let ParsedGitRemote { owner, repo } = remote;
136
137 self.base_url()
138 .join(&format!("{owner}/{repo}/commit/{sha}"))
139 .unwrap()
140 }
141
142 fn build_permalink(&self, remote: ParsedGitRemote, params: BuildPermalinkParams) -> Url {
143 let ParsedGitRemote { owner, repo } = remote;
144 let BuildPermalinkParams {
145 sha,
146 path,
147 selection,
148 } = params;
149
150 let mut permalink = self
151 .base_url()
152 .join(&format!("{owner}/{repo}/blob/{sha}/{path}"))
153 .unwrap();
154 if path.ends_with(".md") {
155 permalink.set_query(Some("plain=1"));
156 }
157 permalink.set_fragment(
158 selection
159 .map(|selection| self.line_fragment(&selection))
160 .as_deref(),
161 );
162 permalink
163 }
164
165 fn extract_pull_request(&self, remote: &ParsedGitRemote, message: &str) -> Option<PullRequest> {
166 let line = message.lines().next()?;
167 let capture = pull_request_number_regex().captures(line)?;
168 let number = capture.get(1)?.as_str().parse::<u32>().ok()?;
169
170 let mut url = self.base_url();
171 let path = format!("/{}/{}/pull/{}", remote.owner, remote.repo, number);
172 url.set_path(&path);
173
174 Some(PullRequest { number, url })
175 }
176
177 async fn commit_author_avatar_url(
178 &self,
179 repo_owner: &str,
180 repo: &str,
181 commit: Oid,
182 http_client: Arc<dyn HttpClient>,
183 ) -> Result<Option<Url>> {
184 let commit = commit.to_string();
185 let avatar_url = self
186 .fetch_github_commit_author(repo_owner, repo, &commit, &http_client)
187 .await?
188 .map(|author| -> Result<Url, url::ParseError> {
189 let mut url = Url::parse(&author.avatar_url)?;
190 url.set_query(Some("size=128"));
191 Ok(url)
192 })
193 .transpose()?;
194 Ok(avatar_url)
195 }
196}
197
198#[cfg(test)]
199mod tests {
200 // TODO: Replace with `indoc`.
201 use unindent::Unindent;
202
203 use super::*;
204
205 #[test]
206 fn test_parse_remote_url_given_https_url_with_username() {
207 let parsed_remote = Github
208 .parse_remote_url("https://jlannister@github.com/some-org/some-repo.git")
209 .unwrap();
210
211 assert_eq!(
212 parsed_remote,
213 ParsedGitRemote {
214 owner: "some-org".into(),
215 repo: "some-repo".into(),
216 }
217 );
218 }
219
220 #[test]
221 fn test_build_github_permalink_from_ssh_url() {
222 let remote = ParsedGitRemote {
223 owner: "zed-industries".into(),
224 repo: "zed".into(),
225 };
226 let permalink = Github.build_permalink(
227 remote,
228 BuildPermalinkParams {
229 sha: "e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7",
230 path: "crates/editor/src/git/permalink.rs",
231 selection: None,
232 },
233 );
234
235 let expected_url = "https://github.com/zed-industries/zed/blob/e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7/crates/editor/src/git/permalink.rs";
236 assert_eq!(permalink.to_string(), expected_url.to_string())
237 }
238
239 #[test]
240 fn test_build_github_permalink_from_ssh_url_single_line_selection() {
241 let remote = ParsedGitRemote {
242 owner: "zed-industries".into(),
243 repo: "zed".into(),
244 };
245 let permalink = Github.build_permalink(
246 remote,
247 BuildPermalinkParams {
248 sha: "e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7",
249 path: "crates/editor/src/git/permalink.rs",
250 selection: Some(6..6),
251 },
252 );
253
254 let expected_url = "https://github.com/zed-industries/zed/blob/e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7/crates/editor/src/git/permalink.rs#L7";
255 assert_eq!(permalink.to_string(), expected_url.to_string())
256 }
257
258 #[test]
259 fn test_build_github_permalink_from_ssh_url_multi_line_selection() {
260 let remote = ParsedGitRemote {
261 owner: "zed-industries".into(),
262 repo: "zed".into(),
263 };
264 let permalink = Github.build_permalink(
265 remote,
266 BuildPermalinkParams {
267 sha: "e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7",
268 path: "crates/editor/src/git/permalink.rs",
269 selection: Some(23..47),
270 },
271 );
272
273 let expected_url = "https://github.com/zed-industries/zed/blob/e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7/crates/editor/src/git/permalink.rs#L24-L48";
274 assert_eq!(permalink.to_string(), expected_url.to_string())
275 }
276
277 #[test]
278 fn test_build_github_permalink_from_https_url() {
279 let remote = ParsedGitRemote {
280 owner: "zed-industries".into(),
281 repo: "zed".into(),
282 };
283 let permalink = Github.build_permalink(
284 remote,
285 BuildPermalinkParams {
286 sha: "b2efec9824c45fcc90c9a7eb107a50d1772a60aa",
287 path: "crates/zed/src/main.rs",
288 selection: None,
289 },
290 );
291
292 let expected_url = "https://github.com/zed-industries/zed/blob/b2efec9824c45fcc90c9a7eb107a50d1772a60aa/crates/zed/src/main.rs";
293 assert_eq!(permalink.to_string(), expected_url.to_string())
294 }
295
296 #[test]
297 fn test_build_github_permalink_from_https_url_single_line_selection() {
298 let remote = ParsedGitRemote {
299 owner: "zed-industries".into(),
300 repo: "zed".into(),
301 };
302 let permalink = Github.build_permalink(
303 remote,
304 BuildPermalinkParams {
305 sha: "b2efec9824c45fcc90c9a7eb107a50d1772a60aa",
306 path: "crates/zed/src/main.rs",
307 selection: Some(6..6),
308 },
309 );
310
311 let expected_url = "https://github.com/zed-industries/zed/blob/b2efec9824c45fcc90c9a7eb107a50d1772a60aa/crates/zed/src/main.rs#L7";
312 assert_eq!(permalink.to_string(), expected_url.to_string())
313 }
314
315 #[test]
316 fn test_build_github_permalink_from_https_url_multi_line_selection() {
317 let remote = ParsedGitRemote {
318 owner: "zed-industries".into(),
319 repo: "zed".into(),
320 };
321 let permalink = Github.build_permalink(
322 remote,
323 BuildPermalinkParams {
324 sha: "b2efec9824c45fcc90c9a7eb107a50d1772a60aa",
325 path: "crates/zed/src/main.rs",
326 selection: Some(23..47),
327 },
328 );
329
330 let expected_url = "https://github.com/zed-industries/zed/blob/b2efec9824c45fcc90c9a7eb107a50d1772a60aa/crates/zed/src/main.rs#L24-L48";
331 assert_eq!(permalink.to_string(), expected_url.to_string())
332 }
333
334 #[test]
335 fn test_github_pull_requests() {
336 let remote = ParsedGitRemote {
337 owner: "zed-industries".into(),
338 repo: "zed".into(),
339 };
340
341 let message = "This does not contain a pull request";
342 assert!(Github.extract_pull_request(&remote, message).is_none());
343
344 // Pull request number at end of first line
345 let message = r#"
346 project panel: do not expand collapsed worktrees on "collapse all entries" (#10687)
347
348 Fixes #10597
349
350 Release Notes:
351
352 - Fixed "project panel: collapse all entries" expanding collapsed worktrees.
353 "#
354 .unindent();
355
356 assert_eq!(
357 Github
358 .extract_pull_request(&remote, &message)
359 .unwrap()
360 .url
361 .as_str(),
362 "https://github.com/zed-industries/zed/pull/10687"
363 );
364
365 // Pull request number in middle of line, which we want to ignore
366 let message = r#"
367 Follow-up to #10687 to fix problems
368
369 See the original PR, this is a fix.
370 "#
371 .unindent();
372 assert_eq!(Github.extract_pull_request(&remote, &message), None);
373 }
374}