gitlab.rs

  1use std::{str::FromStr, sync::Arc};
  2
  3use anyhow::{Context as _, Result, bail};
  4use async_trait::async_trait;
  5use futures::AsyncReadExt;
  6use gpui::SharedString;
  7use http_client::{AsyncBody, HttpClient, HttpRequestExt, Request};
  8use serde::Deserialize;
  9use url::Url;
 10use urlencoding::encode;
 11
 12use git::{
 13    BuildCommitPermalinkParams, BuildPermalinkParams, GitHostingProvider, ParsedGitRemote,
 14    RemoteUrl,
 15};
 16
 17use crate::get_host_from_git_remote_url;
 18
 19#[derive(Debug, Deserialize)]
 20struct CommitDetails {
 21    author_email: String,
 22}
 23
 24#[derive(Debug, Deserialize)]
 25struct AvatarInfo {
 26    avatar_url: String,
 27}
 28
 29#[derive(Debug)]
 30pub struct Gitlab {
 31    name: String,
 32    base_url: Url,
 33}
 34
 35impl Gitlab {
 36    pub fn new(name: impl Into<String>, base_url: Url) -> Self {
 37        Self {
 38            name: name.into(),
 39            base_url,
 40        }
 41    }
 42
 43    pub fn public_instance() -> Self {
 44        Self::new("GitLab", Url::parse("https://gitlab.com").unwrap())
 45    }
 46
 47    pub fn from_remote_url(remote_url: &str) -> Result<Self> {
 48        let host = get_host_from_git_remote_url(remote_url)?;
 49        if host == "gitlab.com" {
 50            bail!("the GitLab instance is not self-hosted");
 51        }
 52
 53        // TODO: detecting self hosted instances by checking whether "gitlab" is in the url or not
 54        // is not very reliable. See https://github.com/zed-industries/zed/issues/26393 for more
 55        // information.
 56        if !host.contains("gitlab") {
 57            bail!("not a GitLab URL");
 58        }
 59
 60        Ok(Self::new(
 61            "GitLab Self-Hosted",
 62            Url::parse(&format!("https://{}", host))?,
 63        ))
 64    }
 65
 66    async fn fetch_gitlab_commit_author(
 67        &self,
 68        repo_owner: &str,
 69        repo: &str,
 70        commit: &str,
 71        client: &Arc<dyn HttpClient>,
 72    ) -> Result<Option<AvatarInfo>> {
 73        let Some(host) = self.base_url.host_str() else {
 74            bail!("failed to get host from gitlab base url");
 75        };
 76        let project_path = format!("{}/{}", repo_owner, repo);
 77        let project_path_encoded = urlencoding::encode(&project_path);
 78        let url = format!(
 79            "https://{host}/api/v4/projects/{project_path_encoded}/repository/commits/{commit}"
 80        );
 81
 82        let request = Request::get(&url)
 83            .header("Content-Type", "application/json")
 84            .follow_redirects(http_client::RedirectPolicy::FollowAll);
 85
 86        let mut response = client
 87            .send(request.body(AsyncBody::default())?)
 88            .await
 89            .with_context(|| format!("error fetching GitLab commit details at {:?}", url))?;
 90
 91        let mut body = Vec::new();
 92        response.body_mut().read_to_end(&mut body).await?;
 93
 94        if response.status().is_client_error() {
 95            let text = String::from_utf8_lossy(body.as_slice());
 96            bail!(
 97                "status error {}, response: {text:?}",
 98                response.status().as_u16()
 99            );
100        }
101
102        let body_str = std::str::from_utf8(&body)?;
103
104        let author_email = serde_json::from_str::<CommitDetails>(body_str)
105            .map(|commit| commit.author_email)
106            .context("failed to deserialize GitLab commit details")?;
107
108        let avatar_info_url = format!("https://{host}/api/v4/avatar?email={author_email}");
109
110        let request = Request::get(&avatar_info_url)
111            .header("Content-Type", "application/json")
112            .follow_redirects(http_client::RedirectPolicy::FollowAll);
113
114        let mut response = client
115            .send(request.body(AsyncBody::default())?)
116            .await
117            .with_context(|| format!("error fetching GitLab avatar info at {:?}", url))?;
118
119        let mut body = Vec::new();
120        response.body_mut().read_to_end(&mut body).await?;
121
122        if response.status().is_client_error() {
123            let text = String::from_utf8_lossy(body.as_slice());
124            bail!(
125                "status error {}, response: {text:?}",
126                response.status().as_u16()
127            );
128        }
129
130        let body_str = std::str::from_utf8(&body)?;
131
132        serde_json::from_str::<Option<AvatarInfo>>(body_str)
133            .context("failed to deserialize GitLab avatar info")
134    }
135}
136
137#[async_trait]
138impl GitHostingProvider for Gitlab {
139    fn name(&self) -> String {
140        self.name.clone()
141    }
142
143    fn base_url(&self) -> Url {
144        self.base_url.clone()
145    }
146
147    fn supports_avatars(&self) -> bool {
148        true
149    }
150
151    fn format_line_number(&self, line: u32) -> String {
152        format!("L{line}")
153    }
154
155    fn format_line_numbers(&self, start_line: u32, end_line: u32) -> String {
156        format!("L{start_line}-{end_line}")
157    }
158
159    fn parse_remote_url(&self, url: &str) -> Option<ParsedGitRemote> {
160        let url = RemoteUrl::from_str(url).ok()?;
161
162        let host = url.host_str()?;
163        if host != self.base_url.host_str()? {
164            return None;
165        }
166
167        let mut path_segments = url.path_segments()?.collect::<Vec<_>>();
168        let repo = path_segments.pop()?.trim_end_matches(".git");
169        let owner = path_segments.join("/");
170
171        Some(ParsedGitRemote {
172            owner: owner.into(),
173            repo: repo.into(),
174        })
175    }
176
177    fn build_commit_permalink(
178        &self,
179        remote: &ParsedGitRemote,
180        params: BuildCommitPermalinkParams,
181    ) -> Url {
182        let BuildCommitPermalinkParams { sha } = params;
183        let ParsedGitRemote { owner, repo } = remote;
184
185        self.base_url()
186            .join(&format!("{owner}/{repo}/-/commit/{sha}"))
187            .unwrap()
188    }
189
190    fn build_permalink(&self, remote: ParsedGitRemote, params: BuildPermalinkParams) -> Url {
191        let ParsedGitRemote { owner, repo } = remote;
192        let BuildPermalinkParams {
193            sha,
194            path,
195            selection,
196        } = params;
197
198        let mut permalink = self
199            .base_url()
200            .join(&format!("{owner}/{repo}/-/blob/{sha}/{path}"))
201            .unwrap();
202        if path.ends_with(".md") {
203            permalink.set_query(Some("plain=1"));
204        }
205        permalink.set_fragment(
206            selection
207                .map(|selection| self.line_fragment(&selection))
208                .as_deref(),
209        );
210        permalink
211    }
212
213    fn build_create_pull_request_url(
214        &self,
215        remote: &ParsedGitRemote,
216        source_branch: &str,
217    ) -> Option<Url> {
218        let mut url = self
219            .base_url()
220            .join(&format!(
221                "{}/{}/-/merge_requests/new",
222                remote.owner, remote.repo
223            ))
224            .ok()?;
225
226        let query = format!("merge_request%5Bsource_branch%5D={}", encode(source_branch));
227
228        url.set_query(Some(&query));
229        Some(url)
230    }
231
232    async fn commit_author_avatar_url(
233        &self,
234        repo_owner: &str,
235        repo: &str,
236        commit: SharedString,
237        http_client: Arc<dyn HttpClient>,
238    ) -> Result<Option<Url>> {
239        let commit = commit.to_string();
240        let avatar_url = self
241            .fetch_gitlab_commit_author(repo_owner, repo, &commit, &http_client)
242            .await?
243            .map(|author| -> Result<Url, url::ParseError> {
244                let mut url = Url::parse(&author.avatar_url)?;
245                if let Some(host) = url.host_str() {
246                    let size_query = if host.contains("gravatar") || host.contains("libravatar") {
247                        Some("s=128")
248                    } else if self
249                        .base_url
250                        .host_str()
251                        .is_some_and(|base_host| host.contains(base_host))
252                    {
253                        Some("width=128")
254                    } else {
255                        None
256                    };
257                    url.set_query(size_query);
258                }
259                Ok(url)
260            })
261            .transpose()?;
262        Ok(avatar_url)
263    }
264}
265
266#[cfg(test)]
267mod tests {
268    use git::repository::repo_path;
269    use pretty_assertions::assert_eq;
270
271    use super::*;
272
273    #[test]
274    fn test_invalid_self_hosted_remote_url() {
275        let remote_url = "https://gitlab.com/zed-industries/zed.git";
276        let gitlab = Gitlab::from_remote_url(remote_url);
277        assert!(gitlab.is_err());
278    }
279
280    #[test]
281    fn test_parse_remote_url_given_ssh_url() {
282        let parsed_remote = Gitlab::public_instance()
283            .parse_remote_url("git@gitlab.com:zed-industries/zed.git")
284            .unwrap();
285
286        assert_eq!(
287            parsed_remote,
288            ParsedGitRemote {
289                owner: "zed-industries".into(),
290                repo: "zed".into(),
291            }
292        );
293    }
294
295    #[test]
296    fn test_parse_remote_url_given_https_url() {
297        let parsed_remote = Gitlab::public_instance()
298            .parse_remote_url("https://gitlab.com/zed-industries/zed.git")
299            .unwrap();
300
301        assert_eq!(
302            parsed_remote,
303            ParsedGitRemote {
304                owner: "zed-industries".into(),
305                repo: "zed".into(),
306            }
307        );
308    }
309
310    #[test]
311    fn test_parse_remote_url_given_self_hosted_ssh_url() {
312        let remote_url = "git@gitlab.my-enterprise.com:zed-industries/zed.git";
313
314        let parsed_remote = Gitlab::from_remote_url(remote_url)
315            .unwrap()
316            .parse_remote_url(remote_url)
317            .unwrap();
318
319        assert_eq!(
320            parsed_remote,
321            ParsedGitRemote {
322                owner: "zed-industries".into(),
323                repo: "zed".into(),
324            }
325        );
326    }
327
328    #[test]
329    fn test_parse_remote_url_given_self_hosted_https_url_with_subgroup() {
330        let remote_url = "https://gitlab.my-enterprise.com/group/subgroup/zed.git";
331        let parsed_remote = Gitlab::from_remote_url(remote_url)
332            .unwrap()
333            .parse_remote_url(remote_url)
334            .unwrap();
335
336        assert_eq!(
337            parsed_remote,
338            ParsedGitRemote {
339                owner: "group/subgroup".into(),
340                repo: "zed".into(),
341            }
342        );
343    }
344
345    #[test]
346    fn test_build_gitlab_permalink() {
347        let permalink = Gitlab::public_instance().build_permalink(
348            ParsedGitRemote {
349                owner: "zed-industries".into(),
350                repo: "zed".into(),
351            },
352            BuildPermalinkParams::new(
353                "e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7",
354                &repo_path("crates/editor/src/git/permalink.rs"),
355                None,
356            ),
357        );
358
359        let expected_url = "https://gitlab.com/zed-industries/zed/-/blob/e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7/crates/editor/src/git/permalink.rs";
360        assert_eq!(permalink.to_string(), expected_url.to_string())
361    }
362
363    #[test]
364    fn test_build_gitlab_permalink_with_single_line_selection() {
365        let permalink = Gitlab::public_instance().build_permalink(
366            ParsedGitRemote {
367                owner: "zed-industries".into(),
368                repo: "zed".into(),
369            },
370            BuildPermalinkParams::new(
371                "e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7",
372                &repo_path("crates/editor/src/git/permalink.rs"),
373                Some(6..6),
374            ),
375        );
376
377        let expected_url = "https://gitlab.com/zed-industries/zed/-/blob/e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7/crates/editor/src/git/permalink.rs#L7";
378        assert_eq!(permalink.to_string(), expected_url.to_string())
379    }
380
381    #[test]
382    fn test_build_gitlab_permalink_with_multi_line_selection() {
383        let permalink = Gitlab::public_instance().build_permalink(
384            ParsedGitRemote {
385                owner: "zed-industries".into(),
386                repo: "zed".into(),
387            },
388            BuildPermalinkParams::new(
389                "e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7",
390                &repo_path("crates/editor/src/git/permalink.rs"),
391                Some(23..47),
392            ),
393        );
394
395        let expected_url = "https://gitlab.com/zed-industries/zed/-/blob/e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7/crates/editor/src/git/permalink.rs#L24-48";
396        assert_eq!(permalink.to_string(), expected_url.to_string())
397    }
398
399    #[test]
400    fn test_build_gitlab_create_pr_url() {
401        let remote = ParsedGitRemote {
402            owner: "zed-industries".into(),
403            repo: "zed".into(),
404        };
405
406        let provider = Gitlab::public_instance();
407
408        let url = provider
409            .build_create_pull_request_url(&remote, "feature/cool stuff")
410            .expect("create PR url should be constructed");
411
412        assert_eq!(
413            url.as_str(),
414            "https://gitlab.com/zed-industries/zed/-/merge_requests/new?merge_request%5Bsource_branch%5D=feature%2Fcool%20stuff"
415        );
416    }
417
418    #[test]
419    fn test_build_gitlab_self_hosted_permalink_from_ssh_url() {
420        let gitlab =
421            Gitlab::from_remote_url("git@gitlab.some-enterprise.com:zed-industries/zed.git")
422                .unwrap();
423        let permalink = gitlab.build_permalink(
424            ParsedGitRemote {
425                owner: "zed-industries".into(),
426                repo: "zed".into(),
427            },
428            BuildPermalinkParams::new(
429                "e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7",
430                &repo_path("crates/editor/src/git/permalink.rs"),
431                None,
432            ),
433        );
434
435        let expected_url = "https://gitlab.some-enterprise.com/zed-industries/zed/-/blob/e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7/crates/editor/src/git/permalink.rs";
436        assert_eq!(permalink.to_string(), expected_url.to_string())
437    }
438
439    #[test]
440    fn test_build_gitlab_self_hosted_permalink_from_https_url() {
441        let gitlab =
442            Gitlab::from_remote_url("https://gitlab-instance.big-co.com/zed-industries/zed.git")
443                .unwrap();
444        let permalink = gitlab.build_permalink(
445            ParsedGitRemote {
446                owner: "zed-industries".into(),
447                repo: "zed".into(),
448            },
449            BuildPermalinkParams::new(
450                "b2efec9824c45fcc90c9a7eb107a50d1772a60aa",
451                &repo_path("crates/zed/src/main.rs"),
452                None,
453            ),
454        );
455
456        let expected_url = "https://gitlab-instance.big-co.com/zed-industries/zed/-/blob/b2efec9824c45fcc90c9a7eb107a50d1772a60aa/crates/zed/src/main.rs";
457        assert_eq!(permalink.to_string(), expected_url.to_string())
458    }
459
460    #[test]
461    fn test_build_create_pull_request_url() {
462        let remote = ParsedGitRemote {
463            owner: "zed-industries".into(),
464            repo: "zed".into(),
465        };
466
467        let github = Gitlab::public_instance();
468        let url = github
469            .build_create_pull_request_url(&remote, "feature/new-feature")
470            .unwrap();
471
472        assert_eq!(
473            url.as_str(),
474            "https://gitlab.com/zed-industries/zed/-/merge_requests/new?merge_request%5Bsource_branch%5D=feature%2Fnew-feature"
475        );
476
477        let base_url = Url::parse("https://gitlab.zed.com").unwrap();
478        let github = Gitlab::new("GitLab Self-Hosted", base_url);
479        let url = github
480            .build_create_pull_request_url(&remote, "feature/new-feature")
481            .expect("should be able to build pull request url");
482
483        assert_eq!(
484            url.as_str(),
485            "https://gitlab.zed.com/zed-industries/zed/-/merge_requests/new?merge_request%5Bsource_branch%5D=feature%2Fnew-feature"
486        );
487    }
488}