feat: configure WebDAV artifact destination path

This commit is contained in:
shenlei
2026-10-08 15:28:48 +09:00
parent 5582aadc09
commit 9465c98859
7 changed files with 228 additions and 19 deletions
+45 -16
View File
@@ -216,12 +216,10 @@ def _delete_webdav(config: dict, remote_paths: list[str]):
if not server_url or not webdav.get("username"):
raise DistributionError("WebDAV 配置不完整")
base_path = webdav.get("base_path", "/ios-builds").strip("/")
auth = (webdav.get("username", ""), webdav.get("password", ""))
with httpx.Client(auth=auth, timeout=120, follow_redirects=True) as client:
for relative_path in remote_paths:
remote_path = posixpath.join(base_path, relative_path)
response = client.delete(f"{server_url}/{remote_path}")
response = client.delete(f"{server_url}/{quote(relative_path, safe='/')}")
# 404 代表文件已不存在,可视为清理完成。
if response.status_code not in (200, 202, 204, 404):
raise DistributionError(f"WebDAV 删除失败: {relative_path} ({response.status_code})")
@@ -258,8 +256,14 @@ def _remote_paths_from_published_url(
return None
remote_path = unquote(parsed.path).lstrip("/")
# base_url 允许带路径前缀;该前缀是公开地址的一部分,不属于 OSS 对象键。
base_url = upload_config.get("oss", {}).get("base_url", "")
if any(segment == ".." for segment in remote_path.split("/")):
return None
# 公开地址允许带路径前缀;该前缀是下载路由的一部分,不属于远端文件路径。
mode = upload_config.get("mode")
if mode == "webdav":
base_url = upload_config.get("webdav", {}).get("public_url", "")
else:
base_url = upload_config.get("oss", {}).get("base_url", "")
base = urlparse(base_url)
if base_url and base.netloc == parsed.netloc:
base_path = unquote(base.path).strip("/")
@@ -267,6 +271,8 @@ def _remote_paths_from_published_url(
remote_path = remote_path[len(base_path) + 1:]
path = Path(remote_path)
if path.suffix == ".zip":
return [str(path)]
if path.suffix not in {".ipa", ".plist", ".html", ".png"}:
return None
if build_config.get("BUILD_TYPE") == "App_Store":
@@ -287,21 +293,25 @@ def delete_published_artifacts(
raise DistributionError("请选择 OSS 或 WebDAV 上传方式")
remote_paths = None
if mode == "oss" and published_url:
if published_url:
remote_paths = _remote_paths_from_published_url(
build_config, upload_config, published_url,
)
if not remote_paths:
ipa_remote, manifest_remote, html_remote, qr_remote = _remote_paths(build_config)
remote_paths = [ipa_remote]
paths = [ipa_remote]
if build_config.get("BUILD_TYPE") != "App_Store":
remote_paths.extend([manifest_remote, html_remote, qr_remote])
paths.extend([manifest_remote, html_remote, qr_remote])
if mode == "webdav":
remote_paths = [_webdav_remote_path(upload_config, Path(path).name) for path in paths]
else:
remote_paths = paths
deleter = _delete_oss if mode == "oss" else _delete_webdav
deleter(upload_config, remote_paths)
def delete_published_dsym(build_config: dict, upload_config: dict):
def delete_published_dsym(build_config: dict, upload_config: dict, published_url: str = ""):
"""删除任务对应的远端 dSYM 压缩包。
dSYM 的远端对象键完全由构建配置(APPID/版本/分支/构建类型)决定,
@@ -311,8 +321,15 @@ def delete_published_dsym(build_config: dict, upload_config: dict):
if mode not in {"oss", "webdav"}:
raise DistributionError("请选择 OSS 或 WebDAV 上传方式")
remote_paths = _remote_paths_from_published_url(build_config, upload_config, published_url) if published_url else None
if not remote_paths:
remote_path = _dsym_remote_path(build_config)
if mode == "webdav":
remote_path = _webdav_remote_path(upload_config, Path(remote_path).name)
remote_paths = [remote_path]
deleter = _delete_oss if mode == "oss" else _delete_webdav
deleter(upload_config, [_dsym_remote_path(build_config)])
deleter(upload_config, remote_paths)
def _sign_oss_url(upload_config: dict, remote_path: str) -> str:
@@ -358,11 +375,23 @@ def _ensure_webdav_dirs(client: httpx.Client, server_url: str, remote_path: str)
path = ""
for segment in remote_path.strip("/").split("/")[:-1]:
path = f"{path}/{segment}"
response = client.request("MKCOL", f"{server_url.rstrip('/')}{path}")
response = client.request("MKCOL", f"{server_url.rstrip('/')}{path}/")
if response.status_code not in (200, 201, 204, 301, 405):
raise DistributionError(f"创建 WebDAV 目录失败: {path} ({response.status_code})")
def _webdav_remote_path(upload_config: dict, filename: str) -> str:
"""Return the exact WebDAV destination path configured in the admin UI."""
webdav = upload_config.get("webdav", {})
base_path = str(webdav.get("base_path") or "/").strip("/")
if any(segment in {".", ".."} for segment in base_path.split("/")):
raise DistributionError("WebDAV 远程路径不能包含 . 或 ..")
filename = Path(filename).name
if not filename or filename in {".", ".."}:
raise DistributionError("WebDAV 文件名无效")
return posixpath.join(base_path, filename) if base_path else filename
def _upload_webdav(config: dict, files: list[tuple[Path, str]]) -> dict[str, str]:
webdav = config.get("webdav", {})
server_url = webdav.get("server_url", "").rstrip("/")
@@ -370,18 +399,18 @@ def _upload_webdav(config: dict, files: list[tuple[Path, str]]) -> dict[str, str
if not server_url or not webdav.get("username"):
raise DistributionError("WebDAV 配置不完整")
base_path = webdav.get("base_path", "/ios-builds").strip("/")
auth = (webdav.get("username", ""), webdav.get("password", ""))
urls = {}
with httpx.Client(auth=auth, timeout=120, follow_redirects=True) as client:
for local_path, relative_path in files:
remote_path = posixpath.join(base_path, relative_path)
for local_path, _relative_path in files:
# WebDAV 的远程路径由后台配置;不沿用 OSS 的 ios-builds/iOS 对象键。
remote_path = _webdav_remote_path(config, local_path.name)
_ensure_webdav_dirs(client, server_url, remote_path)
with local_path.open("rb") as f:
response = client.put(f"{server_url}/{remote_path}", content=f.read())
response = client.put(f"{server_url}/{quote(remote_path, safe='/')}", content=f.read())
if response.status_code not in (200, 201, 204):
raise DistributionError(f"WebDAV 上传失败: {local_path.name} ({response.status_code})")
urls[local_path.suffix] = f"{public_url}/{quote(remote_path)}"
urls[local_path.suffix] = f"{public_url}/{quote(remote_path, safe='/')}"
return urls