1use std::str::FromStr;
2use std::sync::{Arc, LazyLock};
3
4use anyhow::{Context as _, Result, bail};
5use async_trait::async_trait;
6use futures::AsyncReadExt;
7use gpui::SharedString;
8use http_client::{AsyncBody, HttpClient, HttpRequestExt, Request};
9use regex::Regex;
10use serde::Deserialize;
11use url::Url;
12
13use git::{
14 BuildCommitPermalinkParams, BuildPermalinkParams, GitHostingProvider, ParsedGitRemote,
15 PullRequest, RemoteUrl,
16};
17
18use crate::get_host_from_git_remote_url;
19
20fn pull_request_number_regex() -> &'static Regex {
21 static PULL_REQUEST_NUMBER_REGEX: LazyLock<Regex> =
22 LazyLock::new(|| Regex::new(r"\(#(\d+)\)$").unwrap());
23 &PULL_REQUEST_NUMBER_REGEX
24}
25
26#[derive(Debug, Deserialize)]
27struct CommitDetails {
28 commit: Commit,
29 author: Option<User>,
30}
31
32#[derive(Debug, Deserialize)]
33struct Commit {
34 author: Author,
35}
36
37#[derive(Debug, Deserialize)]
38struct Author {
39 email: String,
40}
41
42#[derive(Debug, Deserialize)]
43struct User {
44 pub id: u64,
45 pub avatar_url: String,
46}
47
48#[derive(Debug)]
49pub struct Github {
50 name: String,
51 base_url: Url,
52}
53
54impl Github {
55 pub fn new(name: impl Into<String>, base_url: Url) -> Self {
56 Self {
57 name: name.into(),
58 base_url,
59 }
60 }
61
62 pub fn public_instance() -> Self {
63 Self::new("GitHub", Url::parse("https://github.com").unwrap())
64 }
65
66 pub fn from_remote_url(remote_url: &str) -> Result<Self> {
67 let host = get_host_from_git_remote_url(remote_url)?;
68 if host == "github.com" {
69 bail!("the GitHub instance is not self-hosted");
70 }
71
72 // TODO: detecting self hosted instances by checking whether "github" is in the url or not
73 // is not very reliable. See https://github.com/zed-industries/zed/issues/26393 for more
74 // information.
75 if !host.contains("github") {
76 bail!("not a GitHub URL");
77 }
78
79 Ok(Self::new(
80 "GitHub Self-Hosted",
81 Url::parse(&format!("https://{}", host))?,
82 ))
83 }
84
85 async fn fetch_github_commit_author(
86 &self,
87 repo_owner: &str,
88 repo: &str,
89 commit: &str,
90 client: &Arc<dyn HttpClient>,
91 ) -> Result<Option<User>> {
92 let Some(host) = self.base_url.host_str() else {
93 bail!("failed to get host from github base url");
94 };
95 let url = format!("https://api.{host}/repos/{repo_owner}/{repo}/commits/{commit}");
96
97 let mut request = Request::get(&url)
98 .header("Content-Type", "application/json")
99 .follow_redirects(http_client::RedirectPolicy::FollowAll);
100
101 if let Ok(github_token) = std::env::var("GITHUB_TOKEN") {
102 request = request.header("Authorization", format!("Bearer {}", github_token));
103 }
104
105 let mut response = client
106 .send(request.body(AsyncBody::default())?)
107 .await
108 .with_context(|| format!("error fetching GitHub commit details at {:?}", url))?;
109
110 let mut body = Vec::new();
111 response.body_mut().read_to_end(&mut body).await?;
112
113 if response.status().is_client_error() {
114 let text = String::from_utf8_lossy(body.as_slice());
115 bail!(
116 "status error {}, response: {text:?}",
117 response.status().as_u16()
118 );
119 }
120
121 let body_str = std::str::from_utf8(&body)?;
122
123 serde_json::from_str::<CommitDetails>(body_str)
124 .map(|commit| commit.author)
125 .context("failed to deserialize GitHub commit details")
126 }
127}
128
129#[async_trait]
130impl GitHostingProvider for Github {
131 fn name(&self) -> String {
132 self.name.clone()
133 }
134
135 fn base_url(&self) -> Url {
136 self.base_url.clone()
137 }
138
139 fn supports_avatars(&self) -> bool {
140 // Avatars are not supported for self-hosted GitHub instances
141 // See tracking issue: https://github.com/zed-industries/zed/issues/11043
142 &self.name == "GitHub"
143 }
144
145 fn format_line_number(&self, line: u32) -> String {
146 format!("L{line}")
147 }
148
149 fn format_line_numbers(&self, start_line: u32, end_line: u32) -> String {
150 format!("L{start_line}-L{end_line}")
151 }
152
153 fn parse_remote_url(&self, url: &str) -> Option<ParsedGitRemote> {
154 let url = RemoteUrl::from_str(url).ok()?;
155
156 let host = url.host_str()?;
157 if host != self.base_url.host_str()? {
158 return None;
159 }
160
161 let mut path_segments = url.path_segments()?;
162 let owner = path_segments.next()?;
163 let repo = path_segments.next()?.trim_end_matches(".git");
164
165 Some(ParsedGitRemote {
166 owner: owner.into(),
167 repo: repo.into(),
168 })
169 }
170
171 fn build_commit_permalink(
172 &self,
173 remote: &ParsedGitRemote,
174 params: BuildCommitPermalinkParams,
175 ) -> Url {
176 let BuildCommitPermalinkParams { sha } = params;
177 let ParsedGitRemote { owner, repo } = remote;
178
179 self.base_url()
180 .join(&format!("{owner}/{repo}/commit/{sha}"))
181 .unwrap()
182 }
183
184 fn build_permalink(&self, remote: ParsedGitRemote, params: BuildPermalinkParams) -> Url {
185 let ParsedGitRemote { owner, repo } = remote;
186 let BuildPermalinkParams {
187 sha,
188 path,
189 selection,
190 } = params;
191
192 let mut permalink = self
193 .base_url()
194 .join(&format!("{owner}/{repo}/blob/{sha}/{path}"))
195 .unwrap();
196 if path.ends_with(".md") {
197 permalink.set_query(Some("plain=1"));
198 }
199 permalink.set_fragment(
200 selection
201 .map(|selection| self.line_fragment(&selection))
202 .as_deref(),
203 );
204 permalink
205 }
206
207 fn extract_pull_request(&self, remote: &ParsedGitRemote, message: &str) -> Option<PullRequest> {
208 let line = message.lines().next()?;
209 let capture = pull_request_number_regex().captures(line)?;
210 let number = capture.get(1)?.as_str().parse::<u32>().ok()?;
211
212 let mut url = self.base_url();
213 let path = format!("/{}/{}/pull/{}", remote.owner, remote.repo, number);
214 url.set_path(&path);
215
216 Some(PullRequest { number, url })
217 }
218
219 async fn commit_author_avatar_url(
220 &self,
221 repo_owner: &str,
222 repo: &str,
223 commit: SharedString,
224 http_client: Arc<dyn HttpClient>,
225 ) -> Result<Option<Url>> {
226 let commit = commit.to_string();
227 let avatar_url = self
228 .fetch_github_commit_author(repo_owner, repo, &commit, &http_client)
229 .await?
230 .map(|author| -> Result<Url, url::ParseError> {
231 let mut url = Url::parse(&author.avatar_url)?;
232 url.set_query(Some("size=128"));
233 Ok(url)
234 })
235 .transpose()?;
236 Ok(avatar_url)
237 }
238}
239
240#[cfg(test)]
241mod tests {
242 use indoc::indoc;
243 use pretty_assertions::assert_eq;
244
245 use super::*;
246
247 #[test]
248 fn test_invalid_self_hosted_remote_url() {
249 let remote_url = "git@github.com:zed-industries/zed.git";
250 let github = Github::from_remote_url(remote_url);
251 assert!(github.is_err());
252 }
253
254 #[test]
255 fn test_from_remote_url_ssh() {
256 let remote_url = "git@github.my-enterprise.com:zed-industries/zed.git";
257 let github = Github::from_remote_url(remote_url).unwrap();
258
259 assert!(!github.supports_avatars());
260 assert_eq!(github.name, "GitHub Self-Hosted".to_string());
261 assert_eq!(
262 github.base_url,
263 Url::parse("https://github.my-enterprise.com").unwrap()
264 );
265 }
266
267 #[test]
268 fn test_from_remote_url_https() {
269 let remote_url = "https://github.my-enterprise.com/zed-industries/zed.git";
270 let github = Github::from_remote_url(remote_url).unwrap();
271
272 assert!(!github.supports_avatars());
273 assert_eq!(github.name, "GitHub Self-Hosted".to_string());
274 assert_eq!(
275 github.base_url,
276 Url::parse("https://github.my-enterprise.com").unwrap()
277 );
278 }
279
280 #[test]
281 fn test_parse_remote_url_given_self_hosted_ssh_url() {
282 let remote_url = "git@github.my-enterprise.com:zed-industries/zed.git";
283 let parsed_remote = Github::from_remote_url(remote_url)
284 .unwrap()
285 .parse_remote_url(remote_url)
286 .unwrap();
287
288 assert_eq!(
289 parsed_remote,
290 ParsedGitRemote {
291 owner: "zed-industries".into(),
292 repo: "zed".into(),
293 }
294 );
295 }
296
297 #[test]
298 fn test_parse_remote_url_given_self_hosted_https_url_with_subgroup() {
299 let remote_url = "https://github.my-enterprise.com/zed-industries/zed.git";
300 let parsed_remote = Github::from_remote_url(remote_url)
301 .unwrap()
302 .parse_remote_url(remote_url)
303 .unwrap();
304
305 assert_eq!(
306 parsed_remote,
307 ParsedGitRemote {
308 owner: "zed-industries".into(),
309 repo: "zed".into(),
310 }
311 );
312 }
313
314 #[test]
315 fn test_parse_remote_url_given_ssh_url() {
316 let parsed_remote = Github::public_instance()
317 .parse_remote_url("git@github.com:zed-industries/zed.git")
318 .unwrap();
319
320 assert_eq!(
321 parsed_remote,
322 ParsedGitRemote {
323 owner: "zed-industries".into(),
324 repo: "zed".into(),
325 }
326 );
327 }
328
329 #[test]
330 fn test_parse_remote_url_given_https_url() {
331 let parsed_remote = Github::public_instance()
332 .parse_remote_url("https://github.com/zed-industries/zed.git")
333 .unwrap();
334
335 assert_eq!(
336 parsed_remote,
337 ParsedGitRemote {
338 owner: "zed-industries".into(),
339 repo: "zed".into(),
340 }
341 );
342 }
343
344 #[test]
345 fn test_parse_remote_url_given_https_url_with_username() {
346 let parsed_remote = Github::public_instance()
347 .parse_remote_url("https://jlannister@github.com/some-org/some-repo.git")
348 .unwrap();
349
350 assert_eq!(
351 parsed_remote,
352 ParsedGitRemote {
353 owner: "some-org".into(),
354 repo: "some-repo".into(),
355 }
356 );
357 }
358
359 #[test]
360 fn test_build_github_permalink_from_ssh_url() {
361 let remote = ParsedGitRemote {
362 owner: "zed-industries".into(),
363 repo: "zed".into(),
364 };
365 let permalink = Github::public_instance().build_permalink(
366 remote,
367 BuildPermalinkParams {
368 sha: "e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7",
369 path: "crates/editor/src/git/permalink.rs",
370 selection: None,
371 },
372 );
373
374 let expected_url = "https://github.com/zed-industries/zed/blob/e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7/crates/editor/src/git/permalink.rs";
375 assert_eq!(permalink.to_string(), expected_url.to_string())
376 }
377
378 #[test]
379 fn test_build_github_permalink() {
380 let permalink = Github::public_instance().build_permalink(
381 ParsedGitRemote {
382 owner: "zed-industries".into(),
383 repo: "zed".into(),
384 },
385 BuildPermalinkParams {
386 sha: "b2efec9824c45fcc90c9a7eb107a50d1772a60aa",
387 path: "crates/zed/src/main.rs",
388 selection: None,
389 },
390 );
391
392 let expected_url = "https://github.com/zed-industries/zed/blob/b2efec9824c45fcc90c9a7eb107a50d1772a60aa/crates/zed/src/main.rs";
393 assert_eq!(permalink.to_string(), expected_url.to_string())
394 }
395
396 #[test]
397 fn test_build_github_permalink_with_single_line_selection() {
398 let permalink = Github::public_instance().build_permalink(
399 ParsedGitRemote {
400 owner: "zed-industries".into(),
401 repo: "zed".into(),
402 },
403 BuildPermalinkParams {
404 sha: "e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7",
405 path: "crates/editor/src/git/permalink.rs",
406 selection: Some(6..6),
407 },
408 );
409
410 let expected_url = "https://github.com/zed-industries/zed/blob/e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7/crates/editor/src/git/permalink.rs#L7";
411 assert_eq!(permalink.to_string(), expected_url.to_string())
412 }
413
414 #[test]
415 fn test_build_github_permalink_with_multi_line_selection() {
416 let permalink = Github::public_instance().build_permalink(
417 ParsedGitRemote {
418 owner: "zed-industries".into(),
419 repo: "zed".into(),
420 },
421 BuildPermalinkParams {
422 sha: "e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7",
423 path: "crates/editor/src/git/permalink.rs",
424 selection: Some(23..47),
425 },
426 );
427
428 let expected_url = "https://github.com/zed-industries/zed/blob/e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7/crates/editor/src/git/permalink.rs#L24-L48";
429 assert_eq!(permalink.to_string(), expected_url.to_string())
430 }
431
432 #[test]
433 fn test_github_pull_requests() {
434 let remote = ParsedGitRemote {
435 owner: "zed-industries".into(),
436 repo: "zed".into(),
437 };
438
439 let github = Github::public_instance();
440 let message = "This does not contain a pull request";
441 assert!(github.extract_pull_request(&remote, message).is_none());
442
443 // Pull request number at end of first line
444 let message = indoc! {r#"
445 project panel: do not expand collapsed worktrees on "collapse all entries" (#10687)
446
447 Fixes #10597
448
449 Release Notes:
450
451 - Fixed "project panel: collapse all entries" expanding collapsed worktrees.
452 "#
453 };
454
455 assert_eq!(
456 github
457 .extract_pull_request(&remote, &message)
458 .unwrap()
459 .url
460 .as_str(),
461 "https://github.com/zed-industries/zed/pull/10687"
462 );
463
464 // Pull request number in middle of line, which we want to ignore
465 let message = indoc! {r#"
466 Follow-up to #10687 to fix problems
467
468 See the original PR, this is a fix.
469 "#
470 };
471 assert_eq!(github.extract_pull_request(&remote, &message), None);
472 }
473}