mirror of
https://github.com/github/spec-kit.git
synced 2026-07-03 12:28:06 +08:00
* feat: Stage 2a — CopilotIntegration with shared template primitives - base.py: added granular primitives (shared_commands_dir, shared_templates_dir, list_command_templates, command_filename, commands_dest, copy_command_to_directory, record_file_in_manifest, write_file_and_record, process_template) - CopilotIntegration: uses primitives to produce .agent.md commands, companion .prompt.md files, and .vscode/settings.json - Verified byte-for-byte parity with old release script output - Copilot auto-registered in INTEGRATION_REGISTRY - 70 tests (22 new: base primitives + copilot integration) Part of #1924 * feat: Stage 2b — --integration flag, routing, agent.json, shared infra - Added --integration flag to init() (mutually exclusive with --ai) - --ai copilot auto-promotes to integration path with migration nudge - Integration setup writes .specify/agent.json with integration key - _install_shared_infra() copies scripts and templates to .specify/ - init-options.json records 'integration' key when used - 4 new CLI tests: mutual exclusivity, unknown rejection, copilot end-to-end, auto-promote (74 total integration tests) Part of #1924 * feat: Stage 2 completion — integration scripts, integration.json, shared manifest - Added copilot/scripts/update-context.sh and .ps1 (thin wrappers that delegate to the shared update-agent-context script) - CopilotIntegration.setup() installs integration scripts to .specify/integrations/copilot/scripts/ - Renamed agent.json → integration.json with script paths - _install_shared_infra() now tracks files in integration-shared.manifest.json - Updated tests: scripts installed, integration.json has script paths, shared manifest recorded (74 tests) Part of #1924 * refactor: rename shared manifest to speckit.manifest.json Cleaner naming — the shared infrastructure (scripts, templates) belongs to spec-kit itself, not to any specific integration. * fix: copilot update-context scripts reflect target architecture Scripts now source shared functions (via SPECKIT_SOURCE_ONLY=1) and call update_agent_file directly with .github/copilot-instructions.md, rather than delegating back to the shared case statement. * fix: simplify copilot scripts — dispatcher sources common functions Integration scripts now contain only copilot-specific logic (target path + agent name). The dispatcher is responsible for sourcing shared functions before calling the integration script. * fix: copilot update-context scripts are self-contained implementations These scripts ARE the implementation — the dispatcher calls them. They source common.sh + update-agent-context functions, gather feature/plan data, then call update_agent_file with the copilot target path (.github/copilot-instructions.md). * docs: add Stage 7 activation note to copilot update-context scripts * test: add complete file inventory test for copilot integration Validates every single file (37 total) produced by specify init --integration copilot --script sh --no-git. * test: add PowerShell file inventory test for copilot integration Validates all 37 files produced by --script ps variant, including .specify/scripts/powershell/ instead of bash. * refactor: split test_integrations.py into tests/integrations/ directory - test_base.py: IntegrationOption, IntegrationBase, MarkdownIntegration, primitives - test_manifest.py: IntegrationManifest, path traversal, persistence, validation - test_registry.py: INTEGRATION_REGISTRY - test_copilot.py: CopilotIntegration unit tests - test_cli.py: --integration flag, auto-promote, file inventories (sh + ps) - conftest.py: shared StubIntegration helper 76 integration tests + 48 consistency tests = 124 total, all passing. * refactor: move file inventory tests from test_cli to test_copilot File inventories are copilot-specific. test_cli.py now only tests CLI flag mechanics (mutual exclusivity, unknown rejection, auto-promote). * fix: skip JSONC merge to preserve user settings, fix docstring - _merge_vscode_settings() now returns early (skips merge) when existing settings.json can't be parsed (e.g. JSONC with comments), instead of overwriting with empty settings - Updated _install_shared_infra() docstring to match implementation (scripts + templates, speckit.manifest.json) * fix: warn user when JSONC settings merge is skipped * fix: show template content when JSONC merge is skipped User now sees the exact settings they should add manually. * fix: document process_template requirement, merge scripts without rmtree - base.py setup() docstring now explicitly states raw copy behavior and directs to CopilotIntegration for process_template example - _install_shared_infra() uses merge/overwrite instead of rmtree to preserve user-added files under .specify/scripts/ * fix: don't overwrite pre-existing shared scripts or templates Only write files that don't already exist — preserves any user modifications to shared scripts (common.sh etc.) and templates. * fix: warn user about skipped pre-existing shared files Lists all shared scripts and templates that were not copied because they already existed in the project. * test: add test for shared infra skip behavior on pre-existing files Verifies that _install_shared_infra() preserves user-modified scripts and templates while still installing missing ones. * fix: address review — containment check, deterministic prompts, manifest accuracy - CopilotIntegration.setup() adds dest containment check (relative_to) - Companion prompts generated from templates list, not directory glob - _install_shared_infra() only records files actually copied (not pre-existing) - VS Code settings tests made unconditional (assert template exists) - Inventory tests use .as_posix() for cross-platform paths * fix: correct PS1 function names, document SPECKIT_SOURCE_ONLY prerequisite - Fixed Get-FeaturePaths → Get-FeaturePathsEnv, Read-PlanData → Parse-PlanData - Documented that shared scripts must guard Main with SPECKIT_SOURCE_ONLY before these integration scripts can be activated (Stage 7) * fix: add dict type check for settings merge, simplify PS1 to subprocess - _merge_vscode_settings() skips merge with warning if parsed JSON is not a dict (array, null, etc.) - PS1 update-context.ps1 uses & invocation instead of dot-sourcing since the shared script runs Main unconditionally * fix: skip-write on no-op merge, bash subprocess, dynamic integration list - _merge_vscode_settings() only writes when keys were actually added - update-context.sh uses exec subprocess like PS1 version - Unknown integration error lists available integrations dynamically * fix: align path rewriting with release script, add .specify/.specify/ fix Path rewrite regex matches the release script's rewrite_paths() exactly (verified byte-identical output). Added .specify/.specify/ double-prefix fix for additional safety.
198 lines
7.6 KiB
Python
198 lines
7.6 KiB
Python
"""Copilot integration — GitHub Copilot in VS Code.
|
|
|
|
Copilot has several unique behaviors compared to standard markdown agents:
|
|
- Commands use ``.agent.md`` extension (not ``.md``)
|
|
- Each command gets a companion ``.prompt.md`` file in ``.github/prompts/``
|
|
- Installs ``.vscode/settings.json`` with prompt file recommendations
|
|
- Context file lives at ``.github/copilot-instructions.md``
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import shutil
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
from ..base import IntegrationBase
|
|
from ..manifest import IntegrationManifest
|
|
|
|
|
|
class CopilotIntegration(IntegrationBase):
|
|
"""Integration for GitHub Copilot in VS Code."""
|
|
|
|
key = "copilot"
|
|
config = {
|
|
"name": "GitHub Copilot",
|
|
"folder": ".github/",
|
|
"commands_subdir": "agents",
|
|
"install_url": None,
|
|
"requires_cli": False,
|
|
}
|
|
registrar_config = {
|
|
"dir": ".github/agents",
|
|
"format": "markdown",
|
|
"args": "$ARGUMENTS",
|
|
"extension": ".agent.md",
|
|
}
|
|
context_file = ".github/copilot-instructions.md"
|
|
|
|
def command_filename(self, template_name: str) -> str:
|
|
"""Copilot commands use ``.agent.md`` extension."""
|
|
return f"speckit.{template_name}.agent.md"
|
|
|
|
def setup(
|
|
self,
|
|
project_root: Path,
|
|
manifest: IntegrationManifest,
|
|
parsed_options: dict[str, Any] | None = None,
|
|
**opts: Any,
|
|
) -> list[Path]:
|
|
"""Install copilot commands, companion prompts, and VS Code settings.
|
|
|
|
Uses base class primitives to: read templates, process them
|
|
(replace placeholders, strip script blocks, rewrite paths),
|
|
write as ``.agent.md``, then add companion prompts and VS Code settings.
|
|
"""
|
|
project_root_resolved = project_root.resolve()
|
|
if manifest.project_root != project_root_resolved:
|
|
raise ValueError(
|
|
f"manifest.project_root ({manifest.project_root}) does not match "
|
|
f"project_root ({project_root_resolved})"
|
|
)
|
|
|
|
templates = self.list_command_templates()
|
|
if not templates:
|
|
return []
|
|
|
|
dest = self.commands_dest(project_root)
|
|
dest_resolved = dest.resolve()
|
|
try:
|
|
dest_resolved.relative_to(project_root_resolved)
|
|
except ValueError as exc:
|
|
raise ValueError(
|
|
f"Integration destination {dest_resolved} escapes "
|
|
f"project root {project_root_resolved}"
|
|
) from exc
|
|
dest.mkdir(parents=True, exist_ok=True)
|
|
created: list[Path] = []
|
|
|
|
script_type = opts.get("script_type", "sh")
|
|
arg_placeholder = self.registrar_config.get("args", "$ARGUMENTS")
|
|
|
|
# 1. Process and write command files as .agent.md
|
|
for src_file in templates:
|
|
raw = src_file.read_text(encoding="utf-8")
|
|
processed = self.process_template(raw, self.key, script_type, arg_placeholder)
|
|
dst_name = self.command_filename(src_file.stem)
|
|
dst_file = self.write_file_and_record(
|
|
processed, dest / dst_name, project_root, manifest
|
|
)
|
|
created.append(dst_file)
|
|
|
|
# 2. Generate companion .prompt.md files from the templates we just wrote
|
|
prompts_dir = project_root / ".github" / "prompts"
|
|
for src_file in templates:
|
|
cmd_name = f"speckit.{src_file.stem}"
|
|
prompt_content = f"---\nagent: {cmd_name}\n---\n"
|
|
prompt_file = self.write_file_and_record(
|
|
prompt_content,
|
|
prompts_dir / f"{cmd_name}.prompt.md",
|
|
project_root,
|
|
manifest,
|
|
)
|
|
created.append(prompt_file)
|
|
|
|
# Write .vscode/settings.json
|
|
settings_src = self._vscode_settings_path()
|
|
if settings_src and settings_src.is_file():
|
|
dst_settings = project_root / ".vscode" / "settings.json"
|
|
dst_settings.parent.mkdir(parents=True, exist_ok=True)
|
|
if dst_settings.exists():
|
|
# Merge into existing — don't track since we can't safely
|
|
# remove the user's settings file on uninstall.
|
|
self._merge_vscode_settings(settings_src, dst_settings)
|
|
else:
|
|
shutil.copy2(settings_src, dst_settings)
|
|
self.record_file_in_manifest(dst_settings, project_root, manifest)
|
|
created.append(dst_settings)
|
|
|
|
# 4. Install integration-specific update-context scripts
|
|
scripts_src = Path(__file__).resolve().parent / "scripts"
|
|
if scripts_src.is_dir():
|
|
scripts_dest = project_root / ".specify" / "integrations" / "copilot" / "scripts"
|
|
scripts_dest.mkdir(parents=True, exist_ok=True)
|
|
for src_script in sorted(scripts_src.iterdir()):
|
|
if src_script.is_file():
|
|
dst_script = scripts_dest / src_script.name
|
|
shutil.copy2(src_script, dst_script)
|
|
# Make shell scripts executable
|
|
if dst_script.suffix == ".sh":
|
|
dst_script.chmod(dst_script.stat().st_mode | 0o111)
|
|
self.record_file_in_manifest(dst_script, project_root, manifest)
|
|
created.append(dst_script)
|
|
|
|
return created
|
|
|
|
def _vscode_settings_path(self) -> Path | None:
|
|
"""Return path to the bundled vscode-settings.json template."""
|
|
tpl_dir = self.shared_templates_dir()
|
|
if tpl_dir:
|
|
candidate = tpl_dir / "vscode-settings.json"
|
|
if candidate.is_file():
|
|
return candidate
|
|
return None
|
|
|
|
@staticmethod
|
|
def _merge_vscode_settings(src: Path, dst: Path) -> None:
|
|
"""Merge settings from *src* into existing *dst* JSON file.
|
|
|
|
Top-level keys from *src* are added only if missing in *dst*.
|
|
For dict-valued keys, sub-keys are merged the same way.
|
|
|
|
If *dst* cannot be parsed (e.g. JSONC with comments), the merge
|
|
is skipped to avoid overwriting user settings.
|
|
"""
|
|
try:
|
|
existing = json.loads(dst.read_text(encoding="utf-8"))
|
|
except (json.JSONDecodeError, OSError):
|
|
# Cannot parse existing file (likely JSONC with comments).
|
|
# Skip merge to preserve the user's settings, but show
|
|
# what they should add manually.
|
|
import logging
|
|
template_content = src.read_text(encoding="utf-8")
|
|
logging.getLogger(__name__).warning(
|
|
"Could not parse %s (may contain JSONC comments). "
|
|
"Skipping settings merge to preserve existing file.\n"
|
|
"Please add the following settings manually:\n%s",
|
|
dst, template_content,
|
|
)
|
|
return
|
|
|
|
new_settings = json.loads(src.read_text(encoding="utf-8"))
|
|
|
|
if not isinstance(existing, dict) or not isinstance(new_settings, dict):
|
|
import logging
|
|
logging.getLogger(__name__).warning(
|
|
"Skipping settings merge: %s or template is not a JSON object.", dst
|
|
)
|
|
return
|
|
|
|
changed = False
|
|
for key, value in new_settings.items():
|
|
if key not in existing:
|
|
existing[key] = value
|
|
changed = True
|
|
elif isinstance(existing[key], dict) and isinstance(value, dict):
|
|
for sub_key, sub_value in value.items():
|
|
if sub_key not in existing[key]:
|
|
existing[key][sub_key] = sub_value
|
|
changed = True
|
|
|
|
if not changed:
|
|
return
|
|
|
|
dst.write_text(
|
|
json.dumps(existing, indent=4) + "\n", encoding="utf-8"
|
|
)
|