Skip to main content

gitflow_gitcode/
pipeline.rs

1//! GitCode Pipeline 提供者实现。
2//!
3//! **注意**: GitCode CLI v0.6.1 不支持 `run` 命令,GitCode API 也没有 pipeline 端点。
4//! 所有方法返回友好错误消息。
5
6use async_trait::async_trait;
7use gitflow_core::{
8    CoreError, Result, Session,
9    pipeline::{JobData, PipelineProvider, PipelineReport, PipelineStatus},
10};
11
12/// GitCode Pipeline 提供者,通过 `gitcode` CLI 操作 CI/CD 流水线。
13///
14/// # Examples
15///
16/// ```no_run
17/// use gitflow_gitcode::GitCodePipelineProvider;
18///
19/// let provider = GitCodePipelineProvider::new("octocat/hello-world");
20/// ```
21#[derive(Debug, Clone)]
22pub struct GitCodePipelineProvider {
23    #[allow(dead_code, reason = "Stored for future pipeline API calls")]
24    repo: String,
25}
26
27impl GitCodePipelineProvider {
28    /// 创建新的 GitCode Pipeline 提供者。
29    ///
30    /// `repo` 格式为 `owner/repo`。
31    #[must_use]
32    pub fn new(repo: impl Into<String>) -> Self {
33        Self { repo: repo.into() }
34    }
35
36    /// Create a new provider from a shared [`Session`].
37    ///
38    /// This enables state reuse across multiple operations in workflow chains.
39    #[must_use]
40    pub fn with_session(session: &Session) -> Self {
41        Self {
42            repo: session.repo.clone(),
43        }
44    }
45}
46
47#[async_trait]
48impl PipelineProvider for GitCodePipelineProvider {
49    async fn status(&self, _branch: &str) -> Result<Vec<PipelineStatus>> {
50        Err(CoreError::Platform(
51            "GitCode does not support pipeline management. GitCode CLI v0.6.1 does not have 'run' \
52             command."
53                .into(),
54        ))
55    }
56
57    async fn logs(&self, _pipeline_id: u64) -> Result<String> {
58        Err(CoreError::Platform(
59            "GitCode does not support pipeline management. GitCode CLI v0.6.1 does not have 'run' \
60             command."
61                .into(),
62        ))
63    }
64
65    async fn jobs(&self, _pipeline_id: u64) -> Result<Vec<JobData>> {
66        Err(CoreError::Platform(
67            "GitCode does not support pipeline management. GitCode CLI v0.6.1 does not have 'run' \
68             command."
69                .into(),
70        ))
71    }
72
73    async fn report(&self, _branch: &str, _days: u32) -> Result<PipelineReport> {
74        Err(CoreError::Platform(
75            "GitCode does not support pipeline management. GitCode CLI v0.6.1 does not have 'run' \
76             command."
77                .into(),
78        ))
79    }
80}
81
82#[cfg(test)]
83mod tests {
84    use super::*;
85
86    #[test]
87    fn test_should_construct_gitcode_pipeline_provider() {
88        let provider = GitCodePipelineProvider::new("octocat/hello-world");
89        assert_eq!(provider.repo, "octocat/hello-world");
90    }
91
92    #[test]
93    fn test_should_construct_gitcode_pipeline_provider_from_string() {
94        let repo = String::from("octocat/hello-world");
95        let provider = GitCodePipelineProvider::new(repo);
96        assert_eq!(provider.repo, "octocat/hello-world");
97    }
98
99    #[test]
100    fn test_should_debug_format_provider() {
101        let provider = GitCodePipelineProvider::new("octocat/hello-world");
102        let debug = format!("{provider:?}");
103        assert!(debug.contains("GitCodePipelineProvider"));
104        assert!(debug.contains("octocat/hello-world"));
105    }
106
107    #[test]
108    fn test_should_clone_gitcode_pipeline_provider() {
109        let original = GitCodePipelineProvider::new("owner/repo");
110        let cloned = original.clone();
111        assert_eq!(original.repo, cloned.repo);
112    }
113
114    // --- Stub behavior: all pipeline operations are unsupported on GitCode ---
115
116    #[tokio::test]
117    async fn test_should_return_platform_error_for_status() {
118        let provider = GitCodePipelineProvider::new("owner/repo");
119        let result = provider.status("main").await;
120        assert!(matches!(result.unwrap_err(), CoreError::Platform(_)));
121    }
122
123    #[tokio::test]
124    async fn test_should_return_platform_error_for_logs() {
125        let provider = GitCodePipelineProvider::new("owner/repo");
126        let result = provider.logs(123).await;
127        assert!(matches!(result.unwrap_err(), CoreError::Platform(_)));
128    }
129
130    #[tokio::test]
131    async fn test_should_return_platform_error_for_jobs() {
132        let provider = GitCodePipelineProvider::new("owner/repo");
133        let result = provider.jobs(123).await;
134        assert!(matches!(result.unwrap_err(), CoreError::Platform(_)));
135    }
136
137    #[tokio::test]
138    async fn test_should_return_platform_error_for_report() {
139        let provider = GitCodePipelineProvider::new("owner/repo");
140        let result = provider.report("main", 7).await;
141        assert!(matches!(result.unwrap_err(), CoreError::Platform(_)));
142    }
143}