Files
cowork-local/core/jira_tool.py
T
anhtnm1andClaude Opus 5 e29a0ccdbd refactor: vá 4 hồi quy, tách 4 file chạm trần LOC, docstring lên 100%
Hồi quy đã vá
-------------
F-12  Kéo–thả hoặc dán tệp vào ô chat ném NameError. R08 tách `_Input` sang
      `chat_input_box.py` nhưng để `_paths_from_mime()` ở lại
      `composer_widget.py`, nên hai hàm sự kiện Qt gọi một cái tên không tồn
      tại. Bốn hàm dùng chung chuyển sang `composer_mime.py` — module thứ ba
      là chỗ duy nhất không lặp lại được lỗi này. Đo lại: cả thả lẫn dán đều
      gắn 1 tệp, khớp bản trước refactor.

F-01  Đổi provider thì bộ chọn model AI-Edit không làm gì. Hook cũ kiểm
      `folder.ai_model_combo`, thuộc tính R08-T12 đã dời sang
      `ai_panel.resolver`. Làm mới vô điều kiện, đúng như tab cũ: lần lấy đầu
      tiên hỏng thì đổi provider chính là lúc phải thử lại.

F-07  Hàng chọn kỳ của Dashboard bị đẩy xuống dưới các thẻ số liệu. Hàng này
      lọc CẢ BA thẻ con chứ không riêng biểu đồ, nên để nó nằm dưới là bắt
      người dùng đọc con số trước khi thấy con số đó tính cho kỳ nào. Kèm
      theo: `TokenUsageCardWidget` bị bỏ sót `setContentsMargins(0,0,0,0)`
      mà hai thẻ con còn lại đã có, đẩy cả hàng thẻ lệch 9px.
      `check_layout_geometry` nay khớp TỪNG BYTE với bản trước refactor.

F-11  Hai lớp khai trùng tên phương thức; Python giữ bản sau nên bản đầu là
      mã chết. `co4e_tab.py::showEvent` bản đầu gọi `_narrow_guard.attach()`
      và không bao giờ chạy.

Tách file (F-09)
----------------
Bốn file chạm trần 400 dòng, mỗi lần cắt ra một trách nhiệm thật:

    graph_renderer.py         -> graph_scene_builder.py + graph_export.py
    co4e_workflow_service.py  -> co4e_run_history.py
    json_config_repository.py -> config_sections.py
    agents_admin_tab.py       -> shared/agent_kind_visuals.py

File cuối còn xoá 3 bản sao của hàm đã có trong `shared/formatters.py`,
giống hệt đến từng dòng — nay định dạng thời gian và avatar không lệch nhau
giữa các bảng Giám sát nữa.

Docstring
---------
41,6% -> 100% (3.478/3.478 định nghĩa production), kể cả module dormant và
phương thức dunder. Toàn bộ phần bổ sung viết bằng tiếng Việt; comment tiếng
Anh có sẵn giữ nguyên — dịch ngược là một đợt riêng.

Seam chưa nối dây (F-05)
------------------------
9 seam mang nhãn `SEAM · dựng <ngày>` kèm hai câu: được nối khi nào, và để
dormant thì hỏng gì. Ngày lấy từ lịch sử git, không phải hạn tự đặt. Gate O
đọc nhãn đó và nhắc khi quá 30 ngày.

859 test xanh · 4/4 cổng CASAN · 19/24 checker khớp từng byte bản cũ.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-30 10:41:45 +09:00

159 lines
6.4 KiB
Python

