chromium.rs

  1use std::str::FromStr;
  2use std::sync::{Arc, LazyLock};
  3
  4use anyhow::{Context as _, Result, bail};
  5use async_trait::async_trait;
  6use futures::AsyncReadExt;
  7use git::{
  8    BuildCommitPermalinkParams, BuildPermalinkParams, GitHostingProvider, ParsedGitRemote,
  9    PullRequest, RemoteUrl,
 10};
 11use gpui::SharedString;
 12use http_client::{AsyncBody, HttpClient, HttpRequestExt, Request};
 13use regex::Regex;
 14use serde::Deserialize;
 15use url::Url;
 16
 17const CHROMIUM_REVIEW_URL: &str = "https://chromium-review.googlesource.com";
 18
 19/// Parses Gerrit URLs like
 20/// https://chromium-review.googlesource.com/c/chromium/src/+/3310961.
 21fn pull_request_regex() -> &'static Regex {
 22    static PULL_REQUEST_NUMBER_REGEX: LazyLock<Regex> = LazyLock::new(|| {
 23        Regex::new(&format!(
 24            r#"Reviewed-on: ({CHROMIUM_REVIEW_URL}/c/(.*)/\+/(\d+))"#
 25        ))
 26        .unwrap()
 27    });
 28    &PULL_REQUEST_NUMBER_REGEX
 29}
 30
 31/// https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html
 32#[derive(Debug, Deserialize)]
 33struct ChangeInfo {
 34    owner: AccountInfo,
 35}
 36
 37#[derive(Debug, Deserialize)]
 38pub struct AccountInfo {
 39    #[serde(rename = "_account_id")]
 40    id: u64,
 41}
 42
 43pub struct Chromium;
 44
 45impl Chromium {
 46    async fn fetch_chromium_commit_author(
 47        &self,
 48        _repo: &str,
 49        commit: &str,
 50        client: &Arc<dyn HttpClient>,
 51    ) -> Result<Option<AccountInfo>> {
 52        let url = format!("{CHROMIUM_REVIEW_URL}/changes/{commit}");
 53
 54        let request = Request::get(&url)
 55            .header("Content-Type", "application/json")
 56            .follow_redirects(http_client::RedirectPolicy::FollowAll);
 57
 58        let mut response = client
 59            .send(request.body(AsyncBody::default())?)
 60            .await
 61            .with_context(|| format!("error fetching Gerrit commit details at {:?}", url))?;
 62
 63        let mut body = Vec::new();
 64        response.body_mut().read_to_end(&mut body).await?;
 65
 66        if response.status().is_client_error() {
 67            let text = String::from_utf8_lossy(body.as_slice());
 68            bail!(
 69                "status error {}, response: {text:?}",
 70                response.status().as_u16()
 71            );
 72        }
 73
 74        // Remove XSSI protection prefix.
 75        let body_str = std::str::from_utf8(&body)?.trim_start_matches(")]}'");
 76
 77        serde_json::from_str::<ChangeInfo>(body_str)
 78            .map(|change| Some(change.owner))
 79            .context("failed to deserialize Gerrit change info")
 80    }
 81}
 82
 83#[async_trait]
 84impl GitHostingProvider for Chromium {
 85    fn name(&self) -> String {
 86        "Chromium".to_string()
 87    }
 88
 89    fn base_url(&self) -> Url {
 90        Url::parse("https://chromium.googlesource.com").unwrap()
 91    }
 92
 93    fn supports_avatars(&self) -> bool {
 94        true
 95    }
 96
 97    fn format_line_number(&self, line: u32) -> String {
 98        format!("{line}")
 99    }
100
101    fn format_line_numbers(&self, start_line: u32, _end_line: u32) -> String {
102        format!("{start_line}")
103    }
104
105    fn parse_remote_url(&self, url: &str) -> Option<ParsedGitRemote> {
106        let url = RemoteUrl::from_str(url).ok()?;
107
108        let host = url.host_str()?;
109        if host != self.base_url().host_str()? {
110            return None;
111        }
112
113        let path_segments = url.path_segments()?.collect::<Vec<_>>();
114        let joined_path = path_segments.join("/");
115        let repo = joined_path.trim_end_matches(".git");
116
117        Some(ParsedGitRemote {
118            owner: Arc::from(""),
119            repo: repo.into(),
120        })
121    }
122
123    fn build_commit_permalink(
124        &self,
125        remote: &ParsedGitRemote,
126        params: BuildCommitPermalinkParams,
127    ) -> Url {
128        let BuildCommitPermalinkParams { sha } = params;
129        let ParsedGitRemote { owner: _, repo } = remote;
130
131        self.base_url().join(&format!("{repo}/+/{sha}")).unwrap()
132    }
133
134    fn build_permalink(&self, remote: ParsedGitRemote, params: BuildPermalinkParams) -> Url {
135        let ParsedGitRemote { owner: _, repo } = remote;
136        let BuildPermalinkParams {
137            sha,
138            path,
139            selection,
140        } = params;
141
142        let mut permalink = self
143            .base_url()
144            .join(&format!("{repo}/+/{sha}/{path}"))
145            .unwrap();
146        permalink.set_fragment(
147            selection
148                .map(|selection| self.line_fragment(&selection))
149                .as_deref(),
150        );
151        permalink
152    }
153
154    fn extract_pull_request(&self, remote: &ParsedGitRemote, message: &str) -> Option<PullRequest> {
155        let capture = pull_request_regex().captures(message)?;
156        let url = Url::parse(capture.get(1)?.as_str()).unwrap();
157        let repo = capture.get(2)?.as_str();
158        if repo != remote.repo.as_ref() {
159            return None;
160        }
161
162        let number = capture.get(3)?.as_str().parse::<u32>().ok()?;
163
164        Some(PullRequest { number, url })
165    }
166
167    async fn commit_author_avatar_url(
168        &self,
169        _repo_owner: &str,
170        repo: &str,
171        commit: SharedString,
172        _author_email: Option<SharedString>,
173        http_client: Arc<dyn HttpClient>,
174    ) -> Result<Option<Url>> {
175        let commit = commit.to_string();
176        let Some(author) = self
177            .fetch_chromium_commit_author(repo, &commit, &http_client)
178            .await?
179        else {
180            return Ok(None);
181        };
182
183        let mut avatar_url = Url::parse(&format!(
184            "{CHROMIUM_REVIEW_URL}/accounts/{}/avatar",
185            &author.id
186        ))?;
187        avatar_url.set_query(Some("size=128"));
188
189        Ok(Some(avatar_url))
190    }
191}
192
193#[cfg(test)]
194mod tests {
195    use git::repository::repo_path;
196    use indoc::indoc;
197    use pretty_assertions::assert_eq;
198
199    use super::*;
200
201    #[test]
202    fn test_parse_remote_url_given_https_url() {
203        let parsed_remote = Chromium
204            .parse_remote_url("https://chromium.googlesource.com/chromium/src")
205            .unwrap();
206
207        assert_eq!(
208            parsed_remote,
209            ParsedGitRemote {
210                owner: Arc::from(""),
211                repo: "chromium/src".into(),
212            }
213        );
214    }
215
216    #[test]
217    fn test_build_chromium_permalink() {
218        let permalink = Chromium.build_permalink(
219            ParsedGitRemote {
220                owner: Arc::from(""),
221                repo: "chromium/src".into(),
222            },
223            BuildPermalinkParams::new(
224                "fea5080b182fc92e3be0c01c5dece602fe70b588",
225                &repo_path("ui/base/cursor/cursor.h"),
226                None,
227            ),
228        );
229
230        let expected_url = "https://chromium.googlesource.com/chromium/src/+/fea5080b182fc92e3be0c01c5dece602fe70b588/ui/base/cursor/cursor.h";
231        assert_eq!(permalink.to_string(), expected_url.to_string())
232    }
233
234    #[test]
235    fn test_build_chromium_permalink_with_single_line_selection() {
236        let permalink = Chromium.build_permalink(
237            ParsedGitRemote {
238                owner: Arc::from(""),
239                repo: "chromium/src".into(),
240            },
241            BuildPermalinkParams::new(
242                "fea5080b182fc92e3be0c01c5dece602fe70b588",
243                &repo_path("ui/base/cursor/cursor.h"),
244                Some(18..18),
245            ),
246        );
247
248        let expected_url = "https://chromium.googlesource.com/chromium/src/+/fea5080b182fc92e3be0c01c5dece602fe70b588/ui/base/cursor/cursor.h#19";
249        assert_eq!(permalink.to_string(), expected_url.to_string())
250    }
251
252    #[test]
253    fn test_build_chromium_permalink_with_multi_line_selection() {
254        let permalink = Chromium.build_permalink(
255            ParsedGitRemote {
256                owner: Arc::from(""),
257                repo: "chromium/src".into(),
258            },
259            BuildPermalinkParams::new(
260                "fea5080b182fc92e3be0c01c5dece602fe70b588",
261                &repo_path("ui/base/cursor/cursor.h"),
262                Some(18..30),
263            ),
264        );
265
266        let expected_url = "https://chromium.googlesource.com/chromium/src/+/fea5080b182fc92e3be0c01c5dece602fe70b588/ui/base/cursor/cursor.h#19";
267        assert_eq!(permalink.to_string(), expected_url.to_string())
268    }
269
270    #[test]
271    fn test_chromium_pull_requests() {
272        let remote = ParsedGitRemote {
273            owner: Arc::from(""),
274            repo: "chromium/src".into(),
275        };
276
277        let message = "This does not contain a pull request";
278        assert!(Chromium.extract_pull_request(&remote, message).is_none());
279
280        // Pull request number at end of "Reviewed-on:" line
281        let message = indoc! {r#"
282                Test commit header
283
284                Test commit description with multiple
285                lines.
286
287                Bug: 1193775, 1270302
288                Change-Id: Id15e9b4d75cce43ebd5fe34f0fb37d5e1e811b66
289                Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3310961
290                Reviewed-by: Test reviewer <test@example.com>
291                Cr-Commit-Position: refs/heads/main@{#1054973}
292                "#
293        };
294
295        assert_eq!(
296            Chromium
297                .extract_pull_request(&remote, message)
298                .unwrap()
299                .url
300                .as_str(),
301            "https://chromium-review.googlesource.com/c/chromium/src/+/3310961"
302        );
303    }
304}