jirust_cli/runners/jira_cmd_runners/
project_cmd_runner.rs

1use crate::args::commands::ProjectArgs;
2use crate::config::config_file::{AuthData, ConfigFile};
3use jira_v3_openapi::apis::configuration::Configuration;
4use jira_v3_openapi::apis::issues_api::{
5    get_create_issue_meta_issue_type_id, get_create_issue_meta_issue_types,
6};
7use jira_v3_openapi::apis::projects_api::search_projects;
8use jira_v3_openapi::models::{
9    FieldCreateMetadata, IssueTypeIssueCreateMetadata, project::Project,
10};
11
12/// Project command runner struct.
13///
14/// This struct is responsible for holding the project commands parameters
15/// and it is used to pass the parameters to the project commands runner.
16pub struct ProjectCmdRunner {
17    cfg: Configuration,
18}
19
20/// Project command runner implementation.
21///
22/// # Methods
23///
24/// * `new` - Creates a new instance of the ProjectCmdRunner struct.
25/// * `list_jira_projects` - Lists Jira projects.
26/// * `get_jira_project_issue_types` - Gets Jira project issue types.
27/// * `get_jira_project_issue_type_id` - Gets Jira project issue fields by issue type ID.
28impl ProjectCmdRunner {
29    /// Creates a new instance of the ProjectCmdRunner struct.
30    ///
31    /// # Arguments
32    ///
33    /// * `cfg_file` - A ConfigFile struct.
34    ///
35    /// # Returns
36    ///
37    /// * A new instance of the ProjectCmdRunner struct.
38    ///
39    /// # Examples
40    ///
41    /// ```
42    /// use jirust_cli::config::config_file::ConfigFile;
43    /// use jirust_cli::runners::jira_cmd_runners::project_cmd_runner::ProjectCmdRunner;
44    /// use toml::Table;
45    ///
46    /// let cfg_file = ConfigFile::new("dXNlcm5hbWU6YXBpX2tleQ==".to_string(), "jira_url".to_string(), "standard_resolution".to_string(), "standard_resolution_comment".to_string(), Table::new());
47    ///
48    /// let project_cmd_runner = ProjectCmdRunner::new(cfg_file);
49    /// ```
50    pub fn new(cfg_file: ConfigFile) -> ProjectCmdRunner {
51        let mut config = Configuration::new();
52        let auth_data = AuthData::from_base64(cfg_file.get_auth_key());
53        config.base_path = cfg_file.get_jira_url().to_string();
54        config.basic_auth = Some((auth_data.0, Some(auth_data.1)));
55        ProjectCmdRunner { cfg: config }
56    }
57
58    /// Lists Jira projects.
59    ///
60    /// # Arguments
61    ///
62    /// * `params` - A ProjectCmdParams struct.
63    ///
64    /// # Returns
65    ///
66    /// * A Result with a vector of Project structs or an error message.
67    ///
68    /// # Examples
69    ///
70    /// ```no_run
71    /// use jirust_cli::runners::jira_cmd_runners::project_cmd_runner::{ProjectCmdRunner, ProjectCmdParams};
72    /// use jirust_cli::config::config_file::ConfigFile;
73    /// use toml::Table;
74    ///
75    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
76    /// # tokio_test::block_on(async {
77    /// let cfg_file = ConfigFile::new("auth_token".to_string(), "jira_url".to_string(), "standard_resolution".to_string(), "standard_resolution_comment".to_string(), Table::new());
78    /// let project_cmd_runner = ProjectCmdRunner::new(cfg_file);
79    /// let params = ProjectCmdParams::new();
80    ///
81    /// let projects = project_cmd_runner.list_jira_projects(params).await?;
82    /// # Ok(())
83    /// # })
84    /// # }
85    /// ```
86    pub async fn list_jira_projects(
87        &self,
88        params: ProjectCmdParams,
89    ) -> Result<Vec<Project>, Box<dyn std::error::Error>> {
90        let page_size = Some(params.projects_page_size.unwrap_or(10));
91        let page_offset = Some(i64::from(params.projects_page_offset.unwrap_or(0)));
92        match search_projects(
93            &self.cfg,
94            page_offset,
95            page_size,
96            None,
97            None,
98            None,
99            None,
100            None,
101            None,
102            None,
103            None,
104            None,
105            None,
106            None,
107        )
108        .await?
109        .values
110        {
111            Some(values) => Ok(values),
112            None => Ok(vec![]),
113        }
114    }
115
116    /// Gets Jira project issue types.
117    ///
118    /// # Arguments
119    ///
120    /// * `params` - A ProjectCmdParams struct.
121    ///
122    /// # Returns
123    /// * A Result with a vector of IssueTypeIssueCreateMetadata structs or an error message.
124    ///
125    /// # Examples
126    ///
127    /// ```no_run
128    /// use jirust_cli::runners::jira_cmd_runners::project_cmd_runner::{ProjectCmdRunner, ProjectCmdParams};
129    /// use jirust_cli::config::config_file::ConfigFile;
130    /// use toml::Table;
131    ///
132    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
133    /// # tokio_test::block_on(async {
134    /// let cfg_file = ConfigFile::new("auth_token".to_string(), "jira_url".to_string(), "standard_resolution".to_string(), "standard_resolution_comment".to_string(), Table::new());
135    /// let project_cmd_runner = ProjectCmdRunner::new(cfg_file);
136    /// let params = ProjectCmdParams::new();
137    ///
138    /// let issue_types = project_cmd_runner.get_jira_project_issue_types(params).await?;
139    /// # Ok(())
140    /// # })
141    /// # }
142    /// ```
143    pub async fn get_jira_project_issue_types(
144        &self,
145        params: ProjectCmdParams,
146    ) -> Result<Vec<IssueTypeIssueCreateMetadata>, Box<dyn std::error::Error>> {
147        let page_size = Some(params.projects_page_size.unwrap_or(10));
148        let page_offset = Some(params.projects_page_offset.unwrap_or(0));
149        match get_create_issue_meta_issue_types(
150            &self.cfg,
151            &params.project_key.expect("Project Key is required!"),
152            page_offset,
153            page_size,
154        )
155        .await?
156        .issue_types
157        {
158            Some(issue_types) => Ok(issue_types),
159            None => Ok(vec![]),
160        }
161    }
162
163    /// Gets Jira project issue fields by issue type id.
164    ///
165    /// # Arguments
166    ///
167    /// * `params` - A ProjectCmdParams struct.
168    ///
169    /// # Returns
170    ///
171    /// * A Result with a vector of FieldCreateMetadata structs or an error message.
172    ///
173    /// # Examples
174    ///
175    /// ```no_run
176    /// use jirust_cli::runners::jira_cmd_runners::project_cmd_runner::{ProjectCmdRunner, ProjectCmdParams};
177    /// use jirust_cli::config::config_file::ConfigFile;
178    /// use toml::Table;
179    ///
180    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
181    /// # tokio_test::block_on(async {
182    /// let cfg_file = ConfigFile::new("auth_token".to_string(), "jira_url".to_string(), "standard_resolution".to_string(), "standard_resolution_comment".to_string(), Table::new());
183    /// let project_cmd_runner = ProjectCmdRunner::new(cfg_file);
184    /// let params = ProjectCmdParams::new();
185    ///
186    /// let issue_fields = project_cmd_runner.get_jira_project_issue_type_id(params).await?;
187    /// # Ok(())
188    /// # })
189    /// # }
190    /// ```
191    pub async fn get_jira_project_issue_type_id(
192        &self,
193        params: ProjectCmdParams,
194    ) -> Result<Vec<FieldCreateMetadata>, Box<dyn std::error::Error>> {
195        let page_size = Some(params.projects_page_size.unwrap_or(10));
196        let page_offset = Some(params.projects_page_offset.unwrap_or(0));
197        match get_create_issue_meta_issue_type_id(
198            &self.cfg,
199            &params.project_key.expect("Project Key is required!"),
200            &params.project_issue_type.expect("Issue Type is required!"),
201            page_offset,
202            page_size,
203        )
204        .await?
205        .fields
206        {
207            Some(id) => Ok(id),
208            None => Ok(vec![]),
209        }
210    }
211}
212
213/// This struct defines the parameters for the Project commands
214///
215/// # Fields
216///
217/// * `project_key` - The project key, **required** for get project issue types and issue fields commands.
218/// * `project_issue_type` - The project issue type, **required** for get issue fields command.
219/// * `projects_page_size` - The page size for the project command, optional.
220/// * `projects_page_offset` - The page offset for the project command, optional.
221pub struct ProjectCmdParams {
222    pub project_key: Option<String>,
223    pub project_issue_type: Option<String>,
224    pub projects_page_size: Option<i32>,
225    pub projects_page_offset: Option<i32>,
226}
227
228/// Implementation of the ProjectCmdParams struct
229///
230/// # Methods
231///
232/// * `new` - Creates a new ProjectCmdParams struct.
233///
234impl ProjectCmdParams {
235    /// Creates a new ProjectCmdParams struct instance.
236    ///
237    /// # Returns
238    ///
239    /// * A ProjectCmdParams struct instance.
240    ///
241    /// # Examples
242    ///
243    /// ```
244    /// use jirust_cli::runners::jira_cmd_runners::project_cmd_runner::ProjectCmdParams;
245    ///
246    /// let params = ProjectCmdParams::new();
247    /// ```
248    pub fn new() -> ProjectCmdParams {
249        ProjectCmdParams {
250            project_key: None,
251            project_issue_type: None,
252            projects_page_size: None,
253            projects_page_offset: None,
254        }
255    }
256}
257
258/// Implementation of the From trait for the ProjectCmdParams struct
259/// to convert from ProjectArgs to ProjectCmdParams.
260impl From<&ProjectArgs> for ProjectCmdParams {
261    /// Converts from ProjectArgs to ProjectCmdParams.
262    ///
263    /// # Arguments
264    ///
265    /// * `value` - A ProjectArgs struct.
266    ///
267    /// # Returns
268    ///
269    /// * A ProjectCmdParams struct instance.
270    ///
271    /// # Examples
272    ///
273    /// ```
274    /// use jirust_cli::runners::jira_cmd_runners::project_cmd_runner::ProjectCmdParams;
275    /// use jirust_cli::args::commands::{ProjectArgs, ProjectActionValues, PaginationArgs, OutputArgs};
276    ///
277    /// let project_args = ProjectArgs {
278    ///     project_act: ProjectActionValues::List,
279    ///     project_key: Some("project_key".to_string()),
280    ///     project_issue_type: Some("project_issue_type".to_string()),
281    ///     pagination: PaginationArgs { page_size: Some(10), page_offset: None },
282    ///     output: OutputArgs { output_format: None, output_type: None },
283    /// };
284    ///
285    /// let params = ProjectCmdParams::from(&project_args);
286    ///
287    /// assert_eq!(params.project_key, Some("project_key".to_string()));
288    /// assert_eq!(params.project_issue_type, Some("project_issue_type".to_string()));
289    /// assert_eq!(params.projects_page_size, Some(10));
290    /// assert_eq!(params.projects_page_offset, Some(0));
291    /// ```
292    fn from(value: &ProjectArgs) -> Self {
293        ProjectCmdParams {
294            project_key: value.project_key.clone(),
295            project_issue_type: value.project_issue_type.clone(),
296            projects_page_size: value.pagination.page_size,
297            projects_page_offset: Some(
298                i32::try_from(value.pagination.page_offset.unwrap_or(0))
299                    .expect("Invalid page offset, should fit an i32!"),
300            ),
301        }
302    }
303}
304
305/// Implementation of the Default trait for the ProjectCmdParams struct
306impl Default for ProjectCmdParams {
307    /// Creates a default ProjectCmdParams struct instance.
308    ///
309    /// # Returns
310    ///
311    /// * A ProjectCmdParams struct instance with default values.
312    ///
313    /// # Examples
314    ///
315    /// ```
316    /// use jirust_cli::runners::jira_cmd_runners::project_cmd_runner::ProjectCmdParams;
317    ///
318    /// let params = ProjectCmdParams::default();
319    ///
320    /// assert_eq!(params.project_key, None);
321    /// assert_eq!(params.project_issue_type, None);
322    /// assert_eq!(params.projects_page_size, None);
323    /// assert_eq!(params.projects_page_offset, None);
324    /// ```
325    fn default() -> Self {
326        ProjectCmdParams::new()
327    }
328}