| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent df50f0f commit 4f30ec5
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -66,6 +66,7 @@ class Gitlab: | |||
| 66 | 66 | user_agent: A custom user agent to use for making HTTP requests. | |
| 67 | 67 | retry_transient_errors: Whether to retry after 500, 502, 503, 504 | |
| 68 | 68 | or 52x responses. Defaults to False. | |
| 69 | + persist_base_url: reconstruct next url if found not same as user provided url | ||
| 69 | 70 | """ | |
| 70 | 71 | ||
| 71 | 72 | def __init__( | |
@@ -85,6 +86,7 @@ def __init__( | |||
| 85 | 86 | order_by: Optional[str] = None, | |
| 86 | 87 | user_agent: str = gitlab.const.USER_AGENT, | |
| 87 | 88 | retry_transient_errors: bool = False, | |
| 89 | + persist_base_url: bool = False, | ||
| 88 | 90 | ) -> None: | |
| 89 | 91 | ||
| 90 | 92 | self._api_version = str(api_version) | |
@@ -95,6 +97,7 @@ def __init__( | |||
| 95 | 97 | #: Timeout to use for requests to gitlab server | |
| 96 | 98 | self.timeout = timeout | |
| 97 | 99 | self.retry_transient_errors = retry_transient_errors | |
| 100 | + self.persist_base_url = persist_base_url | ||
| 98 | 101 | #: Headers that will be used in request to GitLab | |
| 99 | 102 | self.headers = {"User-Agent": user_agent} | |
| 100 | 103 | ||
@@ -1133,8 +1136,29 @@ def _query( | |||
| 1133 | 1136 | next_url = requests.utils.parse_header_links(result.headers["links"])[ | |
| 1134 | 1137 | 0 | |
| 1135 | 1138 | ]["url"] | |
| 1136 | - if not next_url.startswith(self._gl._base_url): | ||
| 1137 | - next_url = re.sub(r"^.*?/api", f"{self._gl._base_url}/api", next_url) | ||
| 1139 | + # if the next url is different with user provided server URL | ||
| 1140 | + # then give a warning it may because of misconfiguration | ||
| 1141 | + # but if the option to fix provided then just reconstruct it | ||
| 1142 | + if not next_url.startswith(self._gl.url): | ||
| 1143 | + search_api_url = re.search(r"(^.*?/api)", next_url) | ||
| 1144 | + if search_api_url: | ||
| 1145 | + next_api_url = search_api_url.group(1) | ||
| 1146 | + if self._gl.persist_base_url: | ||
| 1147 | + next_url = next_url.replace( | ||
| 1148 | + next_api_url, f"{self._gl._base_url}/api" | ||
| 1149 | + ) | ||
| 1150 | + else: | ||
| 1151 | + utils.warn( | ||
| 1152 | + message=( | ||
| 1153 | + f"The base url of the returned next page got " | ||
| 1154 | + f"different with the user provided " | ||
| 1155 | + f"{self._gl.url}/api* ~> {next_api_url}*, " | ||
| 1156 | + f"since this may can lead to unexpected behaviour. " | ||
| 1157 | + f"set argument persist_base_url to True for " | ||
| 1158 | + f"resonctruct it with the origin one." | ||
| 1159 | + ), | ||
| 1160 | + category=UserWarning, | ||
| 1161 | + ) | ||
| 1138 | 1162 | self._next_url = next_url | |
| 1139 | 1163 | except KeyError: | |
| 1140 | 1164 | self._next_url = None | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -356,7 +356,33 @@ def test_gitlab_plain_const_does_not_warn(recwarn): | |||
| 356 | 356 | ||
| 357 | 357 | ||
| 358 | 358 | @responses.activate | |
| 359 | - def test_gitlab_keep_base_url(gl): | ||
| 359 | + @pytest.mark.parametrize( | ||
| 360 | + "kwargs,link_header,expected_next_url,show_warning", | ||
| 361 | + [ | ||
| 362 | + # normal execution | ||
| 363 | + ( | ||
| 364 | + {}, | ||
| 365 | + "<http://localhost/api/v4/tests?per_page=1&page=2>;" ' rel="next"', | ||
| 366 | + "http://localhost/api/v4/tests?per_page=1&page=2", | ||
| 367 | + False, | ||
| 368 | + ), | ||
| 369 | + # got different url and will show the warning | ||
| 370 | + ( | ||
| 371 | + {}, | ||
| 372 | + "<http://orig_host/api/v4/tests?per_page=1&page=2>;" ' rel="next"', | ||
| 373 | + "http://orig_host/api/v4/tests?per_page=1&page=2", | ||
| 374 | + True, | ||
| 375 | + ), | ||
| 376 | + # persist the base url | ||
| 377 | + ( | ||
| 378 | + {"persist_base_url": True}, | ||
| 379 | + "<http://orig_host/api/v4/tests?per_page=1&page=2>;" ' rel="next"', | ||
| 380 | + "http://localhost/api/v4/tests?per_page=1&page=2", | ||
| 381 | + False, | ||
| 382 | + ), | ||
| 383 | + ], | ||
| 384 | + ) | ||
| 385 | + def test_gitlab_persist_base_url(kwargs, link_header, expected_next_url, show_warning): | ||
| 360 | 386 | responses.add( | |
| 361 | 387 | **{ | |
| 362 | 388 | "method": responses.GET, | |
@@ -368,22 +394,19 @@ def test_gitlab_keep_base_url(gl): | |||
| 368 | 394 | "X-Per-Page": "1", | |
| 369 | 395 | "X-Total-Pages": "2", | |
| 370 | 396 | "X-Total": "2", | |
| 371 | - "Link": ( | ||
| 372 | - "<http://orig_host/api/v4/tests?per_page=1&page=2>;" ' rel="next"' | ||
| 373 | - ), | ||
| 397 | + "Link": (link_header), | ||
| 374 | 398 | }, | |
| 375 | 399 | "content_type": "application/json", | |
| 376 | 400 | "status": 200, | |
| 377 | 401 | "match": helpers.MATCH_EMPTY_QUERY_PARAMS, | |
| 378 | 402 | } | |
| 379 | 403 | ) | |
| 380 | 404 | ||
| 381 | - obj = gl.http_list("/tests", iterator=True) | ||
| 382 | - assert len(obj) == 2 | ||
| 383 | - assert obj._next_url == "http://localhost/api/v4/tests?per_page=1&page=2" | ||
| 384 | - assert obj.current_page == 1 | ||
| 385 | - assert obj.prev_page is None | ||
| 386 | - assert obj.next_page == 2 | ||
| 387 | - assert obj.per_page == 1 | ||
| 388 | - assert obj.total_pages == 2 | ||
| 389 | - assert obj.total == 2 | ||
| 405 | + gl = gitlab.Gitlab(url="http://localhost", **kwargs) | ||
| 406 | + if show_warning: | ||
| 407 | + with pytest.warns(UserWarning) as warn_record: | ||
| 408 | + obj = gl.http_list("/tests", iterator=True) | ||
| 409 | + assert len(warn_record) == 1 | ||
| 410 | + else: | ||
| 411 | + obj = gl.http_list("/tests", iterator=True) | ||
| 412 | + assert obj._next_url == expected_next_url | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments