github.rs

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