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)]
88pub struct CreateRepositoryRequest {
89    pub scm: String,
90    pub name: Option<String>,
91    pub description: Option<String>,
92    pub is_private: Option<bool>,
93    pub project: Option<ProjectKey>,
94    pub fork_policy: Option<String>,
95    pub language: Option<String>,
96    pub has_issues: Option<bool>,
97    pub has_wiki: Option<bool>,
98}
99
100#[derive(Debug, Clone, Serialize, Deserialize)]
101pub struct ProjectKey {
102    pub key: String,
103}
104
105/// Partial update for an existing repository. Unset fields are omitted from
106/// the request body so the corresponding settings are left untouched.
107#[derive(Debug, Clone, Default, Serialize, Deserialize)]
108pub struct UpdateRepositoryRequest {
109    #[serde(skip_serializing_if = "Option::is_none")]
110    pub name: Option<String>,
111    #[serde(skip_serializing_if = "Option::is_none")]
112    pub description: Option<String>,
113    #[serde(skip_serializing_if = "Option::is_none")]
114    pub is_private: Option<bool>,
115    #[serde(skip_serializing_if = "Option::is_none")]
116    pub language: Option<String>,
117    #[serde(skip_serializing_if = "Option::is_none")]
118    pub fork_policy: Option<String>,
119    #[serde(skip_serializing_if = "Option::is_none")]
120    pub has_issues: Option<bool>,
121    #[serde(skip_serializing_if = "Option::is_none")]
122    pub has_wiki: Option<bool>,
123    #[serde(skip_serializing_if = "Option::is_none")]
124    pub project: Option<ProjectKey>,
125    #[serde(skip_serializing_if = "Option::is_none")]
126    pub mainbranch: Option<Branch>,
127}
128
129impl UpdateRepositoryRequest {
130    pub fn is_empty(&self) -> bool {
131        self.name.is_none()
132            && self.description.is_none()
133            && self.is_private.is_none()
134            && self.language.is_none()
135            && self.fork_policy.is_none()
136            && self.has_issues.is_none()
137            && self.has_wiki.is_none()
138            && self.project.is_none()
139            && self.mainbranch.is_none()
140    }
141}
142
143impl Default for CreateRepositoryRequest {
144    fn default() -> Self {
145        Self {
146            scm: "git".to_string(),
147            name: None,
148            description: None,
149            is_private: Some(true),
150            project: None,
151            fork_policy: Some("no_public_forks".to_string()),
152            language: None,
153            has_issues: Some(true),
154            has_wiki: Some(false),
155        }
156    }
157}
158
159#[cfg(test)]
160mod tests {
161    use super::{Branch, ProjectKey, UpdateRepositoryRequest};
162
163    #[test]
164    fn update_request_omits_unset_fields() {
165        let request = UpdateRepositoryRequest {
166            description: Some("new description".to_string()),
167            ..Default::default()
168        };
169
170        let json = serde_json::to_value(&request).unwrap();
171        assert_eq!(
172            json,
173            serde_json::json!({ "description": "new description" })
174        );
175    }
176
177    #[test]
178    fn update_request_mainbranch_serializes_without_null_type() {
179        let request = UpdateRepositoryRequest {
180            mainbranch: Some(Branch {
181                name: "develop".to_string(),
182                branch_type: None,
183            }),
184            ..Default::default()
185        };
186
187        let json = serde_json::to_value(&request).unwrap();
188        assert_eq!(
189            json,
190            serde_json::json!({ "mainbranch": { "name": "develop" } })
191        );
192    }
193
194    #[test]
195    fn update_request_is_empty() {
196        assert!(UpdateRepositoryRequest::default().is_empty());
197
198        let with_project = UpdateRepositoryRequest {
199            project: Some(ProjectKey {
200                key: "PROJ".to_string(),
201            }),
202            ..Default::default()
203        };
204        assert!(!with_project.is_empty());
205    }
206}