feat: allow args for `--issue`

closes #2
pull/25/head
sgoudham 1 year ago committed by Hamothy
parent 1e8704ce0f
commit 3f9c7bd376

@ -65,6 +65,9 @@ $ git view --issue
# Given branch 'TICKET-123' or some other variation # Given branch 'TICKET-123' or some other variation
# View https://github.com/TRACKED_REMOTE_USER/REPO/issues/123 # View https://github.com/TRACKED_REMOTE_USER/REPO/issues/123
$ git view --issue 42
# View https://github.com/TRACKED_REMOTE_USER/REPO/issues/42
$ git view --suffix releases $ git view --suffix releases
# Given branch 'TICKET-123' or some other variation # Given branch 'TICKET-123' or some other variation
# View https://github.com/TRACKED_REMOTE_USER/REPO/releases # View https://github.com/TRACKED_REMOTE_USER/REPO/releases
@ -91,7 +94,8 @@ OPTIONS:
-c, --commit <hash> The commit to view git repository on -c, --commit <hash> The commit to view git repository on
[default: current commit] [default: current commit]
-s, --suffix <suffix> A suffix to append onto the git repository URL -s, --suffix <suffix> A suffix to append onto the git repository URL
-i, --issue Attempt to parse issue number and open issue link -i, --issue <issue> The issue number to view
[default: open issue from remote branch]
-p, --print Don't open browser and print the URL -p, --print Don't open browser and print the URL
-h, --help Print help information -h, --help Print help information
-V, --version Print version information -V, --version Print version information

