Skip to main content

atlassian_rust_api/jira/endpoints/issues/
get_issue.rs

1use std::sync::Arc;
2
3use crate::{Jira, Result, rest_client::RestClient, web::{Endpoint, QueryParams}};
4
5#[derive(Debug, Clone)]
6pub struct GetIssueBuilder {
7	client: Arc<RestClient>,
8	request: GetIssueRequest,
9}
10
11#[derive(Debug, Clone, Default)]
12struct GetIssueRequest {
13	key: String,
14	fields: Option<Vec<String>>,
15	properties: Option<Vec<String>>,
16	expand: Option<Vec<String>>,
17	update_history: Option<bool>,
18}
19
20impl Endpoint for GetIssueRequest {
21	fn endpoint(&self) -> std::borrow::Cow<'static, str> {
22		format!("issue/{}", &self.key).into()
23	}
24
25	fn parameters(&self) -> crate::web::QueryParams<'_> {
26		let mut params = QueryParams::default();
27		let fields = match &self.fields {
28			Some(fields) => fields.join(","),
29			None => "*all".to_string()
30		};
31		let properties = match &self.properties {
32			Some(properties) => Some(properties.join(",")),
33			None => None
34		};
35		let expand = match &self.expand {
36			Some(expand) => Some(expand.join(",")),
37			None => None
38		};
39		params
40			.push("fields", fields)
41			.push_opt("properties", properties)
42			.push_opt("expand", expand)
43			.push_opt("updateHistory", self.update_history);
44		params
45	}
46}
47
48impl GetIssueBuilder {
49	fn new(client: Arc<RestClient>) -> GetIssueBuilder {
50		GetIssueBuilder { client, request: GetIssueRequest::default() }
51	}
52
53	fn key(mut self, key: impl Into<String>) -> GetIssueBuilder {
54		self.request.key = key.into();
55		self
56	}
57
58	pub fn field(mut self, field: impl Into<String>) -> GetIssueBuilder {
59		match self.request.fields {
60			Some(ref mut f) => f.push(field.into()),
61			None => self.request.fields = Some(vec![field.into()]),
62		};
63		self
64	}
65
66	pub fn fields<S>(mut self, fields: impl IntoIterator<Item = S>) -> GetIssueBuilder 
67	where
68		S: Into<String>,
69	{
70		let fields = fields.into_iter().map(|f| f.into()).collect::<Vec<String>>();
71		match self.request.fields {
72			Some(ref mut f) => f.extend(fields),
73			None => self.request.fields = Some(fields),
74		};
75		self
76	}
77
78	pub fn property(mut self, property: impl Into<String>) -> GetIssueBuilder {
79		match self.request.properties {
80			Some(ref mut p) => p.push(property.into()),
81			None => self.request.properties = Some(vec![property.into()]),
82		};
83		self
84	}
85
86	pub fn properties<S>(mut self, properties: impl IntoIterator<Item = S>) -> GetIssueBuilder 
87	where
88		S: Into<String>,
89	{
90		let properties = properties.into_iter().map(|f| f.into()).collect::<Vec<String>>();
91		match self.request.properties {
92			Some(ref mut f) => f.extend(properties),
93			None => self.request.properties = Some(properties),
94		};
95		self
96	}
97
98	pub fn expand(mut self, expand: impl Into<String>) -> GetIssueBuilder {
99		match self.request.expand {
100			Some(ref mut f) => f.push(expand.into()),
101			None => self.request.expand = Some(vec![expand.into()]),
102		};
103		self
104	}
105
106	pub fn expands<S>(mut self, expands: impl IntoIterator<Item = S>) -> GetIssueBuilder 
107	where
108		S: Into<String>,
109	{
110		let expands = expands.into_iter().map(|f| f.into()).collect::<Vec<String>>();
111		match self.request.expand {
112			Some(ref mut f) => f.extend(expands),
113			None => self.request.expand = Some(expands),
114		};
115		self
116	}
117
118	pub fn update_history(mut self, update_history: bool) -> GetIssueBuilder {
119		self.request.update_history = Some(update_history);
120		self
121	}
122
123	pub async fn send(self) -> Result<serde_json::Value> {
124		self.client.get(self.request).await
125	}
126}
127
128impl Jira {
129	/// Returns a full representation of the issue for the given issue key. By default, all
130	/// fields are returned.
131	pub fn get_issue(&self, key: impl Into<String>) -> GetIssueBuilder {
132		GetIssueBuilder::new(Arc::clone(&self.client)).key(key)
133	}
134
135	/// Alias for [`Jira::get_issue`]
136	pub fn issue(&self, key: impl Into<String>) -> GetIssueBuilder {
137		self.get_issue(key)
138	}
139}