Skip to main content

gitfox_client/models/
repository.rs

1use serde::{Deserialize, Serialize};
2
3/// A repository.
4///
5/// This is the CLI's own model, not the raw API DTO: the serde attributes are
6/// the translation layer, so an upstream rename is absorbed with an `alias`
7/// instead of breaking the JSON schema agents depend on.
8///
9/// GitFox returns two shapes here. `GET /repos/{repo_ref}` and
10/// `GET /spaces/{space_ref}/repos` answer with `RepoRepositoryOutput`, which
11/// carries `is_public` and `importing`; the instance-wide `GET /repos` answers
12/// with `TypesRepository`, which does not. Both decode into this one struct,
13/// and the fields the narrower shape lacks stay `None` — a repository whose
14/// visibility is simply unknown, rather than one wrongly reported as private.
15#[derive(Debug, Clone, Default, Serialize, Deserialize)]
16pub struct Repository {
17    #[serde(default)]
18    pub id: Option<i64>,
19    /// The repository name on its own, e.g. `backend`.
20    #[serde(default)]
21    pub identifier: Option<String>,
22    /// The full space path, e.g. `ai/backend`.
23    #[serde(default)]
24    pub path: Option<String>,
25    #[serde(default)]
26    pub description: Option<String>,
27    #[serde(default)]
28    pub default_branch: Option<String>,
29    /// `None` when the endpoint that produced this does not report visibility.
30    #[serde(default)]
31    pub is_public: Option<bool>,
32    #[serde(default)]
33    pub importing: Option<bool>,
34    #[serde(default)]
35    pub git_url: Option<String>,
36    #[serde(default)]
37    pub git_ssh_url: Option<String>,
38    #[serde(default)]
39    pub is_empty: Option<bool>,
40    #[serde(default)]
41    pub num_open_pulls: Option<i64>,
42    #[serde(default)]
43    pub num_pulls: Option<i64>,
44    #[serde(default)]
45    pub num_forks: Option<i64>,
46    /// Size in KiB, as GitFox reports it.
47    #[serde(default)]
48    pub size: Option<i64>,
49    #[serde(default)]
50    pub created: Option<i64>,
51    #[serde(default)]
52    pub updated: Option<i64>,
53}
54
55impl Repository {
56    /// `ai/backend` — the reference a user would type back into `-R`.
57    pub fn reference(&self) -> String {
58        self.path
59            .clone()
60            .or_else(|| self.identifier.clone())
61            .unwrap_or_default()
62    }
63
64    /// `public`, `private`, or `None` when the endpoint did not say.
65    pub fn visibility(&self) -> Option<&'static str> {
66        self.is_public.map(|p| if p { "public" } else { "private" })
67    }
68
69    /// The URL `git clone` should be given.
70    pub fn clone_url(&self, ssh: bool) -> Option<&str> {
71        let (first, second) = if ssh {
72            (&self.git_ssh_url, &self.git_url)
73        } else {
74            (&self.git_url, &self.git_ssh_url)
75        };
76        first
77            .as_deref()
78            .filter(|u| !u.is_empty())
79            .or_else(|| second.as_deref().filter(|u| !u.is_empty()))
80    }
81}
82
83#[cfg(test)]
84mod tests {
85    use super::*;
86    use serde_json::json;
87
88    #[test]
89    fn the_narrow_list_shape_decodes_with_visibility_unknown() {
90        // `GET /repos` answers with TypesRepository, which has no is_public.
91        let repo: Repository = serde_json::from_value(json!({
92            "identifier": "backend", "path": "ai/backend", "default_branch": "main"
93        }))
94        .unwrap();
95        assert_eq!(repo.reference(), "ai/backend");
96        // Unknown, not "private" — the difference matters in a listing.
97        assert_eq!(repo.visibility(), None);
98    }
99
100    #[test]
101    fn the_wide_shape_reports_visibility() {
102        let public: Repository =
103            serde_json::from_value(json!({ "identifier": "x", "is_public": true })).unwrap();
104        assert_eq!(public.visibility(), Some("public"));
105        let private: Repository =
106            serde_json::from_value(json!({ "identifier": "x", "is_public": false })).unwrap();
107        assert_eq!(private.visibility(), Some("private"));
108    }
109
110    #[test]
111    fn clone_url_honours_the_requested_protocol_and_falls_back() {
112        let both = Repository {
113            git_url: Some("http://h:3000/git/ai/backend.git".into()),
114            git_ssh_url: Some("git@h:ai/backend.git".into()),
115            ..Default::default()
116        };
117        assert_eq!(
118            both.clone_url(false),
119            Some("http://h:3000/git/ai/backend.git")
120        );
121        assert_eq!(both.clone_url(true), Some("git@h:ai/backend.git"));
122
123        // An instance with SSH disabled still clones over HTTP.
124        let http_only = Repository {
125            git_url: Some("http://h:3000/git/ai/backend.git".into()),
126            git_ssh_url: Some(String::new()),
127            ..Default::default()
128        };
129        assert_eq!(
130            http_only.clone_url(true),
131            Some("http://h:3000/git/ai/backend.git")
132        );
133        assert_eq!(Repository::default().clone_url(false), None);
134    }
135
136    #[test]
137    fn reference_falls_back_to_the_identifier() {
138        let repo = Repository {
139            identifier: Some("backend".into()),
140            ..Default::default()
141        };
142        assert_eq!(repo.reference(), "backend");
143    }
144}