1use std::str::FromStr;
2
3use anyhow::{Result, bail};
4use url::Url;
5
6use git::{
7 BuildCommitPermalinkParams, BuildPermalinkParams, GitHostingProvider, ParsedGitRemote,
8 RemoteUrl,
9};
10
11use crate::get_host_from_git_remote_url;
12
13#[derive(Debug)]
14pub struct Gitlab {
15 name: String,
16 base_url: Url,
17}
18
19impl Gitlab {
20 pub fn new(name: impl Into<String>, base_url: Url) -> Self {
21 Self {
22 name: name.into(),
23 base_url,
24 }
25 }
26
27 pub fn public_instance() -> Self {
28 Self::new("GitLab", Url::parse("https://gitlab.com").unwrap())
29 }
30
31 pub fn from_remote_url(remote_url: &str) -> Result<Self> {
32 let host = get_host_from_git_remote_url(remote_url)?;
33 if host == "gitlab.com" {
34 bail!("the GitLab instance is not self-hosted");
35 }
36
37 // TODO: detecting self hosted instances by checking whether "gitlab" is in the url or not
38 // is not very reliable. See https://github.com/zed-industries/zed/issues/26393 for more
39 // information.
40 if !host.contains("gitlab") {
41 bail!("not a GitLab URL");
42 }
43
44 Ok(Self::new(
45 "GitLab Self-Hosted",
46 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 git::repository::repo_path;
130 use pretty_assertions::assert_eq;
131
132 use super::*;
133
134 #[test]
135 fn test_invalid_self_hosted_remote_url() {
136 let remote_url = "https://gitlab.com/zed-industries/zed.git";
137 let github = Gitlab::from_remote_url(remote_url);
138 assert!(github.is_err());
139 }
140
141 #[test]
142 fn test_parse_remote_url_given_ssh_url() {
143 let parsed_remote = Gitlab::public_instance()
144 .parse_remote_url("git@gitlab.com:zed-industries/zed.git")
145 .unwrap();
146
147 assert_eq!(
148 parsed_remote,
149 ParsedGitRemote {
150 owner: "zed-industries".into(),
151 repo: "zed".into(),
152 }
153 );
154 }
155
156 #[test]
157 fn test_parse_remote_url_given_https_url() {
158 let parsed_remote = Gitlab::public_instance()
159 .parse_remote_url("https://gitlab.com/zed-industries/zed.git")
160 .unwrap();
161
162 assert_eq!(
163 parsed_remote,
164 ParsedGitRemote {
165 owner: "zed-industries".into(),
166 repo: "zed".into(),
167 }
168 );
169 }
170
171 #[test]
172 fn test_parse_remote_url_given_self_hosted_ssh_url() {
173 let remote_url = "git@gitlab.my-enterprise.com:zed-industries/zed.git";
174
175 let parsed_remote = Gitlab::from_remote_url(remote_url)
176 .unwrap()
177 .parse_remote_url(remote_url)
178 .unwrap();
179
180 assert_eq!(
181 parsed_remote,
182 ParsedGitRemote {
183 owner: "zed-industries".into(),
184 repo: "zed".into(),
185 }
186 );
187 }
188
189 #[test]
190 fn test_parse_remote_url_given_self_hosted_https_url_with_subgroup() {
191 let remote_url = "https://gitlab.my-enterprise.com/group/subgroup/zed.git";
192 let parsed_remote = Gitlab::from_remote_url(remote_url)
193 .unwrap()
194 .parse_remote_url(remote_url)
195 .unwrap();
196
197 assert_eq!(
198 parsed_remote,
199 ParsedGitRemote {
200 owner: "group/subgroup".into(),
201 repo: "zed".into(),
202 }
203 );
204 }
205
206 #[test]
207 fn test_build_gitlab_permalink() {
208 let permalink = Gitlab::public_instance().build_permalink(
209 ParsedGitRemote {
210 owner: "zed-industries".into(),
211 repo: "zed".into(),
212 },
213 BuildPermalinkParams::new(
214 "e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7",
215 &repo_path("crates/editor/src/git/permalink.rs"),
216 None,
217 ),
218 );
219
220 let expected_url = "https://gitlab.com/zed-industries/zed/-/blob/e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7/crates/editor/src/git/permalink.rs";
221 assert_eq!(permalink.to_string(), expected_url.to_string())
222 }
223
224 #[test]
225 fn test_build_gitlab_permalink_with_single_line_selection() {
226 let permalink = Gitlab::public_instance().build_permalink(
227 ParsedGitRemote {
228 owner: "zed-industries".into(),
229 repo: "zed".into(),
230 },
231 BuildPermalinkParams::new(
232 "e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7",
233 &repo_path("crates/editor/src/git/permalink.rs"),
234 Some(6..6),
235 ),
236 );
237
238 let expected_url = "https://gitlab.com/zed-industries/zed/-/blob/e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7/crates/editor/src/git/permalink.rs#L7";
239 assert_eq!(permalink.to_string(), expected_url.to_string())
240 }
241
242 #[test]
243 fn test_build_gitlab_permalink_with_multi_line_selection() {
244 let permalink = Gitlab::public_instance().build_permalink(
245 ParsedGitRemote {
246 owner: "zed-industries".into(),
247 repo: "zed".into(),
248 },
249 BuildPermalinkParams::new(
250 "e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7",
251 &repo_path("crates/editor/src/git/permalink.rs"),
252 Some(23..47),
253 ),
254 );
255
256 let expected_url = "https://gitlab.com/zed-industries/zed/-/blob/e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7/crates/editor/src/git/permalink.rs#L24-48";
257 assert_eq!(permalink.to_string(), expected_url.to_string())
258 }
259
260 #[test]
261 fn test_build_gitlab_self_hosted_permalink_from_ssh_url() {
262 let gitlab =
263 Gitlab::from_remote_url("git@gitlab.some-enterprise.com:zed-industries/zed.git")
264 .unwrap();
265 let permalink = gitlab.build_permalink(
266 ParsedGitRemote {
267 owner: "zed-industries".into(),
268 repo: "zed".into(),
269 },
270 BuildPermalinkParams::new(
271 "e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7",
272 &repo_path("crates/editor/src/git/permalink.rs"),
273 None,
274 ),
275 );
276
277 let expected_url = "https://gitlab.some-enterprise.com/zed-industries/zed/-/blob/e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7/crates/editor/src/git/permalink.rs";
278 assert_eq!(permalink.to_string(), expected_url.to_string())
279 }
280
281 #[test]
282 fn test_build_gitlab_self_hosted_permalink_from_https_url() {
283 let gitlab =
284 Gitlab::from_remote_url("https://gitlab-instance.big-co.com/zed-industries/zed.git")
285 .unwrap();
286 let permalink = gitlab.build_permalink(
287 ParsedGitRemote {
288 owner: "zed-industries".into(),
289 repo: "zed".into(),
290 },
291 BuildPermalinkParams::new(
292 "b2efec9824c45fcc90c9a7eb107a50d1772a60aa",
293 &repo_path("crates/zed/src/main.rs"),
294 None,
295 ),
296 );
297
298 let expected_url = "https://gitlab-instance.big-co.com/zed-industries/zed/-/blob/b2efec9824c45fcc90c9a7eb107a50d1772a60aa/crates/zed/src/main.rs";
299 assert_eq!(permalink.to_string(), expected_url.to_string())
300 }
301}