1 Commits

+35 -29
View File
@@ -103,24 +103,24 @@ def _gitea_api(base: str, token: str, method: str, path: str, payload: dict | No
def _upload_asset(base: str, token: str, repo: str, release_id: int, path: Path) -> None: def _upload_asset(base: str, token: str, repo: str, release_id: int, path: Path) -> None:
boundary = "----evosci-release" # curl -F: urllib's hand-rolled multipart gets a 403 from Gitea
data = path.read_bytes() subprocess.run(
body = ( [
f"--{boundary}\r\n" "curl",
f'Content-Disposition: form-data; name="attachment"; filename="{path.name}"\r\n' "-sS",
f"Content-Type: application/octet-stream\r\n\r\n" "-f",
).encode() + data + f"\r\n--{boundary}--\r\n".encode() "-X",
req = urllib.request.Request( "POST",
f"{base}/api/v1/repos/{repo}/releases/{release_id}/assets?name={path.name}", "-H",
data=body, f"Authorization: token {token}",
method="POST", "-F",
headers={ f"attachment=@{path}",
"Authorization": f"token {token}", f"{base}/api/v1/repos/{repo}/releases/{release_id}/assets?name={path.name}",
"Content-Type": f"multipart/form-data; boundary={boundary}", ],
}, check=True,
capture_output=True,
text=True,
) )
with urllib.request.urlopen(req, timeout=300):
pass
def main(argv=None, *, run=subprocess.run, repo_root: Path | None = None) -> int: def main(argv=None, *, run=subprocess.run, repo_root: Path | None = None) -> int:
@@ -269,18 +269,24 @@ def main(argv=None, *, run=subprocess.run, repo_root: Path | None = None) -> int
body = "\n\n".join(notes) or f"Release v{target}" body = "\n\n".join(notes) or f"Release v{target}"
if args.breaking_db: if args.breaking_db:
body = "BREAKING-DB\n\n" + body body = "BREAKING-DB\n\n" + body
rel = _gitea_api( try:
base, rel = _gitea_api(base, token, "GET", f"/repos/{repo}/releases/tags/v{target}")
token, print(f" reusing existing release v{target} (id {rel['id']})")
"POST", except urllib.error.HTTPError as e:
f"/repos/{repo}/releases", if e.code != 404:
{ raise
"tag_name": f"v{target}", rel = _gitea_api(
"name": f"v{target}", base,
"body": body, token,
"prerelease": bool(args.breaking_db), "POST",
}, f"/repos/{repo}/releases",
) {
"tag_name": f"v{target}",
"name": f"v{target}",
"body": body,
"prerelease": bool(args.breaking_db),
},
)
release_id = rel["id"] release_id = rel["id"]
for artifact in dist.iterdir(): for artifact in dist.iterdir():
if artifact.name == "checksums.txt" or artifact.suffix in {".whl", ".tgz", ".gz"}: if artifact.name == "checksums.txt" or artifact.suffix in {".whl", ".tgz", ".gz"}: