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

Commit31c9c2f

Browse files
committed
refactor(client): factor out URL check into a helper
1 parent7b61ae6 commit31c9c2f

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
@@ -576,6 +576,36 @@ def _build_url(self, path: str) -> str:
576576
returnpath
577577
returnf"{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+
ifnoturlorurl.startswith(self.url):
586+
returnurl
587+
588+
match=re.match(rf"(^.*?)/{path}",url)
589+
ifnotmatch:
590+
returnurl
591+
592+
base_url=match.group(1)
593+
ifself.keep_base_url:
594+
returnurl.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}).\nThis 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+
returnurl
608+
579609
@staticmethod
580610
def_check_redirects(result:requests.Response)->None:
581611
# Check the requests history to detect 301/302 redirections.
@@ -1131,34 +1161,10 @@ def _query(
11311161
result=self._gl.http_request("get",url,query_data=query_data,**kwargs)
11321162
try:
11331163
next_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-
ifnotnext_url.startswith(self._gl.url):
1139-
search_api_url=re.search(r"(^.*?/api)",next_url)
1140-
ifsearch_api_url:
1141-
next_api_url=search_api_url.group(1)
1142-
ifself._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
11601164
exceptKeyError:
1161-
self._next_url=None
1165+
next_url=None
1166+
1167+
self._next_url=self._gl._check_url(next_url)
11621168
self._current_page:Optional[str]=result.headers.get("X-Page")
11631169
self._prev_page:Optional[str]=result.headers.get("X-Prev-Page")
11641170
self._next_page:Optional[str]=result.headers.get("X-Next-Page")

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp