Skip to main content

bitbucket_cli/models/
repo.rs

1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3
4use super::user::{Link, User, Workspace};
5
6#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct Repository {
8    pub uuid: String,
9    pub name: String,
10    pub full_name: String,
11    pub slug: Option<String>,
12    pub description: Option<String>,
13    pub is_private: Option<bool>,
14    pub scm: Option<String>,
15    pub owner: Option<User>,
16    pub workspace: Option<Workspace>,
17    pub project: Option<Project>,
18    pub created_on: Option<DateTime<Utc>>,
19    pub updated_on: Option<DateTime<Utc>>,
20    pub size: Option<u64>,
21    pub language: Option<String>,
22    pub has_issues: Option<bool>,
23    pub has_wiki: Option<bool>,
24    pub fork_policy: Option<String>,
25    pub mainbranch: Option<Branch>,
26    pub links: Option<RepositoryLinks>,
27    #[serde(rename = "type")]
28    pub repo_type: Option<String>,
29}
30
31#[derive(Debug, Clone, Serialize, Deserialize)]
32pub struct RepositoryLinks {
33    #[serde(rename = "self")]
34    pub self_link: Option<Link>,
35    pub html: Option<Link>,
36    pub avatar: Option<Link>,
37    pub clone: Option<Vec<CloneLink>>,
38    pub pullrequests: Option<Link>,
39    pub commits: Option<Link>,
40    pub forks: Option<Link>,
41    pub watchers: Option<Link>,
42    pub branches: Option<Link>,
43    pub tags: Option<Link>,
44    pub downloads: Option<Link>,
45    pub source: Option<Link>,
46    pub issues: Option<Link>,
47}
48
49#[derive(Debug, Clone, Serialize, Deserialize)]
50pub struct CloneLink {
51    pub href: String,
52    pub name: String,
53}
54
55#[derive(Debug, Clone, Serialize, Deserialize)]
56pub struct Branch {
57    pub name: String,
58    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
59    pub branch_type: Option<String>,
60}
61
62/// A repository download artifact (the "Downloads" area of a repo).
63#[derive(Debug, Clone, Serialize, Deserialize)]
64pub struct Download {
65    pub name: String,
66    pub size: Option<u64>,
67    /// Number of times this artifact has been downloaded.
68    pub downloads: Option<u64>,
69}
70
71#[derive(Debug, Clone, Serialize, Deserialize)]
72pub struct Project {
73    pub uuid: String,
74    pub key: String,
75    pub name: String,
76    #[serde(rename = "type")]
77    pub project_type: String,
78    pub links: Option<ProjectLinks>,
79}
80
81#[derive(Debug, Clone, Serialize, Deserialize)]
82pub struct ProjectLinks {
83    pub html: Option<Link>,
84    pub avatar: Option<Link>,
85}
86
87#[derive(Debug, Clone, Serialize, Deserialize)]
88#[non_exhaustive]
89pub struct CreateRepositoryRequest {
90    pub scm: String,
91    pub name: Option<String>,
92    pub description: Option<String>,
93    pub is_private: Option<bool>,
94    pub project: Option<ProjectKey>,
95    pub fork_policy: Option<String>,
96    pub language: Option<String>,
97    pub has_issues: Option<bool>,
98    pub has_wiki: Option<bool>,
99    #[serde(skip_serializing_if = "Option::is_none")]
100    pub website: Option<String>,
101    #[serde(skip_serializing_if = "Option::is_none")]
102    pub mainbranch: Option<Branch>,
103}
104
105#[derive(Debug, Clone, Serialize, Deserialize)]
106pub struct ProjectKey {
107    pub key: String,
108}
109
110/// Partial update for an existing repository. Unset fields are omitted from
111/// the request body so the corresponding settings are left untouched.
112#[derive(Debug, Clone, Default, Serialize, Deserialize)]
113#[non_exhaustive]
114pub struct UpdateRepositoryRequest {
115    #[serde(skip_serializing_if = "Option::is_none")]
116    pub name: Option<String>,
117    #[serde(skip_serializing_if = "Option::is_none")]
118    pub description: Option<String>,
119    #[serde(skip_serializing_if = "Option::is_none")]
120    pub is_private: Option<bool>,
121    #[serde(skip_serializing_if = "Option::is_none")]
122    pub language: Option<String>,
123    #[serde(skip_serializing_if = "Option::is_none")]
124    pub fork_policy: Option<String>,
125    #[serde(skip_serializing_if = "Option::is_none")]
126    pub has_issues: Option<bool>,
127    #[serde(skip_serializing_if = "Option::is_none")]
128    pub has_wiki: Option<bool>,
129    #[serde(skip_serializing_if = "Option::is_none")]
130    pub website: Option<String>,
131    #[serde(skip_serializing_if = "Option::is_none")]
132    pub project: Option<ProjectKey>,
133    #[serde(skip_serializing_if = "Option::is_none")]
134    pub mainbranch: Option<Branch>,
135}
136
137impl UpdateRepositoryRequest {
138    pub fn is_empty(&self) -> bool {
139        self.name.is_none()
140            && self.description.is_none()
141            && self.is_private.is_none()
142            && self.language.is_none()
143            && self.fork_policy.is_none()
144            && self.has_issues.is_none()
145            && self.has_wiki.is_none()
146            && self.website.is_none()
147            && self.project.is_none()
148            && self.mainbranch.is_none()
149    }
150}
151
152impl Default for CreateRepositoryRequest {
153    fn default() -> Self {
154        Self {
155            scm: "git".to_string(),
156            name: None,
157            description: None,
158            is_private: Some(true),
159            project: None,
160            fork_policy: Some("no_public_forks".to_string()),
161            language: None,
162            has_issues: Some(true),
163            has_wiki: Some(false),
164            website: None,
165            mainbranch: None,
166        }
167    }
168}
169
170#[cfg(test)]
171mod tests {
172    use super::{Branch, ProjectKey, UpdateRepositoryRequest};
173
174    #[test]
175    fn update_request_omits_unset_fields() {
176        let request = UpdateRepositoryRequest {
177            description: Some("new description".to_string()),
178            ..Default::default()
179        };
180
181        let json = serde_json::to_value(&request).unwrap();
182        assert_eq!(
183            json,
184            serde_json::json!({ "description": "new description" })
185        );
186    }
187
188    #[test]
189    fn update_request_mainbranch_serializes_without_null_type() {
190        let request = UpdateRepositoryRequest {
191            mainbranch: Some(Branch {
192                name: "develop".to_string(),
193                branch_type: None,
194            }),
195            ..Default::default()
196        };
197
198        let json = serde_json::to_value(&request).unwrap();
199        assert_eq!(
200            json,
201            serde_json::json!({ "mainbranch": { "name": "develop" } })
202        );
203    }
204
205    #[test]
206    fn update_request_is_empty() {
207        assert!(UpdateRepositoryRequest::default().is_empty());
208
209        let with_project = UpdateRepositoryRequest {
210            project: Some(ProjectKey {
211                key: "PROJ".to_string(),
212            }),
213            ..Default::default()
214        };
215        assert!(!with_project.is_empty());
216    }
217
218    #[test]
219    fn update_request_website_counts_as_non_empty_and_serializes() {
220        let request = UpdateRepositoryRequest {
221            website: Some("https://example.com".to_string()),
222            ..Default::default()
223        };
224
225        assert!(!request.is_empty());
226
227        let json = serde_json::to_value(&request).unwrap();
228        assert_eq!(
229            json,
230            serde_json::json!({ "website": "https://example.com" })
231        );
232    }
233}