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()?;
81 let owner = path_segments.next()?;
82 let repo = path_segments.next()?.trim_end_matches(".git");
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_build_gitlab_permalink() {
183 let permalink = Gitlab::new().build_permalink(
184 ParsedGitRemote {
185 owner: "zed-industries".into(),
186 repo: "zed".into(),
187 },
188 BuildPermalinkParams {
189 sha: "e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7",
190 path: "crates/editor/src/git/permalink.rs",
191 selection: None,
192 },
193 );
194
195 let expected_url = "https://gitlab.com/zed-industries/zed/-/blob/e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7/crates/editor/src/git/permalink.rs";
196 assert_eq!(permalink.to_string(), expected_url.to_string())
197 }
198
199 #[test]
200 fn test_build_gitlab_permalink_with_single_line_selection() {
201 let permalink = Gitlab::new().build_permalink(
202 ParsedGitRemote {
203 owner: "zed-industries".into(),
204 repo: "zed".into(),
205 },
206 BuildPermalinkParams {
207 sha: "e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7",
208 path: "crates/editor/src/git/permalink.rs",
209 selection: Some(6..6),
210 },
211 );
212
213 let expected_url = "https://gitlab.com/zed-industries/zed/-/blob/e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7/crates/editor/src/git/permalink.rs#L7";
214 assert_eq!(permalink.to_string(), expected_url.to_string())
215 }
216
217 #[test]
218 fn test_build_gitlab_permalink_with_multi_line_selection() {
219 let permalink = Gitlab::new().build_permalink(
220 ParsedGitRemote {
221 owner: "zed-industries".into(),
222 repo: "zed".into(),
223 },
224 BuildPermalinkParams {
225 sha: "e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7",
226 path: "crates/editor/src/git/permalink.rs",
227 selection: Some(23..47),
228 },
229 );
230
231 let expected_url = "https://gitlab.com/zed-industries/zed/-/blob/e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7/crates/editor/src/git/permalink.rs#L24-48";
232 assert_eq!(permalink.to_string(), expected_url.to_string())
233 }
234
235 #[test]
236 fn test_build_gitlab_self_hosted_permalink_from_ssh_url() {
237 let gitlab =
238 Gitlab::from_remote_url("git@gitlab.some-enterprise.com:zed-industries/zed.git")
239 .unwrap();
240 let permalink = gitlab.build_permalink(
241 ParsedGitRemote {
242 owner: "zed-industries".into(),
243 repo: "zed".into(),
244 },
245 BuildPermalinkParams {
246 sha: "e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7",
247 path: "crates/editor/src/git/permalink.rs",
248 selection: None,
249 },
250 );
251
252 let expected_url = "https://gitlab.some-enterprise.com/zed-industries/zed/-/blob/e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7/crates/editor/src/git/permalink.rs";
253 assert_eq!(permalink.to_string(), expected_url.to_string())
254 }
255
256 #[test]
257 fn test_build_gitlab_self_hosted_permalink_from_https_url() {
258 let gitlab =
259 Gitlab::from_remote_url("https://gitlab-instance.big-co.com/zed-industries/zed.git")
260 .unwrap();
261 let permalink = gitlab.build_permalink(
262 ParsedGitRemote {
263 owner: "zed-industries".into(),
264 repo: "zed".into(),
265 },
266 BuildPermalinkParams {
267 sha: "b2efec9824c45fcc90c9a7eb107a50d1772a60aa",
268 path: "crates/zed/src/main.rs",
269 selection: None,
270 },
271 );
272
273 let expected_url = "https://gitlab-instance.big-co.com/zed-industries/zed/-/blob/b2efec9824c45fcc90c9a7eb107a50d1772a60aa/crates/zed/src/main.rs";
274 assert_eq!(permalink.to_string(), expected_url.to_string())
275 }
276}