1use std::str::FromStr;
2use std::sync::Arc;
3
4use anyhow::{Context as _, Result, bail};
5use async_trait::async_trait;
6use futures::AsyncReadExt;
7use gpui::SharedString;
8use http_client::{AsyncBody, HttpClient, HttpRequestExt, Request};
9use serde::Deserialize;
10use url::Url;
11
12use git::{
13 BuildCommitPermalinkParams, BuildPermalinkParams, GitHostingProvider, ParsedGitRemote,
14 RemoteUrl,
15};
16
17use crate::get_host_from_git_remote_url;
18
19#[derive(Debug, Deserialize)]
20struct CommitDetails {
21 author: Option<User>,
22}
23
24#[derive(Debug, Deserialize)]
25struct User {
26 pub avatar_url: String,
27}
28
29pub struct Gitea {
30 name: String,
31 base_url: Url,
32}
33
34impl Gitea {
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("Gitea", Url::parse("https://gitea.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 == "gitea.com" {
49 bail!("the Gitea instance is not self-hosted");
50 }
51
52 // TODO: detecting self hosted instances by checking whether "gitea" 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("gitea") {
56 bail!("not a Gitea URL");
57 }
58
59 Ok(Self::new(
60 "Gitea Self-Hosted",
61 Url::parse(&format!("https://{}", host))?,
62 ))
63 }
64
65 async fn fetch_gitea_commit_author(
66 &self,
67 repo_owner: &str,
68 repo: &str,
69 commit: &str,
70 client: &Arc<dyn HttpClient>,
71 ) -> Result<Option<User>> {
72 let Some(host) = self.base_url.host_str() else {
73 bail!("failed to get host from gitea base url");
74 };
75 let url = format!(
76 "https://{host}/api/v1/repos/{repo_owner}/{repo}/git/commits/{commit}?stat=false&verification=false&files=false"
77 );
78
79 let request = Request::get(&url)
80 .header("Content-Type", "application/json")
81 .follow_redirects(http_client::RedirectPolicy::FollowAll);
82
83 let mut response = client
84 .send(request.body(AsyncBody::default())?)
85 .await
86 .with_context(|| format!("error fetching Gitea commit details at {:?}", url))?;
87
88 let mut body = Vec::new();
89 response.body_mut().read_to_end(&mut body).await?;
90
91 if response.status().is_client_error() {
92 let text = String::from_utf8_lossy(body.as_slice());
93 bail!(
94 "status error {}, response: {text:?}",
95 response.status().as_u16()
96 );
97 }
98
99 let body_str = std::str::from_utf8(&body)?;
100
101 serde_json::from_str::<CommitDetails>(body_str)
102 .map(|commit| commit.author)
103 .context("failed to deserialize Gitea commit details")
104 }
105}
106
107#[async_trait]
108impl GitHostingProvider for Gitea {
109 fn name(&self) -> String {
110 self.name.clone()
111 }
112
113 fn base_url(&self) -> Url {
114 self.base_url.clone()
115 }
116
117 fn supports_avatars(&self) -> bool {
118 true
119 }
120
121 fn format_line_number(&self, line: u32) -> String {
122 format!("L{line}")
123 }
124
125 fn format_line_numbers(&self, start_line: u32, end_line: u32) -> String {
126 format!("L{start_line}-L{end_line}")
127 }
128
129 fn parse_remote_url(&self, url: &str) -> Option<ParsedGitRemote> {
130 let url = RemoteUrl::from_str(url).ok()?;
131
132 let host = url.host_str()?;
133 if host != self.base_url.host_str()? {
134 return None;
135 }
136
137 let mut path_segments = url.path_segments()?;
138 let owner = path_segments.next()?;
139 let repo = path_segments.next()?.trim_end_matches(".git");
140
141 Some(ParsedGitRemote {
142 owner: owner.into(),
143 repo: repo.into(),
144 })
145 }
146
147 fn build_commit_permalink(
148 &self,
149 remote: &ParsedGitRemote,
150 params: BuildCommitPermalinkParams,
151 ) -> Url {
152 let BuildCommitPermalinkParams { sha } = params;
153 let ParsedGitRemote { owner, repo } = remote;
154
155 self.base_url()
156 .join(&format!("{owner}/{repo}/commit/{sha}"))
157 .unwrap()
158 }
159
160 fn build_permalink(&self, remote: ParsedGitRemote, params: BuildPermalinkParams) -> Url {
161 let ParsedGitRemote { owner, repo } = remote;
162 let BuildPermalinkParams {
163 sha,
164 path,
165 selection,
166 } = params;
167
168 let mut permalink = self
169 .base_url()
170 .join(&format!("{owner}/{repo}/src/commit/{sha}/{path}"))
171 .unwrap();
172 permalink.set_fragment(
173 selection
174 .map(|selection| self.line_fragment(&selection))
175 .as_deref(),
176 );
177 permalink
178 }
179
180 async fn commit_author_avatar_url(
181 &self,
182 repo_owner: &str,
183 repo: &str,
184 commit: SharedString,
185 _author_email: Option<SharedString>,
186 http_client: Arc<dyn HttpClient>,
187 ) -> Result<Option<Url>> {
188 let commit = commit.to_string();
189 let avatar_url = self
190 .fetch_gitea_commit_author(repo_owner, repo, &commit, &http_client)
191 .await?
192 .map(|author| -> Result<Url, url::ParseError> {
193 let mut url = Url::parse(&author.avatar_url)?;
194 if let Some(host) = url.host_str() {
195 let size_query = if host.contains("gravatar") || host.contains("libravatar") {
196 Some("s=128")
197 } else if self
198 .base_url
199 .host_str()
200 .is_some_and(|base_host| host.contains(base_host))
201 {
202 Some("size=128")
203 } else {
204 None
205 };
206 url.set_query(size_query);
207 }
208 Ok(url)
209 })
210 .transpose()?;
211 Ok(avatar_url)
212 }
213}
214
215#[cfg(test)]
216mod tests {
217 use git::repository::repo_path;
218 use pretty_assertions::assert_eq;
219
220 use super::*;
221
222 #[test]
223 fn test_parse_remote_url_given_ssh_url() {
224 let parsed_remote = Gitea::public_instance()
225 .parse_remote_url("git@gitea.com:zed-industries/zed.git")
226 .unwrap();
227
228 assert_eq!(
229 parsed_remote,
230 ParsedGitRemote {
231 owner: "zed-industries".into(),
232 repo: "zed".into(),
233 }
234 );
235 }
236
237 #[test]
238 fn test_parse_remote_url_given_https_url() {
239 let parsed_remote = Gitea::public_instance()
240 .parse_remote_url("https://gitea.com/zed-industries/zed.git")
241 .unwrap();
242
243 assert_eq!(
244 parsed_remote,
245 ParsedGitRemote {
246 owner: "zed-industries".into(),
247 repo: "zed".into(),
248 }
249 );
250 }
251
252 #[test]
253 fn test_parse_remote_url_given_self_hosted_ssh_url() {
254 let remote_url = "git@gitea.my-enterprise.com:zed-industries/zed.git";
255
256 let parsed_remote = Gitea::from_remote_url(remote_url)
257 .unwrap()
258 .parse_remote_url(remote_url)
259 .unwrap();
260
261 assert_eq!(
262 parsed_remote,
263 ParsedGitRemote {
264 owner: "zed-industries".into(),
265 repo: "zed".into(),
266 }
267 );
268 }
269
270 #[test]
271 fn test_parse_remote_url_given_self_hosted_https_url() {
272 let remote_url = "https://gitea.my-enterprise.com/zed-industries/zed.git";
273 let parsed_remote = Gitea::from_remote_url(remote_url)
274 .unwrap()
275 .parse_remote_url(remote_url)
276 .unwrap();
277
278 assert_eq!(
279 parsed_remote,
280 ParsedGitRemote {
281 owner: "zed-industries".into(),
282 repo: "zed".into(),
283 }
284 );
285 }
286
287 #[test]
288 fn test_build_codeberg_permalink() {
289 let permalink = Gitea::public_instance().build_permalink(
290 ParsedGitRemote {
291 owner: "zed-industries".into(),
292 repo: "zed".into(),
293 },
294 BuildPermalinkParams::new(
295 "faa6f979be417239b2e070dbbf6392b909224e0b",
296 &repo_path("crates/editor/src/git/permalink.rs"),
297 None,
298 ),
299 );
300
301 let expected_url = "https://gitea.com/zed-industries/zed/src/commit/faa6f979be417239b2e070dbbf6392b909224e0b/crates/editor/src/git/permalink.rs";
302 assert_eq!(permalink.to_string(), expected_url.to_string())
303 }
304
305 #[test]
306 fn test_build_codeberg_permalink_with_single_line_selection() {
307 let permalink = Gitea::public_instance().build_permalink(
308 ParsedGitRemote {
309 owner: "zed-industries".into(),
310 repo: "zed".into(),
311 },
312 BuildPermalinkParams::new(
313 "faa6f979be417239b2e070dbbf6392b909224e0b",
314 &repo_path("crates/editor/src/git/permalink.rs"),
315 Some(6..6),
316 ),
317 );
318
319 let expected_url = "https://gitea.com/zed-industries/zed/src/commit/faa6f979be417239b2e070dbbf6392b909224e0b/crates/editor/src/git/permalink.rs#L7";
320 assert_eq!(permalink.to_string(), expected_url.to_string())
321 }
322
323 #[test]
324 fn test_build_codeberg_permalink_with_multi_line_selection() {
325 let permalink = Gitea::public_instance().build_permalink(
326 ParsedGitRemote {
327 owner: "zed-industries".into(),
328 repo: "zed".into(),
329 },
330 BuildPermalinkParams::new(
331 "faa6f979be417239b2e070dbbf6392b909224e0b",
332 &repo_path("crates/editor/src/git/permalink.rs"),
333 Some(23..47),
334 ),
335 );
336
337 let expected_url = "https://gitea.com/zed-industries/zed/src/commit/faa6f979be417239b2e070dbbf6392b909224e0b/crates/editor/src/git/permalink.rs#L24-L48";
338 assert_eq!(permalink.to_string(), expected_url.to_string())
339 }
340
341 #[test]
342 fn test_build_gitea_self_hosted_permalink_from_ssh_url() {
343 let gitea =
344 Gitea::from_remote_url("git@gitea.some-enterprise.com:zed-industries/zed.git").unwrap();
345 let permalink = gitea.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 None,
354 ),
355 );
356
357 let expected_url = "https://gitea.some-enterprise.com/zed-industries/zed/src/commit/e6ebe7974deb6bb6cc0e2595c8ec31f0c71084b7/crates/editor/src/git/permalink.rs";
358 assert_eq!(permalink.to_string(), expected_url.to_string())
359 }
360
361 #[test]
362 fn test_build_gitea_self_hosted_permalink_from_https_url() {
363 let gitea =
364 Gitea::from_remote_url("https://gitea-instance.big-co.com/zed-industries/zed.git")
365 .unwrap();
366 let permalink = gitea.build_permalink(
367 ParsedGitRemote {
368 owner: "zed-industries".into(),
369 repo: "zed".into(),
370 },
371 BuildPermalinkParams::new(
372 "b2efec9824c45fcc90c9a7eb107a50d1772a60aa",
373 &repo_path("crates/zed/src/main.rs"),
374 None,
375 ),
376 );
377
378 let expected_url = "https://gitea-instance.big-co.com/zed-industries/zed/src/commit/b2efec9824c45fcc90c9a7eb107a50d1772a60aa/crates/zed/src/main.rs";
379 assert_eq!(permalink.to_string(), expected_url.to_string())
380 }
381}