roundtripper 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- roundtripper/__init__.py +1 -0
- roundtripper/api_client.py +83 -0
- roundtripper/cli.py +75 -0
- roundtripper/config.py +110 -0
- roundtripper/config_interactive.py +743 -0
- roundtripper/config_store.py +197 -0
- roundtripper/confluence.py +307 -0
- roundtripper/file_utils.py +197 -0
- roundtripper/models.py +254 -0
- roundtripper/pull_service.py +387 -0
- roundtripper/push_service.py +394 -0
- roundtripper/py.typed +0 -0
- roundtripper-0.2.0.dist-info/METADATA +104 -0
- roundtripper-0.2.0.dist-info/RECORD +17 -0
- roundtripper-0.2.0.dist-info/WHEEL +4 -0
- roundtripper-0.2.0.dist-info/entry_points.txt +2 -0
- roundtripper-0.2.0.dist-info/licenses/LICENSE +31 -0
roundtripper/models.py
ADDED
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
"""Pydantic models for Confluence data structures.
|
|
2
|
+
|
|
3
|
+
Based on confluence-markdown-exporter by Sebastian Penhouet.
|
|
4
|
+
https://github.com/Spenhouet/confluence-markdown-exporter
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
from pydantic import BaseModel, Field
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class User(BaseModel):
|
|
13
|
+
"""Confluence user information."""
|
|
14
|
+
|
|
15
|
+
account_id: str = ""
|
|
16
|
+
username: str = ""
|
|
17
|
+
display_name: str = ""
|
|
18
|
+
public_name: str = ""
|
|
19
|
+
email: str = ""
|
|
20
|
+
|
|
21
|
+
@classmethod
|
|
22
|
+
def from_api_response(cls, data: dict[str, Any]) -> "User":
|
|
23
|
+
"""Create a User from API response data.
|
|
24
|
+
|
|
25
|
+
Parameters
|
|
26
|
+
----------
|
|
27
|
+
data
|
|
28
|
+
API response dictionary.
|
|
29
|
+
|
|
30
|
+
Returns
|
|
31
|
+
-------
|
|
32
|
+
User
|
|
33
|
+
User instance.
|
|
34
|
+
"""
|
|
35
|
+
return cls(
|
|
36
|
+
account_id=data.get("accountId", ""),
|
|
37
|
+
username=data.get("username", ""),
|
|
38
|
+
display_name=data.get("displayName", ""),
|
|
39
|
+
public_name=data.get("publicName", ""),
|
|
40
|
+
email=data.get("email", ""),
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class Version(BaseModel):
|
|
45
|
+
"""Page/attachment version information."""
|
|
46
|
+
|
|
47
|
+
number: int = 0
|
|
48
|
+
when: str = ""
|
|
49
|
+
friendly_when: str = ""
|
|
50
|
+
by: User = Field(default_factory=User)
|
|
51
|
+
|
|
52
|
+
@classmethod
|
|
53
|
+
def from_api_response(cls, data: dict[str, Any]) -> "Version":
|
|
54
|
+
"""Create a Version from API response data.
|
|
55
|
+
|
|
56
|
+
Parameters
|
|
57
|
+
----------
|
|
58
|
+
data
|
|
59
|
+
API response dictionary.
|
|
60
|
+
|
|
61
|
+
Returns
|
|
62
|
+
-------
|
|
63
|
+
Version
|
|
64
|
+
Version instance.
|
|
65
|
+
"""
|
|
66
|
+
return cls(
|
|
67
|
+
number=data.get("number", 0),
|
|
68
|
+
when=data.get("when", ""),
|
|
69
|
+
friendly_when=data.get("friendlyWhen", ""),
|
|
70
|
+
by=User.from_api_response(data.get("by", {})),
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
class Label(BaseModel):
|
|
75
|
+
"""Confluence label/tag."""
|
|
76
|
+
|
|
77
|
+
id: str = ""
|
|
78
|
+
name: str = ""
|
|
79
|
+
prefix: str = ""
|
|
80
|
+
|
|
81
|
+
@classmethod
|
|
82
|
+
def from_api_response(cls, data: dict[str, Any]) -> "Label":
|
|
83
|
+
"""Create a Label from API response data.
|
|
84
|
+
|
|
85
|
+
Parameters
|
|
86
|
+
----------
|
|
87
|
+
data
|
|
88
|
+
API response dictionary.
|
|
89
|
+
|
|
90
|
+
Returns
|
|
91
|
+
-------
|
|
92
|
+
Label
|
|
93
|
+
Label instance.
|
|
94
|
+
"""
|
|
95
|
+
return cls(
|
|
96
|
+
id=data.get("id", ""),
|
|
97
|
+
name=data.get("name", ""),
|
|
98
|
+
prefix=data.get("prefix", ""),
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
class SpaceInfo(BaseModel):
|
|
103
|
+
"""Confluence space information."""
|
|
104
|
+
|
|
105
|
+
key: str = ""
|
|
106
|
+
name: str = ""
|
|
107
|
+
description: str = ""
|
|
108
|
+
homepage_id: int | None = None
|
|
109
|
+
|
|
110
|
+
@classmethod
|
|
111
|
+
def from_api_response(cls, data: dict[str, Any]) -> "SpaceInfo":
|
|
112
|
+
"""Create a SpaceInfo from API response data.
|
|
113
|
+
|
|
114
|
+
Parameters
|
|
115
|
+
----------
|
|
116
|
+
data
|
|
117
|
+
API response dictionary.
|
|
118
|
+
|
|
119
|
+
Returns
|
|
120
|
+
-------
|
|
121
|
+
SpaceInfo
|
|
122
|
+
SpaceInfo instance.
|
|
123
|
+
"""
|
|
124
|
+
homepage = data.get("homepage", {})
|
|
125
|
+
homepage_id = homepage.get("id") if homepage else None
|
|
126
|
+
if homepage_id is not None:
|
|
127
|
+
homepage_id = int(homepage_id)
|
|
128
|
+
|
|
129
|
+
return cls(
|
|
130
|
+
key=data.get("key", ""),
|
|
131
|
+
name=data.get("name", ""),
|
|
132
|
+
description=data.get("description", {}).get("plain", {}).get("value", ""),
|
|
133
|
+
homepage_id=homepage_id,
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
class AttachmentInfo(BaseModel):
|
|
138
|
+
"""Confluence attachment information."""
|
|
139
|
+
|
|
140
|
+
id: str = ""
|
|
141
|
+
title: str = ""
|
|
142
|
+
file_size: int = 0
|
|
143
|
+
media_type: str = ""
|
|
144
|
+
file_id: str = ""
|
|
145
|
+
download_link: str = ""
|
|
146
|
+
comment: str = ""
|
|
147
|
+
version: Version = Field(default_factory=Version)
|
|
148
|
+
raw_api_response: dict[str, Any] = Field(default_factory=dict)
|
|
149
|
+
|
|
150
|
+
@classmethod
|
|
151
|
+
def from_api_response(cls, data: dict[str, Any]) -> "AttachmentInfo":
|
|
152
|
+
"""Create an AttachmentInfo from API response data.
|
|
153
|
+
|
|
154
|
+
Parameters
|
|
155
|
+
----------
|
|
156
|
+
data
|
|
157
|
+
API response dictionary.
|
|
158
|
+
|
|
159
|
+
Returns
|
|
160
|
+
-------
|
|
161
|
+
AttachmentInfo
|
|
162
|
+
AttachmentInfo instance.
|
|
163
|
+
"""
|
|
164
|
+
extensions = data.get("extensions", {})
|
|
165
|
+
return cls(
|
|
166
|
+
id=data.get("id", ""),
|
|
167
|
+
title=data.get("title", ""),
|
|
168
|
+
file_size=extensions.get("fileSize", 0),
|
|
169
|
+
media_type=extensions.get("mediaType", ""),
|
|
170
|
+
file_id=extensions.get("fileId", ""),
|
|
171
|
+
download_link=data.get("_links", {}).get("download", ""),
|
|
172
|
+
comment=extensions.get("comment", ""),
|
|
173
|
+
version=Version.from_api_response(data.get("version", {})),
|
|
174
|
+
raw_api_response=data,
|
|
175
|
+
)
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
class PageInfo(BaseModel):
|
|
179
|
+
"""Confluence page information."""
|
|
180
|
+
|
|
181
|
+
id: int = 0
|
|
182
|
+
title: str = ""
|
|
183
|
+
space_key: str = ""
|
|
184
|
+
body_storage: str = ""
|
|
185
|
+
body_view: str = ""
|
|
186
|
+
body_export_view: str = ""
|
|
187
|
+
body_editor2: str = ""
|
|
188
|
+
labels: list[Label] = Field(default_factory=list)
|
|
189
|
+
ancestors: list[int] = Field(default_factory=list)
|
|
190
|
+
version: Version = Field(default_factory=Version)
|
|
191
|
+
raw_api_response: dict[str, Any] = Field(default_factory=dict)
|
|
192
|
+
|
|
193
|
+
@classmethod
|
|
194
|
+
def from_api_response(cls, data: dict[str, Any]) -> "PageInfo":
|
|
195
|
+
"""Create a PageInfo from API response data.
|
|
196
|
+
|
|
197
|
+
Parameters
|
|
198
|
+
----------
|
|
199
|
+
data
|
|
200
|
+
API response dictionary containing page data.
|
|
201
|
+
|
|
202
|
+
Returns
|
|
203
|
+
-------
|
|
204
|
+
PageInfo
|
|
205
|
+
PageInfo instance.
|
|
206
|
+
"""
|
|
207
|
+
body = data.get("body", {})
|
|
208
|
+
labels_data = data.get("metadata", {}).get("labels", {}).get("results", [])
|
|
209
|
+
ancestors_data = data.get("ancestors", [])
|
|
210
|
+
|
|
211
|
+
# Extract space key from expandable link or space object
|
|
212
|
+
space_key = ""
|
|
213
|
+
if "space" in data:
|
|
214
|
+
space_key = data["space"].get("key", "")
|
|
215
|
+
elif "_expandable" in data:
|
|
216
|
+
space_link = data["_expandable"].get("space", "")
|
|
217
|
+
if space_link:
|
|
218
|
+
space_key = space_link.split("/")[-1]
|
|
219
|
+
|
|
220
|
+
return cls(
|
|
221
|
+
id=int(data.get("id", 0)),
|
|
222
|
+
title=data.get("title", ""),
|
|
223
|
+
space_key=space_key,
|
|
224
|
+
body_storage=body.get("storage", {}).get("value", ""),
|
|
225
|
+
body_view=body.get("view", {}).get("value", ""),
|
|
226
|
+
body_export_view=body.get("export_view", {}).get("value", ""),
|
|
227
|
+
body_editor2=body.get("editor2", {}).get("value", ""),
|
|
228
|
+
labels=[Label.from_api_response(label) for label in labels_data],
|
|
229
|
+
ancestors=[int(a.get("id", 0)) for a in ancestors_data],
|
|
230
|
+
version=Version.from_api_response(data.get("version", {})),
|
|
231
|
+
raw_api_response=data,
|
|
232
|
+
)
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
class PullResult(BaseModel):
|
|
236
|
+
"""Result of a pull operation."""
|
|
237
|
+
|
|
238
|
+
pages_downloaded: int = 0
|
|
239
|
+
attachments_downloaded: int = 0
|
|
240
|
+
pages_skipped: int = 0
|
|
241
|
+
attachments_skipped: int = 0
|
|
242
|
+
errors: list[str] = Field(default_factory=list)
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
class PushResult(BaseModel):
|
|
246
|
+
"""Result of a push operation."""
|
|
247
|
+
|
|
248
|
+
pages_updated: int = 0
|
|
249
|
+
pages_created: int = 0
|
|
250
|
+
pages_skipped: int = 0
|
|
251
|
+
attachments_uploaded: int = 0
|
|
252
|
+
attachments_skipped: int = 0
|
|
253
|
+
conflicts: list[str] = Field(default_factory=list)
|
|
254
|
+
errors: list[str] = Field(default_factory=list)
|
|
@@ -0,0 +1,387 @@
|
|
|
1
|
+
"""Confluence pull service for downloading content.
|
|
2
|
+
|
|
3
|
+
This module contains the business logic for pulling Confluence content.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
import logging
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
from typing import Any
|
|
9
|
+
|
|
10
|
+
from atlassian import Confluence
|
|
11
|
+
from tqdm import tqdm
|
|
12
|
+
|
|
13
|
+
from roundtripper.file_utils import build_page_path, format_xml, save_file, save_json
|
|
14
|
+
from roundtripper.models import AttachmentInfo, PageInfo, PullResult, SpaceInfo
|
|
15
|
+
|
|
16
|
+
#: Logger instance.
|
|
17
|
+
LOGGER = logging.getLogger(__name__)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class PullService:
|
|
21
|
+
"""Service for pulling Confluence content to local storage."""
|
|
22
|
+
|
|
23
|
+
def __init__(
|
|
24
|
+
self,
|
|
25
|
+
client: Confluence,
|
|
26
|
+
output_dir: Path,
|
|
27
|
+
*,
|
|
28
|
+
dry_run: bool = False,
|
|
29
|
+
) -> None:
|
|
30
|
+
"""Initialize the pull service.
|
|
31
|
+
|
|
32
|
+
Parameters
|
|
33
|
+
----------
|
|
34
|
+
client
|
|
35
|
+
Confluence API client (atlassian-python-api Confluence instance).
|
|
36
|
+
output_dir
|
|
37
|
+
Base output directory for downloaded content.
|
|
38
|
+
dry_run
|
|
39
|
+
If True, only show what would be downloaded without actually downloading.
|
|
40
|
+
"""
|
|
41
|
+
self.client = client
|
|
42
|
+
self.output_dir = output_dir
|
|
43
|
+
self.dry_run = dry_run
|
|
44
|
+
self.result = PullResult()
|
|
45
|
+
self._ancestor_cache: dict[int, str] = {}
|
|
46
|
+
|
|
47
|
+
def pull_space(self, space_key: str) -> PullResult:
|
|
48
|
+
"""Pull all pages from a Confluence space.
|
|
49
|
+
|
|
50
|
+
Parameters
|
|
51
|
+
----------
|
|
52
|
+
space_key
|
|
53
|
+
The space key to pull.
|
|
54
|
+
|
|
55
|
+
Returns
|
|
56
|
+
-------
|
|
57
|
+
PullResult
|
|
58
|
+
Summary of the pull operation.
|
|
59
|
+
"""
|
|
60
|
+
LOGGER.info("Fetching space info for: %s", space_key)
|
|
61
|
+
|
|
62
|
+
space_data = self.client.get_space(space_key, expand="homepage")
|
|
63
|
+
assert isinstance(space_data, dict) # atlassian SDK return type is too broad
|
|
64
|
+
space = SpaceInfo.from_api_response(space_data)
|
|
65
|
+
|
|
66
|
+
if space.homepage_id is None:
|
|
67
|
+
LOGGER.warning("Space '%s' has no homepage. Nothing to pull.", space_key)
|
|
68
|
+
return self.result
|
|
69
|
+
|
|
70
|
+
# Get all descendant page IDs
|
|
71
|
+
page_ids = self._get_all_descendant_ids(space.homepage_id)
|
|
72
|
+
page_ids.insert(0, space.homepage_id)
|
|
73
|
+
|
|
74
|
+
LOGGER.info("Found %d pages to pull", len(page_ids))
|
|
75
|
+
|
|
76
|
+
# Pull each page with progress bar
|
|
77
|
+
for page_id in tqdm(page_ids, desc="Pulling pages", disable=self.dry_run):
|
|
78
|
+
self._pull_page(page_id)
|
|
79
|
+
|
|
80
|
+
return self.result
|
|
81
|
+
|
|
82
|
+
def pull_page(self, page_id: int, *, recursive: bool = False) -> PullResult:
|
|
83
|
+
"""Pull a specific page and optionally its descendants.
|
|
84
|
+
|
|
85
|
+
Parameters
|
|
86
|
+
----------
|
|
87
|
+
page_id
|
|
88
|
+
The page ID to pull.
|
|
89
|
+
recursive
|
|
90
|
+
If True, also pull all descendant pages.
|
|
91
|
+
|
|
92
|
+
Returns
|
|
93
|
+
-------
|
|
94
|
+
PullResult
|
|
95
|
+
Summary of the pull operation.
|
|
96
|
+
"""
|
|
97
|
+
if recursive:
|
|
98
|
+
page_ids = self._get_all_descendant_ids(page_id)
|
|
99
|
+
page_ids.insert(0, page_id)
|
|
100
|
+
LOGGER.info("Found %d pages to pull (including descendants)", len(page_ids))
|
|
101
|
+
|
|
102
|
+
for pid in tqdm(page_ids, desc="Pulling pages", disable=self.dry_run):
|
|
103
|
+
self._pull_page(pid)
|
|
104
|
+
else:
|
|
105
|
+
self._pull_page(page_id)
|
|
106
|
+
|
|
107
|
+
return self.result
|
|
108
|
+
|
|
109
|
+
def _get_all_descendant_ids(self, page_id: int) -> list[int]:
|
|
110
|
+
"""Get all descendant page IDs for a given page.
|
|
111
|
+
|
|
112
|
+
Parameters
|
|
113
|
+
----------
|
|
114
|
+
page_id
|
|
115
|
+
The parent page ID.
|
|
116
|
+
|
|
117
|
+
Returns
|
|
118
|
+
-------
|
|
119
|
+
list[int]
|
|
120
|
+
List of descendant page IDs.
|
|
121
|
+
"""
|
|
122
|
+
url = "rest/api/content/search"
|
|
123
|
+
params: dict[str, Any] = {
|
|
124
|
+
"cql": f"type=page AND ancestor={page_id}",
|
|
125
|
+
"limit": 100,
|
|
126
|
+
}
|
|
127
|
+
results: list[dict[str, Any]] = []
|
|
128
|
+
|
|
129
|
+
try:
|
|
130
|
+
response = self.client.get(url, params=params)
|
|
131
|
+
assert isinstance(response, dict) # atlassian SDK return type is too broad
|
|
132
|
+
results.extend(response.get("results", []))
|
|
133
|
+
next_path = response.get("_links", {}).get("next")
|
|
134
|
+
|
|
135
|
+
while next_path:
|
|
136
|
+
response = self.client.get(next_path)
|
|
137
|
+
assert isinstance(response, dict) # atlassian SDK return type is too broad
|
|
138
|
+
results.extend(response.get("results", []))
|
|
139
|
+
next_path = response.get("_links", {}).get("next")
|
|
140
|
+
|
|
141
|
+
except Exception as e:
|
|
142
|
+
LOGGER.warning("Error fetching descendants for page %d: %s", page_id, e)
|
|
143
|
+
return []
|
|
144
|
+
|
|
145
|
+
return [int(result["id"]) for result in results]
|
|
146
|
+
|
|
147
|
+
def _pull_page(self, page_id: int) -> None:
|
|
148
|
+
"""Pull a single page and its attachments.
|
|
149
|
+
|
|
150
|
+
Parameters
|
|
151
|
+
----------
|
|
152
|
+
page_id
|
|
153
|
+
The page ID to pull.
|
|
154
|
+
"""
|
|
155
|
+
try:
|
|
156
|
+
page_data = self.client.get_page_by_id(
|
|
157
|
+
page_id,
|
|
158
|
+
expand="body.storage,body.view,body.export_view,body.editor2,"
|
|
159
|
+
"metadata.labels,ancestors,version,space",
|
|
160
|
+
)
|
|
161
|
+
assert isinstance(page_data, dict) # atlassian SDK return type is too broad
|
|
162
|
+
page = PageInfo.from_api_response(page_data)
|
|
163
|
+
except Exception as e:
|
|
164
|
+
error_msg = f"Failed to fetch page {page_id}: {e}"
|
|
165
|
+
LOGGER.warning(error_msg)
|
|
166
|
+
self.result.errors.append(error_msg)
|
|
167
|
+
return
|
|
168
|
+
|
|
169
|
+
# Get ancestor titles for building path
|
|
170
|
+
ancestor_titles = self._get_ancestor_titles(page)
|
|
171
|
+
|
|
172
|
+
# Build output path
|
|
173
|
+
page_dir = build_page_path(
|
|
174
|
+
self.output_dir,
|
|
175
|
+
page.space_key,
|
|
176
|
+
ancestor_titles,
|
|
177
|
+
page.title,
|
|
178
|
+
)
|
|
179
|
+
|
|
180
|
+
if self.dry_run:
|
|
181
|
+
LOGGER.info("[DRY RUN] Would create: %s", page_dir)
|
|
182
|
+
self.result.pages_downloaded += 1
|
|
183
|
+
return
|
|
184
|
+
|
|
185
|
+
# Check if page already exists with same version
|
|
186
|
+
page_json_path = page_dir / "page.json"
|
|
187
|
+
if self._is_up_to_date(page_json_path, page.version.number):
|
|
188
|
+
LOGGER.debug("Page '%s' is up to date, skipping", page.title)
|
|
189
|
+
self.result.pages_skipped += 1
|
|
190
|
+
else:
|
|
191
|
+
# Save page content (Confluence storage format)
|
|
192
|
+
self._save_page_content(page_dir, page)
|
|
193
|
+
self.result.pages_downloaded += 1
|
|
194
|
+
|
|
195
|
+
# Pull attachments
|
|
196
|
+
self._pull_attachments(page_id, page_dir)
|
|
197
|
+
|
|
198
|
+
def _get_ancestor_titles(self, page: PageInfo) -> list[str]:
|
|
199
|
+
"""Get ancestor page titles for building directory path.
|
|
200
|
+
|
|
201
|
+
Parameters
|
|
202
|
+
----------
|
|
203
|
+
page
|
|
204
|
+
The page to get ancestors for.
|
|
205
|
+
|
|
206
|
+
Returns
|
|
207
|
+
-------
|
|
208
|
+
list[str]
|
|
209
|
+
List of ancestor titles from root to parent.
|
|
210
|
+
"""
|
|
211
|
+
titles = []
|
|
212
|
+
for ancestor_id in page.ancestors:
|
|
213
|
+
if ancestor_id in self._ancestor_cache:
|
|
214
|
+
titles.append(self._ancestor_cache[ancestor_id])
|
|
215
|
+
else:
|
|
216
|
+
try:
|
|
217
|
+
ancestor_data = self.client.get_page_by_id(ancestor_id, expand="")
|
|
218
|
+
assert isinstance(ancestor_data, dict) # atlassian SDK return type is too broad
|
|
219
|
+
title = ancestor_data.get("title", f"Page-{ancestor_id}")
|
|
220
|
+
self._ancestor_cache[ancestor_id] = title
|
|
221
|
+
titles.append(title)
|
|
222
|
+
except Exception: # pragma: no cover
|
|
223
|
+
title = f"Page-{ancestor_id}"
|
|
224
|
+
self._ancestor_cache[ancestor_id] = title
|
|
225
|
+
titles.append(title)
|
|
226
|
+
return titles
|
|
227
|
+
|
|
228
|
+
def _is_up_to_date(self, json_path: Path, current_version: int) -> bool:
|
|
229
|
+
"""Check if a page is up to date based on version number.
|
|
230
|
+
|
|
231
|
+
Parameters
|
|
232
|
+
----------
|
|
233
|
+
json_path
|
|
234
|
+
Path to the existing page.json file.
|
|
235
|
+
current_version
|
|
236
|
+
Current version number from the API.
|
|
237
|
+
|
|
238
|
+
Returns
|
|
239
|
+
-------
|
|
240
|
+
bool
|
|
241
|
+
True if the local version matches the current version.
|
|
242
|
+
"""
|
|
243
|
+
if not json_path.exists():
|
|
244
|
+
return False
|
|
245
|
+
|
|
246
|
+
try:
|
|
247
|
+
import json
|
|
248
|
+
|
|
249
|
+
with json_path.open("r") as f:
|
|
250
|
+
existing_data = json.load(f)
|
|
251
|
+
existing_version = existing_data.get("version", {}).get("number", 0)
|
|
252
|
+
return existing_version >= current_version
|
|
253
|
+
except Exception: # pragma: no cover
|
|
254
|
+
return False
|
|
255
|
+
|
|
256
|
+
def _save_page_content(self, page_dir: Path, page: PageInfo) -> None:
|
|
257
|
+
"""Save page content and metadata to disk.
|
|
258
|
+
|
|
259
|
+
Parameters
|
|
260
|
+
----------
|
|
261
|
+
page_dir
|
|
262
|
+
Directory to save the page in.
|
|
263
|
+
page
|
|
264
|
+
Page information to save.
|
|
265
|
+
"""
|
|
266
|
+
# Save Confluence storage format as formatted XML
|
|
267
|
+
xml_path = page_dir / "page.xml"
|
|
268
|
+
formatted_xml = format_xml(page.body_storage)
|
|
269
|
+
save_file(xml_path, formatted_xml)
|
|
270
|
+
|
|
271
|
+
# Save raw API response
|
|
272
|
+
json_path = page_dir / "page.json"
|
|
273
|
+
save_json(json_path, page.raw_api_response)
|
|
274
|
+
|
|
275
|
+
LOGGER.debug("Saved page: %s", page.title)
|
|
276
|
+
|
|
277
|
+
def _pull_attachments(self, page_id: int, page_dir: Path) -> None:
|
|
278
|
+
"""Pull all attachments for a page.
|
|
279
|
+
|
|
280
|
+
Parameters
|
|
281
|
+
----------
|
|
282
|
+
page_id
|
|
283
|
+
The page ID to get attachments for.
|
|
284
|
+
page_dir
|
|
285
|
+
Directory where the page is stored.
|
|
286
|
+
"""
|
|
287
|
+
attachments_dir = page_dir / "attachments"
|
|
288
|
+
start = 0
|
|
289
|
+
limit = 50
|
|
290
|
+
|
|
291
|
+
while True:
|
|
292
|
+
try:
|
|
293
|
+
response = self.client.get_attachments_from_content(
|
|
294
|
+
page_id,
|
|
295
|
+
start=start,
|
|
296
|
+
limit=limit,
|
|
297
|
+
expand="version",
|
|
298
|
+
)
|
|
299
|
+
assert isinstance(response, dict) # atlassian SDK return type is too broad
|
|
300
|
+
except Exception as e: # pragma: no cover
|
|
301
|
+
LOGGER.warning("Failed to fetch attachments for page %d: %s", page_id, e)
|
|
302
|
+
break
|
|
303
|
+
|
|
304
|
+
results = response.get("results", [])
|
|
305
|
+
if not results:
|
|
306
|
+
break
|
|
307
|
+
|
|
308
|
+
for att_data in results:
|
|
309
|
+
attachment = AttachmentInfo.from_api_response(att_data)
|
|
310
|
+
self._download_attachment(attachment, attachments_dir)
|
|
311
|
+
|
|
312
|
+
size = response.get("size", 0)
|
|
313
|
+
if size < limit:
|
|
314
|
+
break
|
|
315
|
+
start += size
|
|
316
|
+
|
|
317
|
+
def _download_attachment(self, attachment: AttachmentInfo, attachments_dir: Path) -> None:
|
|
318
|
+
"""Download a single attachment.
|
|
319
|
+
|
|
320
|
+
Parameters
|
|
321
|
+
----------
|
|
322
|
+
attachment
|
|
323
|
+
Attachment information.
|
|
324
|
+
attachments_dir
|
|
325
|
+
Directory to save attachments in.
|
|
326
|
+
"""
|
|
327
|
+
filename = attachment.title
|
|
328
|
+
file_path = attachments_dir / filename
|
|
329
|
+
json_path = attachments_dir / f"{filename}.json"
|
|
330
|
+
|
|
331
|
+
if self.dry_run: # pragma: no cover
|
|
332
|
+
LOGGER.info("[DRY RUN] Would download: %s", filename)
|
|
333
|
+
self.result.attachments_downloaded += 1
|
|
334
|
+
return
|
|
335
|
+
|
|
336
|
+
# Check if attachment is up to date
|
|
337
|
+
if self._is_attachment_up_to_date(json_path, attachment.version.number):
|
|
338
|
+
LOGGER.debug("Attachment '%s' is up to date, skipping", filename)
|
|
339
|
+
self.result.attachments_skipped += 1
|
|
340
|
+
return
|
|
341
|
+
|
|
342
|
+
# Download attachment content
|
|
343
|
+
try:
|
|
344
|
+
download_url = str(self.client.url) + attachment.download_link
|
|
345
|
+
response = self.client._session.get(download_url)
|
|
346
|
+
response.raise_for_status()
|
|
347
|
+
content = response.content
|
|
348
|
+
except Exception as e:
|
|
349
|
+
error_msg = f"Failed to download attachment '{filename}': {e}"
|
|
350
|
+
LOGGER.warning(error_msg)
|
|
351
|
+
self.result.errors.append(error_msg)
|
|
352
|
+
return
|
|
353
|
+
|
|
354
|
+
# Save attachment and metadata
|
|
355
|
+
save_file(file_path, content)
|
|
356
|
+
save_json(json_path, attachment.raw_api_response)
|
|
357
|
+
|
|
358
|
+
self.result.attachments_downloaded += 1
|
|
359
|
+
LOGGER.debug("Downloaded attachment: %s", filename)
|
|
360
|
+
|
|
361
|
+
def _is_attachment_up_to_date(self, json_path: Path, current_version: int) -> bool:
|
|
362
|
+
"""Check if an attachment is up to date based on version number.
|
|
363
|
+
|
|
364
|
+
Parameters
|
|
365
|
+
----------
|
|
366
|
+
json_path
|
|
367
|
+
Path to the existing attachment metadata JSON file.
|
|
368
|
+
current_version
|
|
369
|
+
Current version number from the API.
|
|
370
|
+
|
|
371
|
+
Returns
|
|
372
|
+
-------
|
|
373
|
+
bool
|
|
374
|
+
True if the local version matches the current version.
|
|
375
|
+
"""
|
|
376
|
+
if not json_path.exists():
|
|
377
|
+
return False
|
|
378
|
+
|
|
379
|
+
try:
|
|
380
|
+
import json
|
|
381
|
+
|
|
382
|
+
with json_path.open("r") as f:
|
|
383
|
+
existing_data = json.load(f)
|
|
384
|
+
existing_version = existing_data.get("version", {}).get("number", 0)
|
|
385
|
+
return existing_version >= current_version
|
|
386
|
+
except Exception: # pragma: no cover
|
|
387
|
+
return False
|