| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 0bb0626 commit fb933c5
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -93,20 +93,26 @@ Examples: | |||
| 93 | 93 | .. code-block:: python | |
| 94 | 94 | ||
| 95 | 95 | # list all the projects | |
| 96 | - projects = gl.projects.list() | ||
| 96 | + projects = gl.projects.list(as_list=False) | ||
| 97 | 97 | for project in projects: | |
| 98 | 98 | print(project) | |
| 99 | 99 | ||
| 100 | 100 | # get the group with id == 2 | |
| 101 | 101 | group = gl.groups.get(2) | |
| 102 | - for project in group.projects.list(): | ||
| 102 | + for project in group.projects.list(as_list=False): | ||
| 103 | 103 | print(project) | |
| 104 | 104 | ||
| 105 | 105 | # create a new user | |
| 106 | 106 | user_data = {'email': 'jen@foo.com', 'username': 'jen', 'name': 'Jen'} | |
| 107 | 107 | user = gl.users.create(user_data) | |
| 108 | 108 | print(user) | |
| 109 | 109 | ||
| 110 | + .. warning:: | ||
| 111 | + Calling `list()` without any arguments will by default not return the complete list | ||
| 112 | + of items. Use either the `all=True` or `as_list=False` parameters to get all the | ||
| 113 | + items when using listing methods. See the :ref:`pagination` section for more | ||
| 114 | + information. | ||
| 115 | + | ||
| 110 | 116 | You can list the mandatory and optional attributes for object creation and | |
| 111 | 117 | update with the manager's ``get_create_attrs()`` and ``get_update_attrs()`` | |
| 112 | 118 | methods. They return 2 tuples, the first one is the list of mandatory | |
@@ -133,7 +139,7 @@ Some objects also provide managers to access related GitLab resources: | |||
| 133 | 139 | ||
| 134 | 140 | # list the issues for a project | |
| 135 | 141 | project = gl.projects.get(1) | |
| 136 | - issues = project.issues.list() | ||
| 142 | + issues = project.issues.list(all=True) | ||
| 137 | 143 | ||
| 138 | 144 | python-gitlab allows to send any data to the GitLab server when making queries. | |
| 139 | 145 | In case of invalid or missing arguments python-gitlab will raise an exception | |
@@ -150,9 +156,9 @@ conflict with python or python-gitlab when using them as kwargs: | |||
| 150 | 156 | ||
| 151 | 157 | .. code-block:: python | |
| 152 | 158 | ||
| 153 | - gl.user_activities.list(from='2019-01-01') ## invalid | ||
| 159 | + gl.user_activities.list(from='2019-01-01', as_list=False) ## invalid | ||
| 154 | 160 | ||
| 155 | - gl.user_activities.list(query_parameters={'from': '2019-01-01'}) # OK | ||
| 161 | + gl.user_activities.list(query_parameters={'from': '2019-01-01'}, as_list=False) # OK | ||
| 156 | 162 | ||
| 157 | 163 | Gitlab Objects | |
| 158 | 164 | ============== | |
@@ -222,6 +228,8 @@ a project (the previous example used 2 API calls): | |||
| 222 | 228 | project = gl.projects.get(1, lazy=True) # no API call | |
| 223 | 229 | project.star() # API call | |
| 224 | 230 | ||
| 231 | + .. _pagination: | ||
| 232 | + | ||
| 225 | 233 | Pagination | |
| 226 | 234 | ========== | |
| 227 | 235 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -24,6 +24,7 @@ | |||
| 24 | 24 | import requests.utils | |
| 25 | 25 | from requests_toolbelt.multipart.encoder import MultipartEncoder # type: ignore | |
| 26 | 26 | ||
| 27 | + import gitlab | ||
| 27 | 28 | import gitlab.config | |
| 28 | 29 | import gitlab.const | |
| 29 | 30 | import gitlab.exceptions | |
@@ -35,6 +36,12 @@ | |||
| 35 | 36 | "{source!r} to {target!r}" | |
| 36 | 37 | ) | |
| 37 | 38 | ||
| 39 | + # https://docs.gitlab.com/ee/api/#offset-based-pagination | ||
| 40 | + _PAGINATION_URL = ( | ||
| 41 | + f"https://python-gitlab.readthedocs.io/en/v{gitlab.__version__}/" | ||
| 42 | + f"api-usage.html#pagination" | ||
| 43 | + ) | ||
| 44 | + | ||
| 38 | 45 | ||
| 39 | 46 | class Gitlab: | |
| 40 | 47 | """Represents a GitLab server connection. | |
@@ -808,7 +815,7 @@ def http_list( | |||
| 808 | 815 | # In case we want to change the default behavior at some point | |
| 809 | 816 | as_list = True if as_list is None else as_list | |
| 810 | 817 | ||
| 811 | - get_all = kwargs.pop("all", False) | ||
| 818 | + get_all = kwargs.pop("all", None) | ||
| 812 | 819 | url = self._build_url(path) | |
| 813 | 820 | ||
| 814 | 821 | page = kwargs.get("page") | |
@@ -818,7 +825,28 @@ def http_list( | |||
| 818 | 825 | ||
| 819 | 826 | if page or as_list is True: | |
| 820 | 827 | # pagination requested, we return a list | |
| 821 | - return list(GitlabList(self, url, query_data, get_next=False, **kwargs)) | ||
| 828 | + gl_list = GitlabList(self, url, query_data, get_next=False, **kwargs) | ||
| 829 | + items = list(gl_list) | ||
| 830 | + if page is None and get_all is None and gl_list.per_page is not None: | ||
| 831 | + if len(items) >= gl_list.per_page and ( | ||
| 832 | + gl_list.total is None or len(items) < gl_list.total | ||
| 833 | + ): | ||
| 834 | + total_items = "10,000+" if gl_list.total is None else gl_list.total | ||
| 835 | + # Warn the user that they are only going to retrieve `per_page` | ||
| 836 | + # maximum items. This is a common cause of issues filed. | ||
| 837 | + utils.warn( | ||
| 838 | + message=( | ||
| 839 | + f"Calling a `list()` method without specifying `all=True` " | ||
| 840 | + f"or `as_list=False` will return a maximum of " | ||
| 841 | + f"{gl_list.per_page} items. Your query returned " | ||
| 842 | + f"{len(items)} of {total_items} items. See " | ||
| 843 | + f"{_PAGINATION_URL} for more details. If this was done " | ||
| 844 | + f"intentionally, then this warning can be supressed by " | ||
| 845 | + f"adding the argument `all=False` to the `list()` call." | ||
| 846 | + ), | ||
| 847 | + category=UserWarning, | ||
| 848 | + ) | ||
| 849 | + return items | ||
| 822 | 850 | ||
| 823 | 851 | # No pagination, generator requested | |
| 824 | 852 | return GitlabList(self, url, query_data, **kwargs) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,3 +1,5 @@ | |||
| 1 | + import warnings | ||
| 2 | + | ||
| 1 | 3 | import pytest | |
| 2 | 4 | ||
| 3 | 5 | import gitlab | |
@@ -81,13 +83,13 @@ def test_template_dockerfile(gl): | |||
| 81 | 83 | ||
| 82 | 84 | ||
| 83 | 85 | def test_template_gitignore(gl): | |
| 84 | - assert gl.gitignores.list() | ||
| 86 | + assert gl.gitignores.list(all=True) | ||
| 85 | 87 | gitignore = gl.gitignores.get("Node") | |
| 86 | 88 | assert gitignore.content is not None | |
| 87 | 89 | ||
| 88 | 90 | ||
| 89 | 91 | def test_template_gitlabciyml(gl): | |
| 90 | - assert gl.gitlabciymls.list() | ||
| 92 | + assert gl.gitlabciymls.list(all=True) | ||
| 91 | 93 | gitlabciyml = gl.gitlabciymls.get("Nodejs") | |
| 92 | 94 | assert gitlabciyml.content is not None | |
| 93 | 95 | ||
@@ -181,3 +183,46 @@ def test_rate_limits(gl): | |||
| 181 | 183 | settings.throttle_authenticated_api_enabled = False | |
| 182 | 184 | settings.save() | |
| 183 | 185 | [project.delete() for project in projects] | |
| 186 | + | ||
| 187 | + | ||
| 188 | + def test_list_default_warning(gl): | ||
| 189 | + """When there are more than 20 items and use default `list()` then warning is | ||
| 190 | + generated""" | ||
| 191 | + with warnings.catch_warnings(record=True) as caught_warnings: | ||
| 192 | + gl.gitlabciymls.list() | ||
| 193 | + assert len(caught_warnings) == 1 | ||
| 194 | + warning = caught_warnings[0] | ||
| 195 | + assert isinstance(warning.message, UserWarning) | ||
| 196 | + message = str(warning.message) | ||
| 197 | + assert "python-gitlab.readthedocs.io" in message | ||
| 198 | + assert __file__ == warning.filename | ||
| 199 | + | ||
| 200 | + | ||
| 201 | + def test_list_page_nowarning(gl): | ||
| 202 | + """Using `page=X` will disable the warning""" | ||
| 203 | + with warnings.catch_warnings(record=True) as caught_warnings: | ||
| 204 | + gl.gitlabciymls.list(page=1) | ||
| 205 | + assert len(caught_warnings) == 0 | ||
| 206 | + | ||
| 207 | + | ||
| 208 | + def test_list_all_false_nowarning(gl): | ||
| 209 | + """Using `all=False` will disable the warning""" | ||
| 210 | + with warnings.catch_warnings(record=True) as caught_warnings: | ||
| 211 | + gl.gitlabciymls.list(all=False) | ||
| 212 | + assert len(caught_warnings) == 0 | ||
| 213 | + | ||
| 214 | + | ||
| 215 | + def test_list_all_true_nowarning(gl): | ||
| 216 | + """Using `all=True` will disable the warning""" | ||
| 217 | + with warnings.catch_warnings(record=True) as caught_warnings: | ||
| 218 | + items = gl.gitlabciymls.list(all=True) | ||
| 219 | + assert len(caught_warnings) == 0 | ||
| 220 | + assert len(items) > 20 | ||
| 221 | + | ||
| 222 | + | ||
| 223 | + def test_list_as_list_false_nowarning(gl): | ||
| 224 | + """Using `as_list=False` will disable the warning""" | ||
| 225 | + with warnings.catch_warnings(record=True) as caught_warnings: | ||
| 226 | + items = gl.gitlabciymls.list(as_list=False) | ||
| 227 | + assert len(caught_warnings) == 0 | ||
| 228 | + assert len(list(items)) > 20 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,3 +1,6 @@ | |||
| 1 | + import copy | ||
| 2 | + import warnings | ||
| 3 | + | ||
| 1 | 4 | import pytest | |
| 2 | 5 | import requests | |
| 3 | 6 | import responses | |
@@ -329,20 +332,115 @@ def test_list_request(gl): | |||
| 329 | 332 | match=MATCH_EMPTY_QUERY_PARAMS, | |
| 330 | 333 | ) | |
| 331 | 334 | ||
| 332 | - result = gl.http_list("/projects", as_list=True) | ||
| 335 | + with warnings.catch_warnings(record=True) as caught_warnings: | ||
| 336 | + result = gl.http_list("/projects", as_list=True) | ||
| 337 | + assert len(caught_warnings) == 0 | ||
| 333 | 338 | assert isinstance(result, list) | |
| 334 | 339 | assert len(result) == 1 | |
| 335 | 340 | ||
| 336 | 341 | result = gl.http_list("/projects", as_list=False) | |
| 337 | 342 | assert isinstance(result, GitlabList) | |
| 338 | - assert len(result) == 1 | ||
| 343 | + assert len(list(result)) == 1 | ||
| 339 | 344 | ||
| 340 | 345 | result = gl.http_list("/projects", all=True) | |
| 341 | 346 | assert isinstance(result, list) | |
| 342 | 347 | assert len(result) == 1 | |
| 343 | 348 | assert responses.assert_call_count(url, 3) is True | |
| 344 | 349 | ||
| 345 | 350 | ||
| 351 | + large_list_response = { | ||
| 352 | + "method": responses.GET, | ||
| 353 | + "url": "http://localhost/api/v4/projects", | ||
| 354 | + "json": [ | ||
| 355 | + {"name": "project01"}, | ||
| 356 | + {"name": "project02"}, | ||
| 357 | + {"name": "project03"}, | ||
| 358 | + {"name": "project04"}, | ||
| 359 | + {"name": "project05"}, | ||
| 360 | + {"name": "project06"}, | ||
| 361 | + {"name": "project07"}, | ||
| 362 | + {"name": "project08"}, | ||
| 363 | + {"name": "project09"}, | ||
| 364 | + {"name": "project10"}, | ||
| 365 | + {"name": "project11"}, | ||
| 366 | + {"name": "project12"}, | ||
| 367 | + {"name": "project13"}, | ||
| 368 | + {"name": "project14"}, | ||
| 369 | + {"name": "project15"}, | ||
| 370 | + {"name": "project16"}, | ||
| 371 | + {"name": "project17"}, | ||
| 372 | + {"name": "project18"}, | ||
| 373 | + {"name": "project19"}, | ||
| 374 | + {"name": "project20"}, | ||
| 375 | + ], | ||
| 376 | + "headers": {"X-Total": "30", "x-per-page": "20"}, | ||
| 377 | + "status": 200, | ||
| 378 | + "match": MATCH_EMPTY_QUERY_PARAMS, | ||
| 379 | + } | ||
| 380 | + | ||
| 381 | + | ||
| 382 | + @responses.activate | ||
| 383 | + def test_list_request_pagination_warning(gl): | ||
| 384 | + responses.add(**large_list_response) | ||
| 385 | + | ||
| 386 | + with warnings.catch_warnings(record=True) as caught_warnings: | ||
| 387 | + result = gl.http_list("/projects", as_list=True) | ||
| 388 | + assert len(caught_warnings) == 1 | ||
| 389 | + warning = caught_warnings[0] | ||
| 390 | + assert isinstance(warning.message, UserWarning) | ||
| 391 | + message = str(warning.message) | ||
| 392 | + assert "Calling a `list()` method" in message | ||
| 393 | + assert "python-gitlab.readthedocs.io" in message | ||
| 394 | + assert __file__ == warning.filename | ||
| 395 | + assert isinstance(result, list) | ||
| 396 | + assert len(result) == 20 | ||
| 397 | + assert len(responses.calls) == 1 | ||
| 398 | + | ||
| 399 | + | ||
| 400 | + @responses.activate | ||
| 401 | + def test_list_request_as_list_false_nowarning(gl): | ||
| 402 | + responses.add(**large_list_response) | ||
| 403 | + with warnings.catch_warnings(record=True) as caught_warnings: | ||
| 404 | + result = gl.http_list("/projects", as_list=False) | ||
| 405 | + assert len(caught_warnings) == 0 | ||
| 406 | + assert isinstance(result, GitlabList) | ||
| 407 | + assert len(list(result)) == 20 | ||
| 408 | + assert len(responses.calls) == 1 | ||
| 409 | + | ||
| 410 | + | ||
| 411 | + @responses.activate | ||
| 412 | + def test_list_request_all_true_nowarning(gl): | ||
| 413 | + responses.add(**large_list_response) | ||
| 414 | + with warnings.catch_warnings(record=True) as caught_warnings: | ||
| 415 | + result = gl.http_list("/projects", all=True) | ||
| 416 | + assert len(caught_warnings) == 0 | ||
| 417 | + assert isinstance(result, list) | ||
| 418 | + assert len(result) == 20 | ||
| 419 | + assert len(responses.calls) == 1 | ||
| 420 | + | ||
| 421 | + | ||
| 422 | + @responses.activate | ||
| 423 | + def test_list_request_all_false_nowarning(gl): | ||
| 424 | + responses.add(**large_list_response) | ||
| 425 | + with warnings.catch_warnings(record=True) as caught_warnings: | ||
| 426 | + result = gl.http_list("/projects", all=False) | ||
| 427 | + assert len(caught_warnings) == 0 | ||
| 428 | + assert isinstance(result, list) | ||
| 429 | + assert len(result) == 20 | ||
| 430 | + assert len(responses.calls) == 1 | ||
| 431 | + | ||
| 432 | + | ||
| 433 | + @responses.activate | ||
| 434 | + def test_list_request_page_nowarning(gl): | ||
| 435 | + response_dict = copy.deepcopy(large_list_response) | ||
| 436 | + response_dict["match"] = [responses.matchers.query_param_matcher({"page": "1"})] | ||
| 437 | + responses.add(**response_dict) | ||
| 438 | + with warnings.catch_warnings(record=True) as caught_warnings: | ||
| 439 | + gl.http_list("/projects", page=1) | ||
| 440 | + assert len(caught_warnings) == 0 | ||
| 441 | + assert len(responses.calls) == 1 | ||
| 442 | + | ||
| 443 | + | ||
| 346 | 444 | @responses.activate | |
| 347 | 445 | def test_list_request_404(gl): | |
| 348 | 446 | url = "http://localhost/api/v4/not_there" | |
| Back | FazBrowse Home | New Git URL |
0 commit comments