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

Implement --clear flag to delete subpages before uploading #114

Open
wants to merge 6 commits into
base: develop
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
27 changes: 27 additions & 0 deletions md2cf/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,11 @@ def get_parser():
help="only upload pages and attachments that have changed. "
"This adds a hash of the page or attachment contents to the update message",
)
parser.add_argument(
"--clear",
action="store_true",
help="delete all subpages of the parent pages before uploading new ones",
)
parser.add_argument(
"file_list",
type=Path,
Expand Down Expand Up @@ -317,6 +322,15 @@ def main():
)
sys.exit(1)

# if --clear is detected, we need to delete all the pages in the space or subpages of the parent pages
if args.clear:
pages_to_delete = []
if args.parent_id:
pages_to_delete = confluence.get_all_pages_from_parent_id(args.parent_id)

for page in pages_to_delete:
delete_page_and_subpages(confluence, page.id)

pages_to_upload = collect_pages_to_upload(args)

page_title_counts = Counter([page.title for page in pages_to_upload])
Expand Down Expand Up @@ -740,5 +754,18 @@ def collect_pages_to_upload(args):
return pages_to_upload


def delete_page_and_subpages(confluence, page_id):
pages_to_delete = confluence.get_all_pages_from_parent_id(page_id)
for page in pages_to_delete:
try:
delete_page_and_subpages(confluence, page.id)
console.log(f"Deleted page {page.title}")
except HTTPError as e:
error_console.log(
f"Failed to delete page {page.title} with error {e.response.content}"
)
confluence.delete_page(page_id)


if __name__ == "__main__":
main()
16 changes: 15 additions & 1 deletion md2cf/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ def __init__(
def _request(self, method, path, **kwargs):
r = self.api.request(method, urljoin(self.host, path), **kwargs)
r.raise_for_status()
return bunchify(r.json())

if r.status_code != 204:
return bunchify(r.json())
else:
return None
stevengoossensB marked this conversation as resolved.
Show resolved Hide resolved

def _get(self, path, **kwargs):
return self._request("GET", path, **kwargs)
Expand All @@ -77,6 +81,9 @@ def _post(self, path, **kwargs):
def _put(self, path, **kwargs):
return self._request("PUT", path, **kwargs)

def _delete(self, path, **kwargs):
return self._request("DELETE", path, **kwargs)

def get_page(
self,
title=None,
Expand Down Expand Up @@ -212,6 +219,13 @@ def update_page(

return self._put(f"content/{page.id}", json=update_structure)

def delete_page(self, confluence_page):
return self._delete(f"content/{confluence_page.id}")

def get_all_pages_from_parent_id(self, parent_id):
stevengoossensB marked this conversation as resolved.
Show resolved Hide resolved
pages = self._get(f"content/{parent_id}/child/page")['results']
return pages

def get_attachment(self, confluence_page, name):
existing_attachments = self._get(
f"content/{confluence_page.id}/child/attachment",
Expand Down