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;
 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 indoc::indoc;
263    use pretty_assertions::assert_eq;
264
265    use super::*;
266
267    #[test]
268    fn test_remote_url_with_root_slash() {
269        let remote_url = "git@github.com:/zed-industries/zed";
270        let parsed_remote = Github::public_instance()
271            .parse_remote_url(remote_url)
272            .unwrap();
273
274        assert_eq!(
275            parsed_remote,
276            ParsedGitRemote {
277                owner: "zed-industries".into(),
278                repo: "zed".into(),
279            }
280        );
281    }
282
283    #[test]
284    fn test_invalid_self_hosted_remote_url() {
285        let remote_url = "git@github.com:zed-industries/zed.git";
286        let github = Github::from_remote_url(remote_url);
287        assert!(github.is_err());
288    }
289
290    #[test]
291    fn test_from_remote_url_ssh() {
292        let remote_url = "git@github.my-enterprise.com:zed-industries/zed.git";
293        let github = Github::from_remote_url(remote_url).unwrap();
294
295        assert!(!github.supports_avatars());
296        assert_eq!(github.name, "GitHub Self-Hosted".to_string());
297        assert_eq!(
298            github.base_url,
299            Url::parse("https://github.my-enterprise.com").unwrap()
300        );
301    }
302
303    #[test]
304    fn test_from_remote_url_https() {
305        let remote_url = "https://github.my-enterprise.com/zed-industries/zed.git";
306        let github = Github::from_remote_url(remote_url).unwrap();
307
308        assert!(!github.supports_avatars());
309        assert_eq!(github.name, "GitHub Self-Hosted".to_string());
310        assert_eq!(
311            github.base_url,
312            Url::parse("https://github.my-enterprise.com").unwrap()
313        );
314    }
315
316    #[test]
317    fn test_parse_remote_url_given_self_hosted_ssh_url() {
318        let remote_url = "git@github.my-enterprise.com:zed-industries/zed.git";
319        let parsed_remote = Github::from_remote_url(remote_url)
320            .unwrap()
321            .parse_remote_url(remote_url)
322            .unwrap();
323
324        assert_eq!(
325            parsed_remote,
326            ParsedGitRemote {
327                owner: "zed-industries".into(),
328                repo: "zed".into(),
329            }
330        );
331    }
332
333    #[test]
334    fn test_parse_remote_url_given_self_hosted_https_url_with_subgroup() {
335        let remote_url = "https://github.my-enterprise.com/zed-industries/zed.git";
336        let parsed_remote = Github::from_remote_url(remote_url)
337            .unwrap()
338            .parse_remote_url(remote_url)
339            .unwrap();
340
341        assert_eq!(
342            parsed_remote,
343            ParsedGitRemote {
344                owner: "zed-industries".into(),
345                repo: "zed".into(),
346            }
347        );
348    }
349
350    #[test]
351    fn test_parse_remote_url_given_ssh_url() {
352        let parsed_remote = Github::public_instance()
353            .parse_remote_url("git@github.com:zed-industries/zed.git")
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_https_url() {
367        let parsed_remote = Github::public_instance()
368            .parse_remote_url("https://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_with_username() {
382        let parsed_remote = Github::public_instance()
383            .parse_remote_url("https://jlannister@github.com/some-org/some-repo.git")
384            .unwrap();
385
386        assert_eq!(
387            parsed_remote,
388            ParsedGitRemote {
389                owner: "some-org".into(),
390                repo: "some-repo".into(),
391            }
392        );
393    }
394
395    #[test]
396    fn test_build_github_permalink_from_ssh_url() {
397        let remote = ParsedGitRemote {
398            owner: "zed-industries".into(),
399            repo: "zed".into(),
400        };
401        let permalink = Github::public_instance().build_permalink(
402            remote,
403            BuildPermalinkParams {
404                sha: "e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7",
405                path: "crates/editor/src/git/permalink.rs",
406                selection: None,
407            },
408        );
409
410        let expected_url = "https://github.com/zed-industries/zed/blob/e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7/crates/editor/src/git/permalink.rs";
411        assert_eq!(permalink.to_string(), expected_url.to_string())
412    }
413
414    #[test]
415    fn test_build_github_permalink() {
416        let permalink = Github::public_instance().build_permalink(
417            ParsedGitRemote {
418                owner: "zed-industries".into(),
419                repo: "zed".into(),
420            },
421            BuildPermalinkParams {
422                sha: "b2efec9824c45fcc90c9a7eb107a50d1772a60aa",
423                path: "crates/zed/src/main.rs",
424                selection: None,
425            },
426        );
427
428        let expected_url = "https://github.com/zed-industries/zed/blob/b2efec9824c45fcc90c9a7eb107a50d1772a60aa/crates/zed/src/main.rs";
429        assert_eq!(permalink.to_string(), expected_url.to_string())
430    }
431
432    #[test]
433    fn test_build_github_permalink_with_single_line_selection() {
434        let permalink = Github::public_instance().build_permalink(
435            ParsedGitRemote {
436                owner: "zed-industries".into(),
437                repo: "zed".into(),
438            },
439            BuildPermalinkParams {
440                sha: "e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7",
441                path: "crates/editor/src/git/permalink.rs",
442                selection: Some(6..6),
443            },
444        );
445
446        let expected_url = "https://github.com/zed-industries/zed/blob/e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7/crates/editor/src/git/permalink.rs#L7";
447        assert_eq!(permalink.to_string(), expected_url.to_string())
448    }
449
450    #[test]
451    fn test_build_github_permalink_with_multi_line_selection() {
452        let permalink = Github::public_instance().build_permalink(
453            ParsedGitRemote {
454                owner: "zed-industries".into(),
455                repo: "zed".into(),
456            },
457            BuildPermalinkParams {
458                sha: "e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7",
459                path: "crates/editor/src/git/permalink.rs",
460                selection: Some(23..47),
461            },
462        );
463
464        let expected_url = "https://github.com/zed-industries/zed/blob/e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7/crates/editor/src/git/permalink.rs#L24-L48";
465        assert_eq!(permalink.to_string(), expected_url.to_string())
466    }
467
468    #[test]
469    fn test_github_pull_requests() {
470        let remote = ParsedGitRemote {
471            owner: "zed-industries".into(),
472            repo: "zed".into(),
473        };
474
475        let github = Github::public_instance();
476        let message = "This does not contain a pull request";
477        assert!(github.extract_pull_request(&remote, message).is_none());
478
479        // Pull request number at end of first line
480        let message = indoc! {r#"
481            project panel: do not expand collapsed worktrees on "collapse all entries" (#10687)
482
483            Fixes #10597
484
485            Release Notes:
486
487            - Fixed "project panel: collapse all entries" expanding collapsed worktrees.
488            "#
489        };
490
491        assert_eq!(
492            github
493                .extract_pull_request(&remote, message)
494                .unwrap()
495                .url
496                .as_str(),
497            "https://github.com/zed-industries/zed/pull/10687"
498        );
499
500        // Pull request number in middle of line, which we want to ignore
501        let message = indoc! {r#"
502            Follow-up to #10687 to fix problems
503
504            See the original PR, this is a fix.
505            "#
506        };
507        assert_eq!(github.extract_pull_request(&remote, message), None);
508    }
509}