@ -56,9 +56,12 @@ fn main() {
) )
.arg( .arg(
Arg::new("issue") Arg::new("issue")
.long_help("Attempt to parse issue number and open issue link") .long_help("The issue number to view\n[default: open issue from remote branch]")
.short('i') .short('i')
.long("issue") .long("issue")
.default_missing_value("branch")
.conflicts_with("commit")
.takes_value(true)
.display_order(5), .display_order(5),
) )
.arg( .arg(
@ -75,7 +78,7 @@ fn main() {
matches.value_of("remote"), matches.value_of("remote"),
matches.value_of("commit"), matches.value_of("commit"),
matches.value_of("suffix"), matches.value_of("suffix"),
matches.is_present("issue"), matches.value_of("issue"),
matches.is_present("print"), matches.is_present("print"),
); );

@ -14,7 +14,7 @@ pub struct GitView<'a> {
branch: Option<&'a str>, branch: Option<&'a str>,
commit: Option<&'a str>, commit: Option<&'a str>,
suffix: Option<&'a str>, suffix: Option<&'a str>,
is_issue: bool, issue: Option<&'a str>,
is_print: bool, is_print: bool,
} }
@ -24,7 +24,7 @@ impl<'a> GitView<'a> {
remote: Option<&'a str>, remote: Option<&'a str>,
commit: Option<&'a str>, commit: Option<&'a str>,
suffix: Option<&'a str>, suffix: Option<&'a str>,
is_issue: bool, issue: Option<&'a str>,
is_print: bool, is_print: bool,
) -> Self { ) -> Self {
Self { Self {
@ -32,7 +32,7 @@ impl<'a> GitView<'a> {
branch, branch,
commit, commit,
suffix, suffix,
is_issue, issue,
is_print, is_print,
} }
} }
@ -231,15 +231,24 @@ impl<'a> GitView<'a> {
return Ok(open_url); return Ok(open_url);
} }
// Handle issue flag and no flags // Handle issue flag
let branch_ref = if self.is_issue { let branch_ref = if let Some(issue) = self.issue {
if issue == "branch" {
format!("/issues/{}", capture_digits(remote_ref)) format!("/issues/{}", capture_digits(remote_ref))
} else {
format!("/issues/{}", issue)
}
} else { } else {
format!("/tree/{}", escape_ascii_chars(remote_ref)) format!("/tree/{}", escape_ascii_chars(remote_ref))
}; };
if remote_ref != "master" && remote_ref != "main" { if remote_ref != "master" && remote_ref != "main" {
open_url.push_str(&branch_ref); open_url.push_str(&branch_ref);
} else {
// Edge Case: If the branch is master/main, still append "/issues"
if self.issue.is_some() {
open_url.push_str("/issues");
}
} }
if let Some(suffix) = self.suffix { if let Some(suffix) = self.suffix {
@ -312,7 +321,7 @@ mod lib_tests {
branch: Option<&'a str>, branch: Option<&'a str>,
commit: Option<&'a str>, commit: Option<&'a str>,
suffix: Option<&'a str>, suffix: Option<&'a str>,
is_issue: bool, issue: Option<&'a str>,
is_print: bool, is_print: bool,
} }
@ -337,8 +346,8 @@ mod lib_tests {
self self
} }
pub(crate) fn with_issue(mut self, is_issue: bool) -> Self { pub(crate) fn with_issue(mut self, issue: &'a str) -> Self {
self.is_issue = is_issue; self.issue = Some(issue);
self self
} }
@ -348,7 +357,7 @@ mod lib_tests {
self.remote, self.remote,
self.commit, self.commit,
self.suffix, self.suffix,
self.is_issue, self.issue,
self.is_print, self.is_print,
) )
} }
@ -800,9 +809,24 @@ mod lib_tests {
assert_eq!(actual_final_url.unwrap(), expected_final_url); assert_eq!(actual_final_url.unwrap(), expected_final_url);
} }
#[test_case("main" ; "main")]
#[test_case("master" ; "master")]
fn is_master_or_main_with_issue_flag(branch: &str) {
let handler = GitView::builder().with_issue("branch").build();
let url = Url::new("https", "github.com", "sgoudham/git-view");
let expected_final_url = "https://github.com/sgoudham/git-view/issues";
let mock = MockGitTrait::default();
let actual_final_url = handler.generate_final_url(branch, &url, &mock);
assert!(actual_final_url.is_ok());
assert_eq!(actual_final_url.unwrap(), expected_final_url);
}
#[test] #[test]
fn is_user_issue() { fn is_user_issue() {
let handler = GitView::builder().with_issue(true).build(); let handler = GitView::builder().with_issue("branch").build();
let url = Url::new("https", "github.com", "sgoudham/git-view"); let url = Url::new("https", "github.com", "sgoudham/git-view");
let expected_final_url = "https://github.com/sgoudham/git-view/issues/1234"; let expected_final_url = "https://github.com/sgoudham/git-view/issues/1234";
let mock = MockGitTrait::default(); let mock = MockGitTrait::default();
@ -813,9 +837,22 @@ mod lib_tests {
assert_eq!(actual_final_url.unwrap(), expected_final_url); assert_eq!(actual_final_url.unwrap(), expected_final_url);
} }
#[test]
fn is_user_issue_with_args() {
let handler = GitView::builder().with_issue("42").build();
let url = Url::new("https", "github.com", "sgoudham/git-view");
let expected_final_url = "https://github.com/sgoudham/git-view/issues/42";
let mock = MockGitTrait::default();
let actual_final_url = handler.generate_final_url("mock ref", &url, &mock);
assert!(actual_final_url.is_ok());
assert_eq!(actual_final_url.unwrap(), expected_final_url);
}
#[test] #[test]
fn is_normal_branch() { fn is_normal_branch() {
let handler = GitView::builder().with_issue(false).build(); let handler = GitView::builder().build();
let url = Url::new("https", "github.com", "sgoudham/git-view"); let url = Url::new("https", "github.com", "sgoudham/git-view");
let expected_final_url = "https://github.com/sgoudham/git-view/tree/%23test%23"; let expected_final_url = "https://github.com/sgoudham/git-view/tree/%23test%23";
let mock = MockGitTrait::default(); let mock = MockGitTrait::default();

Loading…
Cancel
Save