|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import json |
| 4 | +import shutil |
| 5 | +from pathlib import Path |
| 6 | +from typing import Any |
| 7 | + |
| 8 | +from .file_utils import write_text |
| 9 | +from .template_engine import render_template |
| 10 | + |
| 11 | + |
| 12 | +def build_github_issue_templates_context(project: dict[str, Any]) -> dict[str, Any] | None: |
| 13 | + app = project.get("app", {}) |
| 14 | + issue_templates = app.get("issue_templates") |
| 15 | + if not issue_templates: |
| 16 | + return None |
| 17 | + if not isinstance(issue_templates, dict): |
| 18 | + raise RuntimeError("app.issue_templates must be a table.") |
| 19 | + |
| 20 | + blank_issues_enabled = issue_templates.get("blank_issues_enabled") |
| 21 | + if not isinstance(blank_issues_enabled, bool): |
| 22 | + raise RuntimeError("app.issue_templates.blank_issues_enabled must be a boolean.") |
| 23 | + |
| 24 | + assignee = str(issue_templates.get("assignee", "")).strip() |
| 25 | + if not assignee: |
| 26 | + raise RuntimeError("app.issue_templates.assignee must be a non-empty string.") |
| 27 | + |
| 28 | + contact_links = issue_templates.get("contact_links") |
| 29 | + if not isinstance(contact_links, list) or not contact_links: |
| 30 | + raise RuntimeError("app.issue_templates.contact_links must be a non-empty list.") |
| 31 | + |
| 32 | + normalized_contact_links: list[dict[str, str]] = [] |
| 33 | + for index, link in enumerate(contact_links): |
| 34 | + if not isinstance(link, dict): |
| 35 | + raise RuntimeError(f"app.issue_templates.contact_links[{index}] must be a table.") |
| 36 | + name = str(link.get("name", "")).strip() |
| 37 | + url = str(link.get("url", "")).strip() |
| 38 | + about = str(link.get("about", "")).strip() |
| 39 | + if not name or not url or not about: |
| 40 | + raise RuntimeError(f"app.issue_templates.contact_links[{index}] must define name, url, and about.") |
| 41 | + normalized_contact_links.append( |
| 42 | + { |
| 43 | + "name": name, |
| 44 | + "url": url, |
| 45 | + "about": about, |
| 46 | + } |
| 47 | + ) |
| 48 | + |
| 49 | + templates: dict[str, dict[str, Any]] = {} |
| 50 | + for template_name in ("bug_report", "documentation_issue", "feature_request"): |
| 51 | + template_value = issue_templates.get(template_name) |
| 52 | + if not isinstance(template_value, dict): |
| 53 | + raise RuntimeError(f"app.issue_templates.{template_name} must be a table.") |
| 54 | + |
| 55 | + name = str(template_value.get("name", "")).strip() |
| 56 | + description = str(template_value.get("description", "")).strip() |
| 57 | + title = str(template_value.get("title", "")).strip() |
| 58 | + labels = template_value.get("labels") |
| 59 | + if not isinstance(labels, list) or not labels: |
| 60 | + raise RuntimeError(f"app.issue_templates.{template_name}.labels must be a non-empty list.") |
| 61 | + normalized_labels: list[str] = [] |
| 62 | + for index, label in enumerate(labels): |
| 63 | + text = str(label).strip() |
| 64 | + if not text: |
| 65 | + raise RuntimeError(f"app.issue_templates.{template_name}.labels[{index}] must be a non-empty string.") |
| 66 | + normalized_labels.append(text) |
| 67 | + if not name or not description or not title: |
| 68 | + raise RuntimeError(f"app.issue_templates.{template_name} must define name, description, and title.") |
| 69 | + |
| 70 | + templates[template_name] = { |
| 71 | + "name": name, |
| 72 | + "description": description, |
| 73 | + "title": title, |
| 74 | + "labels": normalized_labels, |
| 75 | + } |
| 76 | + |
| 77 | + return { |
| 78 | + "blank_issues_enabled": blank_issues_enabled, |
| 79 | + "assignee": assignee, |
| 80 | + "contact_links": normalized_contact_links, |
| 81 | + "yaml_value": lambda value: json.dumps(value, ensure_ascii=False), |
| 82 | + **templates, |
| 83 | + } |
| 84 | + |
| 85 | + |
| 86 | +def generate_github_issue_templates_project(project: dict[str, Any], output_dir: Path) -> None: |
| 87 | + issue_templates_root = output_dir / ".github" / "ISSUE_TEMPLATE" |
| 88 | + context = build_github_issue_templates_context(project) |
| 89 | + if issue_templates_root.exists(): |
| 90 | + shutil.rmtree(issue_templates_root) |
| 91 | + if context is None: |
| 92 | + return |
| 93 | + |
| 94 | + issue_templates_root.mkdir(parents=True, exist_ok=True) |
| 95 | + write_text( |
| 96 | + issue_templates_root / "config.yml", |
| 97 | + render_template("github/issue_templates/config.yml.tpl", context), |
| 98 | + ) |
| 99 | + write_text( |
| 100 | + issue_templates_root / "bug_report.yml", |
| 101 | + render_template("github/issue_templates/bug_report.yml.tpl", context), |
| 102 | + ) |
| 103 | + write_text( |
| 104 | + issue_templates_root / "documentation_issue.yml", |
| 105 | + render_template("github/issue_templates/documentation_issue.yml.tpl", context), |
| 106 | + ) |
| 107 | + write_text( |
| 108 | + issue_templates_root / "feature_request.yml", |
| 109 | + render_template("github/issue_templates/feature_request.yml.tpl", context), |
| 110 | + ) |
0 commit comments