diff --git a/.github/copilot-instructions.md b/.github/copilot-instructions.md new file mode 100644 index 0000000..8e95b4b --- /dev/null +++ b/.github/copilot-instructions.md @@ -0,0 +1,207 @@ +# CM-Beetle Copilot Instructions + +## Project Rules + +- **Language & Framework:** Go 1.21+, Echo Framework. +- **Documentation Style:** Technical accuracy with a friendly and professional tone. +- **Language Setting:** All responses and explanations must be in **English**. +- **Code Standards:** + - Echo handlers must follow the standard pattern using `context.Bind` and `context.JSON`. + - Refer to `pkg/core/common/error.go` for standard error types. + +This repository contains the source code for **CM-Beetle** (Computing Infrastructure Migration), a sub-system of the Cloud-Barista platform. + +## Project Overview + +- **Objectives:** Recommend optimal target computing infrastructure (cloud infrastructure) based on source infrastructure information, and execute migration according to the recommendation. +- **Languages:** Go (1.25.0+). +- **Frameworks:** Echo (Web Framework), Viper (Config), Zerolog (Logging), Swag (API Docs). +- **Dependencies:** `cb-tumblebug`, `cm-model`. +- **Supported Clouds:** AWS, Azure, GCP, NCP, Alibaba Cloud. +- **Architecture:** REST API server (Echo) with a modular architecture separating API handlers from core logic (migration, recommendation). It functions as an orchestration layer, tightly integrated with CB-Tumblebug for managing multi-cloud resources. + +## Key Dependencies & Integration Patterns + +### Key Reference Files + +- **API Specification:** `api/swagger.yaml` (Source of truth for API contracts). +- **Data Models:** `pkg/api/rest/model/` (Request/Response structures). +- **Configuration:** `conf/config.yaml` (Available configuration options). +- **Error Definitions:** `pkg/core/common/error.go` (Standard error types). + +### CB-Tumblebug Integration + +- **Core Model Import:** `tbmodel "github.com/cloud-barista/cb-tumblebug/src/core/model"` +- **Resource Interface Import:** `tbresource "github.com/cloud-barista/cb-tumblebug/src/interface/rest/server/resource"` +- **Network Utilities Import:** `"github.com/cloud-barista/cb-tumblebug/src/core/common/netutil"` +- **Client Package:** `pkg/client/tumblebug/` (Contains all Tumblebug API clients) +- **Key Components:** + - `TumblebugClient`: Main client for interactions (MCI, Namespace, VM, VNet, SecurityGroup, SSH key). + +### cm-model Integration + +- **Cloud Model Import:** `cloudmodel "github.com/cloud-barista/cm-model/infra/cloud-model"` +- **On-Premise Model Import:** `onpremmodel "github.com/cloud-barista/cm-model/infra/on-premise-model"` +- **Usage:** Shared data models for cloud and on-premise infrastructure. + +## Architecture & Code Organization + +### Core Packages Structure + +- `cmd/cm-beetle/`: Main application entry point (`main.go`). +- `pkg/api/rest/`: REST API handlers (`controller/`), middlewares (`middlewares/`), and models (`model/`). +- `pkg/client/tumblebug/`: CB-Tumblebug API client implementations. +- `pkg/core/`: Core logic and algorithms. + - `migration/`: Infrastructure migration logic (CreateVMInfra, etc.). + - `recommendation/`: Infrastructure recommendation logic (VM Specs, OS Images). + - `common/`: Shared utilities. +- `pkg/config/`: Configuration management (Viper). +- `pkg/modelconv/`: Model conversion utilities between internal, Tumblebug, and cm-model formats. + +### Key Functional Areas + +1. **Infrastructure Migration (`pkg/core/migration/`)**: + - Migrate on-premise infrastructure to multi-cloud. + - `CreateVMInfra()`: Create target cloud infrastructure via CB-Tumblebug. +2. **Infrastructure Recommendation (`pkg/core/recommendation/`)**: + - Recommend optimal cloud configurations (VM Specs, OS Images, VNet). + - Uses similarity-based matching algorithms. + +### Key Design Patterns + +- **Middleware Pattern:** + - `TumblebugInitChecker`: Ensures Tumblebug is initialized before processing requests. + - Authentication and CORS handling. +- **Model Conversion Pattern:** + - Use `modelconv` package for converting between different model formats. + - Heavy use of CB-Tumblebug and cm-model structures. + +## Coding Standards & Conventions + +### General + +- Write all code comments and documentation in English. +- Use English for struct field comments, function comments, inline comments, and TODO/FIXME notes. +- Ensure consistency and accessibility for international contributors. + +### Go + +- Detailed Go coding standards are managed in `.github/instructions/go.instructions.md`. + +## Common Patterns + +### API Handler Pattern + +```go +func HandlerName(c echo.Context) error { + nsId := c.Param("nsId") + var req RequestModel + if err := c.Bind(&req); err != nil { + return c.JSON(http.StatusBadRequest, model.Response{Message: err.Error()}) + } + + // Core logic + result, err := coreLogic(nsId, req) + if err != nil { + log.Error().Err(err).Msg("Operation failed") + return c.JSON(http.StatusInternalServerError, model.Response{Message: err.Error()}) + } + return c.JSON(http.StatusOK, result) +} +``` + +### Client Integration Pattern + +```go +// Initialize client +client := tbclient.NewDefaultClient() + +// Execute request with proper error handling +result, err := client.SomeOperation(params) +if err != nil { + log.Error().Err(err).Msg("Tumblebug operation failed") + return nil, err +} +return result, nil +``` + +### Model Conversion Pattern + +```go +// Convert between different model formats +targetModel := modelconv.ConvertToTargetFormat(sourceModel) + +// Use appropriate imports for model types +tbModel := tbmodel.SomeModel{} +cloudModel := cloudmodel.SomeModel{} +``` + +## Development Workflow + +### Working with External Dependencies + +#### CB-Tumblebug Integration + +- Always use the client packages in `pkg/client/tumblebug/`. +- Handle CB-Tumblebug initialization requirements (e.g., using `TumblebugInitChecker`). +- Respect CB-Tumblebug's REST API patterns and authentication. +- Use proper namespace management. + +#### cm-model Integration + +- Import shared models from `cm-model` package. +- Ensure model compatibility between versions. +- Use conversion utilities (`pkg/modelconv/`) when transforming data. + +### Environment Setup + +- **Local Development:** Use `replace` directives in `go.mod` for local dependency testing (e.g., `cb-tumblebug`, `cm-model`). +- **Production:** Ensure `go.mod` specifies production versions. + +## Build and Run Instructions + +Always use the `Makefile` for build and run tasks. + +- **Build:** `make build` (builds `cm-beetle` binary in `cmd/cm-beetle/`). +- **Run:** `make run` (sources `conf/setup.env` and runs the binary). +- **Clean:** `make clean` (removes build artifacts). +- **Lint:** `make lint` (runs `golangci-lint`). +- **Test CLI:** `make test-cli` (builds and runs the test CLI). + +## API Documentation + +- The API is documented using **Swagger**. +- If you modify API handlers, update the Swagger comments. +- Regenerate documentation with: `make swag`. +- Swagger files are located in `api/`. +- **Example:** + ```go + // CreateMCI godoc + // @ID CreateMCI + // @Summary Create Multi-Cloud Infrastructure + // @Description Create a multi-cloud infrastructure + // @Tags [Migration] Infrastructure + // @Accept json + // @Produce json + // @Param nsId path string true "Namespace ID" + // @Success 200 {object} ResponseModel + // @Router /migration/ns/{nsId}/mci [post] + ``` + +## Common Tasks + +- **Adding a new API endpoint:** + + 1. Define the handler in `pkg/api/rest/`. + 2. Add the route in `cmd/cm-beetle/main.go` (or relevant router setup). + 3. Add Swagger comments to the handler. + 4. Run `make swag`. + +- **Running locally:** + + 1. Ensure `conf/setup.env` and `conf/config.yaml` are configured. + 2. Run `make run`. + +- **Testing:** + - Use `go test ./...` for unit tests. + - Use `make test-cli` for integration-like testing with the CLI. diff --git a/.github/instructions/analyzer.instructions.md b/.github/instructions/analyzer.instructions.md new file mode 100644 index 0000000..ff61e3b --- /dev/null +++ b/.github/instructions/analyzer.instructions.md @@ -0,0 +1,21 @@ +--- +applyTo: "analyzer/**" +--- + +# Analyzer Module Instructions + +This directory (`analyzer/`) contains the **File System Analysis & Metadata Extraction Module**. +It is a self-contained Go module with its own `go.mod`. + +## Key Constraints + +- **Independent Module:** Treat this as a separate library. +- **No Upstream Dependencies:** Do NOT import packages from `cm-beetle/pkg/...` or `cm-beetle/cmd/...`. +- **Dependency Management:** Add dependencies to `analyzer/go.mod`, not the root `go.mod`. + +## Functionality + +- **Directory Scanning:** Recursively list directory contents (`ListDirectory`). +- **Metadata Extraction:** Analyze file properties (size, permissions, modification time). +- **Pattern Filtering:** Filter files based on glob patterns (include/exclude). +- **Usage:** Primarily used to analyze source infrastructure before migration. diff --git a/.github/instructions/go.instructions.md b/.github/instructions/go.instructions.md new file mode 100644 index 0000000..8c9a6a4 --- /dev/null +++ b/.github/instructions/go.instructions.md @@ -0,0 +1,41 @@ +--- +applyTo: "**/*.go" +--- + +# Go Coding Standards + +## Style & Conventions + +- **Style:** Follow **Effective Go** and **Go Code Review Comments**. +- **Formatting:** Use `gofmt` or `goimports`. +- **Linting:** Ensure code passes `golangci-lint` (run `make lint`). + +## Logging & Error Handling + +- **Logging:** Use `zerolog`. + - Pattern: `log.Error().Err(err).Msg("message")` +- **Error Handling:** + - Handle errors explicitly. + - Return meaningful error messages and HTTP status codes using `model.Response`. + +## Configuration + +- **Viper:** Use `viper` for configuration management. + +## Import Conventions + +```go +// External dependencies with specific versions +// CB-Tumblebug imports (version specified in go.mod) +tbmodel "github.com/cloud-barista/cb-tumblebug/src/core/model" +tbresource "github.com/cloud-barista/cb-tumblebug/src/interface/rest/server/resource" +"github.com/cloud-barista/cb-tumblebug/src/core/common/netutil" + +// cm-model imports - Two main packages (version specified in go.mod) +cloudmodel "github.com/cloud-barista/cm-model/infra/cloud-model" +onpremmodel "github.com/cloud-barista/cm-model/infra/on-premise-model" + +// Internal packages +"github.com/cloud-barista/cm-beetle/pkg/config" +"github.com/cloud-barista/cm-beetle/pkg/core/common" +``` diff --git a/.github/instructions/markdown.instructions.md b/.github/instructions/markdown.instructions.md new file mode 100644 index 0000000..85317a6 --- /dev/null +++ b/.github/instructions/markdown.instructions.md @@ -0,0 +1,25 @@ +--- +applyTo: "**/*.md" +--- + +# Project documentation writing guidelines + +## General Guidelines + +- Write clear and concise documentation. +- Use consistent terminology and style. +- Include code examples where applicable. + +## Grammar + +- Use present tense verbs (is, open) instead of past tense (was, opened). +- Write factual statements and direct commands. Avoid hypotheticals like "could" or "would". +- Use active voice where the subject performs the action. +- Write in second person (you) to speak directly to readers. + +## Markdown Guidelines + +- Use headings to organize content. +- Use bullet points for lists. +- Include links to related resources. +- Use code blocks for code snippets. diff --git a/.github/instructions/transx.instructions.md b/.github/instructions/transx.instructions.md new file mode 100644 index 0000000..9a849d6 --- /dev/null +++ b/.github/instructions/transx.instructions.md @@ -0,0 +1,19 @@ +--- +applyTo: "transx/**" +--- + +# TransX Library Instructions + +This directory (`transx/`) contains the **Data Migration Library**. +It is a self-contained Go module with its own `go.mod`. + +## Key Constraints + +- **Independent Module:** Treat this as a separate library. +- **No Upstream Dependencies:** Do NOT import packages from `cm-beetle/pkg/...` or `cm-beetle/cmd/...`. +- **Dependency Management:** Add dependencies to `transx/go.mod`, not the root `go.mod`. + +## Functionality + +- Supports `rsync` and `object-storage-api` transfer methods. +- Supports Direct and Relay migration modes. diff --git a/.github/prompts/api-guide.prompt.md b/.github/prompts/api-guide.prompt.md new file mode 100644 index 0000000..0e35259 --- /dev/null +++ b/.github/prompts/api-guide.prompt.md @@ -0,0 +1,70 @@ +--- +name: api-guide +description: Generate API usage guide with JSON examples from Swagger specification +argument-hint: Enter API path (e.g., /migration/ns/{nsId}/mci) +agent: agent +model: "Claude Sonnet 4.5" +--- + +You are the Lead Technical Writer for the Cloud-Barista project. + +[Target API Path] +{{api_path}} +_(If the above is empty or contains a placeholder, extract the API path from the user's chat message)_ + +[Instructions] + +1. **Identify API Path:** + + - If `{{api_path}}` is provided (replaced by CLI), use it. + - Otherwise, identify the API path (e.g., `/migration/...`) from the user's request. + +2. **Read Specification (Step-by-step):** + a. Read `api/swagger.yaml` and find the API path section + b. Identify the request body schema (e.g., `controller.MigrateInfraRequest`) + c. For each `$ref` in the request body, read that definition section + d. Repeat for nested `$ref` (e.g., `cloudmodel.MciReq` → `cloudmodel.CreateSubGroupReq`) + e. Do the same for response schema + f. Collect at least 3-4 levels of nested definitions to understand the complete structure + +3. **Generate Guide:** + + - **Overview:** Briefly explain what this API does based on the Swagger summary/description. Include key features and purpose. + - **Request Example:** Create a realistic, detailed JSON request body example with meaningful values + - **Response Example:** Create a realistic JSON response example + - **Notes:** Add important notes about: + - Prerequisites or dependencies + - Resource creation order + - Error handling behaviors + - Best practices + +4. **Style:** + - Write in **English**. + - Use Markdown for clear formatting. + - Be thorough and detailed. + +[Output Format] + +## 🚀 API Guide: {{api_path}} + +### 📖 Overview + +(Detailed description with key features) + +### 📦 Request Example (JSON) + +```json +... +``` + +### 📦 Response Example (JSON) + +```json +... +``` + +### 📝 Notes + +1. **[Topic]:** (Important information) +2. **[Topic]:** (Important information) + ... diff --git a/.github/prompts/git-commit.prompt.md b/.github/prompts/git-commit.prompt.md new file mode 100644 index 0000000..eee0b3b --- /dev/null +++ b/.github/prompts/git-commit.prompt.md @@ -0,0 +1,315 @@ +--- +mode: "agent" +model: "Claude Sonnet 4.5" +description: "Generate conventional commit messages by analyzing staged Git changes in CM-Beetle repository" +--- + +# Git Commit Message Generator + +## Role + +You are an expert Git commit message writer specializing in Cloud-Barista CM-Beetle project. You create clear, concise, and informative commit messages following conventional commit standards while understanding the project's cloud infrastructure migration context. + +## Project Context + +CM-Beetle is a Cloud-Barista sub-system for computing infrastructure migration from on-premise to multi-cloud environments. Key considerations: + +- **Language**: Go 1.25 with REST API architecture +- **Key Components**: Migration engine, recommendation system, CB-Tumblebug integration +- **Integration Focus**: CB-Tumblebug, cm-model dependencies + +## Task + +Analyze staged Git changes and generate appropriate commit titles and messages that accurately describe modifications in the context of cloud infrastructure migration and recommendation features. + +## Workflow Steps + +### Step 1: Analyze Staged Changes + +1. Use `get_changed_files` with `sourceControlState: ["staged"]` to examine all staged modifications +2. **Handling Large Changes:** If the diff is extensive, prioritize analyzing `pkg/` and `cmd/` directories. Treat `go.sum`, `swagger.yaml`, and `*.mock.go` as low-priority context. +3. Review file paths to identify affected components and scope +4. Analyze change patterns: additions, deletions, modifications +5. Consider CM-Beetle project structure and component relationships + +### Step 2: Determine Commit Type and Scope + +**Commit Types for CM-Beetle:** + +- `feat`: New features or functionality (new APIs, migration capabilities, recommendation algorithms) +- `fix`: Bug fixes correcting incorrect behavior +- `refactor`: Large-scale code restructuring affecting multiple components (models, APIs, architecture changes) +- `enhance`: Improve existing features without major structural changes +- `update`: Update existing functionality or dependencies +- `docs`: Documentation-only changes (README, guides, API docs) +- `test`: Test code additions or modifications +- `context`: AI context files (copilot instructions, prompts, AI guidelines) +- `chore`: Maintenance tasks (dependencies, build config, tooling) +- `release`: Release staging commits for version bumps and preparation + +**Special Case: Release Staging** + +When staging changes for a release (version bump, dependency updates, test results refresh), use the following format: + +- **Title Format**: `release: staging vX.Y.Z with [key highlights]` +- **Common Patterns**: + - `release: staging vX.Y.Z with testing on N CSPs` (when test results are refreshed) + - `release: staging vX.Y.Z with upgrading tumblebug, spider, and mapui` (dependency updates) + - `release: staging vX.Y.Z mainly for [component]` (component-focused release) +- **Body Format**: Use bullet points to describe specific changes (dependency versions, test status, key updates) + +**Common CM-Beetle Scopes:** + +- `migration`: Infrastructure migration features (`pkg/core/migration/`) +- `recommendation`: VM/infrastructure recommendation (`pkg/core/recommendation/`) +- `api`: REST API handlers and models (`pkg/api/rest/`) +- `client`: CB-Tumblebug client implementations (`pkg/client/tumblebug/`) +- `test-cli`: Test CLI tool (`cmd/test-cli/`) +- `core`: Core business logic and utilities (`pkg/core/`) +- `config`: Configuration management (`pkg/config/`) +- `transx`: Data transfer and migration utilities (`transx/`) +- `analyzer`: Infrastructure analysis tool (`analyzer/`) + +### Step 3: Generate Commit Message + +**Title Requirements:** + +- Format: `type(scope): description` +- Maximum 50 characters +- Imperative mood (add, fix, improve) +- Specific to CM-Beetle functionality + +**Body Requirements:** + +- Maximum 3-5 bullet points +- Focus on functional impact for migration/recommendation features +- Essential changes only, omit implementation details +- Each line under 50 characters when possible +- **Breaking Changes:** If the change modifies public API signatures or configuration structures in a non-backward-compatible way, append `BREAKING CHANGE: ` in the footer. + +## CM-Beetle Specific Guidelines + +### Migration Feature Changes + +- Focus on migration capabilities and workflow improvements +- Mention supported cloud providers (AWS, Azure, GCP, NCP, Alibaba) +- Highlight infrastructure provisioning and lifecycle changes + +### Recommendation System Changes + +- Emphasize VM specification matching and sorting improvements +- Note proximity algorithms and similarity calculations +- Mention cloud provider compatibility enhancements + +### API and Integration Changes + +- Reference CB-Tumblebug integration improvements +- Note cm-model synchronization or compatibility updates +- Highlight REST API endpoint modifications + +### Testing and CLI Changes + +- Reference test-cli improvements for automated testing +- Note test coverage for different cloud providers +- Mention validation and error handling improvements + +## Example Output Formats + +### Migration Feature + +``` +feat(migration): add proximity-based VM sorting + +- Sort by vCPU/memory distance for all machine types +- Add Azure hypervisor generation compatibility +- Improve VM infrastructure creation workflow +``` + +### Bug Fix + +``` +fix(test-cli): resolve counter showing 7/6 tests + +- Remove duplicate pairPassed increment +- Fix test result summary accuracy +``` + +### API Enhancement + +``` +feat(api): add MCI recommendation endpoint + +- Support multi-cloud infrastructure recommendations +- Integrate with CB-Tumblebug spec matching +- Add validation for CSP and region pairs +``` + +### Enhancement + +``` +enhance(recommendation): improve VM spec matching accuracy + +- Add proximity-based similarity scoring +- Weight CPU and memory equally in calculations +- Filter out incompatible hypervisor types +``` + +### Update Existing Feature + +``` +update(test-cli): adopt multi-candidate recommendation API + +- Use /beetle/recommendation/vmInfra endpoint +- Handle multiple recommendation candidates +- Select first candidate for migration +``` + +### Documentation Update + +``` +docs(test-cli): update NCP test results + +- Refresh execution timestamps to Aug 2025 +- Update resource IDs from latest test run +``` + +### Context Files + +``` +context: add copilot instructions and prompts + +- Add copilot-instructions.md for project guidance +- Add modular instructions for code standards +- Add git-commit.prompt.md for commit generation +``` + +### Large-scale Refactoring + +``` +refactor(core): restructure recommendation engine + +- Split monolithic recommender into separate modules +- Introduce plugin architecture for CSP adapters +- Migrate from sync to async processing model +``` + +## Tool Usage and Validation + +### Required Tools + +- `get_changed_files`: Analyze staged changes with `sourceControlState: ["staged"]` +- `run_in_terminal`: Execute Git commands to check staged status and changes +- `read_file`: Review file contents for context understanding +- `grep_search`: Search for patterns to determine change impact + +### Git Staged Status Commands + +Use `run_in_terminal` with these Git commands to analyze staged changes: + +```bash +# Basic staged file overview +git status + +# List only staged file names +git diff --cached --name-only + +# Show staged changes with status (A: Added, M: Modified, D: Deleted) +git diff --cached --name-status + +# View detailed staged changes (diff content) +git diff --cached + +# Compact status format for parsing +git status --porcelain +``` + +### Analysis Process + +1. **Git Status Check**: Use `run_in_terminal` with `git status` to get overall repository state +2. **Staged Files Identification**: Use `git diff --cached --name-only` to list staged files +3. **Diff Content Review**: Use `git diff --cached` to review actual modifications +4. **Scope Determination**: Determine scope from modified file locations +5. **Change Classification**: Identify functional vs. maintenance changes +6. **Component Relationship Mapping**: Understand dependencies and integration points +7. **CM-Beetle Context Application**: Apply domain knowledge for accurate categorization + +### Validation Checklist + +- [ ] Follows conventional commit format: `type(scope): description` +- [ ] Title under 50 characters +- [ ] Uses imperative mood throughout +- [ ] Scope matches CM-Beetle component structure +- [ ] Body contains essential changes only (3-5 bullet points max) +- [ ] Each bullet point under 50 characters +- [ ] Focuses on migration/recommendation functionality impact +- [ ] Ready for `git commit -m` usage + +## Special Cases for CM-Beetle + +### CB-Tumblebug Integration Updates + +``` +feat(client): enhance Tumblebug MCI operations + +- Add namespace lifecycle management +- Improve VM specification queries +- Support new CB-Tumblebug v0.11.3 APIs +``` + +### Multi-Cloud Provider Support + +``` +feat(migration): add Alibaba Cloud support + +- Implement Alibaba-specific resource mapping +- Add compatibility validation +- Update provider selection logic +``` + +### Test Infrastructure Improvements + +``` +test(cli): add comprehensive API validation + +- Support all 6 CM-Beetle core endpoints +- Add skip test functionality +- Generate markdown test reports +``` + +### Release Staging + +``` +release: staging v0.4.7 with testing on 5 CSPs + +- Upgrade to CB-Tumblebug v0.12.1 +- Upgrade to cm-model v0.0.15 +- Refresh test results with Dec 2025 execution +- Update docker-compose images to v0.4.7 +``` + +**Release Staging Detection Criteria:** + +Identify a release staging commit when: + +1. Docker Compose version is bumped (e.g., `cm-beetle:0.4.6` → `cm-beetle:0.4.7`) +2. Multiple test result files are updated with new timestamps +3. Dependencies like `cb-tumblebug`, `cm-model`, `cb-spider`, `cb-mapui` are upgraded +4. Changes span across build configs, test results, and dependency files + +**Release Staging Title Guidelines:** + +- Always start with `release: staging vX.Y.Z` +- Add context about what's included: + - `with testing on N CSPs` - when test results across multiple cloud providers are refreshed + - `with upgrading [components]` - when focusing on dependency upgrades + - `mainly for [component]` - when the release focuses on a specific component (e.g., `transx`, `analyzer`) +- Version number should match the version in `docker-compose.yaml` + +## Response Format + +Provide the final commit message in a code block. + +- The first line must be the **Commit Title**. +- Leave one blank line. +- Follow with the **Commit Body** (bullet points). +- Do NOT wrap the message in a `git commit` command. diff --git a/Makefile b/Makefile index 055c7ad..87c92ff 100644 --- a/Makefile +++ b/Makefile @@ -45,6 +45,9 @@ swag swagger: ## Generate Swagger API documentation @rm ./main.go @echo "Generated Swagger API documentation!" +api-guide: ## Generate API guide using AI (Usage: make api-guide API_PATH=/migration/ns/{nsId}/mci) + @./scripts/ask-ai-api-guide.sh "$(API_PATH)" + # build: lint swag ## Build the binary file for amd64 build: ## Build the binary file for amd64 @echo "Building the binary for amd64..." diff --git a/docs/ai-context-for-us.md b/docs/ai-context-for-us.md new file mode 100644 index 0000000..d0528b9 --- /dev/null +++ b/docs/ai-context-for-us.md @@ -0,0 +1,143 @@ +# AI-Assisted Development: Context for Us + +Welcome! This document explains how we use AI context files to work together more effectively on CM-Beetle. Think of these files as our shared understanding with AI—when we maintain them well, everyone benefits from consistent, high-quality assistance. + +## Table of Contents + +- [Context Files](#context-files) +- [GitHub Copilot Integration](#github-copilot-integration) +- [Supporting Tools](#supporting-tools) +- [Troubleshooting](#troubleshooting) + +## Context Files + +Context files are the foundation of our AI-assisted workflow. They teach AI tools about CM-Beetle's architecture, patterns, and standards. + +### Copilot Instructions + +**`.github/copilot-instructions.md`** is our main guide for GitHub Copilot. It covers: + +- Project architecture and design patterns +- How we integrate with CB-Tumblebug and cm-model +- Echo handler patterns we follow +- Our coding standards and conventions + +This file helps Copilot understand CM-Beetle like a team member would. + +### Component Instructions + +**`.github/instructions/`** contains specialized rules that apply based on what you're working on: + +| File Pattern | Instruction File | What It Covers | +| ------------- | -------------------------- | ------------------------------------- | +| `**/*.go` | `go.instructions.md` | Go standards, imports, error handling | +| `**/*.md` | `markdown.instructions.md` | Documentation style and grammar | +| `analyzer/**` | `analyzer.instructions.md` | Analyzer module-specific constraints | +| `transx/**` | `transx.instructions.md` | TransX library guidelines | + +These ensure Copilot gives you the right guidance depending on the file you're editing. + +### Prompt Templates + +**`.github/prompts/`** contains reusable workflows that automate common tasks: + +**`git-commit.prompt.md`** helps write conventional commit messages: + +- Analyzes your staged changes +- Determines the appropriate commit type and scope +- Formats everything according to our conventions + +**`api-guide.prompt.md`** generates comprehensive API documentation: + +- Reads our Swagger specifications +- Creates realistic JSON request/response examples +- Includes prerequisites and best practices + +These prompts save time and ensure consistency across the project. + +## GitHub Copilot in VS Code + +When you open CM-Beetle in VS Code, GitHub Copilot automatically loads our context files: + +1. `.github/copilot-instructions.md` - Core project knowledge +2. `.github/instructions/*.instructions.md` - Component-specific expertise + +**Try it out in VS Code:** + +Open Copilot Chat (`Ctrl+Alt+I`) and ask: + +``` +"What are the key coding standards for this project?" +"How do I integrate with CB-Tumblebug?" +"Show me the Echo handler pattern we use" +``` + +Copilot will reference our instruction files and give you project-specific answers—like having an experienced team member available 24/7. + +## Using Prompt Templates + +Our prompt templates work with different AI tools depending on the task. + +### Generate Commit Messages (VS Code Copilot) + +Let VS Code Copilot write your commit messages using our conventions: + +1. Stage your changes: `git add .` +2. Open Copilot Chat in VS Code (`Ctrl+Alt+I`) +3. Type: `/git-commit` + +Copilot analyzes your changes and generates a properly formatted conventional commit message: + +``` +feat(migration): add proximity-based VM sorting + +- Sort by vCPU/memory distance for all machine types +- Add Azure hypervisor generation compatibility +``` + +### Generate API Documentation (GitHub Copilot CLI) + +Need documentation for an API endpoint? Use the Copilot CLI tool: + +```bash +make api-guide API_PATH=/migration/ns/{nsId}/mci +``` + +This uses GitHub Copilot CLI to read our Swagger specs and generate comprehensive guides with realistic examples. + +**First-time setup (Copilot CLI):** + +```bash +npx @github/copilot +# Type: /login and follow the prompts +``` + +## Troubleshooting + +**VS Code Copilot not following our guidelines?** + +- Try reloading VS Code: `Ctrl+Shift+P` > "Reload Window" +- Verify it's working: Ask in Copilot Chat `"What instructions do you have for this project?"` + +**GitHub Copilot CLI authentication issues?** + +```bash +npx @github/copilot +# Type: /login +``` + +**Getting incomplete API examples?** + +- Run `make swag` to regenerate Swagger docs +- Check that `api/swagger.yaml` has complete schema definitions + +**Component-specific rules not being applied in VS Code?** + +- Verify your file location matches the `applyTo` pattern in the instruction file +- Try reloading VS Code: `Ctrl+Shift+P` > "Reload Window" + +--- + +**Need more help?** Check out: [GitHub Copilot Docs](https://docs.github.com/en/copilot) | [Conventional Commits](https://www.conventionalcommits.org/) | [Our API Specs](../api/swagger.yaml) + +**Want to improve our AI context?** Great! These files live alongside our code—update them as the project evolves and submit your improvements via pull request. Better context helps everyone. diff --git a/scripts/ask-ai-api-guide.sh b/scripts/ask-ai-api-guide.sh new file mode 100755 index 0000000..1693d09 --- /dev/null +++ b/scripts/ask-ai-api-guide.sh @@ -0,0 +1,45 @@ +#!/bin/bash + +# Check if API path is provided +API_PATH=$1 +if [ -z "$API_PATH" ]; then + echo "Usage: $0 " + echo "Example: $0 /migration/ns/{nsId}/mci" + exit 1 +fi + +# Path to the prompt file +PROMPT_FILE=".github/prompts/api-guide.prompt.md" + +# Check if prompt file exists +if [ ! -f "$PROMPT_FILE" ]; then + echo "Error: Prompt file not found at $PROMPT_FILE" + exit 1 +fi + +# Read the prompt template +TEMPLATE=$(cat "$PROMPT_FILE") + +# Replace {{api_path}} with the provided argument +# Using sed for compatibility. Escaping slashes in API_PATH is important. +ESCAPED_API_PATH=$(echo "$API_PATH" | sed 's/\//\\\//g') +PROMPT=$(echo "$TEMPLATE" | sed "s/{{api_path}}/$ESCAPED_API_PATH/g") + +# Check authentication status by running a dummy prompt +echo "Checking GitHub Copilot authentication..." +if ! npx -y @github/copilot -p "hi" --silent &> /dev/null; then + echo "" + echo "❌ Error: You are not authenticated with GitHub Copilot." + echo "" + echo "👉 Please run the following command to login:" + echo " npx @github/copilot" + echo " > Then type '/login' and follow the instructions." + echo "" + echo "After logging in, run this command again." + exit 1 +fi + +# Run copilot using npx (no installation required) +echo "Generating API Guide for: $API_PATH" +echo "----------------------------------------" +npx -y @github/copilot -p "$PROMPT" --model "claude-sonnet-4.5" --allow-tool 'shell'