Skip to main content

gitfox_client/models/
activity.rs

1use serde::{Deserialize, Serialize};
2
3use super::Principal;
4
5/// One entry in a pull request's timeline: a comment, a code comment, or a
6/// system event such as a merge or a title change.
7///
8/// `kind` and `kind_type` stay strings for the same reason [`crate::CiStatus`]
9/// does — GitFox grows event types, and an unknown one must still decode.
10#[derive(Debug, Clone, Default, Serialize, Deserialize)]
11pub struct PullRequestActivity {
12    #[serde(default)]
13    pub id: i64,
14    /// `comment`, `change-comment` or `system`.
15    #[serde(default)]
16    pub kind: String,
17    /// `comment`, `code-comment`, `merge`, `state-change`, `review-submit`, …
18    #[serde(default, rename = "type")]
19    pub kind_type: String,
20    #[serde(default)]
21    pub text: String,
22    #[serde(default)]
23    pub author: Option<Principal>,
24    #[serde(default)]
25    pub parent_id: Option<i64>,
26    #[serde(default)]
27    pub created: Option<i64>,
28    #[serde(default)]
29    pub updated: Option<i64>,
30    #[serde(default)]
31    pub edited: Option<i64>,
32    /// Set once the comment was deleted; GitFox keeps the entry.
33    #[serde(default)]
34    pub deleted: Option<i64>,
35    #[serde(default)]
36    pub resolved: Option<i64>,
37    #[serde(default)]
38    pub code_comment: Option<CodeComment>,
39}
40
41impl PullRequestActivity {
42    /// A comment a person wrote, as opposed to an event GitFox recorded.
43    pub fn is_comment(&self) -> bool {
44        matches!(self.kind.as_str(), "comment" | "change-comment")
45            && self.deleted.is_none_or(|d| d == 0)
46    }
47}
48
49/// Where a code comment is anchored.
50#[derive(Debug, Clone, Default, Serialize, Deserialize)]
51pub struct CodeComment {
52    #[serde(default)]
53    pub path: String,
54    #[serde(default)]
55    pub line_new: Option<i64>,
56    #[serde(default)]
57    pub line_old: Option<i64>,
58    #[serde(default)]
59    pub outdated: Option<bool>,
60    #[serde(default)]
61    pub source_sha: Option<String>,
62}
63
64/// A reviewer on a pull request, and where their review stands.
65#[derive(Debug, Clone, Default, Serialize, Deserialize)]
66pub struct PullRequestReviewer {
67    #[serde(default)]
68    pub reviewer: Option<Principal>,
69    #[serde(default)]
70    pub added_by: Option<Principal>,
71    /// `approved`, `changereq`, `pending` or `reviewed`.
72    #[serde(default)]
73    pub review_decision: Option<String>,
74    /// The commit the latest review was given against.
75    #[serde(default)]
76    pub sha: Option<String>,
77    /// `assigned`, `requested` or `self_assigned`.
78    #[serde(default, rename = "type")]
79    pub kind_type: Option<String>,
80    #[serde(default)]
81    pub created: Option<i64>,
82    #[serde(default)]
83    pub updated: Option<i64>,
84}
85
86impl PullRequestReviewer {
87    pub fn decision(&self) -> &str {
88        self.review_decision.as_deref().unwrap_or("pending")
89    }
90}
91
92/// What a review says. The wire values are GitFox's.
93#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
94#[serde(rename_all = "lowercase")]
95pub enum ReviewDecision {
96    Approved,
97    Changereq,
98    Reviewed,
99}
100
101impl ReviewDecision {
102    pub fn as_str(self) -> &'static str {
103        match self {
104            Self::Approved => "approved",
105            Self::Changereq => "changereq",
106            Self::Reviewed => "reviewed",
107        }
108    }
109}
110
111#[cfg(test)]
112mod tests {
113    use super::*;
114    use serde_json::json;
115
116    #[test]
117    fn a_system_event_is_not_a_comment_and_a_deleted_comment_is_gone() {
118        let event: PullRequestActivity =
119            serde_json::from_value(json!({ "id": 1, "kind": "system", "type": "merge" })).unwrap();
120        assert!(!event.is_comment());
121        assert_eq!(event.kind_type, "merge");
122
123        let comment: PullRequestActivity = serde_json::from_value(
124            json!({ "id": 2, "kind": "comment", "type": "comment", "text": "LGTM", "deleted": 0 }),
125        )
126        .unwrap();
127        assert!(comment.is_comment());
128
129        let deleted: PullRequestActivity = serde_json::from_value(
130            json!({ "id": 3, "kind": "comment", "type": "comment", "deleted": 1_756_000_000_000i64 }),
131        )
132        .unwrap();
133        assert!(!deleted.is_comment());
134    }
135
136    #[test]
137    fn a_reviewer_without_a_decision_is_pending() {
138        let r: PullRequestReviewer =
139            serde_json::from_value(json!({ "reviewer": { "uid": "whw" } })).unwrap();
140        assert_eq!(r.decision(), "pending");
141    }
142}