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