File size: 10,223 Bytes
2d6b4e9
 
 
 
753d95d
2d6b4e9
753d95d
 
 
 
 
2d6b4e9
 
 
 
 
753d95d
 
 
 
2d6b4e9
 
 
 
 
 
 
753d95d
 
 
 
2d6b4e9
 
 
 
 
 
 
753d95d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d6b4e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
753d95d
 
 
 
 
 
 
 
 
2d6b4e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
753d95d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d6b4e9
 
 
 
 
 
753d95d
2d6b4e9
 
 
 
 
 
 
 
 
 
 
 
 
753d95d
2d6b4e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
753d95d
 
2d6b4e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
753d95d
 
 
 
 
2d6b4e9
 
 
 
 
 
 
 
 
753d95d
 
 
 
 
 
 
2d6b4e9
 
 
 
 
 
 
 
 
 
753d95d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
from __future__ import annotations

from dataclasses import dataclass
from pathlib import Path
import tempfile
from urllib.parse import urlparse
from dotenv import load_dotenv

import base64
import json
import os
import re
import shutil
import subprocess

import requests
from requests.auth import HTTPBasicAuth

load_dotenv()


# examples: https://scilifelab.atlassian.net/wiki/spaces/demo/pages/20381827/Template+pages
# and for gitlab: https://gitlab.com/gitlab-examples/cpp-example
@dataclass(frozen=True)
class DownloadResult:
    local_path: Path
    repository_name: str
    base_url: str


_CONFLUENCE_URL_MAP_FILENAME = ".confluence_url_map.json"


def _sanitize_filename(value: str) -> str:
    cleaned = re.sub(r"[^A-Za-z0-9._-]+", "_", value).strip("._")
    return cleaned or "page"


def _confluence_headers() -> dict[str, str]:
    headers = {"Accept": "application/json"}
    return headers


def _confluence_request(url: str, params: dict | None = None) -> dict:
    response = requests.get(
        url,
        params=params,
        headers=_confluence_headers(),
        auth=HTTPBasicAuth(
            os.getenv("CONFLUENCE_USER_EMAIL", "").strip(),
            os.getenv("ATLASSIAN_API_KEY", "").strip(),
        ),
        timeout=30,
    )
    if response.status_code in {401, 403} and os.getenv("CONFLUENCE_READ_ONLY_KEY"):
        print(response.text)
        raise RuntimeError(
            "Confluence auth failed (401/403). If using an Atlassian API token, set "
            "CONFLUENCE_USER_EMAIL and CONFLUENCE_READ_ONLY_KEY."
        )
    if response.status_code != 200:
        raise RuntimeError(
            f"Confluence request failed ({response.status_code}): {response.text}"
        )
    return response.json()


def _resolve_confluence_page_url(base_url: str, page: dict) -> str:
    links = page.get("_links", {}) if isinstance(page, dict) else {}
    webui = str(links.get("webui") or "").strip()
    base = str(links.get("base") or base_url).strip()
    if webui:
        if webui.startswith("http://") or webui.startswith("https://"):
            return webui
        return f"{base.rstrip('/')}/{webui.lstrip('/')}"
    page_id = str(page.get("id") or "").strip()
    if page_id:
        return f"{base_url.rstrip('/')}/pages/{page_id}"
    return base_url


def _fetch_confluence_homepage_id(base_url: str, space_key: str) -> str:
    payload = _confluence_request(
        f"{base_url}/rest/api/space/{space_key}",
        params={"expand": "homepage"},
    )
    homepage = payload.get("homepage", {})
    return str(homepage.get("id") or "").strip()


def _write_confluence_url_map(dest_path: Path, url_map: dict[str, str]) -> None:
    if not url_map:
        return
    map_path = dest_path / _CONFLUENCE_URL_MAP_FILENAME
    map_path.write_text(
        json.dumps(url_map, ensure_ascii=True, indent=2),
        encoding="utf-8",
    )


def _build_base_url(url: str, branch: str = "") -> str:
    if "gitlab.com" in url:
        return f"{url}/-/blob/{branch}" if branch else url
    elif "github.com" in url:
        return f"{url}/blob/{branch}" if branch else url
    else:
        return url  # Fallback to original URL if we don't recognize the host


def _get_default_branch(url: str) -> str:
    result = subprocess.run(
        ["git", "ls-remote", "--symref", url, "HEAD"],
        capture_output=True,
        text=True,
    )

    if result.returncode != 0:
        raise RuntimeError(result.stderr.strip())

    for line in result.stdout.splitlines():
        if line.startswith("ref:"):
            # ref: refs/heads/main HEAD
            return line.split("refs/heads/")[1].split()[0]

    raise RuntimeError("Could not determine default branch.")


def _infer_git_repo_name(url: str) -> str:
    if "://" in url:
        path = urlparse(url).path
    else:
        # Support scp-style URLs: git@host:org/repo.git
        if ":" in url:
            path = url.split(":", 1)[1]
        else:
            path = url

    path = path.rstrip("/")
    name = Path(path).name
    if name.endswith(".git"):
        name = name[:-4]
    if not name:
        raise ValueError("Could not determine repository name from URL.")
    return name


