1
1
mirror of https://github.com/orhun/git-cliff.git synced 2024-12-02 01:53:45 +03:00

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.
This commit is contained in:
Anthony Butt 2024-07-28 02:12:32 -04:00 committed by GitHub
parent 58b729c71f
commit e3e7c07940
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 30 additions and 1 deletions

7
Cargo.lock generated
View File

@ -812,6 +812,7 @@ dependencies = [
"tokio", "tokio",
"toml", "toml",
"url", "url",
"urlencoding",
] ]
[[package]] [[package]]
@ -2881,6 +2882,12 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "urlencoding"
version = "2.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "daf8dba3b7eb870caf1ddeed7bc9d2a049f3cfdfae7cb521b087cc33ae4c49da"
[[package]] [[package]]
name = "utf8parse" name = "utf8parse"
version = "0.2.1" version = "0.2.1"

View File

@ -74,6 +74,7 @@ tokio = { version = "1.38.0", features = [
futures = { version = "0.3.30", optional = true } futures = { version = "0.3.30", optional = true }
url = "2.5.2" url = "2.5.2"
dyn-clone = "1.0.17" dyn-clone = "1.0.17"
urlencoding = "2.1.3"
[dependencies.git2] [dependencies.git2]
version = "0.19.0" version = "0.19.0"

View File

@ -6,6 +6,7 @@ use serde::{
Serialize, Serialize,
}; };
use std::env; use std::env;
use urlencoding::encode;
use super::*; use super::*;
@ -47,7 +48,7 @@ pub struct GitLabProject {
impl RemoteEntry for GitLabProject { impl RemoteEntry for GitLabProject {
fn url(_id: i64, api_url: &str, remote: &Remote, _page: i32) -> String { 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 { fn buffer_size() -> usize {
@ -285,3 +286,21 @@ impl GitLabClient {
.collect()) .collect())
} }
} }
#[cfg(test)]
mod test {
use super::*;
use pretty_assertions::assert_eq;
fn test_remote_entry_url<R: RemoteEntry>(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::<GitLabProject>("https://gitlab.test.com/api/v4/projects/abc%2Fdef%2Fxyz1")
}
}

View File

@ -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. 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 ## Templating