From e3e7c0794082e418a78f99e7d9c09161f4d14d5f Mon Sep 17 00:00:00 2001 From: Anthony Butt <38566841+tonybutt@users.noreply.github.com> Date: Sun, 28 Jul 2024 02:12:32 -0400 Subject: [PATCH] fix(gitlab): URL-encode the owner in remote requests for GitLab (#742) Gitlab requires that the project path be fully encoded. See the docs for details. https://docs.gitlab.com/ee/api/rest/#namespaced-path-encoding Pulls in the urlencoding crate here. Might not be needed when you have the choice to just update the documentation. I don't think the end user should be required to url encode their own strings. It might be nicer to just add the subgroup paradigm to the gitlab config in general for the future. This will fix the problem for now. --- Cargo.lock | 7 +++++++ git-cliff-core/Cargo.toml | 1 + git-cliff-core/src/remote/gitlab.rs | 21 ++++++++++++++++++++- website/docs/integration/gitlab.md | 2 ++ 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index d7db012..a4f6b78 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -812,6 +812,7 @@ dependencies = [ "tokio", "toml", "url", + "urlencoding", ] [[package]] @@ -2881,6 +2882,12 @@ dependencies = [ "serde", ] +[[package]] +name = "urlencoding" +version = "2.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "daf8dba3b7eb870caf1ddeed7bc9d2a049f3cfdfae7cb521b087cc33ae4c49da" + [[package]] name = "utf8parse" version = "0.2.1" diff --git a/git-cliff-core/Cargo.toml b/git-cliff-core/Cargo.toml index 79cab62..a39529a 100644 --- a/git-cliff-core/Cargo.toml +++ b/git-cliff-core/Cargo.toml @@ -74,6 +74,7 @@ tokio = { version = "1.38.0", features = [ futures = { version = "0.3.30", optional = true } url = "2.5.2" dyn-clone = "1.0.17" +urlencoding = "2.1.3" [dependencies.git2] version = "0.19.0" diff --git a/git-cliff-core/src/remote/gitlab.rs b/git-cliff-core/src/remote/gitlab.rs index 9a211ef..27ff1f9 100644 --- a/git-cliff-core/src/remote/gitlab.rs +++ b/git-cliff-core/src/remote/gitlab.rs @@ -6,6 +6,7 @@ use serde::{ Serialize, }; use std::env; +use urlencoding::encode; use super::*; @@ -47,7 +48,7 @@ pub struct GitLabProject { impl RemoteEntry for GitLabProject { fn url(_id: i64, api_url: &str, remote: &Remote, _page: i32) -> String { - format!("{}/projects/{}%2F{}", api_url, remote.owner, remote.repo) + format!("{}/projects/{}%2F{}", api_url, encode(remote.owner.as_str()), remote.repo) } fn buffer_size() -> usize { @@ -285,3 +286,21 @@ impl GitLabClient { .collect()) } } +#[cfg(test)] +mod test { + use super::*; + use pretty_assertions::assert_eq; + + fn test_remote_entry_url(expects: &str) { + let remote = Remote::new("abc/def", "xyz1"); + assert_eq!( + expects, + R::url(1, "https://gitlab.test.com/api/v4", &remote, 0) + ) + } + + #[test] + fn it_url_encodes_slashes() { + test_remote_entry_url::("https://gitlab.test.com/api/v4/projects/abc%2Fdef%2Fxyz1") + } +} diff --git a/website/docs/integration/gitlab.md b/website/docs/integration/gitlab.md index 77bc305..0700d1b 100644 --- a/website/docs/integration/gitlab.md +++ b/website/docs/integration/gitlab.md @@ -63,6 +63,8 @@ GITLAB_TOKEN="***" git cliff --gitlab-repo "orhun/git-cliff" You can use the `GITLAB_API_URL` environment variable want to override the API URL. This is useful if you are using your own GitLab instance. +When your project on your own Gitlab has one or many subgroups (e.g my.gitlab.com/myGroup/mySubgroup/myProject) set owner in the toml to "myGroup/mySubgroup" and repo to the repo name. + ::: ## Templating