summaryrefslogtreecommitdiff
path: root/gitlab
diff options
context:
space:
mode:
authorOmar Mochtar <iomarmochtar@gmail.com>2022-08-03 22:09:17 +0700
committerGitHub <noreply@github.com>2022-08-03 17:09:17 +0200
commite2ea8b89a7b0aebdb1eb3b99196d7c0034076df8 (patch)
treee16b745e0d1502424e089ede402889a9f5f2d7fa /gitlab
parent1136b17f4e5f36c66c3a67292e508b43ded9ca3e (diff)
downloadgitlab-e2ea8b89a7b0aebdb1eb3b99196d7c0034076df8.tar.gz
fix: optionally keep user-provided base URL for pagination (#2149)
Diffstat (limited to 'gitlab')
-rw-r--r--gitlab/client.py29
1 files changed, 29 insertions, 0 deletions
diff --git a/gitlab/client.py b/gitlab/client.py
index da4036d..dc6b6e6 100644
--- a/gitlab/client.py
+++ b/gitlab/client.py
@@ -17,6 +17,7 @@
"""Wrapper for the GitLab API."""
import os
+import re
import time
from typing import Any, cast, Dict, List, Optional, Tuple, TYPE_CHECKING, Union
@@ -65,6 +66,8 @@ class Gitlab:
user_agent: A custom user agent to use for making HTTP requests.
retry_transient_errors: Whether to retry after 500, 502, 503, 504
or 52x responses. Defaults to False.
+ keep_base_url: keep user-provided base URL for pagination if it
+ differs from response headers
"""
def __init__(
@@ -84,6 +87,7 @@ class Gitlab:
order_by: Optional[str] = None,
user_agent: str = gitlab.const.USER_AGENT,
retry_transient_errors: bool = False,
+ keep_base_url: bool = False,
) -> None:
self._api_version = str(api_version)
@@ -94,6 +98,7 @@ class Gitlab:
#: Timeout to use for requests to gitlab server
self.timeout = timeout
self.retry_transient_errors = retry_transient_errors
+ self.keep_base_url = keep_base_url
#: Headers that will be used in request to GitLab
self.headers = {"User-Agent": user_agent}
@@ -1132,6 +1137,30 @@ class GitlabList:
next_url = requests.utils.parse_header_links(result.headers["links"])[
0
]["url"]
+ # if the next url is different with user provided server URL
+ # then give a warning it may because of misconfiguration
+ # but if the option to fix provided then just reconstruct it
+ if not next_url.startswith(self._gl.url):
+ search_api_url = re.search(r"(^.*?/api)", next_url)
+ if search_api_url:
+ next_api_url = search_api_url.group(1)
+ if self._gl.keep_base_url:
+ next_url = next_url.replace(
+ next_api_url, f"{self._gl._base_url}/api"
+ )
+ else:
+ utils.warn(
+ message=(
+ f"The base URL in the server response"
+ f"differs from the user-provided base URL "
+ f"({self._gl.url}/api/ -> {next_api_url}/). "
+ f"This may lead to unexpected behavior and "
+ f"broken pagination. Use `keep_base_url=True` "
+ f"when initializing the Gitlab instance "
+ f"to follow the user-provided base URL."
+ ),
+ category=UserWarning,
+ )
self._next_url = next_url
except KeyError:
self._next_url = None