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