1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
21pub struct PlatformDetection {
22 pub platform: Platform,
24 pub explicit: bool,
28}
29
30impl PlatformDetection {
31 #[inline]
33 #[must_use]
34 pub fn is_explicit(self) -> bool {
35 self.explicit
36 }
37}
38
39#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
44pub enum Platform {
45 GitHub,
47 GitLab,
49 GitCode,
51}
52
53impl Platform {
54 #[must_use]
82 pub fn detect_from_remote_url(url: &str) -> PlatformDetection {
83 let url_lower = url.to_lowercase();
84 if url_lower.contains("github.com") || url_lower.contains("github.") {
85 PlatformDetection {
86 platform: Self::GitHub,
87 explicit: true,
88 }
89 } else if url_lower.contains("gitcode.com") || url_lower.contains("gitcode.") {
90 PlatformDetection {
91 platform: Self::GitCode,
92 explicit: true,
93 }
94 } else if url_lower.contains("gitlab.com") || url_lower.contains("gitlab.") {
95 PlatformDetection {
96 platform: Self::GitLab,
97 explicit: true,
98 }
99 } else {
100 PlatformDetection {
101 platform: Self::GitLab,
102 explicit: false,
103 }
104 }
105 }
106}
107
108#[cfg(test)]
109mod tests {
110 use super::*;
111
112 #[test]
113 fn test_should_detect_github_from_https_url() {
114 let result = Platform::detect_from_remote_url("https://github.com/owner/repo.git");
115 assert_eq!(result.platform, Platform::GitHub);
116 assert!(result.is_explicit());
117 }
118
119 #[test]
120 fn test_should_detect_github_from_ssh_url() {
121 let result = Platform::detect_from_remote_url("git@github.com:owner/repo.git");
122 assert_eq!(result.platform, Platform::GitHub);
123 assert!(result.is_explicit());
124 }
125
126 #[test]
127 fn test_should_detect_gitlab_from_https_url() {
128 let result = Platform::detect_from_remote_url("https://gitlab.com/group/project.git");
129 assert_eq!(result.platform, Platform::GitLab);
130 assert!(result.is_explicit());
131 }
132
133 #[test]
134 fn test_should_detect_gitlab_from_self_hosted_url() {
135 let result = Platform::detect_from_remote_url("git@gitlab.mycorp.com:group/project.git");
136 assert_eq!(result.platform, Platform::GitLab);
137 assert!(result.is_explicit());
138 }
139
140 #[test]
141 fn test_should_detect_gitcode() {
142 let result = Platform::detect_from_remote_url("https://gitcode.com/owner/repo.git");
143 assert_eq!(result.platform, Platform::GitCode);
144 assert!(result.is_explicit());
145 }
146
147 #[test]
148 fn test_should_detect_gitlab_from_custom_domain() {
149 let result =
150 Platform::detect_from_remote_url("git@xyun.git.nyuncloud.com:fusion-cdn/bff/admin.git");
151 assert_eq!(result.platform, Platform::GitLab);
152 assert!(!result.is_explicit());
153
154 let result =
155 Platform::detect_from_remote_url("https://gitlab.mycorp.com/group/project.git");
156 assert_eq!(result.platform, Platform::GitLab);
157 assert!(result.is_explicit());
158 }
159
160 #[test]
161 fn test_should_fallback_to_gitlab_for_unrecognized_url() {
162 let result = Platform::detect_from_remote_url("https://example.com/repo.git");
163 assert_eq!(result.platform, Platform::GitLab);
164 assert!(!result.is_explicit());
165 }
166
167 #[test]
168 fn test_should_be_case_insensitive() {
169 let result = Platform::detect_from_remote_url("HTTPS://GITHUB.COM/Owner/Repo.git");
170 assert_eq!(result.platform, Platform::GitHub);
171 assert!(result.is_explicit());
172
173 let result = Platform::detect_from_remote_url("GIT@GITLAB.COM:Group/Project.git");
174 assert_eq!(result.platform, Platform::GitLab);
175 assert!(result.is_explicit());
176
177 let result = Platform::detect_from_remote_url("HTTPS://GITCODE.COM/Owner/Repo.git");
178 assert_eq!(result.platform, Platform::GitCode);
179 assert!(result.is_explicit());
180 }
181}