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

[Confluence] added new method get_space_export + docs + examples #1466

Merged
merged 48 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
2e8e1b0
fixing minor issue in scrap_regex_from_issue method
Feb 3, 2024
715463b
new Confluence method scrap_regex_from_page+ docs + examples
Feb 4, 2024
4db577a
Merge branch 'atlassian-api:master' into master
gkowalc Feb 4, 2024
e13b5ba
Merge branch 'atlassian-api:master' into master
gkowalc Feb 8, 2024
9fd7ae4
added method get_attachments_ids_from_page to jira.py
Feb 8, 2024
f695346
added method download_attachments_from_issue
Feb 8, 2024
6f3c1f6
refactoring download_all_attachments_from_page method
Feb 8, 2024
7fd73d8
finished download_attachments_from_issue
Feb 9, 2024
ef752ab
added two new methods: download_attachments.from_issue and get_attac…
Feb 9, 2024
999de01
Merge branch 'atlassian-api:master' into master
gkowalc Feb 9, 2024
2edee0c
Merge branch 'atlassian-api:master' into master
gkowalc Feb 10, 2024
fe7c1f1
Merge branch 'master' of github.com:gkowalc/atlassian-python-api
Feb 15, 2024
bf7dcb4
Merge branch 'master' of github.com:gkowalc/atlassian-python-api
Feb 27, 2024
7d6b0a0
Merge branch 'master' of github.com:gkowalc/atlassian-python-api
Mar 13, 2024
a56b1be
added fix to the infinitive loop
Mar 13, 2024
0802a35
Merge branch 'master' of github.com:gkowalc/atlassian-python-api
Mar 19, 2024
c23e7cd
adding reursion depth condition
Mar 19, 2024
e8e8959
fixed reursion depth condition
Mar 19, 2024
8c2824b
added update4d jira.py with new method + docs +example
Mar 19, 2024
9b1c9c4
added update4d jira.py with new method + docs +exampl
Mar 19, 2024
08d4acb
fix flake8 issue
Mar 19, 2024
20cae0b
Merge branch 'atlassian-api:master' into master
gkowalc Mar 20, 2024
26cf691
hotfix get_issue_tree_recursive
Mar 20, 2024
4cc2a03
Merge branch 'master' of github.com:gkowalc/atlassian-python-api
Mar 22, 2024
10ee67e
added expand to get_issue method, added new method
Mar 22, 2024
273401b
get_issue_status_changelog method
Mar 22, 2024
b70f618
included param expand in get_issue method decription
Mar 22, 2024
6bcd2c0
Merge branch 'atlassian-api:master' into master
gkowalc Mar 29, 2024
6a83b0b
WIP PR changes - https://github.com/atlassian-api/atlassian-python-ap…
Mar 29, 2024
0a6367c
Merge branch 'atlassian-api:master' into master
gkowalc Apr 12, 2024
5596fd5
improving index.rst docs https://github.com/atlassian-api/atlassian-p…
Apr 12, 2024
7b359a2
Merge branch 'atlassian-api:master' into master
gkowalc Apr 21, 2024
19aaf29
added whiteboard methods
Apr 21, 2024
8233f84
Merge branch 'master' of github.com:gkowalc/atlassian-python-api
Apr 21, 2024
fd3c1b6
added confluence whiteboard endpoints
Apr 21, 2024
a8956db
Merge branch 'atlassian-api:master' into master
gkowalc Aug 6, 2024
ec2a1e6
Merge branch 'atlassian-api:master' into master
gkowalc Sep 24, 2024
083da82
Merge branch 'atlassian-api:master' into master
gkowalc Sep 30, 2024
5e0217f
fixing bug with default parameters in jira get_issue_tree_recursive m…
Sep 30, 2024
c880174
Merge branch 'atlassian-api:master' into master
gkowalc Oct 2, 2024
bfa7eae
export to csv/html/xml method
Oct 4, 2024
ab2393d
refactoring get_space_export
Oct 4, 2024
854f58c
Merge branch 'atlassian-api:master' into master
gkowalc Oct 15, 2024
feb3288
Merge branch 'master' of github.com:gkowalc/atlassian-python-api
Oct 15, 2024
b1698ad
added new get_space_export method
Oct 15, 2024
bc2a601
added new get_space_export method + small refactor
Oct 15, 2024
03bd7d6
added examples + focs for confluence/get_space_export method
Oct 15, 2024
b2df92c
fixing small typos
Oct 15, 2024
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
98 changes: 98 additions & 0 deletions atlassian/confluence.py
Original file line number Diff line number Diff line change
Expand Up @@ -2657,6 +2657,103 @@
url = "exportword?pageId={pageId}".format(pageId=page_id)
return self.get(url, headers=headers, not_json_response=True)

