Skip to content

Commit

Permalink
[Jira]: Migrate get all projects methods to use paginated endpoint fo…
Browse files Browse the repository at this point in the history
…r 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
  • Loading branch information
marinone94 authored Oct 29, 2023
1 parent 85f116f commit 364b16f
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 4 deletions.
75 changes: 73 additions & 2 deletions atlassian/jira.py
Original file line number Diff line number Diff line change
Expand Up @@ -2110,12 +2110,83 @@ def get_all_projects(self, included_archived=None, expand=None):
return self.projects(included_archived, expand)

def projects(self, included_archived=None, expand=None):
"""Returns all projects which are visible for the currently logged-in user.
"""
Returns all projects which are visible for the currently logged-in user.
If no user is logged in, it returns the list of projects that are visible when using anonymous access.
:param included_archived: boolean whether to include archived projects in response, default: false
:param expand:
:return:
"""
if self.cloud:
return self.projects_from_cloud(
included_archived=included_archived,
expand=expand,
)
else:
return self.projects_from_server(
included_archived=included_archived,
expand=expand,
)

def projects_from_cloud(self, included_archived=None, expand=None):
"""
Returns all projects which are visible for the currently logged-in user.
Cloud version should use the ``paginated``endpoint to get pages of projects, as the old endpoint is deprecated.
If no user is logged in, it returns the list of projects that are visible when using anonymous access.
:param included_archived: boolean whether to include archived projects in response, default: false
:param expand:
:return:
"""
if not self.cloud:
raise ValueError("``projects_from_cloud`` method is only available for Jira Cloud platform")

projects = self.paginated_projects(
included_archived=included_archived,
expand=expand,
)
while not projects.get("isLast"):
projects["values"].extend(
self.paginated_projects(
included_archived=included_archived,
expand=expand,
url=projects["nextPage"],
)["values"]
)
return projects["values"]

def paginated_projects(self, included_archived=None, expand=None, url=None):
"""
Returns a page of projects which are visible for the currently logged-in user.
Method to be used only for Jira Cloud platform, until tests on Jira Server are executed.
If no user is logged in, it returns the list of projects that are visible when using anonymous access.
:param included_archived: boolean whether to include archived projects in response, default: false
:param expand:
:param url: url to get the next page of projects, default: false (first page)
:return:
"""
if not self.cloud:
raise ValueError("``projects_from_cloud`` method is only available for Jira Cloud platform")

params = {}
if included_archived:
params["includeArchived"] = included_archived
if expand:
params["expand"] = expand
page_url = url or self.resource_url("project/search")
is_url_absolute = bool(page_url.lower().startswith("http"))
return self.get(page_url, params=params, absolute=is_url_absolute)

def projects_from_server(self, included_archived=None, expand=None):
"""
Returns all projects which are visible for the currently logged-in user.
If no user is logged in, it returns the list of projects that are visible when using anonymous access.
:param included_archived: boolean whether to include archived projects in response, default: false
:param expand:
:return:
"""
if self.cloud:
raise ValueError("``projects_from_server`` method is only available for Jira Server platform")

params = {}
if included_archived:
params["includeArchived"] = included_archived
Expand Down Expand Up @@ -2180,7 +2251,7 @@ def archive_project(self, key):
"""
base_url = self.resource_url("project")
url = "{base_url}/{key}/archive".format(base_url=base_url, key=key)
return self.put(url)
return self.post(url)

def project(self, key, expand=None):
"""
Expand Down
19 changes: 17 additions & 2 deletions docs/jira.rst
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,26 @@ Manage projects
# Get all projects
# Returns all projects which are visible for the currently logged in user.
jira.projects(included_archived=None)
jira.projects(included_archived=None, expand=None)
# Get all project alternative call
# Returns all projects which are visible for the currently logged in user.
jira.get_all_projects(included_archived=None)
jira.get_all_projects(included_archived=None, expand=None)
# Get all projects only for Jira Cloud
# Returns all projects which are visible for the currently logged in user.
jira.projects_from_cloud(included_archived=None, expand=None)
# Get one page of projects
# Returns a paginated list of projects visible for the currently logged in user.
# Use the url formatting to get a specific page as shown here:
# url = f"{self.resource_url("project/search")}?startAt={start_at}&maxResults={max_results}"
# Defaults to the first page, which returns a nextPage url when available.
jira.projects_paginated(included_archived=None, expand=None, url=None)
# Get all projects only for Jira Server
# Returns all projects which are visible for the currently logged in user.
jira.projects_from_server(included_archived=None, expand=None)
# Delete project
jira.delete_project(key)
Expand Down

0 comments on commit 364b16f

Please sign in to comment.