github.rs

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