gitpoulpi_hosting/
project.rs1use serde::{Deserialize, Serialize};
9
10#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
11pub struct HostedProject {
12 pub integration_id: String,
16 pub id: String,
20 pub name: String,
22 pub full_name: String,
25 pub description: Option<String>,
26 pub default_branch: Option<String>,
27 pub ssh_url: Option<String>,
30 pub https_url: Option<String>,
31 pub web_url: Option<String>,
33 pub private: bool,
34}
35
36impl HostedProject {
37 pub fn owner(&self) -> &str {
41 self.full_name
42 .rsplit_once('/')
43 .map_or("", |(owner, _)| owner)
44 }
45
46 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 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#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
77pub struct HostedSshKey {
78 pub id: String,
79 pub title: String,
80 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}