gitflow_gitcode/lib.rs
1//! `gitflow-gitcode` —— GitCode 平台实现。
2//!
3//! 本 crate 实现了 `gitflow-core` 中定义的 [`IssueProvider`]、[`PrProvider`]、
4//! [`ReleaseProvider`]、[`ReviewProvider`]、[`AuthProvider`]、[`LabelProvider`]、
5//! [`MilestoneProvider`]、[`CommitProvider`] 与 [`PipelineProvider`] trait,
6//! 通过调用 `gitcode` CLI 获取数据并解析其 JSON 输出。
7//!
8//! # 主要类型
9//!
10//! - [`GitCodeIssueProvider`]:操作 GitCode Issue。
11//! - [`GitCodePrProvider`]:操作 GitCode Pull Request。
12//! - [`GitCodeReleaseProvider`]:操作 GitCode Release。
13//! - [`GitCodeReviewProvider`]:操作 GitCode PR Review。
14//! - [`GitCodeAuthProvider`]:处理 GitCode 认证(登录、登出、状态、Token)。
15//! - [`GitCodeLabelProvider`]:管理 GitCode 仓库标签。
16//! - [`GitCodeMilestoneProvider`]:管理 GitCode 仓库里程碑。
17//! - [`GitCodeCommitProvider`]:查看 GitCode Commit 及 Diff/Patch。
18//! - [`GitCodePipelineProvider`]:查看 GitCode CI/CD 流水线。
19//!
20//! # 错误处理
21//!
22//! 所有平台调用失败时,`gitcode` 的 stderr 会通过 [`error::parse_gitcode_error`] 解析,
23//! 并统一映射为 [`CoreError::Platform`]。
24//!
25//! [`IssueProvider`]: gitflow_core::issue::IssueProvider
26//! [`PrProvider`]: gitflow_core::pr::PrProvider
27//! [`ReleaseProvider`]: gitflow_core::release::ReleaseProvider
28//! [`ReviewProvider`]: gitflow_core::review::ReviewProvider
29//! [`AuthProvider`]: gitflow_core::auth::AuthProvider
30//! [`LabelProvider`]: gitflow_core::label::LabelProvider
31//! [`MilestoneProvider`]: gitflow_core::label::MilestoneProvider
32//! [`CommitProvider`]: gitflow_core::commit::CommitProvider
33//! [`PipelineProvider`]: gitflow_core::pipeline::PipelineProvider
34//! [`CoreError::Platform`]: gitflow_core::CoreError::Platform
35
36#![forbid(unsafe_code)]
37#![allow(
38 clippy::doc_markdown,
39 reason = "GitCode is a platform brand name, not a Rust code item"
40)]
41#![cfg_attr(
42 test,
43 allow(
44 clippy::expect_used,
45 clippy::unwrap_used,
46 clippy::indexing_slicing,
47 clippy::useless_format,
48 clippy::clone_on_copy,
49 reason = "Tests legitimately need to unwrap fixture data and index into known-shape \
50 collections"
51 )
52)]
53
54pub mod auth;
55pub mod commit;
56pub mod error;
57pub mod issue;
58pub mod label;
59pub mod pipeline;
60pub mod pr;
61pub mod release;
62pub mod review;
63pub mod runner;
64
65pub use auth::GitCodeAuthProvider;
66pub use commit::GitCodeCommitProvider;
67pub use error::parse_gitcode_error;
68pub use issue::GitCodeIssueProvider;
69pub use label::{GitCodeLabelProvider, GitCodeMilestoneProvider};
70pub use pipeline::GitCodePipelineProvider;
71pub use pr::GitCodePrProvider;
72pub use release::GitCodeReleaseProvider;
73pub use review::GitCodeReviewProvider;
74
75/// Return the GitCode CLI binary path.
76///
77/// Searches PATH first, then pip user install directories
78/// (`~/Library/Python/*/bin/` on macOS, `~/.local/bin/` on Linux).
79#[allow(
80 clippy::disallowed_methods,
81 reason = "binary discovery runs at startup before async runtime is ready"
82)]
83pub(crate) fn gitcode_binary() -> String {
84 // 1. which gitcode (pip/wheel/DEB/RPM install)
85 if let Ok(p) = which::which("gitcode") {
86 return p.to_string_lossy().into_owned();
87 }
88 // 2. pip user install paths (e.g. ~/Library/Python/3.9/bin/)
89 if let Ok(home) = std::env::var("HOME") {
90 let lib = std::path::PathBuf::from(&home).join("Library/Python");
91 if let Ok(entries) = std::fs::read_dir(&lib) {
92 for entry in entries.flatten() {
93 let p = entry.path().join("bin/gitcode");
94 if p.exists() {
95 return p.to_string_lossy().into_owned();
96 }
97 }
98 }
99 // Also check ~/.local/bin
100 let p = std::path::PathBuf::from(&home).join(".local/bin/gitcode");
101 if p.exists() {
102 return p.to_string_lossy().into_owned();
103 }
104 }
105 // 3. Desperate fallback — hope it's in PATH
106 "gitcode".into()
107}