Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some handling for tag edge cases #118

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
8 changes: 6 additions & 2 deletions md2cf/api.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from urllib.parse import urljoin

import requests
Expand Down Expand Up @@ -207,7 +208,9 @@ def update_page(

if labels is not None:
update_structure["metadata"] = {
"labels": [{"name": label, "prefix": "global"} for label in labels]
"labels": json.dumps(
[{"name": label, "prefix": "global"} for label in labels]
)
}

return self._put(f"content/{page.id}", json=update_structure)
Expand Down Expand Up @@ -243,7 +246,8 @@ def add_labels(self, page, labels):
# return self.api.content(page.id).post(
return self._post(
f"content/{page.id}/label",
data=[{"name": label, "type": "global"} for label in labels],
data=json.dumps([{"name": label, "type": "global"} for label in labels]),
Copy link
Author

@nate-woythaler nate-woythaler Nov 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this is the correct approach, but our Confluence instance was giving me back HTTP 415s when submitting binary data via the **kwargs expansion on line 68

415 Client Error: Unsupported Media Type for url: https://${CONFLUENCE_URL}/rest/api/content/515736017/label - b''

headers={"Content-Type": "application/json"},
)

def get_url(self, page):
Expand Down
6 changes: 5 additions & 1 deletion md2cf/upsert.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,11 @@ def labels_need_updating(page, existing_page):
return False

if sorted(
[label.name for label in existing_page.metadata.labels.results]
[
# Use `get()` here for unit test sanity -- `Mock().name` is reserved.
label.get("name")
for label in existing_page.get("metadata", {"labels": {"results": {}}})
]
) != sorted(page.labels):
return True

Expand Down
47 changes: 31 additions & 16 deletions test_package/unit/test_upsert.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,16 +276,14 @@ def test_page_needs_updating_content_replace_all_labels_and_labels_not_changed(m
)

message_hash = "[v6e71b3cac15d32fe2d36c270887df9479c25c640]"
existing_page_mock = mocker.Mock()
existing_page_mock = mocker.Mock(
get=mocker.Mock(return_value=[{"name": label} for label in labels])
)
ancestor_mock = mocker.Mock()
ancestor_mock.id = mocker.sentinel.parent_id
existing_page_mock.ancestors = [ancestor_mock]
existing_page_mock.version.message = message_hash
existing_page_mock.metadata.labels.results = []
for label in labels:
label_mock = mocker.Mock()
label_mock.name = label
existing_page_mock.metadata.labels.results.append(label_mock)

assert not md2cf.upsert.page_needs_updating(
page, existing_page_mock, replace_all_labels=True
Expand Down Expand Up @@ -424,19 +422,15 @@ def test_page_needs_updating_content_replace_all_labels_and_empty_labels_supplie
)

message_hash = "[v6e71b3cac15d32fe2d36c270887df9479c25c640]"
existing_page_mock = mocker.Mock()
labels = ["label1", "label2"]
existing_page_mock = mocker.Mock(
get=mocker.Mock(return_value=[{"name": label} for label in labels])
)
ancestor_mock = mocker.Mock()
ancestor_mock.id = mocker.sentinel.parent_id
existing_page_mock.ancestors = [ancestor_mock]
existing_page_mock.version.message = message_hash

labels = ["label1", "label2"]
existing_page_mock.metadata.labels.results = []
for label in labels:
label_mock = mocker.Mock()
label_mock.name = label
existing_page_mock.metadata.labels.results.append(label_mock)

assert md2cf.upsert.page_needs_updating(
page, existing_page_mock, replace_all_labels=True
)
Expand All @@ -456,14 +450,35 @@ def test_page_needs_updating_content_replace_all_labels_and_empty_labels_supplie
)

message_hash = "[v6e71b3cac15d32fe2d36c270887df9479c25c640]"
existing_page_mock = mocker.Mock()
existing_page_mock = mocker.Mock(get=mocker.Mock(return_value=[]))
ancestor_mock = mocker.Mock()
ancestor_mock.id = mocker.sentinel.parent_id
existing_page_mock.ancestors = [ancestor_mock]
existing_page_mock.version.message = message_hash

existing_page_mock.metadata.labels.results = []

assert not md2cf.upsert.page_needs_updating(
page, existing_page_mock, replace_all_labels=True
)


def test_page_needs_updated_created_with_no_labels_and_new_ones_were_added(mocker):
"""An existing page with no labels was created, and new labels were added
after the fact. We should update the page with these new labels"""
page = Page(
space=mocker.sentinel.space,
title=mocker.sentinel.title,
body="hello there",
labels=["foo"],
parent_id=mocker.sentinel.parent_id,
)

message_hash = "[v6e71b3cac15d32fe2d36c270887df9479c25c640]"
existing_page_mock = mocker.Mock(get=mocker.Mock(return_value=[]))
ancestor_mock = mocker.Mock()
ancestor_mock.id = mocker.sentinel.parent_id
existing_page_mock.ancestors = [ancestor_mock]
existing_page_mock.version.message = message_hash

assert md2cf.upsert.page_needs_updating(
page, existing_page_mock, replace_all_labels=True
)