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