"""Read-only Jira connector for the agent (jira_search / jira_get_issue).
Talks to Jira Cloud's REST API v2 with Basic auth (email + API token), so no
OAuth/app setup is needed — the user pastes a base URL, their Atlassian email
and an API token (id.atlassian.com → Security → API tokens) once in
Monitoring → Tools. Read-only: it fetches issues/fields, never writes.
Every function returns a human-readable text block (or an explanatory error
string) and never raises, so a Jira hiccup can't break an agent turn.
"""
from __future__ import annotations
import re
from typing import Any, Dict, List, Optional
from urllib.parse import parse_qs, urlparse
_TIMEOUT = (10, 20)
_MAX_RESULTS = 25
_KEY_RE = re.compile(r"\b([A-Z][A-Z0-9]+-\d+)\b")
def _conf(config: Dict[str, Any] | None) -> Dict[str, str]:
"""Ba trường cấu hình Jira đã cắt khoảng trắng: base_url, email, api_token."""
return {k: str((config or {}).get(k, "") or "").strip()
for k in ("base_url", "email", "api_token")}
def configured(config: Dict[str, Any] | None) -> bool:
"""Đã cấu hình đủ ba trường để gọi Jira chưa."""
c = _conf(config)
return bool(c["base_url"] and c["email"] and c["api_token"])
def key_from_url(url: str) -> Optional[str]:
"""Extract an issue key (ABX-123) from a Jira URL — handles /browse/KEY and
boards/backlog links with ?selectedIssue=KEY. Returns None if none found."""
if not url:
return None
try:
parsed = urlparse(url)
except ValueError:
return None
sel = parse_qs(parsed.query or "").get("selectedIssue")
if sel:
m = _KEY_RE.search(sel[0])
if m:
return m.group(1)
m = _KEY_RE.search(parsed.path or "")
return m.group(1) if m else None
def base_url_from_link(url: str) -> str:
"""Derive the Jira site base URL (scheme://host) from ANY pasted Jira link,
so the user can paste an issue/board link and the base URL fills itself."""
try:
p = urlparse((url or "").strip())
except ValueError:
return ""
if p.scheme in ("http", "https") and p.hostname:
return f"{p.scheme}://{p.hostname}"
return ""
def is_jira_issue_url(config: Dict[str, Any] | None, url: str) -> bool:
"""True when ``url`` points at an issue on the CONFIGURED Jira host — so a
pasted link can be resolved through the authenticated API instead of a raw
(login-walled) HTTP fetch."""
if not configured(config) or not url:
return False
try:
host = (urlparse(url).hostname or "").lower()
base = (urlparse(_conf(config)["base_url"]).hostname or "").lower()
except ValueError:
return False
return bool(host and base and host == base and key_from_url(url))
def get_issue_by_url(config: Dict[str, Any] | None, url: str) -> str:
"""Read the issue a Jira URL points at, via the authenticated API."""
key = key_from_url(url)
if not key:
return f"[Jira link: {url}] (couldn't find an issue key in the URL)."
return get_issue(config, key)
def _get(config: Dict[str, Any], path: str, params: dict = None):
"""Gọi Jira REST API bằng xác thực cơ bản, qua lớp TLS có ghim chứng chỉ nội bộ."""
from . import tls_trust
c = _conf(config)
url = c["base_url"].rstrip("/") + path
# Same TLS auto-recovery the LLM provider calls get (core/tls_trust.py) —
# a corporate gateway that terminates TLS with its own certificate used to
# break this outright with SSLCertVerificationError.
resp = tls_trust.request("get", url, params=params or {}, timeout=_TIMEOUT,
auth=(c["email"], c["api_token"]),
headers={"Accept": "application/json"})
resp.raise_for_status()
return resp.json()
def _fmt_issue(it: dict) -> str:
"""Một dòng tóm tắt issue: mã, trạng thái và tiêu đề."""
f = it.get("fields", {}) or {}
status = (f.get("status") or {}).get("name", "?")
assignee = (f.get("assignee") or {}).get("displayName", "unassigned")
prio = (f.get("priority") or {}).get("name", "")
parts = [f"{it.get('key', '?')} — {f.get('summary', '(no summary)')}",
f" status: {status} · assignee: {assignee}" + (f" · priority: {prio}" if prio else "")]
return "\n".join(parts)
def search(config: Dict[str, Any] | None, jql: str, max_results: int = _MAX_RESULTS) -> str:
"""Search issues by JQL, e.g. ``project = ABX AND status = "In Progress"``."""
if not configured(config):
return ("Jira is not configured. Set base URL, email and API token in "
"Monitoring → Tools → Jira first.")
if not (jql or "").strip():
return "jira_search: a JQL query is required."
try:
data = _get(config, "/rest/api/2/search",
{"jql": jql, "maxResults": max(1, min(max_results, 50)),
"fields": "summary,status,assignee,priority"})
except Exception as exc: # noqa: BLE001
return f"Jira search failed: {exc}"
issues: List[dict] = data.get("issues", []) or []
if not issues:
return f"No issues match: {jql}"
total = data.get("total", len(issues))
head = f"Found {total} issue(s) for `{jql}` (showing {len(issues)}):\n"
return head + "\n\n".join(_fmt_issue(it) for it in issues)
def get_issue(config: Dict[str, Any] | None, key: str) -> str:
"""Fetch one issue's key fields + description by key (e.g. ABX-123)."""
if not configured(config):
return ("Jira is not configured. Set base URL, email and API token in "
"Monitoring → Tools → Jira first.")
key = (key or "").strip()
if not key:
return "jira_get_issue: an issue key is required (e.g. ABX-123)."
try:
it = _get(config, f"/rest/api/2/issue/{key}",
{"fields": "summary,status,assignee,priority,description,labels,updated"})
except Exception as exc: # noqa: BLE001
return f"Could not fetch {key}: {exc}"
f = it.get("fields", {}) or {}
desc = f.get("description")
if isinstance(desc, dict): # ADF (v3) → not requested here, but be safe
desc = "(rich-text description — open in Jira)"
lines = [_fmt_issue(it)]
if f.get("labels"):
lines.append(f" labels: {', '.join(f['labels'])}")
if f.get("updated"):
lines.append(f" updated: {f['updated']}")
if desc:
lines.append(f"\n{str(desc)[:4000]}")
return "\n".join(lines)