Skip to main content

gitflow_core/
platform.rs

1//! Platform detection for remote Git repositories.
2//!
3//! Identifies the hosting platform (GitHub, GitLab, or `GitCode`) from a
4//! remote URL so that the CLI can select the correct API and workflow.
5
6/// The Git platform hosting a remote repository.
7///
8/// Used to determine which API client and workflow to use for
9/// platform-specific operations.
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
11pub enum Platform {
12    /// GitHub (github.com or self-hosted GitHub Enterprise).
13    GitHub,
14    /// GitLab (gitlab.com or self-hosted GitLab).
15    GitLab,
16    /// `GitCode` (gitcode.com or self-hosted `GitCode`).
17    GitCode,
18}
19
20impl Platform {
21    /// Detects the platform from a remote Git URL.
22    ///
23    /// Performs a case-insensitive substring match against known platform
24    /// domain patterns. Supports both HTTPS and SSH remote formats, as
25    /// well as self-hosted instances.
26    ///
27    /// # Detection Strategy
28    ///
29    /// - `github.com` or `github.*` → GitHub
30    /// - `gitcode.com` or `gitcode.*` → `GitCode`
31    /// - All other domains → GitLab (default, including self-hosted)
32    ///
33    /// This approach assumes that any unrecognized domain is a GitLab instance,
34    /// which covers custom domains like `xyun.git.nyuncloud.com`.
35    ///
36    /// # Examples
37    ///
38    /// ```ignore
39    /// // Doctest skipped: crate name hyphen-to-underscore mapping
40    /// // prevents `use gitflow_core::Platform` from resolving.
41    /// use gitflow_core::Platform;
42    ///
43    /// assert_eq!(
44    ///     Platform::detect_from_remote_url("https://github.com/owner/repo.git"),
45    ///     Some(Platform::GitHub),
46    /// );
47    /// assert_eq!(
48    ///     Platform::detect_from_remote_url("git@gitlab.mycorp.com:group/project.git"),
49    ///     Some(Platform::GitLab),
50    /// );
51    /// assert_eq!(
52    ///     Platform::detect_from_remote_url("git@xyun.git.nyuncloud.com:fusion-cdn/bff/admin.git"),
53    ///     Some(Platform::GitLab),
54    /// );
55    /// ```
56    #[must_use]
57    pub fn detect_from_remote_url(url: &str) -> Option<Self> {
58        let url_lower = url.to_lowercase();
59        if url_lower.contains("github.com") || url_lower.contains("github.") {
60            Some(Self::GitHub)
61        } else if url_lower.contains("gitcode.com") || url_lower.contains("gitcode.") {
62            Some(Self::GitCode)
63        } else {
64            // Default to GitLab for all other domains (including self-hosted GitLab)
65            Some(Self::GitLab)
66        }
67    }
68}
69
70#[cfg(test)]
71mod tests {
72    use super::*;
73
74    #[test]
75    fn test_should_detect_github_from_https_url() {
76        assert_eq!(
77            Platform::detect_from_remote_url("https://github.com/owner/repo.git"),
78            Some(Platform::GitHub),
79        );
80    }
81
82    #[test]
83    fn test_should_detect_github_from_ssh_url() {
84        assert_eq!(
85            Platform::detect_from_remote_url("git@github.com:owner/repo.git"),
86            Some(Platform::GitHub),
87        );
88    }
89
90    #[test]
91    fn test_should_detect_gitlab_from_https_url() {
92        assert_eq!(
93            Platform::detect_from_remote_url("https://gitlab.com/group/project.git"),
94            Some(Platform::GitLab),
95        );
96    }
97
98    #[test]
99    fn test_should_detect_gitlab_from_self_hosted_url() {
100        assert_eq!(
101            Platform::detect_from_remote_url("git@gitlab.mycorp.com:group/project.git"),
102            Some(Platform::GitLab),
103        );
104    }
105
106    #[test]
107    fn test_should_detect_gitcode() {
108        assert_eq!(
109            Platform::detect_from_remote_url("https://gitcode.com/owner/repo.git"),
110            Some(Platform::GitCode),
111        );
112    }
113
114    #[test]
115    fn test_should_detect_gitlab_from_custom_domain() {
116        // Custom GitLab domains should be detected as GitLab
117        assert_eq!(
118            Platform::detect_from_remote_url("git@xyun.git.nyuncloud.com:fusion-cdn/bff/admin.git"),
119            Some(Platform::GitLab),
120        );
121        assert_eq!(
122            Platform::detect_from_remote_url("https://gitlab.mycorp.com/group/project.git"),
123            Some(Platform::GitLab),
124        );
125    }
126
127    #[test]
128    fn test_should_default_to_gitlab_for_unrecognized_url() {
129        // Unrecognized domains default to GitLab
130        assert_eq!(
131            Platform::detect_from_remote_url("https://example.com/repo.git"),
132            Some(Platform::GitLab),
133        );
134    }
135
136    #[test]
137    fn test_should_be_case_insensitive() {
138        assert_eq!(
139            Platform::detect_from_remote_url("HTTPS://GITHUB.COM/Owner/Repo.git"),
140            Some(Platform::GitHub),
141        );
142        assert_eq!(
143            Platform::detect_from_remote_url("GIT@GITLAB.COM:Group/Project.git"),
144            Some(Platform::GitLab),
145        );
146        assert_eq!(
147            Platform::detect_from_remote_url("HTTPS://GITCODE.COM/Owner/Repo.git"),
148            Some(Platform::GitCode),
149        );
150    }
151}