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 http_client::{AsyncBody, HttpClient, HttpRequestExt, Request};
  8use regex::Regex;
  9use serde::Deserialize;
 10use url::Url;
 11
 12use git::{
 13    BuildCommitPermalinkParams, BuildPermalinkParams, GitHostingProvider, Oid, ParsedGitRemote,
 14    PullRequest, RemoteUrl,
 15};
 16
 17fn pull_request_number_regex() -> &'static Regex {
 18    static PULL_REQUEST_NUMBER_REGEX: LazyLock<Regex> =
 19        LazyLock::new(|| Regex::new(r"\(#(\d+)\)$").unwrap());
 20    &PULL_REQUEST_NUMBER_REGEX
 21}
 22
 23#[derive(Debug, Deserialize)]
 24struct CommitDetails {
 25    commit: Commit,
 26    author: Option<User>,
 27}
 28
 29#[derive(Debug, Deserialize)]
 30struct Commit {
 31    author: Author,
 32}
 33
 34#[derive(Debug, Deserialize)]
 35struct Author {
 36    email: String,
 37}
 38
 39#[derive(Debug, Deserialize)]
 40struct User {
 41    pub id: u64,
 42    pub avatar_url: String,
 43}
 44
 45pub struct Github;
 46
 47impl Github {
 48    async fn fetch_github_commit_author(
 49        &self,
 50        repo_owner: &str,
 51        repo: &str,
 52        commit: &str,
 53        client: &Arc<dyn HttpClient>,
 54    ) -> Result<Option<User>> {
 55        let url = format!("https://api.github.com/repos/{repo_owner}/{repo}/commits/{commit}");
 56
 57        let mut request = Request::get(&url)
 58            .header("Content-Type", "application/json")
 59            .follow_redirects(http_client::RedirectPolicy::FollowAll);
 60
 61        if let Ok(github_token) = std::env::var("GITHUB_TOKEN") {
 62            request = request.header("Authorization", format!("Bearer {}", github_token));
 63        }
 64
 65        let mut response = client
 66            .send(request.body(AsyncBody::default())?)
 67            .await
 68            .with_context(|| format!("error fetching GitHub commit details at {:?}", url))?;
 69
 70        let mut body = Vec::new();
 71        response.body_mut().read_to_end(&mut body).await?;
 72
 73        if response.status().is_client_error() {
 74            let text = String::from_utf8_lossy(body.as_slice());
 75            bail!(
 76                "status error {}, response: {text:?}",
 77                response.status().as_u16()
 78            );
 79        }
 80
 81        let body_str = std::str::from_utf8(&body)?;
 82
 83        serde_json::from_str::<CommitDetails>(body_str)
 84            .map(|commit| commit.author)
 85            .context("failed to deserialize GitHub commit details")
 86    }
 87}
 88
 89#[async_trait]
 90impl GitHostingProvider for Github {
 91    fn name(&self) -> String {
 92        "GitHub".to_string()
 93    }
 94
 95    fn base_url(&self) -> Url {
 96        Url::parse("https://github.com").unwrap()
 97    }
 98
 99    fn supports_avatars(&self) -> bool {
100        true
101    }
102
103    fn format_line_number(&self, line: u32) -> String {
104        format!("L{line}")
105    }
106
107    fn format_line_numbers(&self, start_line: u32, end_line: u32) -> String {
108        format!("L{start_line}-L{end_line}")
109    }
110
111    fn parse_remote_url(&self, url: &str) -> Option<ParsedGitRemote> {
112        let url = RemoteUrl::from_str(url).ok()?;
113
114        let host = url.host_str()?;
115        if host != "github.com" {
116            return None;
117        }
118
119        let mut path_segments = url.path_segments()?;
120        let owner = path_segments.next()?;
121        let repo = path_segments.next()?.trim_end_matches(".git");
122
123        Some(ParsedGitRemote {
124            owner: owner.into(),
125            repo: repo.into(),
126        })
127    }
128
129    fn build_commit_permalink(
130        &self,
131        remote: &ParsedGitRemote,
132        params: BuildCommitPermalinkParams,
133    ) -> Url {
134        let BuildCommitPermalinkParams { sha } = params;
135        let ParsedGitRemote { owner, repo } = remote;
136
137        self.base_url()
138            .join(&format!("{owner}/{repo}/commit/{sha}"))
139            .unwrap()
140    }
141
142    fn build_permalink(&self, remote: ParsedGitRemote, params: BuildPermalinkParams) -> Url {
143        let ParsedGitRemote { owner, repo } = remote;
144        let BuildPermalinkParams {
145            sha,
146            path,
147            selection,
148        } = params;
149
150        let mut permalink = self
151            .base_url()
152            .join(&format!("{owner}/{repo}/blob/{sha}/{path}"))
153            .unwrap();
154        if path.ends_with(".md") {
155            permalink.set_query(Some("plain=1"));
156        }
157        permalink.set_fragment(
158            selection
159                .map(|selection| self.line_fragment(&selection))
160                .as_deref(),
161        );
162        permalink
163    }
164
165    fn extract_pull_request(&self, remote: &ParsedGitRemote, message: &str) -> Option<PullRequest> {
166        let line = message.lines().next()?;
167        let capture = pull_request_number_regex().captures(line)?;
168        let number = capture.get(1)?.as_str().parse::<u32>().ok()?;
169
170        let mut url = self.base_url();
171        let path = format!("/{}/{}/pull/{}", remote.owner, remote.repo, number);
172        url.set_path(&path);
173
174        Some(PullRequest { number, url })
175    }
176
177    async fn commit_author_avatar_url(
178        &self,
179        repo_owner: &str,
180        repo: &str,
181        commit: Oid,
182        http_client: Arc<dyn HttpClient>,
183    ) -> Result<Option<Url>> {
184        let commit = commit.to_string();
185        let avatar_url = self
186            .fetch_github_commit_author(repo_owner, repo, &commit, &http_client)
187            .await?
188            .map(|author| -> Result<Url, url::ParseError> {
189                let mut url = Url::parse(&author.avatar_url)?;
190                url.set_query(Some("size=128"));
191                Ok(url)
192            })
193            .transpose()?;
194        Ok(avatar_url)
195    }
196}
197
198#[cfg(test)]
199mod tests {
200    use indoc::indoc;
201    use pretty_assertions::assert_eq;
202
203    use super::*;
204
205    #[test]
206    fn test_parse_remote_url_given_ssh_url() {
207        let parsed_remote = Github
208            .parse_remote_url("git@github.com:zed-industries/zed.git")
209            .unwrap();
210
211        assert_eq!(
212            parsed_remote,
213            ParsedGitRemote {
214                owner: "zed-industries".into(),
215                repo: "zed".into(),
216            }
217        );
218    }
219
220    #[test]
221    fn test_parse_remote_url_given_https_url() {
222        let parsed_remote = Github
223            .parse_remote_url("https://github.com/zed-industries/zed.git")
224            .unwrap();
225
226        assert_eq!(
227            parsed_remote,
228            ParsedGitRemote {
229                owner: "zed-industries".into(),
230                repo: "zed".into(),
231            }
232        );
233    }
234
235    #[test]
236    fn test_parse_remote_url_given_https_url_with_username() {
237        let parsed_remote = Github
238            .parse_remote_url("https://jlannister@github.com/some-org/some-repo.git")
239            .unwrap();
240
241        assert_eq!(
242            parsed_remote,
243            ParsedGitRemote {
244                owner: "some-org".into(),
245                repo: "some-repo".into(),
246            }
247        );
248    }
249
250    #[test]
251    fn test_build_github_permalink_from_ssh_url() {
252        let remote = ParsedGitRemote {
253            owner: "zed-industries".into(),
254            repo: "zed".into(),
255        };
256        let permalink = Github.build_permalink(
257            remote,
258            BuildPermalinkParams {
259                sha: "e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7",
260                path: "crates/editor/src/git/permalink.rs",
261                selection: None,
262            },
263        );
264
265        let expected_url = "https://github.com/zed-industries/zed/blob/e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7/crates/editor/src/git/permalink.rs";
266        assert_eq!(permalink.to_string(), expected_url.to_string())
267    }
268
269    #[test]
270    fn test_build_github_permalink() {
271        let permalink = Github.build_permalink(
272            ParsedGitRemote {
273                owner: "zed-industries".into(),
274                repo: "zed".into(),
275            },
276            BuildPermalinkParams {
277                sha: "b2efec9824c45fcc90c9a7eb107a50d1772a60aa",
278                path: "crates/zed/src/main.rs",
279                selection: None,
280            },
281        );
282
283        let expected_url = "https://github.com/zed-industries/zed/blob/b2efec9824c45fcc90c9a7eb107a50d1772a60aa/crates/zed/src/main.rs";
284        assert_eq!(permalink.to_string(), expected_url.to_string())
285    }
286
287    #[test]
288    fn test_build_github_permalink_with_single_line_selection() {
289        let permalink = Github.build_permalink(
290            ParsedGitRemote {
291                owner: "zed-industries".into(),
292                repo: "zed".into(),
293            },
294            BuildPermalinkParams {
295                sha: "e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7",
296                path: "crates/editor/src/git/permalink.rs",
297                selection: Some(6..6),
298            },
299        );
300
301        let expected_url = "https://github.com/zed-industries/zed/blob/e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7/crates/editor/src/git/permalink.rs#L7";
302        assert_eq!(permalink.to_string(), expected_url.to_string())
303    }
304
305    #[test]
306    fn test_build_github_permalink_with_multi_line_selection() {
307        let permalink = Github.build_permalink(
308            ParsedGitRemote {
309                owner: "zed-industries".into(),
310                repo: "zed".into(),
311            },
312            BuildPermalinkParams {
313                sha: "e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7",
314                path: "crates/editor/src/git/permalink.rs",
315                selection: Some(23..47),
316            },
317        );
318
319        let expected_url = "https://github.com/zed-industries/zed/blob/e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7/crates/editor/src/git/permalink.rs#L24-L48";
320        assert_eq!(permalink.to_string(), expected_url.to_string())
321    }
322
323    #[test]
324    fn test_github_pull_requests() {
325        let remote = ParsedGitRemote {
326            owner: "zed-industries".into(),
327            repo: "zed".into(),
328        };
329
330        let message = "This does not contain a pull request";
331        assert!(Github.extract_pull_request(&remote, message).is_none());
332
333        // Pull request number at end of first line
334        let message = indoc! {r#"
335            project panel: do not expand collapsed worktrees on "collapse all entries" (#10687)
336
337            Fixes #10597
338
339            Release Notes:
340
341            - Fixed "project panel: collapse all entries" expanding collapsed worktrees.
342            "#
343        };
344
345        assert_eq!(
346            Github
347                .extract_pull_request(&remote, &message)
348                .unwrap()
349                .url
350                .as_str(),
351            "https://github.com/zed-industries/zed/pull/10687"
352        );
353
354        // Pull request number in middle of line, which we want to ignore
355        let message = indoc! {r#"
356            Follow-up to #10687 to fix problems
357
358            See the original PR, this is a fix.
359            "#
360        };
361        assert_eq!(Github.extract_pull_request(&remote, &message), None);
362    }
363}