# AutoChangelog CLI

Agent-friendly command-line interface for the AutoChangelog platform.

## Features

- **Authentication Management**: Secure GitHub authentication with token management
- **Project Management**: Create, list, update, and delete projects
- **Repository Management**: Add, list, update, and remove repositories
- **Changelog Generation**: Generate changelogs with customizable options
- **Statistics & Utilities**: System info, health checks, and usage statistics
- **Agent-Friendly**: JSON output, error handling, and structured responses

## Installation

```bash
npm install -g autochangelog-cli
```

## Quick Start

### Authentication

First, authenticate with GitHub:

```bash
autochangelog auth login
```

### Create a Project

```bash
autochangelog project create --name "My Project" --plan free
```

### Add a Repository

```bash
autochangelog repo add --project-id <project-id> --repo "owner/repo-name"
```

### Generate a Changelog

```bash
autochangelog changelog generate --project-id <project-id> --repo "owner/repo-name" --format markdown
```

## Commands

### Authentication

```bash
# Login with GitHub
autochangelog auth login

# Check authentication status
autochangelog auth status

# Get current token
autochangelog auth token

# Logout
autochangelog auth logout
```

### Projects

```bash
# Create a new project
autochangelog project create --name "Project Name" --plan free

# List all projects
autochangelog project list

# Get project details
autochangelog project get <project-id>

# Update a project
autochangelog project update <project-id> --name "New Name"

# Delete a project
autochangelog project delete <project-id>
```

### Repositories

```bash
# Add a repository to a project
autochangelog repo add --project-id <project-id> --repo "owner/repo-name"

# List repositories for a project
autochangelog repo list --project-id <project-id>

# Get repository details
autochangelog repo get <repo-id>

# Update a repository
autochangelog repo update <repo-id> --private true

# Remove a repository
autochangelog repo remove <repo-id>
```

### Changelogs

```bash
# Generate a changelog
autochangelog changelog generate --project-id <project-id> --repo "owner/repo-name" --format markdown

# List changelogs for a project
autochangelog changelog list --project-id <project-id>

# Get changelog details
autochangelog changelog get <changelog-id>

# Update a changelog
autochangelog changelog update <changelog-id> --public true

# Delete a changelog
autochangelog changelog delete <changelog-id>
```

### Statistics

```bash
# Show system information
autochangelog stats system

# Check system health
autochangelog stats health

# Show project statistics
autochangelog stats project <project-id> --detailed

# Show usage statistics
autochangelog stats usage
```

## Options

### Global Options

- `--json`: Output in JSON format (default in non-TTY)
- `--verbose`: Enable verbose logging
- `--no-color`: Disable colored output
- `--config <path>`: Path to config file
- `--timeout <ms>`: Request timeout in milliseconds

### Authentication Options

- `--token <token>`: GitHub personal access token
- `--scopes <scopes>`: Required scopes (default: repo,user)

### Project Options

- `--name <name>`: Project name
- `--plan <plan>`: Project plan (free, pro, enterprise)
- `--description <desc>`: Project description

### Repository Options

- `--repo <repo>`: Repository in format "owner/repo-name"
- `--private <bool>`: Whether repository is private
- `--sync <bool>`: Whether to sync repository

### Changelog Options

- `--format <format>`: Output format (markdown, json, html)
- `--public <bool>`: Whether changelog is public
- `--template <template>`: Custom template name
- `--from <date>`: Start date for changelog
- `--to <date>`: End date for changelog

## Configuration

The CLI stores configuration in `~/.autochangelog/config.json`:

```json
{
  "github_token": "ghp_...",
  "api_base_url": "https://api.autochangelog.com",
  "timeout": 30000,
  "default_format": "markdown"
}
```

## Environment Variables

- `AUTOCHANGELOG_API_URL`: API base URL
- `AUTOCHANGELOG_GITHUB_TOKEN`: GitHub token
- `AUTOCHANGELOG_CONFIG_PATH`: Config file path
- `AUTOCHANGELOG_CACHE_PATH`: Cache directory path

## Error Handling

The CLI provides structured error responses:

```json
{
  "error": {
    "message": "Authentication failed",
    "type": "authentication_failed",
    "code": 401,
    "details": {
      "stack": "..."
    },
    "suggestions": [
      "Check your GitHub token",
      "Run 'autochangelog auth login'"
    ],
    "recoverable": true,
    "retry_after": null
  }
}
```

## JSON Output

For automation and scripting, use the `--json` flag:

```bash
autochangelog project list --json
```

## Development

### Running from Source

```bash
cd autochangelog-cli
npm install
node bin/autochangelog --help
```

### Testing

```bash
npm test
```

### Building

```bash
npm run build
```

## Contributing

1. Fork the repository
2. Create a feature branch
3. Make your changes
4. Add tests for new functionality
5. Run the test suite
6. Submit a pull request

## License

MIT License - see LICENSE file for details.

## Support

For support and questions:

- Create an issue on GitHub
- Check the documentation
- Contact the development team