Skip to content

Commit 978fa3f

Browse files
authored
[Jira] Fix paged projects (#1344)
* [Jira] Change paged projects * Remove workaround * Fix pipeline errors --------- Co-authored-by: Spacetown <spacetown@users.noreply.github.com>
1 parent 7aeb30f commit 978fa3f

File tree

1 file changed

+66
-73
lines changed

1 file changed

+66
-73
lines changed

atlassian/jira.py

Lines changed: 66 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,62 @@ def __init__(self, url, *args, **kwargs):
2424

2525
super(Jira, self).__init__(url, *args, **kwargs)
2626

27+
def _get_paged(
28+
self,
29+
url,
30+
params=None,
31+
data=None,
32+
flags=None,
33+
trailing=None,
34+
absolute=False,
35+
):
36+
"""
37+
Used to get the paged data
38+
39+
:param url: string: The url to retrieve
40+
:param params: dict (default is None): The parameter's
41+
:param data: dict (default is None): The data
42+
:param flags: string[] (default is None): The flags
43+
:param trailing: bool (default is None): If True, a trailing slash is added to the url
44+
:param absolute: bool (default is False): If True, the url is used absolute and not relative to the root
45+
46+
:return: A generator object for the data elements
47+
"""
48+
49+
if self.cloud:
50+
if params is None:
51+
params = {}
52+
53+
while True:
54+
response = super(Jira, self).get(
55+
url,
56+
trailing=trailing,
57+
params=params,
58+
data=data,
59+
flags=flags,
60+
absolute=absolute,
61+
)
62+
values = response.get("values", [])
63+
for value in values:
64+
yield value
65+
66+
if response.get("isLast", False) or len(values) == 0:
67+
break
68+
69+
url = response.get("nextPage")
70+
if url is None:
71+
break
72+
# From now on we have absolute URLs with parameters
73+
absolute = True
74+
# Params are now provided by the url
75+
params = {}
76+
# Trailing should not be added as it is already part of the url
77+
trailing = False
78+
else:
79+
raise ValueError("``_get_paged`` method is only available for Jira Cloud platform")
80+
81+
return
82+
2783
def get_permissions(
2884
self,
2985
permissions,
@@ -2297,86 +2353,23 @@ def projects(self, included_archived=None, expand=None):
22972353
:param expand:
22982354
:return:
22992355
"""
2300-
if self.cloud:
2301-
return self.projects_from_cloud(
2302-
included_archived=included_archived,
2303-
expand=expand,
2304-
)
2305-
else:
2306-
return self.projects_from_server(
2307-
included_archived=included_archived,
2308-
expand=expand,
2309-
)
2310-
2311-
def projects_from_cloud(self, included_archived=None, expand=None):
2312-
"""
2313-
Returns all projects which are visible for the currently logged-in user.
2314-
Cloud version should use the ``paginated``endpoint to get pages of projects, as the old endpoint is deprecated.
2315-
If no user is logged in, it returns the list of projects that are visible when using anonymous access.
2316-
:param included_archived: boolean whether to include archived projects in response, default: false
2317-
:param expand:
2318-
:return:
2319-
"""
2320-
if not self.cloud:
2321-
raise ValueError("``projects_from_cloud`` method is only available for Jira Cloud platform")
2322-
2323-
projects = self.paginated_projects(
2324-
included_archived=included_archived,
2325-
expand=expand,
2326-
)
2327-
is_last_page = projects.get("isLast")
2328-
next_page_url = projects.get("nextPage")
2329-
while not is_last_page:
2330-
next_page_projects = self.paginated_projects(
2331-
included_archived=included_archived,
2332-
expand=expand,
2333-
url=next_page_url,
2334-
)
2335-
next_page_url = next_page_projects.get("nextPage")
2336-
is_last_page = next_page_projects.get("isLast")
2337-
projects["values"].extend(next_page_projects["values"])
2338-
return projects["values"]
2339-
2340-
def paginated_projects(self, included_archived=None, expand=None, url=None):
2341-
"""
2342-
Returns a page of projects which are visible for the currently logged-in user.
2343-
Method to be used only for Jira Cloud platform, until tests on Jira Server are executed.
2344-
If no user is logged in, it returns the list of projects that are visible when using anonymous access.
2345-
:param included_archived: boolean whether to include archived projects in response, default: false
2346-
:param expand:
2347-
:param url: url to get the next page of projects, default: false (first page)
2348-
:return:
2349-
"""
2350-
if not self.cloud:
2351-
raise ValueError("``projects_from_cloud`` method is only available for Jira Cloud platform")
23522356

23532357
params = {}
23542358
if included_archived:
23552359
params["includeArchived"] = included_archived
23562360
if expand:
23572361
params["expand"] = expand
2358-
page_url = url or self.resource_url("project/search")
2359-
is_url_absolute = bool(page_url.lower().startswith("http"))
2360-
return self.get(page_url, params=params, absolute=is_url_absolute)
2361-
2362-
def projects_from_server(self, included_archived=None, expand=None):
2363-
"""
2364-
Returns all projects which are visible for the currently logged-in user.
2365-
If no user is logged in, it returns the list of projects that are visible when using anonymous access.
2366-
:param included_archived: boolean whether to include archived projects in response, default: false
2367-
:param expand:
2368-
:return:
2369-
"""
23702362
if self.cloud:
2371-
raise ValueError("``projects_from_server`` method is only available for Jira Server platform")
2372-
2373-
params = {}
2374-
if included_archived:
2375-
params["includeArchived"] = included_archived
2376-
if expand:
2377-
params["expand"] = expand
2378-
url = self.resource_url("project")
2379-
return self.get(url, params=params)
2363+
return list(
2364+
self._get_paged(
2365+
self.resource_url("project/search"),
2366+
params,
2367+
paging_workaround=True,
2368+
)
2369+
)
2370+
else:
2371+
url = self.resource_url("project")
2372+
return self.get(url, params=params)
23802373

23812374
def create_project_from_raw_json(self, json):
23822375
"""

0 commit comments

Comments
 (0)