issues.py¶
clew/wiki/issues.py
Issue links: a prefilled "raise an issue" URL for GitHub or Azure DevOps (round 3, P1d).
create
tuple[str, str] clew.wiki.issues.IssueProvider.create(self, str req_id, str title, str body)
Create an issue carrying the requirement id in its title, as a req: label and as a marker line.
Source · 6 lines
def create(self, req_id: str, title: str, body: str) -> tuple[str, str]:
"""@brief Create. @return Id and URL."""
title = title if req_id in title else f"{req_id}: {title}"
body = f"{body}\n\nclew-req: {req_id}".lstrip()
return self._create(title, body, f"req:{req_id}")
Activity · 3 steps
Requirements
fetch_pages
tuple[list[dict], bool] clew.wiki.issues.fetch_pages(Any get_page, int max_pages)
Follow a paged listing of 100 per page until a short page or the page cap.
Source · 17 lines
def fetch_pages(get_page: Any, max_pages: int) -> tuple[list[dict], bool]:
"""R5-CFG D14 (2.0.0): the loop stopped at its cap and returned what it had with NO disclosure,
so a tracker with more open issues than the cap read as having exactly that many — the
"absence is not emptiness" failure. The cap stays; reaching it is now reported.
@brief Page through a listing.
@return Rows and the truncation flag.
"""
out: list[dict] = []
for page in range(1, max_pages + 1):
rows = get_page(page) or []
out += rows
if len(rows) < 100:
return out, False
return out, True
Activity · 7 steps
Requirements
issue_url
str clew.wiki.issues.issue_url(Target target, str req_id, str title, str body)
A prefilled new-issue URL carrying the requirement id three ways.
Called by _issue_link
Tested by
Calls none
Source · 18 lines
def issue_url(target: Target, req_id: str, title: str, body: str) -> str:
"""@brief Build the URL. @return URL."""
provider, project = target
title = title if req_id in title else f"{req_id}: {title}"
body = f"{body}\n\nclew-req: {req_id}".lstrip()
label = f"req:{req_id}"
if provider == "github":
query = urlencode({"title": title, "labels": label, "body": body})
return f"https://github.com/{project}/issues/new?{query}"
query = urlencode(
{"[System.Title]": title, "[System.Tags]": label, "[System.Description]": body}
)
return f"https://dev.azure.com/{project}/_workitems/create/Issue?{query}"
# --- read and write side (round 3, P3a) -----------------------------------------------------------
Activity · 9 steps
Requirements
link_issues
tuple[dict[str, list[Issue]], list[Issue]] clew.wiki.issues.link_issues(list[Issue] issues, list[str] req_ids)
Link open issues to requirements: id in title or body, req: label, marker line.
Source · 26 lines
def link_issues(
issues: list[Issue], req_ids: list[str]
) -> tuple[dict[str, list[Issue]], list[Issue]]:
"""AN ID MATCHES WHOLE: `REQ-W-0011` does not link `REQ-W-001` — the character after the id
may not continue it.
@brief Link.
@return Linked and unlinked.
"""
linked: dict[str, list[Issue]] = {}
unlinked = []
patterns = {r: re.compile(rf"(?<![\w-]){re.escape(r)}(?![\w])") for r in req_ids}
for issue in issues:
text = f"{issue.title}\n{issue.body}"
hits = [
r
for r, pattern in patterns.items()
if f"req:{r}" in issue.labels or pattern.search(text)
]
for r in hits:
linked.setdefault(r, []).append(issue)
if not hits:
unlinked.append(issue)
return linked, unlinked
Activity · 11 steps
Requirements
Flows
list_open
list[Issue] clew.wiki.issues.IssueProvider.list_open(self)
Every open issue.
Source · 4 lines
def list_open(self) -> list[Issue]:
"""@brief List. @return Issues."""
return self._list()
Activity · 1 steps
Requirements
provider_for
IssueProvider clew.wiki.issues.provider_for(Target target, Any opener=None)
The provider for a tracker, its token read from the environment.
Source · 15 lines
def provider_for(target: Target, opener: Any = None) -> IssueProvider:
"""TOKENS COME FROM THE ENVIRONMENT, never the config file: `GITHUB_TOKEN` / `GH_TOKEN`,
`AZURE_DEVOPS_EXT_PAT` / `ADO_PAT`.
@brief Provider.
@return Provider.
"""
provider, project = target
if provider == "github":
token = os.environ.get("GITHUB_TOKEN") or os.environ.get("GH_TOKEN")
return GitHubIssues(project, token, opener)
token = os.environ.get("AZURE_DEVOPS_EXT_PAT") or os.environ.get("ADO_PAT")
return AdoIssues(project, token, opener)
Activity · 6 steps
Requirements
Flows
resolve_target
Target | None clew.wiki.issues.resolve_target(Target|None declared, bool enabled, Path repo_root)
The repository's tracker: declared, else its origin remote's, else none.
Source · 25 lines
def resolve_target(declared: Target | None, enabled: bool, repo_root: Path) -> Target | None:
"""THROUGH THE HARDENED GIT ENVIRONMENT, like every git call: the repository's own config
is data here, never something that runs.
@brief Resolve the tracker.
@return Target or None.
"""
from ..gitenv import git_argv
if not enabled or declared:
return declared if enabled else None
try:
out = subprocess.run(
git_argv("config", "--get", "remote.origin.url"),
cwd=repo_root,
capture_output=True,
text=True,
check=False,
timeout=30,
)
except (OSError, subprocess.TimeoutExpired):
return None
return target_from_remote(out.stdout) if out.stdout.strip() else None
Activity · 8 steps
Requirements
target_from_remote
Target | None clew.wiki.issues.target_from_remote(str url)
The tracker a remote URL belongs to.
Source · 13 lines
def target_from_remote(url: str) -> Target | None:
"""@brief Recognise GitHub and Azure DevOps remotes. @return Target or None."""
url = url.strip()
found = _GITHUB.search(url)
if found:
return "github", f"{found.group(1)}/{found.group(2)}"
for pattern in (_ADO_HTTPS, _ADO_SSH, _ADO_LEGACY):
found = pattern.search(url)
if found:
return "ado", f"{found.group(1)}/{found.group(2)}"
return None