diff --git a/Cargo.lock b/Cargo.lock index 7bacea6..d999487 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -487,7 +487,7 @@ checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" [[package]] name = "github-cli" -version = "1.13.0" +version = "1.14.0" dependencies = [ "clap", "fzf-wrapped", diff --git a/Cargo.toml b/Cargo.toml index c62f162..cd4f6b6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "github-cli" -version = "1.13.0" +version = "1.14.0" edition = "2021" [dependencies] diff --git a/Readme.md b/Readme.md index 6b140b1..c999a51 100644 --- a/Readme.md +++ b/Readme.md @@ -54,7 +54,7 @@ github-cli release --help - [x] Сделать создание комментария в issue/pull request - [x] Сделать получение комментариев для issue/pull request - [x] Сделать получение review комментариев для pull request -- [ ] Сделать редактирование комментария для issue/pull request +- [x] Сделать редактирование комментария для issue/pull request - [ ] Сделать удаление комментария для issue/pull request ### issue diff --git a/src/cli_in/comment_command.rs b/src/cli_in/comment_command.rs index 202d863..8097e7f 100644 --- a/src/cli_in/comment_command.rs +++ b/src/cli_in/comment_command.rs @@ -55,4 +55,20 @@ pub enum CommentCommand { #[clap(long, default_value = "desc")] order: Orders, }, + + /// Update comment for issue/pull request by comment id + Update { + /// Repo owner (optional) + #[clap(long, short, default_value = None)] + owner: Option, + /// Repo name (optional) + #[clap(long, short, default_value = None)] + repo: Option, + /// Update comment from issue/pull request by id + #[clap(long, short)] + comment_id: i64, + /// Comment body (optional) + #[clap(long, short, default_value = "")] + body: String, + }, } diff --git a/src/cli_in/issue_command.rs b/src/cli_in/issue_command.rs index d4ebe5c..28b6990 100644 --- a/src/cli_in/issue_command.rs +++ b/src/cli_in/issue_command.rs @@ -33,8 +33,8 @@ pub enum IssueCommand { iss_on_page: i64, }, - /// Get issue by number - Get { + /// Get issue by number + Get { /// Repo owner (optional) #[clap(long, short, default_value = None)] owner: Option, @@ -44,10 +44,10 @@ pub enum IssueCommand { /// Get issue with number #[clap(long, short)] number: i64, - }, + }, - /// Get issue with comment from issues list - GetFromList { + /// Get issue with comment from issues list + GetFromList { /// Repo owner (optional) #[clap(long, short, default_value = None)] owner: Option, @@ -72,7 +72,7 @@ pub enum IssueCommand { /// Results on page (max 100) (optional) #[clap(long, short, default_value = "30", value_parser = clap::value_parser!(i64).range(1..=100))] iss_on_page: i64, - }, + }, /// Create issue Create { diff --git a/src/cli_in/set_vars.rs b/src/cli_in/set_vars.rs index d77fa52..a883ba3 100644 --- a/src/cli_in/set_vars.rs +++ b/src/cli_in/set_vars.rs @@ -1,4 +1,4 @@ -use std::str::FromStr; +use std::{fmt::Display, str::FromStr}; use octorust::types::{ IssuesListState, Order, ReposCreateInOrgRequestVisibility, ReposListOrgSort, ReposListOrgType, @@ -127,13 +127,13 @@ impl FromStr for IssuesListStates { } } -impl ToString for IssuesListStates { - fn to_string(&self) -> String { +impl Display for IssuesListStates { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self.0 { - IssuesListState::Open => String::from("open"), - IssuesListState::Closed => String::from("closed"), - IssuesListState::All => String::from("all"), - IssuesListState::FallthroughString => String::from("FallthroughString"), + IssuesListState::Open => write!(f, "open"), + IssuesListState::Closed => write!(f, "closed"), + IssuesListState::All => write!(f, "all"), + IssuesListState::FallthroughString => write!(f, "FallthroughString"), } } } diff --git a/src/cli_out/mod.rs b/src/cli_out/mod.rs index 513cfbf..0349e0e 100644 --- a/src/cli_out/mod.rs +++ b/src/cli_out/mod.rs @@ -1,2 +1,2 @@ -pub mod print_in_cli; pub mod fuzzy_select; +pub mod print_in_cli; diff --git a/src/cli_out/print_in_cli.rs b/src/cli_out/print_in_cli.rs index e1f56c1..85ee5be 100644 --- a/src/cli_out/print_in_cli.rs +++ b/src/cli_out/print_in_cli.rs @@ -13,6 +13,8 @@ pub struct PrintError { enum PrintErrorKind { /// Github response was wrong BadResponse, + /// Trying get pull request data from issue command + NotIssue, } impl Error for PrintError {} @@ -27,6 +29,12 @@ impl fmt::Display for PrintError { &self.description ) } + PrintErrorKind::NotIssue => { + write!( + f, + "you tried get pull request from issue command.\n Please use pull request commands" + ) + } } } } @@ -58,6 +66,9 @@ pub fn print_issues(list_issues: Vec, state: IssuesListStates, numb ); println!(); for issue in list_issues { + if issue.pull_request.is_some() { + continue; + } println!("╭────────────────────────────────────────────────────────────────────────────────────────────────"); println!(" Issue {}: {};", issue.number, issue.title); println!(" Body: {}", issue.body); @@ -65,17 +76,21 @@ pub fn print_issues(list_issues: Vec, state: IssuesListStates, numb for label in issue.labels { println!(" {}: {}", label.name, label.description); } - match issue.created_at { - Some(time) => { - println!(" Created at: {}", time); - } - None => {} + + if let Some(time) = issue.created_at { + println!(" Created at: {time}"); }; println!("╰────────────────────────────────────────────────────────────────────────────────────────────────"); } } -pub fn print_simple_issue(issue: IssueSimple) { +pub fn print_simple_issue(issue: IssueSimple) -> Result<(), Box> { + if issue.pull_request.is_some() { + return Err(Box::new(PrintError { + kind: PrintErrorKind::NotIssue, + description: "Trying get pull request from issue command".to_string(), + })); + } println!("╭────────────────────────────────────────────────────────────────────────────────────────────────"); println!(" Issue {}: {};", issue.number, issue.title); println!(" State: {}", issue.state); @@ -84,36 +99,38 @@ pub fn print_simple_issue(issue: IssueSimple) { for label in issue.labels { println!(" {}: {}", label.name, label.description); } - match issue.created_at { - Some(time) => { - println!(" Created at: {}", time); - } - None => {} + + if let Some(time) = issue.created_at { + println!(" Created at: {time}"); }; println!("╰────────────────────────────────────────────────────────────────────────────────────────────────"); + Ok(()) } -pub fn print_issue(issue: Issue) { +pub fn print_issue(issue: Issue) -> Result<(), Box> { + if issue.pull_request.is_some() { + return Err(Box::new(PrintError { + kind: PrintErrorKind::NotIssue, + description: "Trying get pull request from issue command".to_string(), + })); + } + println!("╭────────────────────────────────────────────────────────────────────────────────────────────────"); println!(" Issue {}: {};", issue.number, issue.title); println!(" State: {}", issue.state); println!(" Body: {}", issue.body); println!(" labels:"); for label in issue.labels { - match label.labels_data() { - Some(data) => { - println!(" {}: {}", data.name, data.description); - } - None => {} + if let Some(data) = label.labels_data() { + println!(" {}: {}", data.name, data.description); } } - match issue.created_at { - Some(time) => { - println!(" Created at: {}", time); - } - None => {} + + if let Some(time) = issue.created_at { + println!(" Created at: {time}"); }; println!("╰────────────────────────────────────────────────────────────────────────────────────────────────"); + Ok(()) } pub fn print_url(result: String, description: &str) { diff --git a/src/cli_parse/entities.rs b/src/cli_parse/entities.rs new file mode 100644 index 0000000..8f6dc52 --- /dev/null +++ b/src/cli_parse/entities.rs @@ -0,0 +1,52 @@ +use crate::cli_in::set_vars::{IssuesListStates, States}; + +pub struct ListIssueArgs { + pub creator: String, + pub assignee: String, + pub state: IssuesListStates, + pub labels: String, + pub numb_of_page: i64, + pub iss_on_page: i64, +} + +pub struct UpdateIssueArgs { + pub title: Option, + pub body: Option, + pub number: i64, + pub state: States, +} + +pub struct CreateRepoArgs { + pub allow_auto_merge: Option, + pub allow_merge_commit: Option, + pub allow_rebase_merge: Option, + pub allow_squash_merge: Option, + pub auto_init: Option, + pub delete_branch_on_merge: Option, + pub has_issues: Option, + pub has_projects: Option, + pub has_wiki: Option, + pub is_template: Option, + pub private: Option, + pub description: String, + pub gitignore_template: String, + pub homepage: String, + pub license_template: String, + pub name: String, +} + +pub struct CreateRepoFromTemplateArgs { + pub description: String, + pub include_all_branches: Option, + pub private: Option, +} + +pub struct CreateReleaseArgs { + pub body: String, + pub name: String, + pub discussion_category_name: String, + pub tag_name: String, + pub draft: Option, + pub prerelease: Option, + pub target_commitish: String, +} diff --git a/src/cli_parse/handle_cli.rs b/src/cli_parse/handle_cli.rs index 85cd147..aa33bab 100644 --- a/src/cli_parse/handle_cli.rs +++ b/src/cli_parse/handle_cli.rs @@ -13,22 +13,22 @@ pub async fn handle_cli_command(args: Args, github_client: Client) -> Result<(), match args.command { CliCommand::Issue { subcommand } => { handle_issue_command(github_client, subcommand).await?; - Ok(()) + Ok(()) } CliCommand::Comment { subcommand } => { handle_comment_command(github_client, subcommand).await?; - Ok(()) + Ok(()) } CliCommand::Repo { subcommand } => { handle_repo_command(github_client, subcommand).await?; - Ok(()) + Ok(()) } CliCommand::Release { subcommand } => { handle_release_command(github_client, subcommand).await?; - Ok(()) + Ok(()) } } } diff --git a/src/cli_parse/handle_commands/handle_comment.rs b/src/cli_parse/handle_commands/handle_comment.rs index 952e010..7ba1ef2 100644 --- a/src/cli_parse/handle_commands/handle_comment.rs +++ b/src/cli_parse/handle_commands/handle_comment.rs @@ -42,6 +42,17 @@ pub async fn handle_comment_command( handle_get_all_from_review(github_client, owner, repo, number, sort, order).await?; Ok(()) } + + CommentCommand::Update { + owner, + repo, + comment_id, + body, + } => { + handle_update(github_client, owner, repo, comment_id, body).await?; + + Ok(()) + } } } @@ -116,3 +127,20 @@ async fn handle_get_all_from_review( print_review_comments(result)?; Ok(()) } + +async fn handle_update( + github_client: Client, + owner: Option, + repo: Option, + comment_id: i64, + body: String, +) -> Result<(), Box> { + let repo_info = match owner { + Some(_) => RepoInfo::new(Repo::Input, owner, repo)?, + None => RepoInfo::new(Repo::Current, None, None)?, + }; + let result = comments::update(&github_client, &repo_info, &comment_id, &body).await?; + + println!("{result}"); + Ok(()) +} diff --git a/src/cli_parse/handle_commands/handle_issue.rs b/src/cli_parse/handle_commands/handle_issue.rs index a7679d1..59d2b67 100644 --- a/src/cli_parse/handle_commands/handle_issue.rs +++ b/src/cli_parse/handle_commands/handle_issue.rs @@ -2,13 +2,13 @@ use octorust::{self, Client}; use std::error::Error; use crate::cli_in::issue_command::IssueCommand; -use crate::cli_in::set_vars::IssuesListStates; -use crate::cli_in::set_vars::States; use crate::cli_out::fuzzy_select::choose_issue; use crate::cli_out::print_in_cli::print_comments; use crate::cli_out::print_in_cli::print_issue; use crate::cli_out::print_in_cli::print_issues; use crate::cli_out::print_in_cli::print_simple_issue; +use crate::cli_parse::entities::ListIssueArgs; +use crate::cli_parse::entities::UpdateIssueArgs; use crate::git_utils::comments; use crate::git_utils::issues; use crate::git_utils::repo_info::Repo; @@ -30,18 +30,15 @@ pub async fn handle_issue_command( numb_of_page, iss_on_page, } => { - handle_list( - github_client, - owner, - repo, + let command_args = ListIssueArgs { creator, assignee, state, labels, numb_of_page, iss_on_page, - ) - .await?; + }; + handle_list(github_client, owner, repo, command_args).await?; Ok(()) } @@ -64,18 +61,15 @@ pub async fn handle_issue_command( numb_of_page, iss_on_page, } => { - handle_get_form_list( - github_client, - owner, - repo, + let command_args = ListIssueArgs { creator, assignee, state, labels, numb_of_page, iss_on_page, - ) - .await?; + }; + handle_get_form_list(github_client, owner, repo, command_args).await?; Ok(()) } @@ -111,18 +105,13 @@ pub async fn handle_issue_command( state, labels, } => { - handle_update( - github_client, - owner, - repo, + let command_args = UpdateIssueArgs { title, body, number, - labels, - assignees, state, - ) - .await?; + }; + handle_update(github_client, owner, repo, labels, assignees, command_args).await?; Ok(()) } } @@ -132,31 +121,16 @@ async fn handle_list( github_client: Client, owner: Option, repo: Option, - creator: String, - assignee: String, - state: IssuesListStates, - labels: String, - numb_of_page: i64, - iss_on_page: i64, + command_args: ListIssueArgs, ) -> Result<(), Box> { let repo_info = match owner { Some(_) => RepoInfo::new(Repo::Input, owner, repo)?, None => RepoInfo::new(Repo::Current, None, None)?, }; - let list_issues = issues::get_list( - &github_client, - &repo_info, - &creator, - &assignee, - &state.0, - &labels, - &numb_of_page, - &iss_on_page, - ) - .await?; + let list_issues = issues::get_list(&github_client, &repo_info, &command_args).await?; - print_issues(list_issues, state, numb_of_page); + print_issues(list_issues, command_args.state, command_args.numb_of_page); Ok(()) } @@ -175,7 +149,7 @@ async fn handle_get( let list_comments = comments::get_all(&github_client, &repo_info, &number).await?; - print_issue(result); + print_issue(result)?; print_comments(list_comments)?; Ok(()) } @@ -184,36 +158,21 @@ async fn handle_get_form_list( github_client: Client, owner: Option, repo: Option, - creator: String, - assignee: String, - state: IssuesListStates, - labels: String, - numb_of_page: i64, - iss_on_page: i64, + command_args: ListIssueArgs, ) -> Result<(), Box> { let repo_info = match owner { Some(_) => RepoInfo::new(Repo::Input, owner, repo)?, None => RepoInfo::new(Repo::Current, None, None)?, }; - let list_issues = issues::get_list( - &github_client, - &repo_info, - &creator, - &assignee, - &state.0, - &labels, - &numb_of_page, - &iss_on_page, - ) - .await?; + let list_issues = issues::get_list(&github_client, &repo_info, &command_args).await?; let choosed_issue = choose_issue(list_issues)?; if let Some(ch_i) = choosed_issue { let list_comments = comments::get_all(&github_client, &repo_info, &ch_i.number).await?; - print_simple_issue(ch_i); + print_simple_issue(ch_i)?; print_comments(list_comments)?; } else { println!("Issue not choosed or not find"); @@ -275,12 +234,9 @@ async fn handle_update( github_client: Client, owner: Option, repo: Option, - title: Option, - body: Option, - number: i64, labels: Option, assignees: Option, - state: States, + command_args: UpdateIssueArgs, ) -> Result<(), Box> { let repo_info = match owner { Some(_) => RepoInfo::new(Repo::Input, owner, repo)?, @@ -299,12 +255,9 @@ async fn handle_update( let result = issues::update( &github_client, repo_info, - &number, - title, - body, + command_args, &assignees_list, &labels_list, - &state.0, ) .await?; diff --git a/src/cli_parse/handle_commands/handle_release.rs b/src/cli_parse/handle_commands/handle_release.rs index a62a598..e8caa9a 100644 --- a/src/cli_parse/handle_commands/handle_release.rs +++ b/src/cli_parse/handle_commands/handle_release.rs @@ -4,6 +4,7 @@ use std::error::Error; use crate::cli_in::release_command::ReleaseCommand; use crate::cli_out::print_in_cli::print_release; use crate::cli_out::print_in_cli::print_url; +use crate::cli_parse::entities::CreateReleaseArgs; use crate::git_utils::releases; use crate::git_utils::repo_info::Repo; use crate::git_utils::repo_info::RepoInfo; @@ -26,19 +27,16 @@ pub async fn handle_release_command( tag_name, target_commitish, } => { - handle_create( - github_client, - owner, - repo, + let command_args = CreateReleaseArgs { body, - discussion_category_name, name, - draft, - prerelease, tag_name, target_commitish, - ) - .await?; + discussion_category_name, + draft, + prerelease, + }; + handle_create(github_client, owner, repo, command_args).await?; Ok(()) } @@ -63,28 +61,11 @@ async fn handle_create( github_client: Client, owner: RepoOwner, repo: RepoName, - body: String, - discussion_category_name: String, - name: String, - draft: Option, - prerelease: Option, - tag_name: String, - target_commitish: String, + command_args: CreateReleaseArgs, ) -> Result<(), Box> { let repo_info: RepoInfo = RepoInfo::new(Repo::Input, Some(owner), Some(repo))?; - let result = releases::create( - &github_client, - repo_info, - body, - discussion_category_name, - draft, - &name, - prerelease, - &tag_name, - target_commitish, - ) - .await?; + let result = releases::create(&github_client, repo_info, command_args).await?; print_url(result, "New release"); Ok(()) diff --git a/src/cli_parse/handle_commands/handle_repo.rs b/src/cli_parse/handle_commands/handle_repo.rs index 5c2f0d3..e83fd92 100644 --- a/src/cli_parse/handle_commands/handle_repo.rs +++ b/src/cli_parse/handle_commands/handle_repo.rs @@ -9,6 +9,8 @@ use crate::cli_in::set_vars::ReposListUserTypes; use crate::cli_in::set_vars::Visibilities; use crate::cli_out::print_in_cli::print_repos; use crate::cli_out::print_in_cli::print_url; +use crate::cli_parse::entities::CreateRepoArgs; +use crate::cli_parse::entities::CreateRepoFromTemplateArgs; use crate::git_utils::repo_info::Repo; use crate::git_utils::repo_info::RepoInfo; use crate::git_utils::repo_info::{RepoName, RepoOwner}; @@ -37,26 +39,26 @@ pub async fn handle_repo_command( name, private, } => { - handle_create_for_auth_user( - github_client, + let command_args = CreateRepoArgs { allow_auto_merge, allow_merge_commit, allow_rebase_merge, allow_squash_merge, auto_init, delete_branch_on_merge, - description, - gitignore_template, has_issues, has_projects, has_wiki, - homepage, is_template, + private, + description, + gitignore_template, + homepage, license_template, name, - private, - ) - .await?; + }; + + handle_create_for_auth_user(github_client, command_args).await?; Ok(()) } @@ -80,22 +82,28 @@ pub async fn handle_repo_command( team_name, visibility, } => { - handle_create_in_org( - github_client, + let command_args = CreateRepoArgs { allow_auto_merge, allow_merge_commit, allow_rebase_merge, allow_squash_merge, auto_init, delete_branch_on_merge, - description, - gitignore_template, has_issues, has_projects, has_wiki, - homepage, is_template, + private: None, + description, + gitignore_template, + homepage, license_template, + name: String::new(), + }; + + handle_create_in_org( + github_client, + command_args, name, org, team_name, @@ -124,15 +132,18 @@ pub async fn handle_repo_command( private, include_all_branches, } => { + let command_args = CreateRepoFromTemplateArgs { + description, + private, + include_all_branches, + }; handle_create_using_template( github_client, owner, name, template_owner, template_name, - description, - include_all_branches, - private, + command_args, ) .await?; Ok(()) @@ -157,43 +168,9 @@ pub async fn handle_repo_command( async fn handle_create_for_auth_user( github_client: Client, - allow_auto_merge: Option, - allow_merge_commit: Option, - allow_rebase_merge: Option, - allow_squash_merge: Option, - auto_init: Option, - delete_branch_on_merge: Option, - description: String, - gitignore_template: String, - has_issues: Option, - has_projects: Option, - has_wiki: Option, - homepage: String, - is_template: Option, - license_template: String, - name: String, - private: Option, + command_args: CreateRepoArgs, ) -> Result<(), Box> { - let result = repos::create_for_authenticated_user( - &github_client, - allow_auto_merge, - allow_merge_commit, - allow_rebase_merge, - allow_squash_merge, - auto_init, - delete_branch_on_merge, - &description, - &gitignore_template, - has_issues, - has_projects, - has_wiki, - &homepage, - is_template, - &license_template, - &name, - private, - ) - .await?; + let result = repos::create_for_authenticated_user(&github_client, command_args).await?; println!("{result}"); Ok(()) @@ -201,20 +178,7 @@ async fn handle_create_for_auth_user( async fn handle_create_in_org( github_client: Client, - allow_auto_merge: Option, - allow_merge_commit: Option, - allow_rebase_merge: Option, - allow_squash_merge: Option, - auto_init: Option, - delete_branch_on_merge: Option, - description: String, - gitignore_template: String, - has_issues: Option, - has_projects: Option, - has_wiki: Option, - homepage: String, - is_template: Option, - license_template: String, + command_args: CreateRepoArgs, name: RepoName, org: RepoOwner, team_name: String, @@ -225,20 +189,7 @@ async fn handle_create_in_org( let result = repos::create_in_org( &github_client, repo_info, - allow_auto_merge, - allow_merge_commit, - allow_rebase_merge, - allow_squash_merge, - auto_init, - delete_branch_on_merge, - &description, - &gitignore_template, - has_issues, - has_projects, - has_wiki, - &homepage, - is_template, - &license_template, + command_args, &team_name, Some(visibility.0), ) @@ -268,23 +219,15 @@ async fn handle_create_using_template( name: RepoName, template_owner: RepoOwner, template_name: RepoName, - description: String, - include_all_branches: Option, - private: Option, + command_args: CreateRepoFromTemplateArgs, ) -> Result<(), Box> { let repo_info: RepoInfo = RepoInfo::new(Repo::Input, Some(owner), Some(name))?; let template_info: RepoInfo = RepoInfo::new(Repo::Input, Some(template_owner), Some(template_name))?; - let result = repos::create_using_template( - &github_client, - template_info, - repo_info, - &description, - include_all_branches, - private, - ) - .await?; + let result = + repos::create_using_template(&github_client, template_info, repo_info, command_args) + .await?; print_url(result, "New repo"); Ok(()) diff --git a/src/cli_parse/mod.rs b/src/cli_parse/mod.rs index 1087c8a..08f2cd4 100644 --- a/src/cli_parse/mod.rs +++ b/src/cli_parse/mod.rs @@ -1,2 +1,3 @@ +pub mod entities; pub mod handle_cli; mod handle_commands; diff --git a/src/git_utils/comments.rs b/src/git_utils/comments.rs index bb12cba..08533d2 100644 --- a/src/git_utils/comments.rs +++ b/src/git_utils/comments.rs @@ -11,24 +11,26 @@ pub async fn create( github_client: &Client, repo_info: &RepoInfo, issue_number: &i64, - body: &String, + body: &str, ) -> Result> { - let request = PullsUpdateReviewRequest { body: body.clone() }; + let request = PullsUpdateReviewRequest { + body: body.to_owned(), + }; let comment = github_client .issues() .create_comment( - &repo_info.get_owner().trim(), - &repo_info.get_name().trim(), - issue_number.clone(), + &repo_info.get_owner(), + &repo_info.get_name(), + *issue_number, &request, ) .await; - return match comment { + match comment { Ok(_) => Ok("Comment create successed".to_string()), Err(er) => Err(Box::new(er)), - }; + } } // Get all Comments for issue/pull request without review comments @@ -39,18 +41,13 @@ pub async fn get_all( ) -> Result, Box> { let list_comments = github_client .issues() - .list_all_comments( - &repo_info.get_owner(), - &repo_info.get_name(), - number.clone(), - None, - ) + .list_all_comments(&repo_info.get_owner(), &repo_info.get_name(), *number, None) .await; - return match list_comments { + match list_comments { Ok(c) => Ok(c.body), Err(er) => Err(Box::new(er)), - }; + } } // Get all review Comments for pull request @@ -66,15 +63,41 @@ pub async fn get_all_from_review( .list_all_review_comments( &repo_info.get_owner(), &repo_info.get_name(), - number.clone(), + *number, sort, direction, None, ) .await; - return match list_comments { + match list_comments { Ok(c) => Ok(c.body), Err(er) => Err(Box::new(er)), + } +} + +pub async fn update( + github_client: &Client, + repo_info: &RepoInfo, + comment_id: &i64, + body: &str, +) -> Result> { + let request = PullsUpdateReviewRequest { + body: body.to_owned(), }; + + let comment = github_client + .issues() + .update_comment( + &repo_info.get_owner(), + &repo_info.get_name(), + *comment_id, + &request, + ) + .await; + + match comment { + Ok(_) => Ok("Comment update successed".to_string()), + Err(er) => Err(Box::new(er)), + } } diff --git a/src/git_utils/issues.rs b/src/git_utils/issues.rs index 31fa38c..f2a7408 100644 --- a/src/git_utils/issues.rs +++ b/src/git_utils/issues.rs @@ -6,6 +6,7 @@ use octorust::types::{ }; use octorust::Client; +use crate::cli_parse::entities::{ListIssueArgs, UpdateIssueArgs}; use crate::git_utils::comments; use crate::git_utils::repo_info::RepoInfo; @@ -25,61 +26,56 @@ pub async fn get( pub async fn get_list( github_client: &Client, repo_info: &RepoInfo, - creator: &String, - assignee: &String, - state: &types::IssuesListState, - labels: &String, - numb_of_page: &i64, - iss_on_page: &i64, + command_args: &ListIssueArgs, ) -> Result, Box> { let sort = types::IssuesListSort::Created; let issues = github_client .issues() .list_for_repo( - &repo_info.get_owner().trim(), - &repo_info.get_name().trim(), + &repo_info.get_owner(), + &repo_info.get_name(), "", - state.clone(), - assignee, - creator, + command_args.state.0.to_owned(), + &command_args.assignee, + &command_args.creator, "", - labels, + &command_args.labels, sort, types::Order::Noop, None, - iss_on_page.clone(), - numb_of_page.clone(), + command_args.iss_on_page, + command_args.numb_of_page, ) .await; - return match issues { + match issues { Ok(info) => Ok(info.body), Err(er) => Err(Box::new(er)), - }; + } } fn get_create_request( - title: &String, - body: &String, - assignees: &Vec, - labels: &Vec, + title: &str, + body: &str, + assignees: &[String], + labels: &[String], ) -> IssuesCreateRequest { - let new_title = TitleOneOf::String(title.clone()); + let new_title = TitleOneOf::String(title.to_owned()); let new_labels = labels - .into_iter() + .iter() .map(|s| IssuesCreateRequestLabelsOneOf::String(s.into())) .collect(); IssuesCreateRequest { title: new_title, - body: body.clone(), + body: body.to_owned(), assignee: String::new(), assignees: if assignees.is_empty() { Vec::new() } else { - assignees.clone() + assignees.to_owned() }, labels: new_labels, milestone: None, @@ -89,39 +85,32 @@ fn get_create_request( pub async fn create( github_client: &Client, repo_info: RepoInfo, - title: &String, - body: &String, - assignees: &Vec, - labels: &Vec, + title: &str, + body: &str, + assignees: &[String], + labels: &[String], ) -> Result> { let request = get_create_request(title, body, assignees, labels); let new_issue = github_client .issues() - .create( - &repo_info.get_owner().trim(), - &repo_info.get_name().trim(), - &request, - ) + .create(&repo_info.get_owner(), &repo_info.get_name(), &request) .await; - return match new_issue { + match new_issue { Ok(_) => Ok("Success".to_string()), Err(er) => Err(Box::new(er)), - }; + } } fn get_update_request( title: Option, body: Option, - assignees: Option<&Vec>, - labels: Option<&Vec>, + assignees: Option<&[String]>, + labels: Option<&[String]>, state: &State, ) -> IssuesUpdateRequest { - let new_title = match title { - Some(t) => Some(TitleOneOf::String(t.to_string())), - None => None, - }; + let new_title = title.map(|t| TitleOneOf::String(t.to_string())); let new_labels = match labels { Some(l) => l @@ -146,9 +135,9 @@ pub async fn close( github_client: &Client, repo_info: RepoInfo, issue_number: &i64, - comment: &String, + comment: &str, ) -> Result> { - if comment != "" { + if comment.is_empty() { let new_comment = comments::create(github_client, &repo_info, issue_number, comment).await?; println!("{new_comment}"); @@ -159,43 +148,46 @@ pub async fn close( let close = github_client .issues() .update( - &repo_info.get_owner().trim(), - &repo_info.get_name().trim(), - issue_number.clone(), + &repo_info.get_owner(), + &repo_info.get_name(), + *issue_number, &request, ) .await; - return match close { + match close { Ok(_) => Ok("Success".to_string()), Err(er) => Err(Box::new(er)), - }; + } } pub async fn update( github_client: &Client, repo_info: RepoInfo, - issue_number: &i64, - title: Option, - body: Option, - assignees: &Vec, - labels: &Vec, - state: &State, + command_args: UpdateIssueArgs, + assignees: &[String], + labels: &[String], ) -> Result> { - let request = get_update_request(title, body, Some(assignees), Some(labels), state); + let request = get_update_request( + command_args.title, + command_args.body, + Some(assignees), + Some(labels), + &command_args.state.0, + ); let update_iss = github_client .issues() .update( - &repo_info.get_owner().trim(), - &repo_info.get_name().trim(), - issue_number.clone(), + &repo_info.get_owner(), + &repo_info.get_name(), + command_args.number, &request, ) .await; - return match update_iss { + match update_iss { Ok(_) => Ok("Success".to_string()), Err(er) => Err(Box::new(er)), - }; + } } diff --git a/src/git_utils/releases.rs b/src/git_utils/releases.rs index 9b5f9c7..e7fd35e 100644 --- a/src/git_utils/releases.rs +++ b/src/git_utils/releases.rs @@ -5,42 +5,32 @@ use octorust::{ Client, }; -use crate::git_utils::repo_info::RepoInfo; +use crate::{cli_parse::entities::CreateReleaseArgs, git_utils::repo_info::RepoInfo}; pub async fn create( github_client: &Client, repo_info: RepoInfo, - body: String, - discussion_category_name: String, - draft: Option, - name: &String, - prerelease: Option, - tag_name: &String, - target_commitish: String, + command_args: CreateReleaseArgs, ) -> Result> { let request = ReposCreateReleaseRequest { - body: body, - discussion_category_name: discussion_category_name, - draft: draft, - name: name.clone(), - prerelease: prerelease, - tag_name: tag_name.clone(), - target_commitish: target_commitish, + body: command_args.body, + discussion_category_name: command_args.discussion_category_name, + draft: command_args.draft, + name: command_args.name, + prerelease: command_args.prerelease, + tag_name: command_args.tag_name, + target_commitish: command_args.target_commitish.to_owned(), }; let result = github_client .repos() - .create_release( - repo_info.get_owner().trim(), - repo_info.get_name().trim(), - &request, - ) + .create_release(&repo_info.get_owner(), &repo_info.get_name(), &request) .await; - return match result { - Ok(_) => Ok(repo_info.get_release_url(tag_name)), + match result { + Ok(_) => Ok(repo_info.get_release_url(&command_args.target_commitish)), Err(er) => Err(Box::new(er)), - }; + } } pub async fn get_latest( @@ -49,13 +39,13 @@ pub async fn get_latest( ) -> Result> { let result = github_client .repos() - .get_latest_release(&repo_info.get_owner().trim(), &repo_info.get_name().trim()) + .get_latest_release(&repo_info.get_owner(), &repo_info.get_name()) .await; - return match result { + match result { Ok(r) => Ok(r.body), Err(er) => Err(Box::new(er)), - }; + } } pub async fn get_by_tag( @@ -65,17 +55,13 @@ pub async fn get_by_tag( ) -> Result> { let result = github_client .repos() - .get_release_by_tag( - &repo_info.get_owner().trim(), - &repo_info.get_name().trim(), - &tag, - ) + .get_release_by_tag(&repo_info.get_owner(), &repo_info.get_name(), &tag) .await; - return match result { + match result { Ok(r) => Ok(r.body), Err(er) => Err(Box::new(er)), - }; + } } pub async fn get_by_id( @@ -85,15 +71,11 @@ pub async fn get_by_id( ) -> Result> { let result = github_client .repos() - .get_release( - &repo_info.get_owner().trim(), - &repo_info.get_name().trim(), - id, - ) + .get_release(&repo_info.get_owner(), &repo_info.get_name(), id) .await; - return match result { + match result { Ok(r) => Ok(r.body), Err(er) => Err(Box::new(er)), - }; + } } diff --git a/src/git_utils/repo_info.rs b/src/git_utils/repo_info.rs index 4693151..a7911df 100644 --- a/src/git_utils/repo_info.rs +++ b/src/git_utils/repo_info.rs @@ -16,13 +16,13 @@ pub struct RepoSsh(String); impl RepoOwner { pub fn trim(&self) -> &str { - return self.0.trim(); + self.0.trim() } } impl RepoName { pub fn trim(&self) -> &str { - return self.0.trim(); + self.0.trim() } } @@ -84,12 +84,12 @@ impl RepoInfo { if parts.len() > 2 { let owner = parts[parts.len() - 2]; let repo = parts[parts.len() - 1]; - return Ok((RepoOwner(owner.to_string()), RepoName(repo.to_string()))); + Ok((RepoOwner(owner.to_string()), RepoName(repo.to_string()))) } else { - return Err(io::Error::new( + Err(io::Error::new( io::ErrorKind::InvalidData, "Can't parse string", - )); + )) } } @@ -97,13 +97,12 @@ impl RepoInfo { fn get_current_repo(mut self) -> Result { // Creating a command to search for a link to a remote repository let git_link = Command::new("git") - .args(&["remote", "get-url", "origin"]) + .args(["remote", "get-url", "origin"]) .output()?; // Git repository was not found in this directory if !git_link.status.success() { - return Err(io::Error::new( - io::ErrorKind::Other, + return Err(io::Error::other( "Git repository not found in this directory", )); } @@ -116,7 +115,7 @@ impl RepoInfo { Self::set_url(&mut self); Self::set_ssh(&mut self); - return Ok(self); + Ok(self) } fn set_url(&mut self) { @@ -136,22 +135,22 @@ impl RepoInfo { } pub fn get_owner(&self) -> String { - return self.owner.clone().0; + self.owner.clone().0 } pub fn get_name(&self) -> String { - return self.name.clone().0; + self.name.clone().0 } - pub fn get_release_url(&self, tag: &String) -> String { + pub fn get_release_url(&self, tag: &str) -> String { let mut release_url = self.url.clone(); release_url.push_str("releases/tag/"); release_url.push_str(tag.trim()); - return release_url.0; + release_url.0 } pub fn get_ssh(&self) -> String { - return self.ssh.clone().0; + self.ssh.clone().0 } fn create_repo_info( @@ -159,15 +158,15 @@ impl RepoInfo { name: Option, ) -> Result { if owner.is_none() { - return Err(io::Error::new( + Err(io::Error::new( io::ErrorKind::NotFound, "Not found repo owner", - )); + )) } else if name.is_none() { - return Err(io::Error::new( + Err(io::Error::new( io::ErrorKind::NotFound, "Not found repo name", - )); + )) } else { let mut new_repo = RepoInfo { owner: RepoOwner(owner.unwrap().0.trim().to_string()), @@ -178,7 +177,7 @@ impl RepoInfo { Self::set_url(&mut new_repo); Self::set_ssh(&mut new_repo); - return Ok(new_repo); + Ok(new_repo) } } @@ -195,11 +194,9 @@ impl RepoInfo { url: RepoUrl(String::new()), ssh: RepoSsh(String::new()), }; - return Self::get_current_repo(new_repo); - } - Repo::Input => { - return Self::create_repo_info(owner, name); + Self::get_current_repo(new_repo) } + Repo::Input => Self::create_repo_info(owner, name), } } } diff --git a/src/git_utils/repos.rs b/src/git_utils/repos.rs index 5000173..d3511bf 100644 --- a/src/git_utils/repos.rs +++ b/src/git_utils/repos.rs @@ -9,45 +9,31 @@ use octorust::{ Client, }; +use crate::cli_parse::entities::{CreateRepoArgs, CreateRepoFromTemplateArgs}; use crate::git_utils::{repo_info::RepoInfo, teams::get_id}; pub async fn create_for_authenticated_user( github_client: &Client, - allow_auto_merge: Option, - allow_merge_commit: Option, - allow_rebase_merge: Option, - allow_squash_merge: Option, - auto_init: Option, - delete_branch_on_merge: Option, - description: &String, - gitignore_template: &String, - has_issues: Option, - has_projects: Option, - has_wiki: Option, - homepage: &String, - is_template: Option, - license_template: &String, - name: &String, - private: Option, + command_args: CreateRepoArgs, ) -> Result> { let request = ReposCreateRequest { - allow_auto_merge: allow_auto_merge, - allow_merge_commit: allow_merge_commit, - allow_rebase_merge: allow_rebase_merge, - allow_squash_merge: allow_squash_merge, - auto_init: auto_init, - delete_branch_on_merge: delete_branch_on_merge, - description: description.clone(), - gitignore_template: gitignore_template.clone(), + allow_auto_merge: command_args.allow_auto_merge, + allow_merge_commit: command_args.allow_merge_commit, + allow_rebase_merge: command_args.allow_rebase_merge, + allow_squash_merge: command_args.allow_squash_merge, + auto_init: command_args.auto_init, + delete_branch_on_merge: command_args.delete_branch_on_merge, + description: command_args.description.clone(), + gitignore_template: command_args.gitignore_template.clone(), has_downloads: None, - has_issues: has_issues, - has_projects: has_projects, - has_wiki: has_wiki, - homepage: homepage.clone(), - is_template: is_template, - license_template: license_template.clone(), - name: name.clone(), - private: private, + has_issues: command_args.has_issues, + has_projects: command_args.has_projects, + has_wiki: command_args.has_wiki, + homepage: command_args.homepage.clone(), + is_template: command_args.is_template, + license_template: command_args.license_template.clone(), + name: command_args.name.clone(), + private: command_args.private, team_id: 1, }; @@ -56,55 +42,42 @@ pub async fn create_for_authenticated_user( .create_for_authenticated_user(&request) .await; - return match new_repo { + match new_repo { Ok(_) => Ok("Success".to_string()), Err(er) => Err(Box::new(er)), - }; + } } pub async fn create_in_org( github_client: &Client, repo_info: RepoInfo, - allow_auto_merge: Option, - allow_merge_commit: Option, - allow_rebase_merge: Option, - allow_squash_merge: Option, - auto_init: Option, - delete_branch_on_merge: Option, - description: &String, - gitignore_template: &String, - has_issues: Option, - has_projects: Option, - has_wiki: Option, - homepage: &String, - is_template: Option, - license_template: &String, - team_name: &String, + command_args: CreateRepoArgs, + team_name: &str, visibility: Option, ) -> Result> { - let team = get_id(github_client, &repo_info.get_owner(), team_name).await; + let team = get_id(github_client, &repo_info.get_owner(), team_name).await?; let team_id = team.id; let request = ReposCreateInOrgRequest { - allow_auto_merge: allow_auto_merge, - allow_merge_commit: allow_merge_commit, - allow_rebase_merge: allow_rebase_merge, - allow_squash_merge: allow_squash_merge, - auto_init: auto_init, - delete_branch_on_merge: delete_branch_on_merge, - description: description.clone(), - gitignore_template: gitignore_template.clone(), - has_issues: has_issues, - has_projects: has_projects, - has_wiki: has_wiki, - homepage: homepage.clone(), - is_template: is_template, - license_template: license_template.clone(), + allow_auto_merge: command_args.allow_auto_merge, + allow_merge_commit: command_args.allow_merge_commit, + allow_rebase_merge: command_args.allow_rebase_merge, + allow_squash_merge: command_args.allow_squash_merge, + auto_init: command_args.auto_init, + delete_branch_on_merge: command_args.delete_branch_on_merge, + description: command_args.description.clone(), + gitignore_template: command_args.gitignore_template.clone(), + has_issues: command_args.has_issues, + has_projects: command_args.has_projects, + has_wiki: command_args.has_wiki, + homepage: command_args.homepage.clone(), + is_template: command_args.is_template, + license_template: command_args.license_template.clone(), name: repo_info.get_name(), private: None, - team_id: team_id, - visibility: visibility, + team_id, + visibility, }; let new_repo = github_client @@ -112,15 +85,15 @@ pub async fn create_in_org( .create_in_org(repo_info.get_owner().trim(), &request) .await; - return match new_repo { + match new_repo { Ok(_) => Ok(repo_info.get_ssh()), Err(er) => Err(Box::new(er)), - }; + } } pub async fn get_all_from_org( github_client: &Client, - org: &String, + org: &str, order: Order, type_value: ReposListOrgType, sort_value: ReposListOrgSort, @@ -130,65 +103,59 @@ pub async fn get_all_from_org( .list_all_for_org(org.trim(), type_value, sort_value, order) .await; - return match all_repos { + match all_repos { Ok(r) => Ok(r.body), Err(er) => Err(Box::new(er)), - }; + } } pub async fn create_using_template( github_client: &Client, template_info: RepoInfo, repo_info: RepoInfo, - description: &String, - include_all_branches: Option, - private: Option, + command_args: CreateRepoFromTemplateArgs, ) -> Result> { let request = ReposCreateUsingTemplateRequest { - description: description.clone(), - include_all_branches: include_all_branches, + description: command_args.description, + include_all_branches: command_args.include_all_branches, name: repo_info.get_name(), owner: repo_info.get_owner(), - private: private, + private: command_args.private, }; let new_repo = github_client .repos() .create_using_template( - &template_info.get_owner().trim(), - &template_info.get_name().trim(), + &template_info.get_owner(), + &template_info.get_name(), &request, ) .await; - return match new_repo { + match new_repo { Ok(_) => Ok(repo_info.get_ssh()), Err(er) => Err(Box::new(er)), - }; + } } pub async fn create_fork( github_client: &Client, - org: &String, + org: &str, fork_info: RepoInfo, ) -> Result> { let request = ReposCreateForkRequest { - organization: org.clone(), + organization: org.to_owned(), }; let new_fork = github_client .repos() - .create_fork( - &fork_info.get_owner().trim(), - &fork_info.get_name().trim(), - &request, - ) + .create_fork(&fork_info.get_owner(), &fork_info.get_name(), &request) .await; - return match new_fork { + match new_fork { Ok(_) => Ok("Success".to_string()), Err(er) => Err(Box::new(er)), - }; + } } pub async fn get_all_from_user( @@ -203,8 +170,8 @@ pub async fn get_all_from_user( .list_all_for_user(owner.trim(), type_value, sort_value, order) .await; - return match all_repos { + match all_repos { Ok(reps) => Ok(reps.body), Err(er) => Err(Box::new(er)), - }; + } } diff --git a/src/git_utils/teams.rs b/src/git_utils/teams.rs index c7a7753..3fdbdc9 100644 --- a/src/git_utils/teams.rs +++ b/src/git_utils/teams.rs @@ -1,17 +1,16 @@ -use std::process; +use std::error::Error; use octorust::{types::FullTeam, Client}; -pub async fn get_id(github_client: &Client, org: &String, name: &String) -> FullTeam { - let team = github_client - .teams() - .get_by_name(org.trim(), name.trim()) - .await; - return match team { - Ok(t) => t.body, - Err(message) => { - eprintln!("Error: {message}"); - process::exit(1); - } - }; +pub async fn get_id( + github_client: &Client, + org: &str, + name: &str, +) -> Result> { + let team = github_client.teams().get_by_name(org, name).await; + + match team { + Ok(t) => Ok(t.body), + Err(err) => Err(Box::new(err)), + } }