diff --git a/md2cf/__main__.py b/md2cf/__main__.py index 7db41fb..20f6f92 100644 --- a/md2cf/__main__.py +++ b/md2cf/__main__.py @@ -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, @@ -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]) @@ -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() diff --git a/md2cf/api.py b/md2cf/api.py index 54ba44a..f96b2bd 100644 --- a/md2cf/api.py +++ b/md2cf/api.py @@ -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 def _get(self, path, **kwargs): return self._request("GET", path, **kwargs) @@ -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, @@ -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): + 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",