Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitaf21a18

Browse files
nejchJohnVillalovos
authored andcommitted
refactor(client): factor out URL check into a helper
1 parent77c04b1 commitaf21a18

File tree

1 file changed

+33
-27
lines changed

1 file changed

+33
-27
lines changed

‎gitlab/client.py‎

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,36 @@ def _build_url(self, path: str) -> str:
577577
returnpath
578578
returnf"{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+
ifnoturlorurl.startswith(self.url):
587+
returnurl
588+
589+
match=re.match(rf"(^.*?)/{path}",url)
590+
ifnotmatch:
591+
returnurl
592+
593+
base_url=match.group(1)
594+
ifself.keep_base_url:
595+
returnurl.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}).\nThis 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+
returnurl
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-
ifnotnext_url.startswith(self._gl.url):
1144-
search_api_url=re.search(r"(^.*?/api)",next_url)
1145-
ifsearch_api_url:
1146-
next_api_url=search_api_url.group(1)
1147-
ifself._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
exceptKeyError:
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

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp