1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
//! Interfaces for accessing and managing users
//!
//! This module provides methods to search for users, get user details,
//! and find users assignable to projects and issues.
use url::form_urlencoded;
use crate::{Jira, Result, User};
#[cfg(feature = "async")]
use crate::r#async::Jira as AsyncJira;
/// User management interface
#[derive(Debug)]
pub struct Users {
jira: Jira,
}
/// Options for searching users
#[derive(Debug, Default, Clone)]
pub struct UserSearchOptions {
pub query: Option<String>,
pub start_at: Option<u64>,
pub max_results: Option<u64>,
}
impl UserSearchOptions {
pub fn builder() -> UserSearchOptionsBuilder {
UserSearchOptionsBuilder::default()
}
fn to_query_params(&self) -> Vec<(String, String)> {
let mut params = Vec::new();
if let Some(ref query) = self.query {
params.push(("query".to_string(), query.clone()));
}
if let Some(start_at) = self.start_at {
params.push(("startAt".to_string(), start_at.to_string()));
}
if let Some(max_results) = self.max_results {
params.push(("maxResults".to_string(), max_results.to_string()));
}
params
}
}
/// Builder for UserSearchOptions
#[derive(Debug, Default)]
pub struct UserSearchOptionsBuilder {
query: Option<String>,
start_at: Option<u64>,
max_results: Option<u64>,
}
impl UserSearchOptionsBuilder {
pub fn query<S: Into<String>>(mut self, query: S) -> Self {
self.query = Some(query.into());
self
}
pub fn start_at(mut self, start_at: u64) -> Self {
self.start_at = Some(start_at);
self
}
pub fn max_results(mut self, max_results: u64) -> Self {
self.max_results = Some(max_results);
self
}
pub fn build(self) -> UserSearchOptions {
UserSearchOptions {
query: self.query,
start_at: self.start_at,
max_results: self.max_results,
}
}
}
/// Options for finding assignable users
#[derive(Debug, Default, Clone)]
pub struct AssignableUserOptions {
pub query: Option<String>,
pub start_at: Option<u64>,
pub max_results: Option<u64>,
}
impl AssignableUserOptions {
pub fn builder() -> AssignableUserOptionsBuilder {
AssignableUserOptionsBuilder::default()
}
fn to_query_params(&self) -> Vec<(String, String)> {
let mut params = Vec::new();
if let Some(ref query) = self.query {
params.push(("query".to_string(), query.clone()));
}
if let Some(start_at) = self.start_at {
params.push(("startAt".to_string(), start_at.to_string()));
}
if let Some(max_results) = self.max_results {
params.push(("maxResults".to_string(), max_results.to_string()));
}
params
}
}
/// Builder for AssignableUserOptions
#[derive(Debug, Default)]
pub struct AssignableUserOptionsBuilder {
query: Option<String>,
start_at: Option<u64>,
max_results: Option<u64>,
}
impl AssignableUserOptionsBuilder {
pub fn query<S: Into<String>>(mut self, query: S) -> Self {
self.query = Some(query.into());
self
}
pub fn start_at(mut self, start_at: u64) -> Self {
self.start_at = Some(start_at);
self
}
pub fn max_results(mut self, max_results: u64) -> Self {
self.max_results = Some(max_results);
self
}
pub fn build(self) -> AssignableUserOptions {
AssignableUserOptions {
query: self.query,
start_at: self.start_at,
max_results: self.max_results,
}
}
}
impl Users {
pub fn new(jira: &Jira) -> Users {
Users { jira: jira.clone() }
}
/// Get user by account ID
///
/// Returns details of a single user identified by their account ID.
///
/// # Examples
///
/// ```no_run
/// use gouqi::{Jira, Credentials};
///
/// let jira = Jira::new("https://example.atlassian.net", Credentials::Anonymous).unwrap();
/// let user = jira.users().get("5b10a2844c20165700ede21g").unwrap();
/// println!("User: {}", user.display_name);
/// ```
///
/// See [Jira docs](https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-users/#api-rest-api-3-user-get)
/// for more information
pub fn get<I>(&self, account_id: I) -> Result<User>
where
I: Into<String>,
{
let account_id = account_id.into();
let query = form_urlencoded::Serializer::new(String::new())
.append_pair("accountId", &account_id)
.finish();
self.jira.get("api", &format!("/user?{}", query))
}
/// Search for users
///
/// Returns a list of users matching the search query.
///
/// # Examples
///
/// ```no_run
/// use gouqi::{Jira, Credentials};
/// use gouqi::users::UserSearchOptions;
///
/// let jira = Jira::new("https://example.atlassian.net", Credentials::Anonymous).unwrap();
/// let options = UserSearchOptions::builder()
/// .query("john")
/// .max_results(10)
/// .build();
/// let users = jira.users().search(&options).unwrap();
/// ```
///
/// See [Jira docs](https://developer.atlassian.com/cloud/jira/platform/rest/v2/api-group-user-search/#api-rest-api-2-user-search-get)
/// for more information
pub fn search(&self, options: &UserSearchOptions) -> Result<Vec<User>> {
let params = options.to_query_params();
let query = form_urlencoded::Serializer::new(String::new())
.extend_pairs(params)
.finish();
let path = if query.is_empty() {
"/user/search".to_string()
} else {
format!("/user/search?{}", query)
};
self.jira.get("api", &path)
}
/// Get users assignable to a project
///
/// Returns users who can be assigned to issues in the specified project.
///
/// # Examples
///
/// ```no_run
/// use gouqi::{Jira, Credentials};
/// use gouqi::users::AssignableUserOptions;
///
/// let jira = Jira::new("https://example.atlassian.net", Credentials::Anonymous).unwrap();
/// let options = AssignableUserOptions::builder()
/// .max_results(50)
/// .build();
/// let users = jira.users().get_assignable_users("PROJ", &options).unwrap();
/// ```
///
/// See [Jira docs](https://developer.atlassian.com/cloud/jira/platform/rest/v2/api-group-user-search/#api-rest-api-2-user-assignable-search-get)
/// for more information
pub fn get_assignable_users<P>(
&self,
project: P,
options: &AssignableUserOptions,
) -> Result<Vec<User>>
where
P: Into<String>,
{
let mut params = options.to_query_params();
params.push(("project".to_string(), project.into()));
let query = form_urlencoded::Serializer::new(String::new())
.extend_pairs(params)
.finish();
self.jira
.get("api", &format!("/user/assignable/search?{}", query))
}
/// Get users assignable to an issue
///
/// Returns users who can be assigned to the specified issue.
///
/// # Examples
///
/// ```no_run
/// use gouqi::{Jira, Credentials};
/// use gouqi::users::AssignableUserOptions;
///
/// let jira = Jira::new("https://example.atlassian.net", Credentials::Anonymous).unwrap();
/// let options = AssignableUserOptions::default();
/// let users = jira.users().get_assignable_users_for_issue("PROJ-123", &options).unwrap();
/// ```
///
/// See [Jira docs](https://developer.atlassian.com/cloud/jira/platform/rest/v2/api-group-user-search/#api-rest-api-2-user-assignable-search-get)
/// for more information
pub fn get_assignable_users_for_issue<I>(
&self,
issue_key: I,
options: &AssignableUserOptions,
) -> Result<Vec<User>>
where
I: Into<String>,
{
let mut params = options.to_query_params();
params.push(("issueKey".to_string(), issue_key.into()));
let query = form_urlencoded::Serializer::new(String::new())
.extend_pairs(params)
.finish();
self.jira
.get("api", &format!("/user/assignable/search?{}", query))
}
}
#[cfg(feature = "async")]
/// Async version of the Users interface
#[derive(Debug)]
pub struct AsyncUsers {
jira: AsyncJira,
}
#[cfg(feature = "async")]
impl AsyncUsers {
pub fn new(jira: &AsyncJira) -> AsyncUsers {
AsyncUsers { jira: jira.clone() }
}
/// Get user by account ID (async)
///
/// Returns details of a single user identified by their account ID.
///
/// See [Jira docs](https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-users/#api-rest-api-3-user-get)
/// for more information
pub async fn get<I>(&self, account_id: I) -> Result<User>
where
I: Into<String>,
{
let account_id = account_id.into();
let query = form_urlencoded::Serializer::new(String::new())
.append_pair("accountId", &account_id)
.finish();
self.jira.get("api", &format!("/user?{}", query)).await
}
/// Search for users (async)
///
/// Returns a list of users matching the search query.
///
/// See [Jira docs](https://developer.atlassian.com/cloud/jira/platform/rest/v2/api-group-user-search/#api-rest-api-2-user-search-get)
/// for more information
pub async fn search(&self, options: &UserSearchOptions) -> Result<Vec<User>> {
let params = options.to_query_params();
let query = form_urlencoded::Serializer::new(String::new())
.extend_pairs(params)
.finish();
let path = if query.is_empty() {
"/user/search".to_string()
} else {
format!("/user/search?{}", query)
};
self.jira.get("api", &path).await
}
/// Get users assignable to a project (async)
///
/// Returns users who can be assigned to issues in the specified project.
///
/// See [Jira docs](https://developer.atlassian.com/cloud/jira/platform/rest/v2/api-group-user-search/#api-rest-api-2-user-assignable-search-get)
/// for more information
pub async fn get_assignable_users<P>(
&self,
project: P,
options: &AssignableUserOptions,
) -> Result<Vec<User>>
where
P: Into<String>,
{
let mut params = options.to_query_params();
params.push(("project".to_string(), project.into()));
let query = form_urlencoded::Serializer::new(String::new())
.extend_pairs(params)
.finish();
self.jira
.get("api", &format!("/user/assignable/search?{}", query))
.await
}
/// Get users assignable to an issue (async)
///
/// Returns users who can be assigned to the specified issue.
///
/// See [Jira docs](https://developer.atlassian.com/cloud/jira/platform/rest/v2/api-group-user-search/#api-rest-api-2-user-assignable-search-get)
/// for more information
pub async fn get_assignable_users_for_issue<I>(
&self,
issue_key: I,
options: &AssignableUserOptions,
) -> Result<Vec<User>>
where
I: Into<String>,
{
let mut params = options.to_query_params();
params.push(("issueKey".to_string(), issue_key.into()));
let query = form_urlencoded::Serializer::new(String::new())
.extend_pairs(params)
.finish();
self.jira
.get("api", &format!("/user/assignable/search?{}", query))
.await
}
}