# 🎉 mrkit-cli — Supercharged Merge Request / Pull Request Workflow Automation

**Create Merge Requests and Pull Requests faster than ever — with templates, multi-step deployment chains, and secure token management.**

`mrkit-cli` (command: `mr`) is a powerful, developer-friendly CLI tool that streamlines working with:

- GitLab Merge Requests  
- GitHub Pull Requests  
- Multi-step chain MRs/PRs (e.g., `main → dev → stage → prod`)  
- Reusable MR/PR templates  
- Chain deployment templates  
- Secure encrypted token storage (AES-256)

---

## 📦 Installation

```bash
npm install -g mrkit-cli
```

Verify installation:

```bash
mr --help
```

---

## 🚀 Quick Start

### 1. Initialize configuration

```bash
mr config init
```

You will be prompted for:

- GitLab personal access token
- GitHub personal access token
- Encryption password (used to protect tokens)
- Default branches and platform preferences
- Frequently used projects (optional but recommended)

All sensitive data is encrypted with AES-256 and stored in:

```
~/.mr-cli/config.json
```

---

## 🔐 Token Setup Guide

### GitLab Token

**Required scopes:** `api`, `read_repository`, `write_repository`

1. Go to https://gitlab.com/-/profile/personal_access_tokens
2. Create a token with the above scopes
3. Run `mr config init` and paste it when prompted

### GitHub Token

**Required scopes:** `repo` (includes public_repo & repo), `workflow` (optional)

1. Go to https://github.com/settings/tokens
2. Generate a fine-grained or classic token
3. Run `mr config init` and paste it

Your tokens are immediately encrypted using the password you provide.

---

## 🔒 Encryption & Security

- **Encryption:** Tokens are encrypted with AES-256 using your chosen password during `mr config init`
- **Decryption:** On every `mr create` or `mr chain run`, the CLI temporarily decrypts tokens after you enter the correct password
- **Wrong password** → clear error, no token exposure

---

## 📚 Full Command Reference

### ⚙️ Configuration

```bash
mr config init              # First-time setup (interactive)
mr config reset             # Wipe all config (tokens, projects, etc.)
mr config set <key> <value> # Set a specific config value
mr config list              # Show current (decrypted) config
```

### 🧩 MR/PR Templates

```bash
mr template add             # Create a new reusable template (interactive)
mr template list            # List all templates
mr template show <name>     # View template details
mr template remove <name>   # Remove a template
```

### 🚀 Create an MR/PR

```bash
mr create my-template       # Using a saved template
mr create                   # Fully interactive mode
```

### 🔗 Chain Templates (Multi-step Deployments)

Perfect for environments like `main → dev → stage → prod`

```bash
mr chain add                # Create a new chain template
mr chain list               # List all chain templates
mr chain show <name>        # View chain details
mr chain remove <name>      # Remove a chain template
```

### ⚡ Run a Deployment Chain

```bash
mr chain run deploy-prod            # Execute all steps in sequence
mr chain run deploy-prod --open     # Open the final MR/PR in browser
```

**Example output:**

```
[1] https://gitlab.com/org/app/-/merge_requests/123
[2] https://gitlab.com/org/app/-/merge_requests/124
[3] https://gitlab.com/org/app/-/merge_requests/125  ✔️ Opened
```

### 🏗 Saved Projects

```bash
mr project add              # Add a frequently used GitLab/GitHub project
mr project list             # List saved projects
```

Saved projects appear as quick-select options during template/chain creation.

---

## 🧠 How Chain Execution Works (Internals)

1. Load chain template
2. Prompt for decryption password → decrypt token
3. For each step:
   - Create MR/PR from source → target branch
   - Wait for previous step to be created (sequential)
4. Collect all URLs
5. Display numbered list
6. Optionally open the last one with `--open`

---

## 🛡 Troubleshooting & Edge Cases

| Issue | Solution |
|-------|----------|
| Failed to decrypt token | Enter the exact password used during `mr config init` |
| Branch not found | Ensure the source branch exists remotely (`git push`) |
| Template/chain not appearing | Run `mr template list` or `mr chain list` to verify |
| Wrong project selected | Use `mr project list` and re-add correct ones |
| Browser not opening (spinner) | The CLI stops the spinner before opening URLs (fixed in latest version) |

---

## 🧪 Example Developer Workflow

```bash
# 1. Create a reusable MR template
mr template add
# → choose project, source: feature/x, target: dev, title/description, etc.

# 2. Open a new MR in seconds
mr create feature-x-template

# 3. Create a deployment chain (main → dev → prod)
mr chain add
# → define 2 steps: main→dev, then dev→prod

# 4. Deploy with one command
mr chain run prod-deployment --open
```

---

## 🎁 Features Summary

✅ Fully encrypted token storage (AES-256)  
✅ Works seamlessly with GitLab and GitHub  
✅ Single and multi-step (chain) MR/PR automation  
✅ Reusable templates & chain templates  
✅ Interactive prompts with sensible defaults  
✅ Saved projects for ultra-fast selection  
✅ Built for maximum developer productivity

---

**Made for developers who hate repetitive merge request forms.**

Start automating your workflow today:

```bash
npm install -g mrkit-cli
mr config init
mr create          # ← feel the speed
```

**Happy merging! 🚀**