diff --git a/Cargo.lock b/Cargo.lock index cdce842..a87c0fb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -412,7 +412,7 @@ checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" [[package]] name = "github-cli" -version = "1.10.0" +version = "1.11.0" dependencies = [ "clap", "octorust", @@ -1358,21 +1358,20 @@ dependencies = [ [[package]] name = "rstest" -version = "0.25.0" +version = "0.26.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6fc39292f8613e913f7df8fa892b8944ceb47c247b78e1b1ae2f09e019be789d" +checksum = "f5a3193c063baaa2a95a33f03035c8a72b83d97a54916055ba22d35ed3839d49" dependencies = [ "futures-timer", "futures-util", "rstest_macros", - "rustc_version", ] [[package]] name = "rstest_macros" -version = "0.25.0" +version = "0.26.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1f168d99749d307be9de54d23fd226628d99768225ef08f6ffb52e0182a27746" +checksum = "9c845311f0ff7951c5506121a9ad75aec44d083c31583b2ea5a30bcb0b0abba0" dependencies = [ "cfg-if", "glob", diff --git a/Cargo.toml b/Cargo.toml index d27a1fb..d8abc04 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,11 +1,11 @@ [package] name = "github-cli" -version = "1.10.0" +version = "1.11.0" edition = "2021" [dependencies] octorust = "0.10.0" clap = { version = "4.5.39", features = ["derive"] } tokio = { version = "1.45.1", features = ["full"] } -rstest = "0.25.0" +rstest = "0.26.1" paste = "1.0.15" diff --git a/Readme.md b/Readme.md index 835a018..85210a9 100644 --- a/Readme.md +++ b/Readme.md @@ -53,6 +53,7 @@ github-cli release --help ### comment - [x] Сделать создание комментария в issue/pull request - [x] Сделать получение комментариев для issue/pull request +- [x] Сделать получение review комментариев для pull request - [ ] Сделать редактирование комментария для issue/pull request - [ ] Сделать удаление комментария для issue/pull request diff --git a/src/cli_in/comment_command.rs b/src/cli_in/comment_command.rs index 2b4a72a..051a56a 100644 --- a/src/cli_in/comment_command.rs +++ b/src/cli_in/comment_command.rs @@ -1,6 +1,6 @@ use clap::Subcommand; -use crate::cli_in::set_vars::CommentTarget; +use crate::cli_in::set_vars::{CommentTarget, Orders, Sorts}; #[derive(Subcommand)] pub enum CommentCommand { @@ -23,4 +23,17 @@ pub enum CommentCommand { #[clap(long, short)] target: CommentTarget, }, + + /// Get all comments from issue/pull request + GetAllFromReview { + /// Get all comments from issue/pull request with number + #[clap(long, short)] + number: i64, + /// Can be only 'created' or 'updated' (optional) + #[clap(long, short, default_value = "created")] + sort: Sorts, + /// Can be only 'asc' or 'desc' (optional) + #[clap(long, short, default_value = "desc")] + order: Orders, + }, } diff --git a/src/cli_in/set_vars.rs b/src/cli_in/set_vars.rs index 5832138..d77fa52 100644 --- a/src/cli_in/set_vars.rs +++ b/src/cli_in/set_vars.rs @@ -1,7 +1,9 @@ use std::str::FromStr; -use octorust::types::{IssuesListState, Order, ReposListOrgSort, ReposListUserType, State}; -use octorust::types::{ReposCreateInOrgRequestVisibility, ReposListOrgType}; +use octorust::types::{ + IssuesListState, Order, ReposCreateInOrgRequestVisibility, ReposListOrgSort, ReposListOrgType, + ReposListUserType, Sort, State, +}; #[derive(Debug, Clone)] pub struct Orders(pub Order); @@ -139,17 +141,32 @@ impl ToString for IssuesListStates { #[derive(Debug, Clone)] pub enum CommentTarget { Issue, - PullRequest, + PullRequest, } impl FromStr for CommentTarget { - type Err = String; - - fn from_str(s: &str) -> Result { + type Err = String; + + fn from_str(s: &str) -> Result { match s { "issue" => Ok(CommentTarget::Issue), "pull-request" => Ok(CommentTarget::PullRequest), _ => Err("Bad input. Comment target can be only 'issue' or 'pull-request'".to_string()), } - } + } +} + +#[derive(Debug, Clone)] +pub struct Sorts(pub Sort); + +impl FromStr for Sorts { + type Err = String; + + fn from_str(s: &str) -> Result { + match s { + "created" => Ok(Self(Sort::Created)), + "updated" => Ok(Self(Sort::Updated)), + _ => Err("Bad input. Sort can be only 'created' or 'updated'".to_string()), + } + } } diff --git a/src/cli_out/print_in_cli.rs b/src/cli_out/print_in_cli.rs index 7844d42..bd8cebd 100644 --- a/src/cli_out/print_in_cli.rs +++ b/src/cli_out/print_in_cli.rs @@ -30,6 +30,7 @@ pub fn print_issues(list_issues: Vec, state: IssuesListStates, numb println!("╭────────────────────────────────────────────────────────────────────────────────────────────────"); println!("│Issue {}: {};", issue.number, issue.title); println!("│Body: {}", issue.body); + println!("│Time: {}", issue.timeline_url); println!("╰────────────────────────────────────────────────────────────────────────────────────────────────"); } } diff --git a/src/cli_parse/handle_cli.rs b/src/cli_parse/handle_cli.rs index fcf9b5b..85cd147 100644 --- a/src/cli_parse/handle_cli.rs +++ b/src/cli_parse/handle_cli.rs @@ -1,3 +1,5 @@ +use std::error::Error; + use octorust::{self, Client}; use crate::cli_in::read_cli::Args; @@ -7,22 +9,26 @@ use crate::cli_parse::handle_commands::handle_issue::handle_issue_command; use crate::cli_parse::handle_commands::handle_release::handle_release_command; use crate::cli_parse::handle_commands::handle_repo::handle_repo_command; -pub async fn handle_cli_command(args: Args, github_client: Client) { +pub async fn handle_cli_command(args: Args, github_client: Client) -> Result<(), Box> { match args.command { CliCommand::Issue { subcommand } => { - handle_issue_command(github_client, subcommand).await; + handle_issue_command(github_client, subcommand).await?; + Ok(()) } CliCommand::Comment { subcommand } => { - handle_comment_command(github_client, subcommand).await; + handle_comment_command(github_client, subcommand).await?; + Ok(()) } CliCommand::Repo { subcommand } => { - handle_repo_command(github_client, subcommand).await; + handle_repo_command(github_client, subcommand).await?; + Ok(()) } CliCommand::Release { subcommand } => { - handle_release_command(github_client, subcommand).await; + handle_release_command(github_client, subcommand).await?; + Ok(()) } } } diff --git a/src/cli_parse/handle_commands/handle_comment.rs b/src/cli_parse/handle_commands/handle_comment.rs index 8bc25e4..c76e1f2 100644 --- a/src/cli_parse/handle_commands/handle_comment.rs +++ b/src/cli_parse/handle_commands/handle_comment.rs @@ -1,50 +1,58 @@ use octorust::{self, Client}; -use std::process; -// use std::str::FromStr; +use std::error::Error; use crate::cli_in::comment_command::CommentCommand; -use crate::cli_in::set_vars::CommentTarget; +use crate::cli_in::set_vars::{CommentTarget, Orders, Sorts}; use crate::cli_out::print_in_cli::{print_comments, print_review_comments}; use crate::git_utils::comments; use crate::git_utils::repo_info::{Repo, RepoInfo}; -// use crate::git_utils::repo_info::{RepoName, RepoOwner}; -pub async fn handle_comment_command(github_client: Client, subcommand: CommentCommand) { +pub async fn handle_comment_command( + github_client: Client, + subcommand: CommentCommand, +) -> Result<(), Box> { match subcommand { CommentCommand::Create { number, body } => { - handle_create(github_client, number, body).await; + handle_create(github_client, number, body).await?; + Ok(()) } CommentCommand::GetAll { number, target } => { - handle_get_all(github_client, number, target).await + handle_get_all(github_client, number, target).await?; + Ok(()) } - }; -} -async fn handle_create(github_client: Client, number: i64, body: String) { - let repo_info: RepoInfo = match RepoInfo::new(Repo::Current, None, None) { - Ok(rep) => rep, - Err(message) => { - eprintln!("Error: {message}"); - process::exit(1); + CommentCommand::GetAllFromReview { + number, + sort, + order, + } => { + handle_get_all_from_review(github_client, number, sort, order).await?; + Ok(()) } - }; + } +} - let result = comments::create(&github_client, &repo_info, &number, &body).await; +async fn handle_create( + github_client: Client, + number: i64, + body: String, +) -> Result<(), Box> { + let repo_info: RepoInfo = RepoInfo::new(Repo::Current, None, None)?; + let result = comments::create(&github_client, &repo_info, &number, &body).await?; println!("{result}"); + Ok(()) } -async fn handle_get_all(github_client: Client, number: i64, target: CommentTarget) { - let repo_info: RepoInfo = match RepoInfo::new(Repo::Current, None, None) { - Ok(rep) => rep, - Err(message) => { - eprintln!("Error: {message}"); - process::exit(1); - } - }; +async fn handle_get_all( + github_client: Client, + number: i64, + target: CommentTarget, +) -> Result<(), Box> { + let repo_info: RepoInfo = RepoInfo::new(Repo::Current, None, None)?; - let result = comments::get_all(&github_client, &repo_info, &number).await; + let result = comments::get_all(&github_client, &repo_info, &number).await?; print_comments(result); @@ -57,11 +65,27 @@ async fn handle_get_all(github_client: Client, number: i64, target: CommentTarge octorust::types::Sort::Created, octorust::types::Order::Asc, ) - .await + .await? } CommentTarget::Issue => Vec::new(), }; if !review_comments.is_empty() { print_review_comments(review_comments); } + Ok(()) +} + +async fn handle_get_all_from_review( + github_client: Client, + number: i64, + sort: Sorts, + order: Orders, +) -> Result<(), Box> { + let repo_info: RepoInfo = RepoInfo::new(Repo::Current, None, None)?; + + let result = + comments::get_all_from_review(&github_client, &repo_info, &number, sort.0, order.0).await?; + + print_review_comments(result); + Ok(()) } diff --git a/src/cli_parse/handle_commands/handle_issue.rs b/src/cli_parse/handle_commands/handle_issue.rs index 4588db1..b6a199f 100644 --- a/src/cli_parse/handle_commands/handle_issue.rs +++ b/src/cli_parse/handle_commands/handle_issue.rs @@ -1,5 +1,5 @@ use octorust::{self, Client}; -use std::process; +use std::error::Error; use crate::cli_in::issue_command::IssueCommand; use crate::cli_in::set_vars::IssuesListStates; @@ -9,7 +9,10 @@ use crate::git_utils::issues; use crate::git_utils::repo_info::Repo; use crate::git_utils::repo_info::RepoInfo; -pub async fn handle_issue_command(github_client: Client, subcommand: IssueCommand) { +pub async fn handle_issue_command( + github_client: Client, + subcommand: IssueCommand, +) -> Result<(), Box> { match subcommand { IssueCommand::List { creator, @@ -28,7 +31,8 @@ pub async fn handle_issue_command(github_client: Client, subcommand: IssueComman numb_of_page, iss_on_page, ) - .await; + .await?; + Ok(()) } IssueCommand::Create { @@ -37,11 +41,13 @@ pub async fn handle_issue_command(github_client: Client, subcommand: IssueComman assignees, labels, } => { - handle_create(github_client, title, body, assignees, labels).await; + handle_create(github_client, title, body, assignees, labels).await?; + Ok(()) } IssueCommand::Close { number, comment } => { - handle_close(github_client, number, comment).await; + handle_close(github_client, number, comment).await?; + Ok(()) } IssueCommand::Update { @@ -52,7 +58,8 @@ pub async fn handle_issue_command(github_client: Client, subcommand: IssueComman state, labels, } => { - handle_update(github_client, title, body, number, labels, assignees, state).await; + handle_update(github_client, title, body, number, labels, assignees, state).await?; + Ok(()) } } } @@ -65,14 +72,8 @@ async fn handle_list( labels: String, numb_of_page: i64, iss_on_page: i64, -) { - let repo_info: RepoInfo = match RepoInfo::new(Repo::Current, None, None) { - Ok(rep) => rep, - Err(message) => { - eprintln!("Error: {message}"); - process::exit(1); - } - }; +) -> Result<(), Box> { + let repo_info: RepoInfo = RepoInfo::new(Repo::Current, None, None)?; let list_issues = issues::get_list( &github_client, @@ -84,9 +85,10 @@ async fn handle_list( &numb_of_page, &iss_on_page, ) - .await; + .await?; print_issues(list_issues, state, numb_of_page); + Ok(()) } async fn handle_create( @@ -95,14 +97,9 @@ async fn handle_create( body: String, assignees: String, labels: String, -) { - let repo_info: RepoInfo = match RepoInfo::new(Repo::Current, None, None) { - Ok(rep) => rep, - Err(message) => { - eprintln!("Error: {message}"); - process::exit(1); - } - }; +) -> Result<(), Box> { + let repo_info: RepoInfo = RepoInfo::new(Repo::Current, None, None)?; + let labels_list: Vec = labels.split(",").map(|s| s.to_string()).collect(); let assignees_list: Vec = assignees.split(",").map(|s| s.to_string()).collect(); @@ -114,22 +111,23 @@ async fn handle_create( &assignees_list, &labels_list, ) - .await; + .await?; println!("{result}"); + Ok(()) } -async fn handle_close(github_client: Client, number: i64, comment: String) { - let repo_info: RepoInfo = match RepoInfo::new(Repo::Current, None, None) { - Ok(rep) => rep, - Err(message) => { - eprintln!("Error: {message}"); - process::exit(1); - } - }; - let result = issues::close(&github_client, repo_info, &number, &comment).await; +async fn handle_close( + github_client: Client, + number: i64, + comment: String, +) -> Result<(), Box> { + let repo_info: RepoInfo = RepoInfo::new(Repo::Current, None, None)?; + + let result = issues::close(&github_client, repo_info, &number, &comment).await?; println!("{result}"); + Ok(()) } async fn handle_update( @@ -140,14 +138,8 @@ async fn handle_update( labels: Option, assignees: Option, state: States, -) { - let repo_info: RepoInfo = match RepoInfo::new(Repo::Current, None, None) { - Ok(rep) => rep, - Err(message) => { - eprintln!("Error: {message}"); - process::exit(1); - } - }; +) -> Result<(), Box> { + let repo_info: RepoInfo = RepoInfo::new(Repo::Current, None, None)?; let labels_list: Vec = match labels { Some(l) => l.split(",").map(|s| s.to_string()).collect(), @@ -168,7 +160,8 @@ async fn handle_update( &labels_list, &state.0, ) - .await; + .await?; println!("{result}"); + Ok(()) } diff --git a/src/cli_parse/handle_commands/handle_release.rs b/src/cli_parse/handle_commands/handle_release.rs index d32f4b2..8467fc3 100644 --- a/src/cli_parse/handle_commands/handle_release.rs +++ b/src/cli_parse/handle_commands/handle_release.rs @@ -1,5 +1,5 @@ use octorust::{self, Client}; -use std::process; +use std::error::Error; use crate::cli_in::release_command::ReleaseCommand; use crate::cli_out::print_in_cli::print_release; @@ -10,7 +10,10 @@ use crate::git_utils::repo_info::RepoInfo; use crate::git_utils::repo_info::RepoName; use crate::git_utils::repo_info::RepoOwner; -pub async fn handle_release_command(github_client: Client, subcommand: ReleaseCommand) { +pub async fn handle_release_command( + github_client: Client, + subcommand: ReleaseCommand, +) -> Result<(), Box> { match subcommand { ReleaseCommand::Create { owner, @@ -35,21 +38,25 @@ pub async fn handle_release_command(github_client: Client, subcommand: ReleaseCo tag_name, target_commitish, ) - .await; + .await?; + Ok(()) } ReleaseCommand::GetLatest { owner, repo } => { - handle_get_latest(github_client, owner, repo).await; + handle_get_latest(github_client, owner, repo).await?; + Ok(()) } ReleaseCommand::GetByTag { owner, repo, tag } => { - handle_get_by_tag(github_client, owner, repo, tag).await; + handle_get_by_tag(github_client, owner, repo, tag).await?; + Ok(()) } ReleaseCommand::GetById { owner, repo, id } => { - handle_get_by_id(github_client, owner, repo, id).await; + handle_get_by_id(github_client, owner, repo, id).await?; + Ok(()) } - }; + } } async fn handle_create( @@ -63,14 +70,8 @@ async fn handle_create( prerelease: Option, tag_name: String, target_commitish: String, -) { - let repo_info: RepoInfo = match RepoInfo::new(Repo::Input, Some(owner), Some(repo)) { - Ok(rep) => rep, - Err(message) => { - eprintln!("Error: {message}"); - process::exit(1); - } - }; +) -> Result<(), Box> { + let repo_info: RepoInfo = RepoInfo::new(Repo::Input, Some(owner), Some(repo))?; let result = releases::create( &github_client, @@ -83,49 +84,49 @@ async fn handle_create( &tag_name, target_commitish, ) - .await; + .await?; print_url(result, "New release"); + Ok(()) } -async fn handle_get_latest(github_client: Client, owner: RepoOwner, repo: RepoName) { - let repo_info: RepoInfo = match RepoInfo::new(Repo::Input, Some(owner), Some(repo)) { - Ok(rep) => rep, - Err(message) => { - eprintln!("Error: {message}"); - process::exit(1); - } - }; +async fn handle_get_latest( + github_client: Client, + owner: RepoOwner, + repo: RepoName, +) -> Result<(), Box> { + let repo_info: RepoInfo = RepoInfo::new(Repo::Input, Some(owner), Some(repo))?; - let result = releases::get_latest(&github_client, repo_info).await; + let result = releases::get_latest(&github_client, repo_info).await?; print_release(result); + Ok(()) } -async fn handle_get_by_tag(github_client: Client, owner: RepoOwner, repo: RepoName, tag: String) { - let repo_info: RepoInfo = match RepoInfo::new(Repo::Input, Some(owner), Some(repo)) { - Ok(rep) => rep, - Err(message) => { - eprintln!("Error: {message}"); - process::exit(1); - } - }; +async fn handle_get_by_tag( + github_client: Client, + owner: RepoOwner, + repo: RepoName, + tag: String, +) -> Result<(), Box> { + let repo_info: RepoInfo = RepoInfo::new(Repo::Input, Some(owner), Some(repo))?; - let result = releases::get_by_tag(&github_client, repo_info, tag).await; + let result = releases::get_by_tag(&github_client, repo_info, tag).await?; print_release(result); + Ok(()) } -async fn handle_get_by_id(github_client: Client, owner: RepoOwner, repo: RepoName, id: i64) { - let repo_info: RepoInfo = match RepoInfo::new(Repo::Input, Some(owner), Some(repo)) { - Ok(rep) => rep, - Err(message) => { - eprintln!("Error: {message}"); - process::exit(1); - } - }; +async fn handle_get_by_id( + github_client: Client, + owner: RepoOwner, + repo: RepoName, + id: i64, +) -> Result<(), Box> { + let repo_info: RepoInfo = RepoInfo::new(Repo::Input, Some(owner), Some(repo))?; - let result = releases::get_by_id(&github_client, repo_info, id).await; + let result = releases::get_by_id(&github_client, repo_info, id).await?; print_release(result); + Ok(()) } diff --git a/src/cli_parse/handle_commands/handle_repo.rs b/src/cli_parse/handle_commands/handle_repo.rs index ddd325a..5c2f0d3 100644 --- a/src/cli_parse/handle_commands/handle_repo.rs +++ b/src/cli_parse/handle_commands/handle_repo.rs @@ -1,5 +1,5 @@ use octorust::{self, Client}; -use std::process; +use std::error::Error; use crate::cli_in::repo_command::RepoCommand; use crate::cli_in::set_vars::Orders; @@ -14,7 +14,10 @@ use crate::git_utils::repo_info::RepoInfo; use crate::git_utils::repo_info::{RepoName, RepoOwner}; use crate::git_utils::repos; -pub async fn handle_repo_command(github_client: Client, subcommand: RepoCommand) { +pub async fn handle_repo_command( + github_client: Client, + subcommand: RepoCommand, +) -> Result<(), Box> { match subcommand { RepoCommand::CreateForAuthenticatedUser { allow_auto_merge, @@ -53,7 +56,8 @@ pub async fn handle_repo_command(github_client: Client, subcommand: RepoCommand) name, private, ) - .await; + .await?; + Ok(()) } RepoCommand::CreateInOrg { @@ -97,7 +101,8 @@ pub async fn handle_repo_command(github_client: Client, subcommand: RepoCommand) team_name, visibility, ) - .await; + .await?; + Ok(()) } RepoCommand::GetAllFromOrg { @@ -106,7 +111,8 @@ pub async fn handle_repo_command(github_client: Client, subcommand: RepoCommand) sort_value, type_value, } => { - handle_get_all_from_org(github_client, org, order, type_value, sort_value).await; + handle_get_all_from_org(github_client, org, order, type_value, sort_value).await?; + Ok(()) } RepoCommand::CreateUsingTemplate { @@ -128,11 +134,13 @@ pub async fn handle_repo_command(github_client: Client, subcommand: RepoCommand) include_all_branches, private, ) - .await; + .await?; + Ok(()) } RepoCommand::CreateFork { org, name, owner } => { - handle_create_fork(github_client, owner, name, org).await; + handle_create_fork(github_client, owner, name, org).await?; + Ok(()) } RepoCommand::GetAllFromUser { @@ -141,7 +149,8 @@ pub async fn handle_repo_command(github_client: Client, subcommand: RepoCommand) sort_value, type_value, } => { - handle_get_all_from_user(github_client, owner, type_value, sort_value, order).await; + handle_get_all_from_user(github_client, owner, type_value, sort_value, order).await?; + Ok(()) } } } @@ -164,7 +173,7 @@ async fn handle_create_for_auth_user( license_template: String, name: String, private: Option, -) { +) -> Result<(), Box> { let result = repos::create_for_authenticated_user( &github_client, allow_auto_merge, @@ -184,9 +193,10 @@ async fn handle_create_for_auth_user( &name, private, ) - .await; + .await?; println!("{result}"); + Ok(()) } async fn handle_create_in_org( @@ -209,14 +219,8 @@ async fn handle_create_in_org( org: RepoOwner, team_name: String, visibility: Visibilities, -) { - let repo_info: RepoInfo = match RepoInfo::new(Repo::Input, Some(org), Some(name)) { - Ok(rep) => rep, - Err(message) => { - eprintln!("Error: {message}"); - process::exit(1); - } - }; +) -> Result<(), Box> { + let repo_info: RepoInfo = RepoInfo::new(Repo::Input, Some(org), Some(name))?; let result = repos::create_in_org( &github_client, @@ -238,9 +242,10 @@ async fn handle_create_in_org( &team_name, Some(visibility.0), ) - .await; + .await?; print_url(result, "New repo"); + Ok(()) } async fn handle_get_all_from_org( @@ -249,11 +254,12 @@ async fn handle_get_all_from_org( order: Orders, type_value: ReposListOrgTypes, sort_value: ReposListOrgSorts, -) { +) -> Result<(), Box> { let all_repos = - repos::get_all_from_org(&github_client, &org, order.0, type_value.0, sort_value.0).await; + repos::get_all_from_org(&github_client, &org, order.0, type_value.0, sort_value.0).await?; print_repos(all_repos, org, "org"); + Ok(()) } async fn handle_create_using_template( @@ -265,22 +271,10 @@ async fn handle_create_using_template( description: String, include_all_branches: Option, private: Option, -) { - let repo_info: RepoInfo = match RepoInfo::new(Repo::Input, Some(owner), Some(name)) { - Ok(rep) => rep, - Err(message) => { - eprintln!("Error: {message}"); - process::exit(1); - } - }; +) -> Result<(), Box> { + let repo_info: RepoInfo = RepoInfo::new(Repo::Input, Some(owner), Some(name))?; let template_info: RepoInfo = - match RepoInfo::new(Repo::Input, Some(template_owner), Some(template_name)) { - Ok(rep) => rep, - Err(message) => { - eprintln!("Error: {message}"); - process::exit(1); - } - }; + RepoInfo::new(Repo::Input, Some(template_owner), Some(template_name))?; let result = repos::create_using_template( &github_client, @@ -290,22 +284,23 @@ async fn handle_create_using_template( include_all_branches, private, ) - .await; + .await?; print_url(result, "New repo"); + Ok(()) } -async fn handle_create_fork(github_client: Client, owner: RepoOwner, name: RepoName, org: String) { - let fork_info: RepoInfo = match RepoInfo::new(Repo::Input, Some(owner), Some(name)) { - Ok(rep) => rep, - Err(message) => { - eprintln!("Error: {message}"); - process::exit(1); - } - }; - let result = repos::create_fork(&github_client, &org, fork_info).await; +async fn handle_create_fork( + github_client: Client, + owner: RepoOwner, + name: RepoName, + org: String, +) -> Result<(), Box> { + let fork_info: RepoInfo = RepoInfo::new(Repo::Input, Some(owner), Some(name))?; + let result = repos::create_fork(&github_client, &org, fork_info).await?; println!("{result}"); + Ok(()) } async fn handle_get_all_from_user( @@ -314,7 +309,7 @@ async fn handle_get_all_from_user( type_value: ReposListUserTypes, sort_value: ReposListOrgSorts, order: Orders, -) { +) -> Result<(), Box> { let result = repos::get_all_from_user( &github_client, owner.clone(), @@ -322,7 +317,8 @@ async fn handle_get_all_from_user( sort_value.0, order.0, ) - .await; + .await?; print_repos(result, owner, "user"); + Ok(()) } diff --git a/src/git_utils/comments.rs b/src/git_utils/comments.rs index 8aeec4b..bb12cba 100644 --- a/src/git_utils/comments.rs +++ b/src/git_utils/comments.rs @@ -1,4 +1,4 @@ -use std::process; +use std::error::Error; use octorust::types::{ IssueComment, Order, PullRequestReviewComment, PullsUpdateReviewRequest, Sort, @@ -12,7 +12,7 @@ pub async fn create( repo_info: &RepoInfo, issue_number: &i64, body: &String, -) -> String { +) -> Result> { let request = PullsUpdateReviewRequest { body: body.clone() }; let comment = github_client @@ -26,11 +26,8 @@ pub async fn create( .await; return match comment { - Ok(_) => "Comment create successed".to_string(), - Err(message) => { - eprintln!("Error: {message}"); - process::exit(1); - } + Ok(_) => Ok("Comment create successed".to_string()), + Err(er) => Err(Box::new(er)), }; } @@ -39,7 +36,7 @@ pub async fn get_all( github_client: &Client, repo_info: &RepoInfo, number: &i64, -) -> Vec { +) -> Result, Box> { let list_comments = github_client .issues() .list_all_comments( @@ -51,20 +48,19 @@ pub async fn get_all( .await; return match list_comments { - Ok(c) => c.body, - Err(message) => { - eprintln!("Error: {message}"); - process::exit(1); - } + Ok(c) => Ok(c.body), + Err(er) => Err(Box::new(er)), }; } + +// Get all review Comments for pull request pub async fn get_all_from_review( github_client: &Client, repo_info: &RepoInfo, number: &i64, sort: Sort, direction: Order, -) -> Vec { +) -> Result, Box> { let list_comments = github_client .pulls() .list_all_review_comments( @@ -78,10 +74,7 @@ pub async fn get_all_from_review( .await; return match list_comments { - Ok(c) => c.body, - Err(message) => { - eprintln!("Error: {message}"); - process::exit(1); - } + Ok(c) => Ok(c.body), + Err(er) => Err(Box::new(er)), }; } diff --git a/src/git_utils/issues.rs b/src/git_utils/issues.rs index f2d47bf..611f3bc 100644 --- a/src/git_utils/issues.rs +++ b/src/git_utils/issues.rs @@ -1,4 +1,4 @@ -use std::process; +use std::error::Error; use octorust::types::{ self, IssuesCreateRequest, IssuesCreateRequestLabelsOneOf, IssuesUpdateRequest, State, @@ -18,7 +18,7 @@ pub async fn get_list( labels: &String, numb_of_page: &i64, iss_on_page: &i64, -) -> Vec { +) -> Result, Box> { let sort = types::IssuesListSort::Created; let issues = github_client @@ -41,11 +41,8 @@ pub async fn get_list( .await; return match issues { - Ok(info) => info.body, - Err(message) => { - eprintln!("Error: {message}"); - process::exit(1); - } + Ok(info) => Ok(info.body), + Err(er) => Err(Box::new(er)), }; } @@ -83,7 +80,7 @@ pub async fn create( body: &String, assignees: &Vec, labels: &Vec, -) -> String { +) -> Result> { let request = get_create_request(title, body, assignees, labels); let new_issue = github_client @@ -96,11 +93,8 @@ pub async fn create( .await; return match new_issue { - Ok(_) => "Success".to_string(), - Err(message) => { - eprintln!("Error: {message}"); - process::exit(1); - } + Ok(_) => Ok("Success".to_string()), + Err(er) => Err(Box::new(er)), }; } @@ -137,9 +131,10 @@ pub async fn close( repo_info: RepoInfo, issue_number: &i64, comment: &String, -) -> String { +) -> Result> { if comment != "" { - let new_comment = comments::create(github_client, &repo_info, issue_number, comment).await; + let new_comment = + comments::create(github_client, &repo_info, issue_number, comment).await?; println!("{new_comment}"); } @@ -156,11 +151,8 @@ pub async fn close( .await; return match close { - Ok(_) => "Success".to_string(), - Err(message) => { - eprintln!("Error: {message}"); - process::exit(1); - } + Ok(_) => Ok("Success".to_string()), + Err(er) => Err(Box::new(er)), }; } @@ -173,7 +165,7 @@ pub async fn update( assignees: &Vec, labels: &Vec, state: &State, -) -> String { +) -> Result> { let request = get_update_request(title, body, Some(assignees), Some(labels), state); let update_iss = github_client @@ -187,10 +179,7 @@ pub async fn update( .await; return match update_iss { - Ok(_) => "Success".to_string(), - Err(message) => { - eprintln!("Error: {message}"); - process::exit(1); - } + 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 2616f71..9b5f9c7 100644 --- a/src/git_utils/releases.rs +++ b/src/git_utils/releases.rs @@ -1,4 +1,4 @@ -use std::process; +use std::error::Error; use octorust::{ types::{Release, ReposCreateReleaseRequest}, @@ -17,7 +17,7 @@ pub async fn create( prerelease: Option, tag_name: &String, target_commitish: String, -) -> String { +) -> Result> { let request = ReposCreateReleaseRequest { body: body, discussion_category_name: discussion_category_name, @@ -38,30 +38,31 @@ pub async fn create( .await; return match result { - Ok(_) => repo_info.get_release_url(tag_name), - Err(message) => { - eprintln!("{message}"); - process::exit(1); - } + Ok(_) => Ok(repo_info.get_release_url(tag_name)), + Err(er) => Err(Box::new(er)), }; } -pub async fn get_latest(github_client: &Client, repo_info: RepoInfo) -> Release { +pub async fn get_latest( + github_client: &Client, + repo_info: RepoInfo, +) -> Result> { let result = github_client .repos() .get_latest_release(&repo_info.get_owner().trim(), &repo_info.get_name().trim()) .await; return match result { - Ok(r) => r.body, - Err(message) => { - eprintln!("{message}"); - process::exit(1); - } + Ok(r) => Ok(r.body), + Err(er) => Err(Box::new(er)), }; } -pub async fn get_by_tag(github_client: &Client, repo_info: RepoInfo, tag: String) -> Release { +pub async fn get_by_tag( + github_client: &Client, + repo_info: RepoInfo, + tag: String, +) -> Result> { let result = github_client .repos() .get_release_by_tag( @@ -70,16 +71,18 @@ pub async fn get_by_tag(github_client: &Client, repo_info: RepoInfo, tag: String &tag, ) .await; + return match result { - Ok(r) => r.body, - Err(message) => { - eprintln!("{message}"); - process::exit(1); - } + Ok(r) => Ok(r.body), + Err(er) => Err(Box::new(er)), }; } -pub async fn get_by_id(github_client: &Client, repo_info: RepoInfo, id: i64) -> Release { +pub async fn get_by_id( + github_client: &Client, + repo_info: RepoInfo, + id: i64, +) -> Result> { let result = github_client .repos() .get_release( @@ -88,11 +91,9 @@ pub async fn get_by_id(github_client: &Client, repo_info: RepoInfo, id: i64) -> id, ) .await; + return match result { - Ok(r) => r.body, - Err(message) => { - eprintln!("{message}"); - process::exit(1); - } + Ok(r) => Ok(r.body), + Err(er) => Err(Box::new(er)), }; } diff --git a/src/git_utils/repos.rs b/src/git_utils/repos.rs index 4f06f8a..5000173 100644 --- a/src/git_utils/repos.rs +++ b/src/git_utils/repos.rs @@ -1,4 +1,4 @@ -use std::process; +use std::error::Error; use octorust::{ types::{ @@ -29,7 +29,7 @@ pub async fn create_for_authenticated_user( license_template: &String, name: &String, private: Option, -) -> String { +) -> Result> { let request = ReposCreateRequest { allow_auto_merge: allow_auto_merge, allow_merge_commit: allow_merge_commit, @@ -57,11 +57,8 @@ pub async fn create_for_authenticated_user( .await; return match new_repo { - Ok(_) => "Success".to_string(), - Err(message) => { - eprintln!("Error: {message}"); - process::exit(1); - } + Ok(_) => Ok("Success".to_string()), + Err(er) => Err(Box::new(er)), }; } @@ -84,7 +81,7 @@ pub async fn create_in_org( license_template: &String, team_name: &String, visibility: Option, -) -> String { +) -> Result> { let team = get_id(github_client, &repo_info.get_owner(), team_name).await; let team_id = team.id; @@ -116,11 +113,8 @@ pub async fn create_in_org( .await; return match new_repo { - Ok(_) => repo_info.get_ssh(), - Err(message) => { - eprintln!("Error: {message}"); - process::exit(1); - } + Ok(_) => Ok(repo_info.get_ssh()), + Err(er) => Err(Box::new(er)), }; } @@ -130,18 +124,15 @@ pub async fn get_all_from_org( order: Order, type_value: ReposListOrgType, sort_value: ReposListOrgSort, -) -> Vec { +) -> Result, Box> { let all_repos = github_client .repos() .list_all_for_org(org.trim(), type_value, sort_value, order) .await; return match all_repos { - Ok(r) => r.body, - Err(message) => { - eprintln!("Error: {message}"); - process::exit(1); - } + Ok(r) => Ok(r.body), + Err(er) => Err(Box::new(er)), }; } @@ -152,7 +143,7 @@ pub async fn create_using_template( description: &String, include_all_branches: Option, private: Option, -) -> String { +) -> Result> { let request = ReposCreateUsingTemplateRequest { description: description.clone(), include_all_branches: include_all_branches, @@ -171,15 +162,16 @@ pub async fn create_using_template( .await; return match new_repo { - Ok(_) => repo_info.get_ssh(), - Err(message) => { - eprintln!("Error: {message}"); - process::exit(1); - } + Ok(_) => Ok(repo_info.get_ssh()), + Err(er) => Err(Box::new(er)), }; } -pub async fn create_fork(github_client: &Client, org: &String, fork_info: RepoInfo) -> String { +pub async fn create_fork( + github_client: &Client, + org: &String, + fork_info: RepoInfo, +) -> Result> { let request = ReposCreateForkRequest { organization: org.clone(), }; @@ -194,11 +186,8 @@ pub async fn create_fork(github_client: &Client, org: &String, fork_info: RepoIn .await; return match new_fork { - Ok(_) => "Success".to_string(), - Err(message) => { - eprintln!("Error: {message}"); - process::exit(1); - } + Ok(_) => Ok("Success".to_string()), + Err(er) => Err(Box::new(er)), }; } @@ -208,17 +197,14 @@ pub async fn get_all_from_user( type_value: ReposListUserType, sort_value: ReposListOrgSort, order: Order, -) -> Vec { +) -> Result, Box> { let all_repos = github_client .repos() .list_all_for_user(owner.trim(), type_value, sort_value, order) .await; return match all_repos { - Ok(reps) => reps.body, - Err(message) => { - eprintln!("Error: {message}"); - process::exit(1); - } + Ok(reps) => Ok(reps.body), + Err(er) => Err(Box::new(er)), }; } diff --git a/src/main.rs b/src/main.rs index 97419da..f45195c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,5 +25,11 @@ async fn main() { Client::new("github-cli".to_string(), Credentials::Token(github_token)) .expect("Failed to create Github client"); - handle_cli_command(args, github_client).await; + match handle_cli_command(args, github_client).await { + Ok(_) => {} + Err(message) => { + eprintln!("Error: {message}"); + process::exit(1); + } + }; }