Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CLI flag to hide token from repository URL #1319

Merged
merged 5 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ pub struct InfoCliOptions {
/// Display repository URL as HTTP
#[arg(long)]
pub http_url: bool,
/// Hide token in repository URL
#[arg(long)]
pub hide_token: bool,
/// Count hidden files and directories
#[arg(long)]
pub include_hidden: bool,
Expand Down Expand Up @@ -247,6 +250,7 @@ impl Default for InfoCliOptions {
no_merges: Default::default(),
email: Default::default(),
http_url: Default::default(),
hide_token: Default::default(),
include_hidden: Default::default(),
r#type: vec![LanguageType::Programming, LanguageType::Markup],
disabled_fields: Vec::default(),
Expand Down
6 changes: 5 additions & 1 deletion src/info/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,11 @@ pub fn build_info(cli_options: &CliOptions) -> Result<Info> {
.ok()
.context("BUG: panic in language statistics thread")??;
let manifest = get_manifest(&repo_path)?;
let repo_url = get_repo_url(&repo, cli_options.info.http_url);
let repo_url = get_repo_url(
&repo,
cli_options.info.hide_token,
cli_options.info.http_url,
);

let git_metrics = traverse_commit_graph(
&repo,
Expand Down
129 changes: 65 additions & 64 deletions src/info/url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl UrlInfo {
}
}

pub fn get_repo_url(repo: &Repository, http_url: bool) -> String {
pub fn get_repo_url(repo: &Repository, hide_token: bool, http_url: bool) -> String {
let config = repo.config_snapshot();
let remotes = match config.plumbing().sections_by_name("remote") {
Some(sections) => sections,
Expand All @@ -36,17 +36,22 @@ pub fn get_repo_url(repo: &Repository, http_url: bool) -> String {
}

match remote_url {
Some(url) => format_url(&url, http_url),
Some(url) => format_url(&url, hide_token, http_url),
None => String::default(),
}
}

fn format_url(url: &str, http_url: bool) -> String {
let removed_token = remove_token_from_url(&url);
if !http_url || removed_token.starts_with("http") {
removed_token
fn format_url(url: &str, hide_token: bool, http_url: bool) -> String {
let formatted_url = if hide_token {
remove_token_from_url(url)
} else {
create_http_url_from_ssh(url)
String::from(url)
};

if http_url && !formatted_url.starts_with("http") {
create_http_url_from_ssh(&formatted_url)
} else {
formatted_url
}
}

Expand Down Expand Up @@ -76,6 +81,7 @@ impl InfoField for UrlInfo {
#[cfg(test)]
mod test {
use super::*;
use rstest::rstest;

#[test]
fn test_display_url_info() {
Expand All @@ -89,69 +95,64 @@ mod test {
);
}

#[test]
fn test_format_url_http() {
let remote_url_github =
"https://1234567890abcdefghijklmnopqrstuvwxyz@github.com/0spotter0/onefetch.git";
let res_url_github = format_url(remote_url_github, true);
assert_eq!("https://github.com/0spotter0/onefetch.git", res_url_github);

let remote_url_gitlab =
"https://john:abc123personaltoken@gitlab.com/0spotter0/onefetch.git";
let res_url_gitlab = format_url(remote_url_gitlab, true);
assert_eq!("https://gitlab.com/0spotter0/onefetch.git", res_url_gitlab);
#[rstest]
#[case(
"https://username:token@github.com/user/repo",
true,
false,
"https://github.com/user/repo"
)]
#[case(
"https://user:token@gitlab.com/user/repo",
true,
false,
"https://gitlab.com/user/repo"
)]
#[case(
"git@github.com:user/repo.git",
false,
true,
"https://github.com/user/repo.git"
)]
#[case(
"git@gitlab.com:user/repo",
false,
true,
"https://gitlab.com/user/repo"
)]
#[case(
"https://github.com/user/repo",
true,
true,
"https://github.com/user/repo"
)]
#[case(
"https://username:token@github.com/user/repo",
false,
false,
"https://username:token@github.com/user/repo"
)]
fn test_format_url(
#[case] url: &str,
#[case] hide_token: bool,
#[case] http_url: bool,
#[case] expected: &str,
) {
assert_eq!(format_url(url, hide_token, http_url), expected);
}

#[test]
fn test_format_url_ssh() {
let remote_url_github = "git@github.com:0spotter0/onefetch.git";
let res_url_github_force_http_true = format_url(remote_url_github, true);
let res_url_github_force_http_false = format_url(remote_url_github, false);
assert_eq!(
"https://github.com/0spotter0/onefetch.git",
res_url_github_force_http_true
);
assert_eq!(
"git@github.com:0spotter0/onefetch.git",
res_url_github_force_http_false
);

let remote_url_gitlab = "git@gitlab.com:0spotter0/onefetch.git";
let res_url_gitlab_force_http_true = format_url(remote_url_gitlab, true);
let res_url_gitlab_force_http_false = format_url(remote_url_gitlab, false);
fn test_remove_token_from_url() {
assert_eq!(
"https://gitlab.com/0spotter0/onefetch.git",
res_url_gitlab_force_http_true
remove_token_from_url("https://username:token@github.com/user/repo"),
"https://github.com/user/repo"
);
assert_eq!(
"git@gitlab.com:0spotter0/onefetch.git",
res_url_gitlab_force_http_false
);
}

#[test]
fn test_token_removal_github() {
let remote_url =
"https://1234567890abcdefghijklmnopqrstuvwxyz@github.com/jim4067/onefetch.git";
let res_url = remove_token_from_url(remote_url);
assert_eq!("https://github.com/jim4067/onefetch.git", res_url);
}

#[test]
fn test_token_removal_gitlab() {
let remote_url = "https://john:abc123personaltoken@gitlab.com/jim4067/myproject.git";
let res_url = remove_token_from_url(remote_url);
assert_eq!("https://gitlab.com/jim4067/myproject.git", res_url);
}

#[test]
fn test_create_http_url_from_ssh() {
let remote_url_github = "git@github.com:0spotter0/onefetch.git";
let res_url_github = create_http_url_from_ssh(remote_url_github);
assert_eq!("https://github.com/0spotter0/onefetch.git", res_url_github);

let remote_url_gitlab = "git@gitlab.com:0spotter0/onefetch.git";
let res_url_gitlab = create_http_url_from_ssh(remote_url_gitlab);
assert_eq!("https://gitlab.com/0spotter0/onefetch.git", res_url_gitlab);
#[rstest]
#[case("git@github.com:user/repo.git", "https://github.com/user/repo.git")]
#[case("git@gitlab.com:user/repo", "https://gitlab.com/user/repo")]
fn test_create_http_url_from_ssh(#[case] url: &str, #[case] expected: &str) {
assert_eq!(create_http_url_from_ssh(url), expected);
}
}
Loading