opensession_git_native/
url.rs1pub fn generate_raw_url(remote_url: &str, rel_path: &str) -> String {
3 let normalized = remote_url
5 .trim_end_matches(".git")
6 .replace("git@github.com:", "https://github.com/")
7 .replace("git@gitlab.com:", "https://gitlab.com/");
8
9 if normalized.contains("github.com") {
10 let path = normalized.trim_start_matches("https://github.com/");
12 format!(
13 "https://raw.githubusercontent.com/{}/opensession/sessions/{}",
14 path, rel_path
15 )
16 } else if normalized.contains("gitlab.com") {
17 let path = normalized.trim_start_matches("https://gitlab.com/");
19 format!(
20 "https://gitlab.com/{}/-/raw/opensession/sessions/{}",
21 path, rel_path
22 )
23 } else {
24 format!("{}/raw/opensession/sessions/{}", normalized, rel_path)
26 }
27}
28
29#[cfg(test)]
30mod tests {
31 use super::*;
32
33 #[test]
34 fn test_github_ssh_url() {
35 let url = generate_raw_url("git@github.com:user/repo.git", "v1/ab/abc123.hail.jsonl");
36 assert_eq!(
37 url,
38 "https://raw.githubusercontent.com/user/repo/opensession/sessions/v1/ab/abc123.hail.jsonl"
39 );
40 }
41
42 #[test]
43 fn test_github_https_url() {
44 let url = generate_raw_url("https://github.com/user/repo", "v1/ab/abc123.hail.jsonl");
45 assert_eq!(
46 url,
47 "https://raw.githubusercontent.com/user/repo/opensession/sessions/v1/ab/abc123.hail.jsonl"
48 );
49 }
50
51 #[test]
52 fn test_gitlab_url() {
53 let url = generate_raw_url("git@gitlab.com:user/repo.git", "v1/ab/abc123.hail.jsonl");
54 assert_eq!(
55 url,
56 "https://gitlab.com/user/repo/-/raw/opensession/sessions/v1/ab/abc123.hail.jsonl"
57 );
58 }
59}