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//! This module is URL-detection logic only — it does **not** define a
7//! unified "Platform trait". The actual per-platform capability
8//! abstraction is the set of fine-grained provider traits (`IssueProvider`,
9//! `PrProvider`, `LabelProvider`, `ReleaseProvider`, `PipelineProvider`,
10//! `CommitProvider`, `ReviewProvider`, `AuthProvider`, `AuthChecker`,
11//! `HealthCheck`, `MilestoneProvider`), each defined in its own module and
12//! implemented per platform in the adapter crates.
13
14/// Result of detecting a platform from a remote URL.
15///
16/// Carries both the detected [`Platform`] and whether the match was
17/// explicit (the URL contained a known platform domain) or a fallback
18/// default. Callers can use [`is_explicit`](Self::is_explicit) to decide
19/// whether to warn the user about an uncertain detection.
20#[derive(Debug, Clone, Copy, PartialEq, Eq)]
21pub struct PlatformDetection {
22    /// The detected or default platform.
23    pub platform: Platform,
24    /// `true` when the URL matched a known platform domain pattern;
25    /// `false` when the result is a fallback default for an unrecognized
26    /// domain.
27    pub explicit: bool,
28}
29
30impl PlatformDetection {
31    /// Returns `true` if the URL explicitly matched a known platform domain.
32    #[inline]
33    #[must_use]
34    pub fn is_explicit(self) -> bool {
35        self.explicit
36    }
37}
38
39/// The Git platform hosting a remote repository.
40///
41/// Used to determine which API client and workflow to use for
42/// platform-specific operations.
43#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
44pub enum Platform {
45    /// GitHub (github.com or self-hosted GitHub Enterprise).
46    GitHub,
47    /// GitLab (gitlab.com or self-hosted GitLab).
48    GitLab,
49    /// `GitCode` (gitcode.com or self-hosted `GitCode`).
50    GitCode,
51}
52
53impl Platform {
54    /// Detects the platform from a remote Git URL.
55    ///
56    /// Returns a [`PlatformDetection`] containing the platform and whether
57    /// the URL explicitly matched a known domain pattern. Unrecognized
58    /// domains default to GitLab with `explicit: false`, so callers can
59    /// warn the user.
60    ///
61    /// # Detection Strategy
62    ///
63    /// - `github.com` or `github.*` → GitHub (explicit)
64    /// - `gitcode.com` or `gitcode.*` → `GitCode` (explicit)
65    /// - `gitlab.com` or `gitlab.*` → GitLab (explicit)
66    /// - All other domains → GitLab (fallback, `explicit: false`)
67    ///
68    /// # Examples
69    ///
70    /// ```ignore
71    /// use gitflow_core::platform::{Platform, PlatformDetection};
72    ///
73    /// let result = Platform::detect_from_remote_url("https://github.com/owner/repo.git");
74    /// assert_eq!(result.platform, Platform::GitHub);
75    /// assert!(result.is_explicit());
76    ///
77    /// let result = Platform::detect_from_remote_url("https://example.com/repo.git");
78    /// assert_eq!(result.platform, Platform::GitLab);
79    /// assert!(!result.is_explicit());
80    /// ```
81    #[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}