# Gitea Sync Skill

**Skill Name:** `gitea-sync`  
**Category:** `quick`  
**Load Skills:** `[]` (standalone)

---

## 🎯 Purpose

Automatically sync repositories to Gitea (git.moreminimore.com):
- Create new repositories
- Update existing repositories
- Push code automatically
- Auto-detect new vs existing repos

---

## 🔧 Prerequisites

### Gitea API Token

Get your API token from:
`https://git.moreminimore.com/user/settings/applications`

1. Login to Gitea
2. Go to Settings → Applications
3. Generate new token (name it "opencode-skills")
4. Copy the token
5. Add to unified `.env` file

---

## 🚀 Usage

### Sync New Repository

```bash
python3 scripts/sync.py \
  --repo my-website \
  --path ./my-website \
  --description "My PDPA-compliant website"
```

### Sync Without Pushing

```bash
python3 scripts/sync.py \
  --repo my-website \
  --path ./my-website \
  --no-push
```

### Parameters

| Parameter | Required | Default | Description |
|-----------|----------|---------|-------------|
| `--repo` | ✅ | - | Repository name |
| `--path` | ✅ | - | Path to code directory |
| `--description` | ❌ | "" | Repository description |
| `--no-push` | ❌ | false | Don't push code |
| `--private` | ❌ | false | Make private (not implemented) |

---

## 🔄 Workflow

### Auto-Detection

The script automatically detects:
- **New repository** → Creates with `auto_init`
- **Existing repository** → Updates metadata

### Push Process

1. Initialize git (if not already)
2. Add `.gitignore` (if not exists)
3. Configure authentication (uses API token)
4. Add all files
5. Commit with message "Auto-sync from website-creator"
6. Push to Gitea (force push for initial push)

---

## 📁 Files

```
gitea-sync/
├── SKILL.md
└── scripts/
    ├── sync.py         # Main script
    ├── .env.example    # Configuration template
    └── requirements.txt
```

---

## 🔐 Authentication

Uses Gitea API token for authentication:
- Stored in unified `.env` file
- Format: `Authorization: token <API_TOKEN>`
- Token embedded in git URL for push operations

---

## ✅ Success Criteria

After sync:
- ✅ Repository created/updated on Gitea
- ✅ Code pushed to `main` branch
- ✅ `.gitignore` created
- ✅ Git remote configured
- ✅ Repository URL returned

---

## 🌐 Repository URL

Format:
```
https://git.moreminimore.com/<username>/<repo-name>
```

---

## ⚠️ Troubleshooting

| Issue | Solution |
|-------|----------|
| 401 Unauthorized | Check API token in .env |
| 409 Conflict | Repository already exists (normal) |
| Push failed | Check git credentials, verify token |
| Not a git repo | Script auto-initializes (shouldn't fail) |

---

## 🔄 Integration

Used by:
- `website-creator` skill (auto-deploy workflow)
- Manual sync (standalone usage)

---

## 📝 Example Output

```
🔄 Gitea Sync
==================================================
Repository: my-website
Path: ./my-website
Description: My PDPA-compliant website
==================================================

🔐 Authenticated as: kunthawatgreethong

📦 Creating repository: my-website
✅ Repository created: my-website

🚀 Pushing code to Gitea
  → Initializing git repository
  → Adding remote: https://git.moreminimore.com/...
  → Adding files
  → Committing changes
  → Pushing to Gitea
✅ Code pushed successfully

🌐 Repository URL: https://git.moreminimore.com/kunthawatgreethong/my-website

==================================================
✅ Sync complete!
Repository: my-website
URL: https://git.moreminimore.com/kunthawatgreethong/my-website
Status: Created new repository
==================================================
```

---

## 🎯 API Endpoints Used

| Endpoint | Method | Purpose |
|----------|--------|---------|
| `/api/v1/user` | GET | Verify authentication |
| `/api/v1/repos/{user}/{repo}` | GET | Check if repo exists |
| `/api/v1/user/repos` | POST | Create repository |
| `/api/v1/repos/{user}/{repo}` | PATCH | Update repository |
| Git push | POST | Push code (via git protocol) |

---

## 📞 Support

For issues with Gitea:
- Check API token validity
- Verify repository permissions
- Review Gitea logs at: `https://git.moreminimore.com/explore`
