Skip to content

Commit

Permalink
Merge pull request #270 from jordilin/mymrfilter
Browse files Browse the repository at this point in the history
My mr cli filter by assignee, author or reviewer
  • Loading branch information
jordilin authored Aug 1, 2024
2 parents 9528f67 + 8435b31 commit 2b076db
Show file tree
Hide file tree
Showing 9 changed files with 385 additions and 70 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "git-ar"
version = "0.1.89"
version = "0.1.90"
edition = "2021"

license = "MIT"
Expand Down
144 changes: 134 additions & 10 deletions src/cli/my.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use clap::Parser;

use crate::cmds::{
gist::GistListCliArgs, merge_request::MergeRequestListCliArgs, project::ProjectListCliArgs,
gist::GistListCliArgs,
merge_request::{MergeRequestListCliArgs, MergeRequestUser},
project::ProjectListCliArgs,
};

use super::{common::ListArgs, merge_request::ListMergeRequest, project::ListProject};
Expand All @@ -14,8 +16,11 @@ pub struct MyCommand {

#[derive(Parser)]
enum MySubcommand {
#[clap(about = "Lists your assigned merge requests", name = "mr")]
MergeRequest(ListMergeRequest),
#[clap(
about = "Lists the merge requests where you are the author, assignee or the reviewer",
name = "mr"
)]
MergeRequest(ListMyMergeRequest),
#[clap(about = "Lists your projects", name = "pj")]
Project(ListProject),
#[clap(about = "Lists your starred projects", name = "st")]
Expand All @@ -24,6 +29,22 @@ enum MySubcommand {
Gist(ListGist),
}

#[derive(Parser)]
struct ListMyMergeRequest {
/// Filter merge requests where you are the assignee. Gitlab and Github.
#[clap(long, group = "merge_request")]
assignee: bool,
/// Filter merge requests where you are the author. Default if none
/// provided. Gitlab and Github.
#[clap(long, group = "merge_request")]
author: bool,
/// Filter merge requests where you are the reviewer. Gitlab only.
#[clap(long, group = "merge_request")]
reviewer: bool,
#[clap(flatten)]
list_merge_request: ListMergeRequest,
}

pub enum MyOptions {
MergeRequest(MergeRequestListCliArgs),
Project(ProjectListCliArgs),
Expand All @@ -41,12 +62,33 @@ impl From<MyCommand> for MyOptions {
}
}

impl From<ListMergeRequest> for MyOptions {
fn from(options: ListMergeRequest) -> Self {
MyOptions::MergeRequest(MergeRequestListCliArgs::new(
options.state.into(),
options.list_args.into(),
))
impl From<ListMyMergeRequest> for MyOptions {
fn from(options: ListMyMergeRequest) -> Self {
MyOptions::MergeRequest(
MergeRequestListCliArgs::builder()
.state(options.list_merge_request.state.into())
.list_args(options.list_merge_request.list_args.into())
.assignee(if options.assignee {
Some(MergeRequestUser::Me)
} else {
None
})
// Author is the default if none is provided.
.author(
if options.author || (!options.assignee && !options.reviewer) {
Some(MergeRequestUser::Me)
} else {
None
},
)
.reviewer(if options.reviewer {
Some(MergeRequestUser::Me)
} else {
None
})
.build()
.unwrap(),
)
}
}

Expand Down Expand Up @@ -111,7 +153,88 @@ mod tests {
Command::My(MyCommand {
subcommand: MySubcommand::MergeRequest(options),
}) => {
assert_eq!(options.state, MergeRequestStateStateCli::Opened);
assert_eq!(
options.list_merge_request.state,
MergeRequestStateStateCli::Opened
);
options
}
_ => panic!("Expected MyCommand"),
};
let options: MyOptions = my_command.into();
match options {
MyOptions::MergeRequest(options) => {
assert_eq!(options.state, MergeRequestState::Opened);
assert_eq!(options.author, Some(MergeRequestUser::Me));
}
_ => panic!("Expected MyOptions::MergeRequest"),
}
}

#[test]
fn test_my_merge_request_cli_args_reviewer() {
let args = Args::parse_from(vec!["gr", "my", "mr", "opened", "--reviewer"]);
let my_command = match args.command {
Command::My(MyCommand {
subcommand: MySubcommand::MergeRequest(options),
}) => {
assert_eq!(
options.list_merge_request.state,
MergeRequestStateStateCli::Opened
);
assert!(options.reviewer);
options
}
_ => panic!("Expected MyCommand"),
};
let options: MyOptions = my_command.into();
match options {
MyOptions::MergeRequest(options) => {
assert_eq!(options.state, MergeRequestState::Opened);
assert_eq!(options.reviewer, Some(MergeRequestUser::Me));
}
_ => panic!("Expected MyOptions::MergeRequest"),
}
}

#[test]
fn test_my_merge_request_cli_args_author() {
let args = Args::parse_from(vec!["gr", "my", "mr", "opened", "--author"]);
let my_command = match args.command {
Command::My(MyCommand {
subcommand: MySubcommand::MergeRequest(options),
}) => {
assert_eq!(
options.list_merge_request.state,
MergeRequestStateStateCli::Opened
);
assert!(options.author);
options
}
_ => panic!("Expected MyCommand"),
};
let options: MyOptions = my_command.into();
match options {
MyOptions::MergeRequest(options) => {
assert_eq!(options.state, MergeRequestState::Opened);
assert_eq!(options.author, Some(MergeRequestUser::Me));
}
_ => panic!("Expected MyOptions::MergeRequest"),
}
}

#[test]
fn test_my_merge_request_cli_args_assignee() {
let args = Args::parse_from(vec!["gr", "my", "mr", "opened", "--assignee"]);
let my_command = match args.command {
Command::My(MyCommand {
subcommand: MySubcommand::MergeRequest(options),
}) => {
assert_eq!(
options.list_merge_request.state,
MergeRequestStateStateCli::Opened
);
assert!(options.assignee);
options
}
_ => panic!("Expected MyCommand"),
Expand All @@ -120,6 +243,7 @@ mod tests {
match options {
MyOptions::MergeRequest(options) => {
assert_eq!(options.state, MergeRequestState::Opened);
assert_eq!(options.assignee, Some(MergeRequestUser::Me));
}
_ => panic!("Expected MyOptions::MergeRequest"),
}
Expand Down
21 changes: 19 additions & 2 deletions src/cmds/common.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::display;
use crate::remote::MergeRequestListBodyArgs;
use crate::config::Config;
use crate::remote::{Member, MergeRequestListBodyArgs};
/// Common functions and macros that are used by multiple commands
use crate::Result;
use crate::{api_traits::MergeRequest, remote::ListRemoteCliArgs};
use crate::{display, remote};
use std::fmt::Display;
use std::io::Write;
use std::sync::Arc;
Expand Down Expand Up @@ -239,3 +240,19 @@ list_resource!(
);

list_resource!(list_trending, TrendingProjectURL, String, TrendingCliArgs);

pub fn get_user(
domain: &str,
path: &str,
config: &Arc<Config>,
cli_args: &ListRemoteCliArgs,
) -> Result<Member> {
let remote = remote::get_auth_user(
domain.to_string(),
path.to_string(),
config.clone(),
cli_args.get_args.refresh_cache,
)?;
let user = remote.get()?;
Ok(user)
}
Loading

0 comments on commit 2b076db

Please sign in to comment.