Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions atlassian/confluence/cloud/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,16 @@ def get_descendant_pages(self, content_id, **kwargs):
return self.get(f"rest/api/content/{content_id}/descendant/page", **kwargs)

def get_content_ancestors(self, content_id, **kwargs):
"""Get ancestor content."""
return self.get(f"rest/api/content/{content_id}/ancestors", **kwargs)
"""Get ancestor content.

The v1 content API has no ancestors endpoint, so the ancestors are read
from the content itself with ``expand=ancestors``.

:return: list of ancestors, from the top-level page down to the parent
"""
params = {**(kwargs.pop("params", None) or {}), "expand": "ancestors"}
content = self.get(f"rest/api/content/{content_id}", params=params, **kwargs)
return (content or {}).get("ancestors", [])

def get_page_by_title(self, space_key, title, **kwargs):
"""Get page by title and space key."""
Expand Down
24 changes: 21 additions & 3 deletions tests/confluence/test_confluence_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,10 +564,28 @@ def test_get_content_descendants(self, mock_get, confluence_cloud):
@patch.object(ConfluenceCloud, "get")
def test_get_content_ancestors(self, mock_get, confluence_cloud):
"""Test get_content_ancestors method."""
mock_get.return_value = {"results": [{"id": "111", "title": "Ancestor Page"}]}
mock_get.return_value = {"id": "123", "ancestors": [{"id": "111", "title": "Ancestor Page"}]}
result = confluence_cloud.get_content_ancestors("123")
mock_get.assert_called_once_with("rest/api/content/123/ancestors", **{})
assert result == {"results": [{"id": "111", "title": "Ancestor Page"}]}
mock_get.assert_called_once_with("rest/api/content/123", params={"expand": "ancestors"})
assert result == [{"id": "111", "title": "Ancestor Page"}]

@patch.object(ConfluenceCloud, "get")
def test_get_content_ancestors_keeps_extra_params(self, mock_get, confluence_cloud):
mock_get.return_value = {"id": "123", "ancestors": []}

assert confluence_cloud.get_content_ancestors("123", params={"status": "draft"}) == []
mock_get.assert_called_once_with("rest/api/content/123", params={"status": "draft", "expand": "ancestors"})

def test_get_content_ancestors_requests_wiki_content_endpoint(self, confluence_cloud):
response = Response()
response.status_code = 200
response._content = b'{"id": "123", "ancestors": [{"id": "111"}, {"id": "112"}]}'

with patch.object(confluence_cloud._session, "request", return_value=response) as mock_request:
assert confluence_cloud.get_content_ancestors("123") == [{"id": "111"}, {"id": "112"}]

url = mock_request.call_args.kwargs["url"]
assert url == "https://test.atlassian.net/wiki/rest/api/content/123?expand=ancestors"

# Space Management Tests
@patch.object(ConfluenceCloud, "get")
Expand Down
Loading