def download_git_repo(url: str, dest_root: Path) -> DownloadResult:
    if shutil.which("git") is None:
        raise RuntimeError("git is not available on this system.")

    repo_name = _infer_git_repo_name(url)
    dest_path = dest_root / repo_name

    result = subprocess.run(
        ["git", "clone", "--depth", "1", url, str(dest_path)],
        capture_output=True,
        text=True,
    )
    if result.returncode != 0:
        stderr = (result.stderr or result.stdout or "").strip()
        raise RuntimeError(f"git clone failed: {stderr}")

    branch = _get_default_branch(url)

    print("Baseurl: " + _build_base_url(url, branch))

    return DownloadResult(
        local_path=dest_path,
        repository_name=repo_name,
        base_url=_build_base_url(url, branch),
    )


def _parse_confluence_base_and_page(url: str) -> tuple[str, str]:
    parsed = urlparse(url)
    if not parsed.scheme or not parsed.netloc:
        raise ValueError("Invalid Confluence URL.")

    page_id = ""
    query_params = parsed.query.split("&") if parsed.query else []
    for entry in query_params:
        if entry.startswith("pageId="):
            page_id = entry.split("=", 1)[1]
            break

    path = parsed.path or ""
    if not page_id:
        match = re.search(r"/pages/(\d+)", path)
        if match:
            page_id = match.group(1)

    base_path = "/wiki" if "/wiki/" in path or path.startswith("/wiki") else ""
    base_url = f"{parsed.scheme}://{parsed.netloc}{base_path}"
    if page_id:
        return base_url, page_id

    space_key = ""
    match = re.search(r"/spaces/([^/]+)", path)
    if not match:
        match = re.search(r"/display/([^/]+)", path)
    if match:
        space_key = match.group(1)
    if space_key:
        homepage_id = _fetch_confluence_homepage_id(base_url, space_key)
        if homepage_id:
            return base_url, homepage_id

    raise ValueError(
        "Could not find a Confluence page ID in the URL. "
        "Use a link with '/pages/<PAGEID>' or '?pageId=<PAGEID>', "
        "or a space URL like '/spaces/<SPACEKEY>' or '/display/<SPACEKEY>'."
    )


def _fetch_confluence_page(
    base_url: str,
    page_id: str,
) -> dict:
    return _confluence_request(
        f"{base_url}/rest/api/content/{page_id}",
        params={"expand": "body.storage,title"},
    )


def _fetch_confluence_child_pages(
    base_url: str,
    page_id: str,
    limit: int,
) -> list[dict]:
    children: list[dict] = []
    start = 0
    while True:
        payload = _confluence_request(
            f"{base_url}/rest/api/content/{page_id}/child/page",
            params={"limit": limit, "start": start, "expand": "body.storage,title"},
        )
        results = payload.get("results", [])
        children.extend(results)
        if not results:
            break
        next_link = payload.get("_links", {}).get("next")
        if next_link:
            start += len(results)
            continue
        if len(results) < limit:
            break
        start += len(results)
    return children


def download_confluence_space(
    url: str,
    dest_root: Path,
    limit: int = 50,
) -> DownloadResult:
    base_url, root_page_id = _parse_confluence_base_and_page(url)
    folder_name = _sanitize_filename(f"confluence_{root_page_id}")
    dest_path = dest_root / folder_name
    dest_path.mkdir(parents=True, exist_ok=True)

    total_written = 0
    url_map: dict[str, str] = {}
    root_page_url = ""

    queue = [root_page_id]
    seen: set[str] = set()

    while queue:
        page_id = queue.pop(0)
        if page_id in seen:
            continue
        seen.add(page_id)

        page = _fetch_confluence_page(base_url, page_id)
        title = str(page.get("title") or "untitled")
        body = page.get("body", {}).get("storage", {}).get("value", "")
        html = f"<h1>{title}</h1>\n{body}"
        filename = f"{_sanitize_filename(title)}_{page_id}.html"
        (dest_path / filename).write_text(html, encoding="utf-8")
        total_written += 1

        page_url = _resolve_confluence_page_url(base_url, page)
        url_map[Path(filename).as_posix()] = page_url
        if page_id == root_page_id:
            root_page_url = page_url

        children = _fetch_confluence_child_pages(base_url, page_id, limit)
        for child in children:
            child_id = str(child.get("id") or "")
            if child_id:
                queue.append(child_id)

    if total_written == 0:
        raise RuntimeError("No pages were downloaded.")

    _write_confluence_url_map(dest_path, url_map)

    return DownloadResult(
        local_path=dest_path,
        repository_name=folder_name,
        base_url=root_page_url or f"{base_url.rstrip('/')}/pages/{root_page_id}",
    )


def download_source(url: str, source_type: str, dest_root: Path) -> DownloadResult:
    normalized = source_type.strip().lower()
    if normalized in {"git", "git repository", "git repo"}:
        return download_git_repo(url, dest_root)
    if normalized in {"confluence", "confluence space"}:
        return download_confluence_space(url, dest_root)

    raise ValueError(f"Unsupported source type: {source_type}")


if __name__ == "__main__":
    with tempfile.TemporaryDirectory() as temp_dir:
        temp_dir_path = Path(temp_dir)
        download_result = download_source(
            "https://fortissgmbh.atlassian.net/wiki/spaces/MA/overview?homepageId=486113542",
            "confluence",
            temp_dir_path,
        )
        # for debugging print filetree
        print(download_result)
        for path in Path(temp_dir).rglob("*"):
            print(path)
    # url = "https://fortissgmbh.atlassian.net/wiki/api/v2/pages/620199938"

    # auth = HTTPBasicAuth("amougou@fortiss.org", os.getenv("CONFLUENCE_READ_2", "").strip())

    # headers = {
    # "Accept": "application/json"
    # }

    # response = requests.request(
    # "GET",
    # url,
    # headers=headers,
    # auth=auth
    # )

    # print(response.text)