def get_space_export(self, space_key: str, export_type: str) -> str:
"""
Export a Confluence space to a file of the specified type.
(!) This method was developed for Confluence Cloud and may not work with Confluence on-prem.
(!) This is an experimental method that does not trigger an officially supported REST endpoint. It may break if Atlassian changes the space export front-end logic.

:param space_key: The key of the space to export.
:param export_type: The type of export to perform. Valid values are: 'html', 'csv', 'xml', 'pdf'.
:return: The URL to download the exported file.
"""

def get_atl_request(url: str):

Check warning on line 2671 in atlassian/confluence.py

View check run for this annotation

Codecov / codecov/patch

atlassian/confluence.py#L2671

Added line #L2671 was not covered by tests
# Nested function used to get atl_token used for XSRF protection. this is only applicable to html/csv/xml space exports
try:
response = self.get(url, advanced_mode=True)
parsed_html = BeautifulSoup(response.text, "html.parser")
atl_token = parsed_html.find("input", {"name": "atl_token"}).get("value")
return atl_token
except Exception as e:
raise ApiError("Problems with getting the atl_token for get_space_export method :", reason=e)

Check warning on line 2679 in atlassian/confluence.py

View check run for this annotation

Codecov / codecov/patch

atlassian/confluence.py#L2673-L2679

Added lines #L2673 - L2679 were not covered by tests

# Checks if space_ke parameter is valid and if api_token has relevant permissions to space
self.get_space(space_key=space_key, expand="permissions")

Check warning on line 2682 in atlassian/confluence.py

View check run for this annotation

Codecov / codecov/patch

atlassian/confluence.py#L2682

Added line #L2682 was not covered by tests

try:
log.info(

Check warning on line 2685 in atlassian/confluence.py

View check run for this annotation

Codecov / codecov/patch

atlassian/confluence.py#L2684-L2685

Added lines #L2684 - L2685 were not covered by tests
"Initiated experimental get_space_export method for export type: "
+ export_type
+ " from Confluence space: "
+ space_key
)
if export_type == "csv":
form_data = {

Check warning on line 2692 in atlassian/confluence.py

View check run for this annotation

Codecov / codecov/patch

atlassian/confluence.py#L2692

Added line #L2692 was not covered by tests
"atl_token": get_atl_request(f"spaces/exportspacecsv.action?key={space_key}"),
"exportType": "TYPE_CSV",
"contentOption": "all",
"includeComments": "true",
"confirm": "Export",
}
elif export_type == "html":
form_data = {

Check warning on line 2700 in atlassian/confluence.py

View check run for this annotation

Codecov / codecov/patch

atlassian/confluence.py#L2700

Added line #L2700 was not covered by tests
"atl_token": get_atl_request(f"spaces/exportspacehtml.action?key={space_key}"),
"exportType": "TYPE_HTML",
"contentOption": "visibleOnly",
"includeComments": "true",
"confirm": "Export",
}
elif export_type == "xml":
form_data = {

Check warning on line 2708 in atlassian/confluence.py

View check run for this annotation

Codecov / codecov/patch

atlassian/confluence.py#L2708

Added line #L2708 was not covered by tests
"atl_token": get_atl_request(f"spaces/exportspacexml.action?key={space_key}"),
"exportType": "TYPE_XML",
"contentOption": "all",
"includeComments": "true",
"confirm": "Export",
}
elif export_type == "pdf":
url = "spaces/flyingpdf/doflyingpdf.action?key=" + space_key
log.info("Initiate PDF space export from space " + str(space_key))
return self.get_pdf_download_url_for_confluence_cloud(url)

Check warning on line 2718 in atlassian/confluence.py

View check run for this annotation

Codecov / codecov/patch

atlassian/confluence.py#L2716-L2718

Added lines #L2716 - L2718 were not covered by tests
else:
raise ValueError("Invalid export_type parameter value. Valid values are: 'html/csv/xml/pdf'")
url = self.url_joiner(url=self.url, path=f"spaces/doexportspace.action?key={space_key}")

Check warning on line 2721 in atlassian/confluence.py

View check run for this annotation

Codecov / codecov/patch

atlassian/confluence.py#L2720-L2721

Added lines #L2720 - L2721 were not covered by tests

