gitlab.rs

  1use std::str::FromStr;
  2
  3use anyhow::{anyhow, bail, Result};
  4use url::Url;
  5use util::maybe;
  6
  7use git::{
  8    BuildCommitPermalinkParams, BuildPermalinkParams, GitHostingProvider, ParsedGitRemote,
  9    RemoteUrl,
 10};
 11
 12#[derive(Debug)]
 13pub struct Gitlab {
 14    name: String,
 15    base_url: Url,
 16}
 17
 18impl Gitlab {
 19    pub fn new() -> Self {
 20        Self {
 21            name: "GitLab".to_string(),
 22            base_url: Url::parse("https://gitlab.com").unwrap(),
 23        }
 24    }
 25
 26    pub fn from_remote_url(remote_url: &str) -> Result<Self> {
 27        let host = maybe!({
 28            if let Some(remote_url) = remote_url.strip_prefix("git@") {
 29                if let Some((host, _)) = remote_url.trim_start_matches("git@").split_once(':') {
 30                    return Some(host.to_string());
 31                }
 32            }
 33
 34            Url::parse(&remote_url)
 35                .ok()
 36                .and_then(|remote_url| remote_url.host_str().map(|host| host.to_string()))
 37        })
 38        .ok_or_else(|| anyhow!("URL has no host"))?;
 39
 40        if !host.contains("gitlab") {
 41            bail!("not a GitLab URL");
 42        }
 43
 44        Ok(Self {
 45            name: "GitLab Self-Hosted".to_string(),
 46            base_url: Url::parse(&format!("https://{}", host))?,
 47        })
 48    }
 49}
 50
 51impl GitHostingProvider for Gitlab {
 52    fn name(&self) -> String {
 53        self.name.clone()
 54    }
 55
 56    fn base_url(&self) -> Url {
 57        self.base_url.clone()
 58    }
 59
 60    fn supports_avatars(&self) -> bool {
 61        false
 62    }
 63
 64    fn format_line_number(&self, line: u32) -> String {
 65        format!("L{line}")
 66    }
 67
 68    fn format_line_numbers(&self, start_line: u32, end_line: u32) -> String {
 69        format!("L{start_line}-{end_line}")
 70    }
 71
 72    fn parse_remote_url(&self, url: &str) -> Option<ParsedGitRemote> {
 73        let url = RemoteUrl::from_str(url).ok()?;
 74
 75        let host = url.host_str()?;
 76        if host != self.base_url.host_str()? {
 77            return None;
 78        }
 79
 80        let mut path_segments = url.path_segments()?.collect::<Vec<_>>();
 81        let repo = path_segments.pop()?.trim_end_matches(".git");
 82        let owner = path_segments.join("/");
 83
 84        Some(ParsedGitRemote {
 85            owner: owner.into(),
 86            repo: repo.into(),
 87        })
 88    }
 89
 90    fn build_commit_permalink(
 91        &self,
 92        remote: &ParsedGitRemote,
 93        params: BuildCommitPermalinkParams,
 94    ) -> Url {
 95        let BuildCommitPermalinkParams { sha } = params;
 96        let ParsedGitRemote { owner, repo } = remote;
 97
 98        self.base_url()
 99            .join(&format!("{owner}/{repo}/-/commit/{sha}"))
100            .unwrap()
101    }
102
103    fn build_permalink(&self, remote: ParsedGitRemote, params: BuildPermalinkParams) -> Url {
104        let ParsedGitRemote { owner, repo } = remote;
105        let BuildPermalinkParams {
106            sha,
107            path,
108            selection,
109        } = params;
110
111        let mut permalink = self
112            .base_url()
113            .join(&format!("{owner}/{repo}/-/blob/{sha}/{path}"))
114            .unwrap();
115        if path.ends_with(".md") {
116            permalink.set_query(Some("plain=1"));
117        }
118        permalink.set_fragment(
119            selection
120                .map(|selection| self.line_fragment(&selection))
121                .as_deref(),
122        );
123        permalink
124    }
125}
126
127#[cfg(test)]
128mod tests {
129    use pretty_assertions::assert_eq;
130
131    use super::*;
132
133    #[test]
134    fn test_parse_remote_url_given_ssh_url() {
135        let parsed_remote = Gitlab::new()
136            .parse_remote_url("git@gitlab.com:zed-industries/zed.git")
137            .unwrap();
138
139        assert_eq!(
140            parsed_remote,
141            ParsedGitRemote {
142                owner: "zed-industries".into(),
143                repo: "zed".into(),
144            }
145        );
146    }
147
148    #[test]
149    fn test_parse_remote_url_given_https_url() {
150        let parsed_remote = Gitlab::new()
151            .parse_remote_url("https://gitlab.com/zed-industries/zed.git")
152            .unwrap();
153
154        assert_eq!(
155            parsed_remote,
156            ParsedGitRemote {
157                owner: "zed-industries".into(),
158                repo: "zed".into(),
159            }
160        );
161    }
162
163    #[test]
164    fn test_parse_remote_url_given_self_hosted_ssh_url() {
165        let remote_url = "git@gitlab.my-enterprise.com:zed-industries/zed.git";
166
167        let parsed_remote = Gitlab::from_remote_url(remote_url)
168            .unwrap()
169            .parse_remote_url(remote_url)
170            .unwrap();
171
172        assert_eq!(
173            parsed_remote,
174            ParsedGitRemote {
175                owner: "zed-industries".into(),
176                repo: "zed".into(),
177            }
178        );
179    }
180
181    #[test]
182    fn test_parse_remote_url_given_self_hosted_https_url_with_subgroup() {
183        let remote_url = "https://gitlab.my-enterprise.com/group/subgroup/zed.git";
184        let parsed_remote = Gitlab::from_remote_url(remote_url)
185            .unwrap()
186            .parse_remote_url(remote_url)
187            .unwrap();
188
189        assert_eq!(
190            parsed_remote,
191            ParsedGitRemote {
192                owner: "group/subgroup".into(),
193                repo: "zed".into(),
194            }
195        );
196    }
197
198    #[test]
199    fn test_build_gitlab_permalink() {
200        let permalink = Gitlab::new().build_permalink(
201            ParsedGitRemote {
202                owner: "zed-industries".into(),
203                repo: "zed".into(),
204            },
205            BuildPermalinkParams {
206                sha: "e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7",
207                path: "crates/editor/src/git/permalink.rs",
208                selection: None,
209            },
210        );
211
212        let expected_url = "https://gitlab.com/zed-industries/zed/-/blob/e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7/crates/editor/src/git/permalink.rs";
213        assert_eq!(permalink.to_string(), expected_url.to_string())
214    }
215
216    #[test]
217    fn test_build_gitlab_permalink_with_single_line_selection() {
218        let permalink = Gitlab::new().build_permalink(
219            ParsedGitRemote {
220                owner: "zed-industries".into(),
221                repo: "zed".into(),
222            },
223            BuildPermalinkParams {
224                sha: "e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7",
225                path: "crates/editor/src/git/permalink.rs",
226                selection: Some(6..6),
227            },
228        );
229
230        let expected_url = "https://gitlab.com/zed-industries/zed/-/blob/e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7/crates/editor/src/git/permalink.rs#L7";
231        assert_eq!(permalink.to_string(), expected_url.to_string())
232    }
233
234    #[test]
235    fn test_build_gitlab_permalink_with_multi_line_selection() {
236        let permalink = Gitlab::new().build_permalink(
237            ParsedGitRemote {
238                owner: "zed-industries".into(),
239                repo: "zed".into(),
240            },
241            BuildPermalinkParams {
242                sha: "e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7",
243                path: "crates/editor/src/git/permalink.rs",
244                selection: Some(23..47),
245            },
246        );
247
248        let expected_url = "https://gitlab.com/zed-industries/zed/-/blob/e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7/crates/editor/src/git/permalink.rs#L24-48";
249        assert_eq!(permalink.to_string(), expected_url.to_string())
250    }
251
252    #[test]
253    fn test_build_gitlab_self_hosted_permalink_from_ssh_url() {
254        let gitlab =
255            Gitlab::from_remote_url("git@gitlab.some-enterprise.com:zed-industries/zed.git")
256                .unwrap();
257        let permalink = gitlab.build_permalink(
258            ParsedGitRemote {
259                owner: "zed-industries".into(),
260                repo: "zed".into(),
261            },
262            BuildPermalinkParams {
263                sha: "e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7",
264                path: "crates/editor/src/git/permalink.rs",
265                selection: None,
266            },
267        );
268
269        let expected_url = "https://gitlab.some-enterprise.com/zed-industries/zed/-/blob/e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7/crates/editor/src/git/permalink.rs";
270        assert_eq!(permalink.to_string(), expected_url.to_string())
271    }
272
273    #[test]
274    fn test_build_gitlab_self_hosted_permalink_from_https_url() {
275        let gitlab =
276            Gitlab::from_remote_url("https://gitlab-instance.big-co.com/zed-industries/zed.git")
277                .unwrap();
278        let permalink = gitlab.build_permalink(
279            ParsedGitRemote {
280                owner: "zed-industries".into(),
281                repo: "zed".into(),
282            },
283            BuildPermalinkParams {
284                sha: "b2efec9824c45fcc90c9a7eb107a50d1772a60aa",
285                path: "crates/zed/src/main.rs",
286                selection: None,
287            },
288        );
289
290        let expected_url = "https://gitlab-instance.big-co.com/zed-industries/zed/-/blob/b2efec9824c45fcc90c9a7eb107a50d1772a60aa/crates/zed/src/main.rs";
291        assert_eq!(permalink.to_string(), expected_url.to_string())
292    }
293}