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