#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PlatformDetection {
pub platform: Platform,
pub explicit: bool,
}
impl PlatformDetection {
#[inline]
#[must_use]
pub fn is_explicit(self) -> bool {
self.explicit
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Platform {
GitHub,
GitLab,
GitCode,
}
impl Platform {
#[must_use]
pub fn detect_from_remote_url(url: &str) -> PlatformDetection {
let url_lower = url.to_lowercase();
if url_lower.contains("github.com") || url_lower.contains("github.") {
PlatformDetection {
platform: Self::GitHub,
explicit: true,
}
} else if url_lower.contains("gitcode.com") || url_lower.contains("gitcode.") {
PlatformDetection {
platform: Self::GitCode,
explicit: true,
}
} else if url_lower.contains("gitlab.com") || url_lower.contains("gitlab.") {
PlatformDetection {
platform: Self::GitLab,
explicit: true,
}
} else {
PlatformDetection {
platform: Self::GitLab,
explicit: false,
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_should_detect_github_from_https_url() {
let result = Platform::detect_from_remote_url("https://github.com/owner/repo.git");
assert_eq!(result.platform, Platform::GitHub);
assert!(result.is_explicit());
}
#[test]
fn test_should_detect_github_from_ssh_url() {
let result = Platform::detect_from_remote_url("git@github.com:owner/repo.git");
assert_eq!(result.platform, Platform::GitHub);
assert!(result.is_explicit());
}
#[test]
fn test_should_detect_gitlab_from_https_url() {
let result = Platform::detect_from_remote_url("https://gitlab.com/group/project.git");
assert_eq!(result.platform, Platform::GitLab);
assert!(result.is_explicit());
}
#[test]
fn test_should_detect_gitlab_from_self_hosted_url() {
let result = Platform::detect_from_remote_url("git@gitlab.mycorp.com:group/project.git");
assert_eq!(result.platform, Platform::GitLab);
assert!(result.is_explicit());
}
#[test]
fn test_should_detect_gitcode() {
let result = Platform::detect_from_remote_url("https://gitcode.com/owner/repo.git");
assert_eq!(result.platform, Platform::GitCode);
assert!(result.is_explicit());
}
#[test]
fn test_should_detect_gitlab_from_custom_domain() {
let result =
Platform::detect_from_remote_url("git@xyun.git.nyuncloud.com:fusion-cdn/bff/admin.git");
assert_eq!(result.platform, Platform::GitLab);
assert!(!result.is_explicit());
let result =
Platform::detect_from_remote_url("https://gitlab.mycorp.com/group/project.git");
assert_eq!(result.platform, Platform::GitLab);
assert!(result.is_explicit());
}
#[test]
fn test_should_fallback_to_gitlab_for_unrecognized_url() {
let result = Platform::detect_from_remote_url("https://example.com/repo.git");
assert_eq!(result.platform, Platform::GitLab);
assert!(!result.is_explicit());
}
#[test]
fn test_should_be_case_insensitive() {
let result = Platform::detect_from_remote_url("HTTPS://GITHUB.COM/Owner/Repo.git");
assert_eq!(result.platform, Platform::GitHub);
assert!(result.is_explicit());
let result = Platform::detect_from_remote_url("GIT@GITLAB.COM:Group/Project.git");
assert_eq!(result.platform, Platform::GitLab);
assert!(result.is_explicit());
let result = Platform::detect_from_remote_url("HTTPS://GITCODE.COM/Owner/Repo.git");
assert_eq!(result.platform, Platform::GitCode);
assert!(result.is_explicit());
}
}