# Sending a POST request that triggers the space export.
response = self.session.post(url, headers=self.form_token_headers, data=form_data)
parsed_html = BeautifulSoup(response.text, "html.parser")

Check warning on line 2725 in atlassian/confluence.py

View check run for this annotation

Codecov / codecov/patch

atlassian/confluence.py#L2724-L2725

Added lines #L2724 - L2725 were not covered by tests
# Getting the poll URL to get the export progress status
try:
poll_url = parsed_html.find("meta", {"name": "ajs-pollURI"}).get("content")
except Exception as e:
raise ApiError("Problems with getting the poll_url for get_space_export method :", reason=e)
running_task = True

Check warning on line 2731 in atlassian/confluence.py

View check run for this annotation

Codecov / codecov/patch

atlassian/confluence.py#L2727-L2731

Added lines #L2727 - L2731 were not covered by tests
while running_task:
try:
progress_response = self.get(poll_url)
log.info("Space" + space_key + " export status: " + progress_response["message"])

Check warning on line 2735 in atlassian/confluence.py

View check run for this annotation

Codecov / codecov/patch

atlassian/confluence.py#L2733-L2735

Added lines #L2733 - L2735 were not covered by tests
if progress_response["complete"]:
parsed_html = BeautifulSoup(progress_response["message"], "html.parser")
download_url = parsed_html.find("a", {"class": "space-export-download-path"}).get("href")

Check warning on line 2738 in atlassian/confluence.py

View check run for this annotation

Codecov / codecov/patch

atlassian/confluence.py#L2737-L2738

Added lines #L2737 - L2738 were not covered by tests
if self.url in download_url:
return download_url

Check warning on line 2740 in atlassian/confluence.py

View check run for this annotation

Codecov / codecov/patch

atlassian/confluence.py#L2740

Added line #L2740 was not covered by tests
else:
combined_url = self.url + download_url

Check warning on line 2742 in atlassian/confluence.py

View check run for this annotation

Codecov / codecov/patch

atlassian/confluence.py#L2742

Added line #L2742 was not covered by tests
# Ensure only one /wiki is included in the path
if combined_url.count("/wiki") > 1:
combined_url = combined_url.replace("/wiki/wiki", "/wiki")
return combined_url
time.sleep(30)
except Exception as e:
raise ApiError(

Check warning on line 2749 in atlassian/confluence.py

View check run for this annotation

Codecov / codecov/patch

atlassian/confluence.py#L2745-L2749

Added lines #L2745 - L2749 were not covered by tests
"Encountered error during space export status check from space " + space_key, reason=e
)

return "None" # Return None if the while loop does not return a value
except Exception as e:
raise ApiError("Encountered error during space export from space " + space_key, reason=e)

Check warning on line 2755 in atlassian/confluence.py

View check run for this annotation

Codecov / codecov/patch

atlassian/confluence.py#L2753-L2755

Added lines #L2753 - L2755 were not covered by tests

def export_page(self, page_id):
"""
Alias method for export page as pdf
Expand Down Expand Up @@ -2905,6 +3002,7 @@
and provides a link to download the PDF once the process completes.
This functions polls the long-running task page and returns the
download url of the PDF.
This method is used in get_space_export() method for space-> PDF export.
:param url: URL to initiate PDF export
:return: Download url for PDF file
"""
Expand Down
3 changes: 3 additions & 0 deletions docs/confluence.rst
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,9 @@ Get spaces info
# Get Space permissions set based on json-rpc call
confluence.get_space_permissions(space_key)

# Get Space export download url
confluence.get_space_export(space_key, export_type)

Users and Groups
----------------

Expand Down
25 changes: 25 additions & 0 deletions examples/confluence/confluence_get_space_export.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from atlassian import Confluence

# init the Confluence object
host = "<cloud_instance_url/wiki>"
username = "<user_email>"
password = "<API_TOKEN>"
confluence = Confluence(
url=host,
username=username,
password=password,
)
space_key = "TEST"
confluence.get_space_export(space_key=space_key, export_type="html")
# This method should be used to trigger the space export action.
# Provide `space_key` and `export_type` (html/pdf/xml/csv) as arguments.

# It was tested on Confluence Cloud and might not work properly with Confluence on-prem.
# (!) This is an experimental method that should be considered a workaround for the missing space export REST endpoint.
# (!) The method might break if Atlassian implements changes to their space export front-end logic.

# The while loop does not have an exit condition; it will run until the space export is completed.
# It is possible that the space export progress might get stuck. It is up to the library user to handle this scenario.

# Method returns the link to the space export file.
# It is up to the library user to handle the file download action.