| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 7ac39f6 commit c790f8c
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -18,12 +18,14 @@ | |||
| 18 | 18 | ||
| 19 | 19 | import os | |
| 20 | 20 | import time | |
| 21 | + import warnings | ||
| 21 | 22 | from typing import Any, cast, Dict, List, Optional, Tuple, TYPE_CHECKING, Union | |
| 22 | 23 | ||
| 23 | 24 | import requests | |
| 24 | 25 | import requests.utils | |
| 25 | 26 | from requests_toolbelt.multipart.encoder import MultipartEncoder # type: ignore | |
| 26 | 27 | ||
| 28 | + import gitlab | ||
| 27 | 29 | import gitlab.config | |
| 28 | 30 | import gitlab.const | |
| 29 | 31 | import gitlab.exceptions | |
@@ -35,6 +37,12 @@ | |||
| 35 | 37 | "{source!r} to {target!r}" | |
| 36 | 38 | ) | |
| 37 | 39 | ||
| 40 | + # https://docs.gitlab.com/ee/api/#offset-based-pagination | ||
| 41 | + _PAGINATION_URL = ( | ||
| 42 | + f"https://python-gitlab.readthedocs.io/en/v{gitlab.__version__}/" | ||
| 43 | + f"api-usage.html#pagination" | ||
| 44 | + ) | ||
| 45 | + | ||
| 38 | 46 | ||
| 39 | 47 | class Gitlab: | |
| 40 | 48 | """Represents a GitLab server connection. | |
@@ -818,7 +826,26 @@ def http_list( | |||
| 818 | 826 | ||
| 819 | 827 | if page or as_list is True: | |
| 820 | 828 | # pagination requested, we return a list | |
| 821 | - return list(GitlabList(self, url, query_data, get_next=False, **kwargs)) | ||
| 829 | + gl_list = GitlabList(self, url, query_data, get_next=False, **kwargs) | ||
| 830 | + items = list(gl_list) | ||
| 831 | + if ( | ||
| 832 | + page is None | ||
| 833 | + and (gl_list.per_page is not None and len(items) >= gl_list.per_page) | ||
| 834 | + and (gl_list.total is None or len(items) < gl_list.total) | ||
| 835 | + ): | ||
| 836 | + total_items = "10,000+" if gl_list.total is None else gl_list.total | ||
| 837 | + # Warn the user that they are only going to retrieve `per_page` maximum | ||
| 838 | + # items. This is a common cause of issues filed. | ||
| 839 | + warnings.warn( | ||
| 840 | + ( | ||
| 841 | + f"Calling a `list()` method without specifying `all=True` or " | ||
| 842 | + f"`as_list=False` will return a maximum of {gl_list.per_page} " | ||
| 843 | + f"items. Your query returned {len(items)} of {total_items} " | ||
| 844 | + f"items. See {_PAGINATION_URL} for more details" | ||
| 845 | + ), | ||
| 846 | + category=UserWarning, | ||
| 847 | + ) | ||
| 848 | + return items | ||
| 822 | 849 | ||
| 823 | 850 | # No pagination, generator requested | |
| 824 | 851 | return GitlabList(self, url, query_data, **kwargs) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -81,13 +81,13 @@ def test_template_dockerfile(gl): | |||
| 81 | 81 | ||
| 82 | 82 | ||
| 83 | 83 | def test_template_gitignore(gl): | |
| 84 | - assert gl.gitignores.list() | ||
| 84 | + assert gl.gitignores.list(all=True) | ||
| 85 | 85 | gitignore = gl.gitignores.get("Node") | |
| 86 | 86 | assert gitignore.content is not None | |
| 87 | 87 | ||
| 88 | 88 | ||
| 89 | 89 | def test_template_gitlabciyml(gl): | |
| 90 | - assert gl.gitlabciymls.list() | ||
| 90 | + assert gl.gitlabciymls.list(all=True) | ||
| 91 | 91 | gitlabciyml = gl.gitlabciymls.get("Nodejs") | |
| 92 | 92 | assert gitlabciyml.content is not None | |
| 93 | 93 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,7 +14,7 @@ services: | |||
| 14 | 14 | GITLAB_ROOT_PASSWORD: 5iveL!fe | |
| 15 | 15 | GITLAB_SHARED_RUNNERS_REGISTRATION_TOKEN: registration-token | |
| 16 | 16 | GITLAB_OMNIBUS_CONFIG: | | |
| 17 | - external_url 'http://127.0.0.1:8080' | ||
| 17 | + external_url 'http://localhost:8080' | ||
| 18 | 18 | registry['enable'] = false | |
| 19 | 19 | nginx['redirect_http_to_https'] = false | |
| 20 | 20 | nginx['listen_port'] = 80 | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,3 +1,5 @@ | |||
| 1 | + import warnings | ||
| 2 | + | ||
| 1 | 3 | import pytest | |
| 2 | 4 | import requests | |
| 3 | 5 | import responses | |
@@ -329,20 +331,83 @@ def test_list_request(gl): | |||
| 329 | 331 | match=MATCH_EMPTY_QUERY_PARAMS, | |
| 330 | 332 | ) | |
| 331 | 333 | ||
| 332 | - result = gl.http_list("/projects", as_list=True) | ||
| 334 | + with warnings.catch_warnings(record=True) as caught_warnings: | ||
| 335 | + result = gl.http_list("/projects", as_list=True) | ||
| 336 | + assert len(caught_warnings) == 0 | ||
| 333 | 337 | assert isinstance(result, list) | |
| 334 | 338 | assert len(result) == 1 | |
| 335 | 339 | ||
| 336 | 340 | result = gl.http_list("/projects", as_list=False) | |
| 337 | 341 | assert isinstance(result, GitlabList) | |
| 338 | - assert len(result) == 1 | ||
| 342 | + assert len(list(result)) == 1 | ||
| 339 | 343 | ||
| 340 | 344 | result = gl.http_list("/projects", all=True) | |
| 341 | 345 | assert isinstance(result, list) | |
| 342 | 346 | assert len(result) == 1 | |
| 343 | 347 | assert responses.assert_call_count(url, 3) is True | |
| 344 | 348 | ||
| 345 | 349 | ||
| 350 | + @responses.activate | ||
| 351 | + def test_list_request_pagination_warning(gl): | ||
| 352 | + url = "http://localhost/api/v4/projects" | ||
| 353 | + responses.add( | ||
| 354 | + method=responses.GET, | ||
| 355 | + url=url, | ||
| 356 | + json=[ | ||
| 357 | + {"name": "project01"}, | ||
| 358 | + {"name": "project02"}, | ||
| 359 | + {"name": "project03"}, | ||
| 360 | + {"name": "project04"}, | ||
| 361 | + {"name": "project05"}, | ||
| 362 | + {"name": "project06"}, | ||
| 363 | + {"name": "project07"}, | ||
| 364 | + {"name": "project08"}, | ||
| 365 | + {"name": "project09"}, | ||
| 366 | + {"name": "project10"}, | ||
| 367 | + {"name": "project11"}, | ||
| 368 | + {"name": "project12"}, | ||
| 369 | + {"name": "project13"}, | ||
| 370 | + {"name": "project14"}, | ||
| 371 | + {"name": "project15"}, | ||
| 372 | + {"name": "project16"}, | ||
| 373 | + {"name": "project17"}, | ||
| 374 | + {"name": "project18"}, | ||
| 375 | + {"name": "project19"}, | ||
| 376 | + {"name": "project20"}, | ||
| 377 | + ], | ||
| 378 | + headers={"X-Total": "30", "x-per-page": "20"}, | ||
| 379 | + status=200, | ||
| 380 | + match=MATCH_EMPTY_QUERY_PARAMS, | ||
| 381 | + ) | ||
| 382 | + | ||
| 383 | + with warnings.catch_warnings(record=True) as caught_warnings: | ||
| 384 | + result = gl.http_list("/projects", as_list=True) | ||
| 385 | + assert len(caught_warnings) == 1 | ||
| 386 | + warning = caught_warnings[0] | ||
| 387 | + assert isinstance(warning.message, UserWarning) | ||
| 388 | + message = str(caught_warnings[0].message) | ||
| 389 | + assert "Calling" in message | ||
| 390 | + assert "return a maximum of" in message | ||
| 391 | + assert "readthedocs" in message | ||
| 392 | + | ||
| 393 | + assert isinstance(result, list) | ||
| 394 | + assert len(result) == 20 | ||
| 395 | + | ||
| 396 | + with warnings.catch_warnings(record=True) as caught_warnings: | ||
| 397 | + result = gl.http_list("/projects", as_list=False) | ||
| 398 | + assert len(caught_warnings) == 0 | ||
| 399 | + assert isinstance(result, GitlabList) | ||
| 400 | + assert len(list(result)) == 20 | ||
| 401 | + | ||
| 402 | + with warnings.catch_warnings(record=True) as caught_warnings: | ||
| 403 | + result = gl.http_list("/projects", all=True) | ||
| 404 | + assert len(caught_warnings) == 0 | ||
| 405 | + | ||
| 406 | + assert isinstance(result, list) | ||
| 407 | + assert len(result) == 20 | ||
| 408 | + assert responses.assert_call_count(url, 3) is True | ||
| 409 | + | ||
| 410 | + | ||
| 346 | 411 | @responses.activate | |
| 347 | 412 | def test_list_request_404(gl): | |
| 348 | 413 | url = "http://localhost/api/v4/not_there" | |
| Back | FazBrowse Home | New Git URL |
0 commit comments