79 أسطر
2.2 KiB
Python
79 أسطر
2.2 KiB
Python
import os
|
|
from typing import Dict, Any, Optional, List
|
|
from helpers import get_headers, build_client
|
|
|
|
BASE_URL = os.getenv(
|
|
"GITPASHA_BASE_URL",
|
|
"https://app.gitpasha.com/api/v1"
|
|
).rstrip("/")
|
|
|
|
|
|
def api_update_issue(
|
|
repo: str,
|
|
issue: str,
|
|
title: Optional[str] = None,
|
|
body: Optional[str] = None,
|
|
state: Optional[str] = None,
|
|
labels: Optional[List[str]] = None,
|
|
assignees: Optional[List[str]] = None,
|
|
comment: Optional[str] = None
|
|
) -> Dict[str, Any]:
|
|
repo = repo.strip()
|
|
issue = issue.strip()
|
|
|
|
username = os.getenv("GITPASHA__USERNAME", "")
|
|
|
|
if "/" not in repo:
|
|
if not username:
|
|
raise ValueError("GITPASHA__USERNAME not set in .env")
|
|
repo = f"{username}/{repo}"
|
|
|
|
out: Dict[str, Any] = {}
|
|
with build_client() as client:
|
|
patch_payload: Dict[str, Any] = {}
|
|
if title is not None:
|
|
patch_payload["title"] = title
|
|
if body is not None:
|
|
patch_payload["body"] = body
|
|
if state is not None:
|
|
patch_payload["state"] = state
|
|
if labels is not None:
|
|
patch_payload["labels"] = labels
|
|
if assignees is not None:
|
|
patch_payload["assignees"] = assignees
|
|
|
|
if patch_payload:
|
|
url = f"{BASE_URL}/repos/{repo}/issues/{issue}"
|
|
res = client.patch(
|
|
url,
|
|
headers=get_headers(),
|
|
json=patch_payload
|
|
)
|
|
|
|
if res.status_code == 405:
|
|
res = client.put(
|
|
url,
|
|
headers=get_headers(),
|
|
json=patch_payload
|
|
)
|
|
res.raise_for_status()
|
|
out["update"] = res.json() if res.text else {"status": "ok"}
|
|
|
|
if comment:
|
|
url_c = f"{BASE_URL}/repos/{repo}/issues/{issue}/comments"
|
|
res_c = client.post(
|
|
url_c,
|
|
headers=get_headers(),
|
|
json={
|
|
"body": comment
|
|
}
|
|
)
|
|
res_c.raise_for_status()
|
|
out["comment"] = res_c.json() if res_c.text else {
|
|
"status": "commented"
|
|
}
|
|
|
|
return out or {
|
|
"status": "no-op"
|
|
}
|