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

feat(args): allow GitLab groups with --gitlab-repo #807

Merged
merged 2 commits into from
Aug 24, 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
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ log = "0.4.21"
secrecy = { version = "0.8.0", features = ["serde"] }
lazy_static = "1.5.0"
dirs = "5.0.1"
url = "2.5.2"

[profile.dev]
opt-level = 0
Expand Down
2 changes: 1 addition & 1 deletion git-cliff-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ tokio = { version = "1.38.0", features = [
"macros",
], optional = true }
futures = { version = "0.3.30", optional = true }
url = "2.5.2"
url.workspace = true
dyn-clone = "1.0.17"
urlencoding = "2.1.3"
cacache = { version = "13.0.0", features = ["mmap"], default-features = false }
Expand Down
1 change: 1 addition & 0 deletions git-cliff/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ indicatif = { version = "0.17.8", optional = true }
env_logger = "0.10.2"
pprof = { version = "0.13", optional = true }
rand = { version = "0.8.4", optional = true }
url.workspace = true

[dependencies.git-cliff-core]
version = "2.4.0" # managed by release.sh
Expand Down
37 changes: 30 additions & 7 deletions git-cliff/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use glob::Pattern;
use regex::Regex;
use secrecy::SecretString;
use std::path::PathBuf;
use url::Url;

#[derive(Debug, Clone, Copy, ValueEnum)]
pub enum Strip {
Expand Down Expand Up @@ -345,10 +346,16 @@ impl TypedValueParser for RemoteValueParser {
value: &std::ffi::OsStr,
) -> Result<Self::Value, clap::Error> {
let inner = clap::builder::StringValueParser::new();
let value = inner.parse_ref(cmd, arg, value)?;
let parts = value.split('/').rev().collect::<Vec<&str>>();
if let (Some(owner), Some(repo)) = (parts.get(1), parts.first()) {
Ok(RemoteValue(Remote::new(*owner, *repo)))
let mut value = inner.parse_ref(cmd, arg, value)?;
if let Ok(url) = Url::parse(&value) {
value = url.path().trim_start_matches('/').to_string();
}
let parts = value.rsplit_once('/');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One idea is to parse it as URL then split it (just to be safe):

Suggested change
let parts = value.rsplit_once('/');
let mut value = inner.parse_ref(cmd, arg, value)?;
if let Ok(url) = Url::parse(&value) {
value = url.path().trim_start_matches('/').to_string();
}
let parts = value.rsplit_once('/');

(you need to add url crate to the workspace dependencies)

if let Some((owner, repo)) = parts {
Ok(RemoteValue(Remote::new(
owner.to_string(),
repo.to_string(),
)))
} else {
let mut err = clap::Error::new(ErrorKind::ValueValidation).with_cmd(cmd);
if let Some(arg) = arg {
Expand Down Expand Up @@ -452,9 +459,14 @@ mod tests {
OsStr::new("test/repo")
)?
);
assert!(remote_value_parser
.parse_ref(&Opt::command(), None, OsStr::new("test"))
.is_err());
assert_eq!(
RemoteValue(Remote::new("gitlab/group/test", "repo")),
remote_value_parser.parse_ref(
&Opt::command(),
None,
OsStr::new("gitlab/group/test/repo")
)?
);
assert_eq!(
RemoteValue(Remote::new("test", "testrepo")),
remote_value_parser.parse_ref(
Expand All @@ -463,6 +475,17 @@ mod tests {
OsStr::new("https://github.com/test/testrepo")
)?
);
assert_eq!(
RemoteValue(Remote::new("archlinux/packaging/packages", "arch-repro-status")),
remote_value_parser.parse_ref(
&Opt::command(),
None,
OsStr::new("https://gitlab.archlinux.org/archlinux/packaging/packages/arch-repro-status")
)?
);
assert!(remote_value_parser
.parse_ref(&Opt::command(), None, OsStr::new("test"))
.is_err());
assert!(remote_value_parser
.parse_ref(&Opt::command(), None, OsStr::new(""))
.is_err());
Expand Down
Loading