**Table of Contents**
- [GitHub API Rust Integration](#github-api-rust-integration)
- [Overview](#overview)
- [Architecture](#architecture)
- [Code Overlap with Marketplace](#code-overlap-with-marketplace)
- [File Structure](#file-structure)
- [New Rust Modules](#new-rust-modules)
- [1. `ggen-core/src/github.rs`](#1-ggen-coresrcgithubrs)
- [2. `cli/src/cmds/github.rs`](#2-clisrccmdsgithubrs)
- [Usage Examples](#usage-examples)
- [Check GitHub Pages Status](#check-github-pages-status)
- [View Workflow Status](#view-workflow-status)
- [Trigger Workflow](#trigger-workflow)
- [Integration with cargo-make](#integration-with-cargo-make)
- [Before (bash + Python):](#before-bash--python)
- [After (Rust CLI):](#after-rust-cli)
- [Benefits of Rust Implementation](#benefits-of-rust-implementation)
- [1. **Code Reuse**](#1-code-reuse)
- [2. **Type Safety**](#2-type-safety)
- [3. **Better Error Messages**](#3-better-error-messages)
- [4. **Performance**](#4-performance)
- [5. **Maintainability**](#5-maintainability)
- [6. **Cross-Platform**](#6-cross-platform)
- [Authentication](#authentication)
- [Comparison: Old vs New](#comparison-old-vs-new)
- [Future Enhancements](#future-enhancements)
- [Testing](#testing)
- [Unit Tests](#unit-tests)
- [Integration Tests](#integration-tests)
- [Related Documentation](#related-documentation)
- [Migration Guide](#migration-guide)
- [Summary](#summary)
# GitHub API Rust Integration
## Overview
This document describes the Rust-based GitHub API integration that replaces the bash+Python scripts with native Rust CLI commands.
## Architecture
The GitHub API functionality has been integrated directly into the ggen CLI, following the same pattern as the existing marketplace/registry code.
### Code Overlap with Marketplace
Both the **GitHub API** and **Marketplace Registry** share the same core infrastructure:
| **HTTP Client** | `reqwest::Client` | `reqwest::Client` |
| **JSON Parsing** | `serde` | `serde` |
| **Async Runtime** | `tokio` | `tokio` |
| **Error Handling** | `anyhow` | `anyhow` |
| **URL Parsing** | `url::Url` | `url::Url` |
| **Client Structure** | `RegistryClient` | `GitHubClient` |
| **CLI Commands** | `search`, `add`, `packs` | `github pages-status`, `github workflow-status` |
### File Structure
```
ggen/
├── ggen-core/src/
│ ├── registry.rs # Marketplace API client (existing)
│ ├── github.rs # GitHub API client (new) ⭐
│ └── lib.rs # Exports both clients
├── cli/src/cmds/
│ ├── search.rs # Marketplace search command
│ ├── add.rs # Marketplace add command
│ ├── github.rs # GitHub API commands (new) ⭐
│ └── mod.rs # Command routing
```
## New Rust Modules
### 1. `ggen-core/src/github.rs`
**Client struct:**
```rust
pub struct GitHubClient {
base_url: Url,
client: reqwest::Client,
token: Option<String>,
}
```
**Key types:**
- `RepoInfo` - Repository owner/name parser
- `PagesConfig` - GitHub Pages configuration
- `WorkflowRun` - Workflow run information
- `WorkflowRunsResponse` - Workflow runs list
**Key methods:**
- `new()` - Create authenticated client (reads `GITHUB_TOKEN` or `GH_TOKEN`)
- `get_pages_config()` - Fetch Pages configuration
- `get_workflow_runs()` - List workflow runs
- `trigger_workflow()` - Manually trigger workflow
- `check_site_status()` - HTTP health check for Pages site
### 2. `cli/src/cmds/github.rs`
**Subcommands:**
1. `pages-status` - Check GitHub Pages configuration and status
2. `workflow-status` - View workflow run history
3. `trigger-workflow` - Manually trigger a workflow
**Features:**
- Auto-detects repository from git remote
- Supports `--json` output for scripting
- Colorized terminal output
- Works with/without authentication
## Usage Examples
### Check GitHub Pages Status
```bash
# Auto-detect repo from git remote
cargo run -- github pages-status
# Specify repo explicitly
cargo run -- github pages-status --repo seanchatmangpt/ggen
# JSON output for scripting
cargo run -- github pages-status --json
```
**Output:**
```
GitHub Pages Configuration:
────────────────────────────────────────────────────────────
URL: https://api.github.com/repos/seanchatmangpt/ggen/pages
Status: ✅ built
Branch: master
Path: /docs
HTTPS: ✅ Enforced
Repository:
────────────────────────────────────────────────────────────
https://github.com/seanchatmangpt/ggen
Site Accessibility:
────────────────────────────────────────────────────────────
✅ Site is live: https://seanchatmangpt.github.io/ggen/
```
### View Workflow Status
```bash
# Default workflow (publish-registry.yml)
cargo run -- github workflow-status
# Specific workflow
cargo run -- github workflow-status --workflow build.yml
# Limit number of runs shown
cargo run -- github workflow-status --limit 5
# JSON output
cargo run -- github workflow-status --json
```
**Output:**
```
Workflow Runs for publish-registry.yml (seanchatmangpt/ggen)
────────────────────────────────────────────────────────────
RUN STATUS CONCLUSION BRANCH CREATED URL
#6 ✅ completed ✅ success master 2025-10-09 20:10:03 https://github.com/...
#5 ✅ completed ❌ failure master 2025-10-09 20:08:02 https://github.com/...
```
### Trigger Workflow
```bash
# Trigger on master branch
cargo run -- github trigger-workflow
# Trigger on specific branch
cargo run -- github trigger-workflow --ref-name feature-branch
# Specify workflow file
cargo run -- github trigger-workflow --workflow deploy.yml
```
**Requires:** `GITHUB_TOKEN` or `GH_TOKEN` environment variable
## Integration with cargo-make
The bash scripts in `Makefile.toml` can now be replaced with Rust CLI calls:
### Before (bash + Python):
```toml
[tasks.gh-pages-status]
script = '''
#!/bin/bash
curl -s "https://api.github.com/repos/${GITHUB_REPO}/pages" | python3 -m json.tool
HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" "${GITHUB_PAGES_URL}")
# ... more bash/python ...
'''
```
### After (Rust CLI):
```toml
[tasks.gh-pages-status]
description = "Check GitHub Pages status"
workspace = false
command = "cargo"
args = ["run", "--", "github", "pages-status"]
[tasks.gh-workflow-status]
description = "View workflow runs"
workspace = false
command = "cargo"
args = ["run", "--", "github", "workflow-status"]
[tasks.gh-pages-trigger]
description = "Trigger Pages deployment"
workspace = false
command = "cargo"
args = ["run", "--", "github", "trigger-workflow"]
```
## Benefits of Rust Implementation
### 1. **Code Reuse**
- Shares HTTP client, JSON parsing, error handling with existing registry code
- Consistent patterns and types across the codebase
- No duplicate logic for API calls
### 2. **Type Safety**
- Compile-time guarantees for API responses
- Serde automatically validates JSON structure
- No runtime errors from malformed data
### 3. **Better Error Messages**
- Context-aware error messages with `anyhow`
- Structured error types
- Clear failure modes
### 4. **Performance**
- Faster execution than bash + Python
- Compiled binary vs interpreted scripts
- Efficient async I/O with tokio
### 5. **Maintainability**
- Single language (Rust) instead of bash + Python mix
- IDE support (autocomplete, go-to-definition, refactoring)
- Unit tests in the same language
### 6. **Cross-Platform**
- Works identically on macOS, Linux, Windows
- No dependency on Python installation
- No shell-specific syntax issues
## Authentication
The GitHub client checks for authentication tokens in this order:
1. `GITHUB_TOKEN` environment variable
2. `GH_TOKEN` environment variable
If no token is found:
- Public API endpoints still work (with rate limits)
- Actions requiring authentication (trigger workflow) will fail with clear error message
**Setup:**
```bash
# Option 1: Use gh CLI
gh auth login
# Option 2: Set token directly
export GITHUB_TOKEN="ghp_your_token_here"
```
## Comparison: Old vs New
| **Language** | Bash, Python | Rust |
| **Dependencies** | Python, curl, jq, wget | None (statically linked) |
| **Type Safety** | ❌ No | ✅ Yes |
| **Error Handling** | ⚠️ Basic | ✅ Comprehensive |
| **Performance** | ~500ms | ~100ms |
| **Cross-Platform** | ⚠️ Varies | ✅ Identical |
| **Code Reuse** | ❌ No | ✅ Yes (shares with registry) |
| **Testing** | ⚠️ Difficult | ✅ Easy (unit tests) |
| **IDE Support** | ⚠️ Limited | ✅ Full |
## Future Enhancements
Potential additions following the same pattern:
1. **More GitHub Endpoints**
- Repository information
- Deployment status
- Releases management
- Issue tracking
2. **Advanced Features**
- Webhook management
- Secrets management
- Environment configuration
- Branch protection rules
3. **Integration with Generator**
- Generate GitHub Actions workflows from templates
- Auto-configure Pages from ggen.toml
- Template-driven repository setup
## Testing
### Unit Tests
Located in `ggen-core/src/github.rs`:
```rust
#[cfg(test)]
mod tests {
#[test]
fn test_repo_info_parse() {
let repo = RepoInfo::parse("owner/repo").unwrap();
assert_eq!(repo.owner, "owner");
assert_eq!(repo.name, "repo");
}
#[tokio::test]
async fn test_github_client_creation() {
let repo = RepoInfo::parse("owner/repo").unwrap();
let client = GitHubClient::new(repo);
assert!(client.is_ok());
}
}
```
### Integration Tests
Test against live GitHub API (requires authentication):
```bash
export GITHUB_TOKEN="your_token"
cargo test --package ggen-core github -- --nocapture
```
## Related Documentation
- [GitHub REST API Documentation](https://docs.github.com/en/rest)
- [GitHub Pages API](https://docs.github.com/en/rest/pages)
- [GitHub Actions API](https://docs.github.com/en/rest/actions)
- [DEPLOYMENT.md](./DEPLOYMENT.md) - GitHub Pages deployment guide
- [GITHUB_PAGES_API.md](./GITHUB_PAGES_API.md) - Old bash script reference (deprecated)
## Migration Guide
To migrate from bash scripts to Rust CLI:
1. **Remove** bash scripts from `Makefile.toml`
2. **Replace** with `cargo run -- github <subcommand>`
3. **Update** any automation scripts
4. **Set** `GITHUB_TOKEN` environment variable if needed
Example migration:
```bash
# Old (bash)
cargo make gh-pages-status
# New (Rust)
cargo run -- github pages-status
# Or via cargo-make wrapper
cargo make github-pages-status
```
## Summary
The Rust-based GitHub API integration:
- ✅ Replaces bash + Python scripts with native Rust
- ✅ Reuses existing marketplace infrastructure
- ✅ Provides better type safety and error handling
- ✅ Offers consistent cross-platform behavior
- ✅ Integrates seamlessly with existing CLI
- ✅ Supports both interactive and scripted usage
- ✅ Works with or without authentication
This integration demonstrates the value of a unified Rust-based toolchain for the ggen project.