Skip to content

Commit 364b16f

Browse files
authored
[Jira]: Migrate get all projects methods to use paginated endpoint for Jira Cloud. (#1270)
* [Jira]: Migrate get all projects to use paginated endpoint for Jira Cloud. Use correct method in archive project function. * [Jira]: use absolute for projects paginated function
1 parent 85f116f commit 364b16f

File tree

2 files changed

+90
-4
lines changed

2 files changed

+90
-4
lines changed

atlassian/jira.py

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2110,12 +2110,83 @@ def get_all_projects(self, included_archived=None, expand=None):
21102110
return self.projects(included_archived, expand)
21112111

21122112
def projects(self, included_archived=None, expand=None):
2113-
"""Returns all projects which are visible for the currently logged-in user.
2113+
"""
2114+
Returns all projects which are visible for the currently logged-in user.
2115+
If no user is logged in, it returns the list of projects that are visible when using anonymous access.
2116+
:param included_archived: boolean whether to include archived projects in response, default: false
2117+
:param expand:
2118+
:return:
2119+
"""
2120+
if self.cloud:
2121+
return self.projects_from_cloud(
2122+
included_archived=included_archived,
2123+
expand=expand,
2124+
)
2125+
else:
2126+
return self.projects_from_server(
2127+
included_archived=included_archived,
2128+
expand=expand,
2129+
)
2130+
2131+
def projects_from_cloud(self, included_archived=None, expand=None):
2132+
"""
2133+
Returns all projects which are visible for the currently logged-in user.
2134+
Cloud version should use the ``paginated``endpoint to get pages of projects, as the old endpoint is deprecated.
21142135
If no user is logged in, it returns the list of projects that are visible when using anonymous access.
21152136
:param included_archived: boolean whether to include archived projects in response, default: false
21162137
:param expand:
21172138
:return:
21182139
"""
2140+
if not self.cloud:
2141+
raise ValueError("``projects_from_cloud`` method is only available for Jira Cloud platform")
2142+
2143+
projects = self.paginated_projects(
2144+
included_archived=included_archived,
2145+
expand=expand,
2146+
)
2147+
while not projects.get("isLast"):
2148+
projects["values"].extend(
2149+
self.paginated_projects(
2150+
included_archived=included_archived,
2151+
expand=expand,
2152+
url=projects["nextPage"],
2153+
)["values"]
2154+
)
2155+
return projects["values"]
2156+
2157+
def paginated_projects(self, included_archived=None, expand=None, url=None):
2158+
"""
2159+
Returns a page of projects which are visible for the currently logged-in user.
2160+
Method to be used only for Jira Cloud platform, until tests on Jira Server are executed.
2161+
If no user is logged in, it returns the list of projects that are visible when using anonymous access.
2162+
:param included_archived: boolean whether to include archived projects in response, default: false
2163+
:param expand:
2164+
:param url: url to get the next page of projects, default: false (first page)
2165+
:return:
2166+
"""
2167+
if not self.cloud:
2168+
raise ValueError("``projects_from_cloud`` method is only available for Jira Cloud platform")
2169+
2170+
params = {}
2171+
if included_archived:
2172+
params["includeArchived"] = included_archived
2173+
if expand:
2174+
params["expand"] = expand
2175+
page_url = url or self.resource_url("project/search")
2176+
is_url_absolute = bool(page_url.lower().startswith("http"))
2177+
return self.get(page_url, params=params, absolute=is_url_absolute)
2178+
2179+
def projects_from_server(self, included_archived=None, expand=None):
2180+
"""
2181+
Returns all projects which are visible for the currently logged-in user.
2182+
If no user is logged in, it returns the list of projects that are visible when using anonymous access.
2183+
:param included_archived: boolean whether to include archived projects in response, default: false
2184+
:param expand:
2185+
:return:
2186+
"""
2187+
if self.cloud:
2188+
raise ValueError("``projects_from_server`` method is only available for Jira Server platform")
2189+
21192190
params = {}
21202191
if included_archived:
21212192
params["includeArchived"] = included_archived
@@ -2180,7 +2251,7 @@ def archive_project(self, key):
21802251
"""
21812252
base_url = self.resource_url("project")
21822253
url = "{base_url}/{key}/archive".format(base_url=base_url, key=key)
2183-
return self.put(url)
2254+
return self.post(url)
21842255

21852256
def project(self, key, expand=None):
21862257
"""

docs/jira.rst

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,26 @@ Manage projects
112112
113113
# Get all projects
114114
# Returns all projects which are visible for the currently logged in user.
115-
jira.projects(included_archived=None)
115+
jira.projects(included_archived=None, expand=None)
116116
117117
# Get all project alternative call
118118
# Returns all projects which are visible for the currently logged in user.
119-
jira.get_all_projects(included_archived=None)
119+
jira.get_all_projects(included_archived=None, expand=None)
120+
121+
# Get all projects only for Jira Cloud
122+
# Returns all projects which are visible for the currently logged in user.
123+
jira.projects_from_cloud(included_archived=None, expand=None)
124+
125+
# Get one page of projects
126+
# Returns a paginated list of projects visible for the currently logged in user.
127+
# Use the url formatting to get a specific page as shown here:
128+
# url = f"{self.resource_url("project/search")}?startAt={start_at}&maxResults={max_results}"
129+
# Defaults to the first page, which returns a nextPage url when available.
130+
jira.projects_paginated(included_archived=None, expand=None, url=None)
131+
132+
# Get all projects only for Jira Server
133+
# Returns all projects which are visible for the currently logged in user.
134+
jira.projects_from_server(included_archived=None, expand=None)
120135
121136
# Delete project
122137
jira.delete_project(key)

0 commit comments

Comments
 (0)