jira_core/model/
comment.rs1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3
4use crate::adf::{adf_to_text, mentioned_account_ids};
5
6#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct Comment {
8 pub id: String,
9 pub issue_key: String,
10 pub author: Option<String>,
11 pub author_account_id: Option<String>,
12 pub body: Option<String>,
13 pub mentions: Vec<String>,
14 pub created: String,
15 pub updated: String,
16}
17
18impl Comment {
19 pub fn from_value(v: &Value, issue_key: &str) -> Option<Self> {
20 let body_value = v.get("body");
21 Some(Comment {
22 id: v.get("id")?.as_str()?.to_string(),
23 issue_key: issue_key.to_string(),
24 author: v
25 .get("author")
26 .and_then(|a| a.get("displayName").or_else(|| a.get("emailAddress")))
27 .and_then(|n| n.as_str())
28 .map(|s| s.to_string()),
29 author_account_id: v
30 .get("author")
31 .and_then(|a| a.get("accountId"))
32 .and_then(|n| n.as_str())
33 .map(|s| s.to_string()),
34 body: body_value.and_then(|body| {
35 if body.is_object() {
36 Some(adf_to_text(body))
37 } else {
38 body.as_str().map(|s| s.to_string())
39 }
40 }),
41 mentions: body_value
42 .filter(|body| body.is_object())
43 .map(mentioned_account_ids)
44 .unwrap_or_default(),
45 created: v
46 .get("created")
47 .and_then(|t| t.as_str())
48 .unwrap_or("")
49 .to_string(),
50 updated: v
51 .get("updated")
52 .and_then(|t| t.as_str())
53 .unwrap_or("")
54 .to_string(),
55 })
56 }
57}