Skip to main content

gitpoulpi_hosting/
project.rs

1//! The one project shape the UI renders, whichever host it came from.
2//!
3//! GitHub calls them repositories and keys them by `owner/name`; GitLab
4//! calls them projects and keys them by a numeric id with a
5//! `group/subgroup/name` path. Normalising here rather than in each
6//! frontend is what lets the *clone from this list* flow be written once.
7
8use serde::{Deserialize, Serialize};
9
10#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
11pub struct HostedProject {
12    /// The integration this came from, so a list merged across several
13    /// hosts can still say where each row belongs - and so *clone this
14    /// row* knows which token to use.
15    pub integration_id: String,
16    /// The host's own identifier, as a string: a number for GitLab, the
17    /// `owner/name` path for GitHub. Opaque to everything except the
18    /// client that produced it.
19    pub id: String,
20    /// The short name (`gitpoulpi`).
21    pub name: String,
22    /// The full path (`my-org/gitpoulpi`, `group/sub/thing`) - what
23    /// the list is filtered on, because that is what a user types.
24    pub full_name: String,
25    pub description: Option<String>,
26    pub default_branch: Option<String>,
27    /// The two clone URLs, either of which can be missing when the host
28    /// has that protocol disabled.
29    pub ssh_url: Option<String>,
30    pub https_url: Option<String>,
31    /// The page a *Open in browser* action goes to.
32    pub web_url: Option<String>,
33    pub private: bool,
34}
35
36impl HostedProject {
37    /// The owner section this project belongs to: the user, the GitHub
38    /// organisation or the GitLab group / sub-group path (`group/sub` for
39    /// `group/sub/thing`). Empty when the path has no owner.
40    pub fn owner(&self) -> &str {
41        self.full_name
42            .rsplit_once('/')
43            .map_or("", |(owner, _)| owner)
44    }
45
46    /// The URL a clone should use, given whether the user prefers SSH.
47    /// Falls back to the other protocol rather than failing: a host with
48    /// SSH disabled is legitimate.
49    pub fn clone_url(&self, prefer_ssh: bool) -> Option<&str> {
50        let (first, second) = if prefer_ssh {
51            (&self.ssh_url, &self.https_url)
52        } else {
53            (&self.https_url, &self.ssh_url)
54        };
55        first.as_deref().or(second.as_deref())
56    }
57
58    /// Whether this project matches a filter the user typed:
59    /// case-insensitive substring over the full path. Not fuzzy, because a
60    /// list of several thousand rows is re-filtered on every keystroke.
61    pub fn matches(&self, filter: &str) -> bool {
62        let filter = filter.trim().to_lowercase();
63        if filter.is_empty() {
64            return true;
65        }
66        self.full_name.to_lowercase().contains(&filter)
67            || self.name.to_lowercase().contains(&filter)
68            || self
69                .description
70                .as_deref()
71                .is_some_and(|d| d.to_lowercase().contains(&filter))
72    }
73}
74
75/// An SSH public key as the host holds it.
76#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
77pub struct HostedSshKey {
78    pub id: String,
79    pub title: String,
80    /// The `ssh-ed25519 AAAA... comment` line. Public by definition --
81    /// the private half never reaches this crate.
82    pub key: String,
83}
84
85#[cfg(test)]
86mod tests {
87    use super::*;
88
89    fn project(ssh: Option<&str>, https: Option<&str>) -> HostedProject {
90        HostedProject {
91            integration_id: "h".into(),
92            id: "1".into(),
93            name: "thing".into(),
94            full_name: "group/sub/thing".into(),
95            description: Some("A useful Widget".into()),
96            default_branch: Some("main".into()),
97            ssh_url: ssh.map(str::to_string),
98            https_url: https.map(str::to_string),
99            web_url: None,
100            private: false,
101        }
102    }
103
104    #[test]
105    fn the_preferred_protocol_wins_but_the_other_is_a_fallback() {
106        let both = project(Some("git@h:g/t.git"), Some("https://h/g/t.git"));
107        assert_eq!(both.clone_url(true), Some("git@h:g/t.git"));
108        assert_eq!(both.clone_url(false), Some("https://h/g/t.git"));
109
110        let https_only = project(None, Some("https://h/g/t.git"));
111        assert_eq!(
112            https_only.clone_url(true),
113            Some("https://h/g/t.git"),
114            "a host with ssh disabled must still be clonable"
115        );
116
117        assert_eq!(project(None, None).clone_url(true), None);
118    }
119
120    #[test]
121    fn the_owner_is_the_path_without_the_last_segment() {
122        assert_eq!(project(None, None).owner(), "group/sub");
123        let mut p = project(None, None);
124        p.full_name = "me/thing".into();
125        assert_eq!(p.owner(), "me");
126        p.full_name = "thing".into();
127        assert_eq!(p.owner(), "");
128    }
129
130    #[test]
131    fn filtering_is_case_insensitive_over_path_name_and_description() {
132        let p = project(None, None);
133        assert!(p.matches(""));
134        assert!(p.matches("  "));
135        assert!(p.matches("SUB/TH"));
136        assert!(p.matches("thing"));
137        assert!(p.matches("widget"), "description must be searchable");
138        assert!(!p.matches("nothing-like-this"));
139    }
140}