Skip to main content

bitbucket_cli/models/
commit.rs

1//! Models for refs (branches and tags), commits, and source browsing.
2
3use chrono::{DateTime, Utc};
4use serde::{Deserialize, Serialize};
5
6// The refs/commits API returns the same author shape as the pull request
7// commit payload; reuse it instead of redefining a colliding `CommitAuthor`
8// (models are glob re-exported from `crate::models`).
9use super::pr::CommitAuthor;
10
11/// A branch ref as returned by the `refs/branches` API, including its target
12/// commit. Distinct from [`super::repo::Branch`], which is the bare
13/// `{ name }` shape embedded in repository payloads.
14#[derive(Debug, Clone, Serialize, Deserialize)]
15pub struct BranchDetail {
16    pub name: String,
17    pub target: Option<CommitSummary>,
18    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
19    pub ref_type: Option<String>,
20}
21
22/// A tag ref as returned by the `refs/tags` API.
23#[derive(Debug, Clone, Serialize, Deserialize)]
24pub struct TagDetail {
25    pub name: String,
26    /// Annotation message, for annotated tags.
27    pub message: Option<String>,
28    pub target: Option<CommitSummary>,
29    /// Tag creation date, for annotated tags.
30    pub date: Option<DateTime<Utc>>,
31}
32
33/// A commit as returned by the `commits` / `commit/{hash}` endpoints and
34/// embedded as the `target` of branch and tag refs.
35#[derive(Debug, Clone, Serialize, Deserialize)]
36pub struct CommitSummary {
37    pub hash: String,
38    pub message: Option<String>,
39    pub date: Option<DateTime<Utc>>,
40    pub author: Option<CommitAuthor>,
41}
42
43/// A file or directory entry from the `src/{ref}/{path}` listing endpoint.
44#[derive(Debug, Clone, Serialize, Deserialize)]
45pub struct SourceEntry {
46    pub path: String,
47    /// `commit_file` or `commit_directory`.
48    #[serde(rename = "type")]
49    pub entry_type: String,
50    /// File size in bytes; absent for directories.
51    pub size: Option<u64>,
52}
53
54/// One entry from the `filehistory/{ref}/{path}` endpoint: a commit that
55/// touched the file, plus the path the file had at that commit.
56#[derive(Debug, Clone, Serialize, Deserialize)]
57pub struct FileHistoryEntry {
58    pub commit: Option<CommitSummary>,
59    pub path: Option<String>,
60}
61
62/// The `target` object of a ref-creation request: the commit to point at.
63#[derive(Debug, Clone, Serialize, Deserialize)]
64pub struct RefTarget {
65    pub hash: String,
66}
67
68/// Request body for `POST refs/branches`.
69#[derive(Debug, Clone, Serialize, Deserialize)]
70#[non_exhaustive]
71pub struct CreateBranchRequest {
72    pub name: String,
73    pub target: RefTarget,
74}
75
76/// Request body for `POST refs/tags`. `message` is omitted from the payload
77/// when unset so Bitbucket creates a lightweight tag.
78#[derive(Debug, Clone, Serialize, Deserialize)]
79#[non_exhaustive]
80pub struct CreateTagRequest {
81    pub name: String,
82    pub target: RefTarget,
83    #[serde(skip_serializing_if = "Option::is_none")]
84    pub message: Option<String>,
85}
86
87#[cfg(test)]
88mod tests {
89    use super::*;
90
91    #[test]
92    fn create_branch_request_serializes_name_and_target_hash() {
93        let request = CreateBranchRequest {
94            name: "feature/login".to_string(),
95            target: RefTarget {
96                hash: "a1b2c3d".to_string(),
97            },
98        };
99
100        let json = serde_json::to_value(&request).unwrap();
101        assert_eq!(
102            json,
103            serde_json::json!({
104                "name": "feature/login",
105                "target": { "hash": "a1b2c3d" }
106            })
107        );
108    }
109
110    #[test]
111    fn create_tag_request_omits_unset_message() {
112        let request = CreateTagRequest {
113            name: "v1.0.0".to_string(),
114            target: RefTarget {
115                hash: "a1b2c3d".to_string(),
116            },
117            message: None,
118        };
119
120        let json = serde_json::to_value(&request).unwrap();
121        assert_eq!(
122            json,
123            serde_json::json!({
124                "name": "v1.0.0",
125                "target": { "hash": "a1b2c3d" }
126            })
127        );
128    }
129
130    #[test]
131    fn create_tag_request_includes_message_when_set() {
132        let request = CreateTagRequest {
133            name: "v1.0.0".to_string(),
134            target: RefTarget {
135                hash: "a1b2c3d".to_string(),
136            },
137            message: Some("First release".to_string()),
138        };
139
140        let json = serde_json::to_value(&request).unwrap();
141        assert_eq!(json["message"], serde_json::json!("First release"));
142    }
143
144    #[test]
145    fn branch_detail_maps_type_field() {
146        let branch: BranchDetail = serde_json::from_str(
147            r#"{
148                "name": "main",
149                "type": "branch",
150                "target": {
151                    "hash": "deadbeefcafe",
152                    "message": "Initial commit\n",
153                    "date": "2024-01-01T00:00:00+00:00",
154                    "author": { "raw": "Jo <jo@example.com>" }
155                }
156            }"#,
157        )
158        .unwrap();
159
160        assert_eq!(branch.ref_type.as_deref(), Some("branch"));
161        let target = branch.target.unwrap();
162        assert_eq!(target.hash, "deadbeefcafe");
163        assert_eq!(
164            target.author.unwrap().raw.as_deref(),
165            Some("Jo <jo@example.com>")
166        );
167    }
168
169    #[test]
170    fn source_entry_maps_type_field() {
171        let entry: SourceEntry = serde_json::from_str(
172            r#"{ "path": "src/main.rs", "type": "commit_file", "size": 1024 }"#,
173        )
174        .unwrap();
175
176        assert_eq!(entry.entry_type, "commit_file");
177        assert_eq!(entry.size, Some(1024));
178    }
179}