@@ -577,6 +577,36 @@ def _build_url(self, path: str) -> str:
577577 return path
578578 return f"{ self ._url } { path } "
579579
580+ def _check_url (self , url : Optional [str ], * , path : str = "api" ) -> Optional [str ]:
581+ """
582+ Checks if ``url`` starts with a different base URL from the user-provided base
583+ URL and warns the user before returning it. If ``keep_base_url`` is set to
584+ ``True``, instead returns the URL massaged to match the user-provided base URL.
585+ """
586+ if not url or url .startswith (self .url ):
587+ return url
588+
589+ match = re .match (rf"(^.*?)/{ path } " , url )
590+ if not match :
591+ return url
592+
593+ base_url = match .group (1 )
594+ if self .keep_base_url :
595+ return url .replace (base_url , f"{ self ._base_url } " )
596+
597+ utils .warn (
598+ message = (
599+ f"The base URL in the server response differs from the user-provided "
600+ f"base URL ({ self .url } -> { base_url } ).\n This is usually caused by a "
601+ f"misconfigured base URL on your side or a misconfigured external_url "
602+ f"on the server side, and can lead to broken pagination and unexpected "
603+ f"behavior. If this is intentional, use `keep_base_url=True` when "
604+ f"initializing the Gitlab instance to keep the user-provided base URL."
605+ ),
606+ category = UserWarning ,
607+ )
608+ return url
609+
580610 @staticmethod
581611 def _check_redirects (result : requests .Response ) -> None :
582612 # Check the requests history to detect 301/302 redirections.
@@ -1136,34 +1166,10 @@ def _query(
11361166 result = self ._gl .http_request ("get" , url , query_data = query_data , ** kwargs )
11371167 try :
11381168 next_url = result .links ["next" ]["url" ]
1139-
1140- # if the next url is different with user provided server URL
1141- # then give a warning it may because of misconfiguration
1142- # but if the option to fix provided then just reconstruct it
1143- if not next_url .startswith (self ._gl .url ):
1144- search_api_url = re .search (r"(^.*?/api)" , next_url )
1145- if search_api_url :
1146- next_api_url = search_api_url .group (1 )
1147- if self ._gl .keep_base_url :
1148- next_url = next_url .replace (
1149- next_api_url , f"{ self ._gl ._base_url } /api"
1150- )
1151- else :
1152- utils .warn (
1153- message = (
1154- f"The base URL in the server response"
1155- f"differs from the user-provided base URL "
1156- f"({ self ._gl .url } /api/ -> { next_api_url } /). "
1157- f"This may lead to unexpected behavior and "
1158- f"broken pagination. Use `keep_base_url=True` "
1159- f"when initializing the Gitlab instance "
1160- f"to follow the user-provided base URL."
1161- ),
1162- category = UserWarning ,
1163- )
1164- self ._next_url = next_url
11651169 except KeyError :
1166- self ._next_url = None
1170+ next_url = None
1171+
1172+ self ._next_url = self ._gl ._check_url (next_url )
11671173 self ._current_page : Optional [str ] = result .headers .get ("X-Page" )
11681174 self ._prev_page : Optional [str ] = result .headers .get ("X-Prev-Page" )
11691175 self ._next_page : Optional [str ] = result .headers .get ("X-Next-Page" )
0 commit comments