Skip to content

Commit

Permalink
fix(gitlab): URL-encode the owner in remote requests for GitLab (#742)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tonybutt authored Jul 28, 2024
1 parent 58b729c commit e3e7c07
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
7 changes: 7 additions & 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 git-cliff-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
21 changes: 20 additions & 1 deletion git-cliff-core/src/remote/gitlab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use serde::{
Serialize,
};
use std::env;
use urlencoding::encode;

use super::*;

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -285,3 +286,21 @@ impl GitLabClient {
.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")
}
}
2 changes: 2 additions & 0 deletions website/docs/integration/gitlab.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e3e7c07

Please sign in to comment.