# Implementation Guide: Option 1 - Add tbi-devops-mgt Workspace

This guide walks through adding the tbi-devops-mgt repository as a workspace folder so we can directly integrate the pipeline deployment capabilities into tbi-agent.

## Prerequisites

- Access to `/Users/iakpabio/Desktop/git-code/tbi-code/tbi-devops-mgt`
- Kiro IDE with current workspace at `tbi-devops-environments`
- Git access to tbi-devops-common repository

## Step 1: Add tbi-devops-mgt to Workspace

### In Kiro IDE:

1. **Open Command Palette**: `Cmd+Shift+P` (Mac) or `Ctrl+Shift+P` (Windows/Linux)
2. **Type**: "Workspaces: Add Folder to Workspace"
3. **Navigate to**: `/Users/iakpabio/Desktop/git-code/tbi-code/tbi-devops-mgt`
4. **Click**: "Add"

This will add tbi-devops-mgt as a second workspace folder, allowing Kiro to read and modify files in both repositories.

### Verify Workspace Setup:

You should now see both folders in your workspace:
```
- tbi-devops-environments (current)
- tbi-devops-mgt (newly added)
```

## Step 2: Review Current tbi-agent Structure

Once the workspace is added, I'll be able to read the current tbi-agent code to understand:
- Current command structure in `devops-agent.js`
- Bot initialization in `index.js`
- Existing dependencies in `package.json`
- Current Dockerfile setup

## Step 3: Integration Plan

After reviewing the code, I'll:

### A. Add Pipeline Deployer Module
Create new file: `src/pipeline-deployer.js`
- Core pipeline deployment logic
- Claude AI integration
- Repository management
- File generation

### B. Update devops-agent.js
Add new command handlers:
```javascript
// Handle pipeline creation commands
case 'create pipeline':
case 'deploy pipeline':
  await handlePipelineDeployment(context, args);
  break;
```

### C. Update package.json
Add dependencies:
```json
{
  "dependencies": {
    "simple-git": "^3.22.0",
    "js-yaml": "^4.1.0"
  }
}
```

### D. Update Dockerfile (if needed)
Ensure git client is available:
```dockerfile
RUN apt-get update && apt-get install -y git
```

## Step 4: Create Kubernetes ConfigMaps

### ConfigMap 1: System Prompt
```bash
kubectl create configmap tbi-agent-pipeline-config \
  --from-file=system-prompt.txt=integration/tbi-agent-pipeline-integration/k8s-configmaps/system-prompt.txt \
  -n tbi-agent --dry-run=client -o yaml > /Users/iakpabio/Desktop/git-code/tbi-code/tbi-devops-common/app/monitor/apps/tbi-agent/configmap-pipeline-config.yaml
```

### ConfigMap 2: Templates
```bash
kubectl create configmap tbi-agent-pipeline-templates \
  --from-file=integration/tbi-agent-pipeline-integration/k8s-configmaps/templates/ \
  -n tbi-agent --dry-run=client -o yaml > /Users/iakpabio/Desktop/git-code/tbi-code/tbi-devops-common/app/monitor/apps/tbi-agent/configmap-pipeline-templates.yaml
```

## Step 5: Update Deployment Manifest

Update `/Users/iakpabio/Desktop/git-code/tbi-code/tbi-devops-common/app/monitor/apps/tbi-agent/deployment.yaml`:

### Add Volume Mounts:
```yaml
spec:
  template:
    spec:
      volumes:
        - name: pipeline-config
          configMap:
            name: tbi-agent-pipeline-config
        - name: pipeline-templates
          configMap:
            name: tbi-agent-pipeline-templates
      containers:
        - name: tbi-agent
          volumeMounts:
            - name: pipeline-config
              mountPath: /app/config
            - name: pipeline-templates
              mountPath: /app/templates
```

### Add Environment Variables:
```yaml
env:
  - name: TBI_DEVOPS_ENVIRONMENTS_REPO
    value: "git@github.com:travelbrands/tbi-devops-environments.git"
  - name: TBI_ENVIRONMENTS_REPO
    value: "git@github.com:travelbrands/tbi-environments.git"
  - name: GITHUB_TOKEN
    valueFrom:
      secretKeyRef:
        name: tbi-agent-secrets
        key: github-token
```

## Step 6: Update Secrets

Create or update the secret with GitHub token:
```bash
kubectl create secret generic tbi-agent-secrets \
  --from-literal=github-token=<your-github-token> \
  --from-literal=anthropic-api-key=<your-anthropic-key> \
  -n tbi-agent --dry-run=client -o yaml | kubectl apply -f -
```

## Step 7: Build and Deploy

### Option A: Local Development
```bash
cd /Users/iakpabio/Desktop/git-code/tbi-code/tbi-devops-mgt/src
npm install
npm test  # if tests exist
```

### Option B: Deploy via ArgoCD
```bash
cd /Users/iakpabio/Desktop/git-code/tbi-code/tbi-devops-common

# Commit changes
git add app/monitor/apps/tbi-agent/
git commit -m "Add pipeline deployment capabilities to tbi-agent"
git push

# ArgoCD will automatically sync
```

## Step 8: Test Integration

### Test Command in Teams:
```
@tbi-agent create pipeline tbi-test-service redtag nodejs
```

### Expected Response:
```
✅ Successfully created CircleCI pipeline for tbi-test-service

Configuration:
- Namespace: redtag
- Technology: nodejs
- Files created: 15

tbi-devops-environments:
- dev/tbi-test-service/.circleci/config.yml
- dev/tbi-test-service/docker/Dockerfile
- dev/tbi-test-service/scripts/deploy-services.sh
- dev/tbi-test-service/scripts/setup-kubectl.sh
- dev/tbi-test-service/charts/...

tbi-environments:
- dev/values/tbi-test-service.yaml
- dev/secrets/secrets.tbi-test-service.yaml

Next Steps:
1. Review generated files in repositories
2. Update secrets file with actual values
3. Create VERSION file in service repository
...
```

## Step 9: Verify Deployment

### Check Pod Logs:
```bash
kubectl logs -n tbi-agent -l app=tbi-agent --tail=100
```

### Check ConfigMaps:
```bash
kubectl get configmap -n tbi-agent
kubectl describe configmap tbi-agent-pipeline-config -n tbi-agent
```

### Check Secrets:
```bash
kubectl get secret tbi-agent-secrets -n tbi-agent
```

## Troubleshooting

### Issue: "Cannot read files in tbi-devops-mgt"
**Solution**: Ensure workspace folder was added correctly. Check workspace settings.

### Issue: "Module not found: simple-git"
**Solution**: Run `npm install` in tbi-devops-mgt/src directory

### Issue: "ConfigMap not found"
**Solution**: Apply ConfigMaps to cluster:
```bash
kubectl apply -f configmap-pipeline-config.yaml -n tbi-agent
kubectl apply -f configmap-pipeline-templates.yaml -n tbi-agent
```

### Issue: "Permission denied: git clone"
**Solution**: Ensure GitHub token has repo access and is correctly set in secret

### Issue: "Claude API error"
**Solution**: Verify ANTHROPIC_API_KEY is set correctly in secret

## Next Steps After Implementation

1. **Test with Real Service**: Create pipeline for an actual new service
2. **Add More Commands**: 
   - `@tbi-agent update pipeline <service-name>`
   - `@tbi-agent pipeline status <service-name>`
   - `@tbi-agent list pipelines`
3. **Add Validation**: Pre-flight checks before creating pipeline
4. **Add Rollback**: Ability to revert pipeline changes
5. **Add Documentation**: Auto-generate documentation for created pipelines
6. **Add Notifications**: Slack/Teams notifications for pipeline events

## Files Created in This Integration

```
integration/tbi-agent-pipeline-integration/
├── README.md                                    # Overview
├── IMPLEMENTATION-GUIDE.md                      # This file
├── code-updates/
│   ├── pipeline-deployer.js                    # Main module
│   ├── devops-agent-updates.js                 # Command handlers
│   └── package-json-updates.json               # Dependencies
├── k8s-configmaps/
│   ├── system-prompt.txt                       # AI system prompt
│   └── templates/                              # Template files
└── k8s-manifests/
    ├── deployment-updates.yaml                 # Deployment changes
    └── secrets-template.yaml                   # Secrets template
```

## Timeline

- **Step 1-2**: 5 minutes (add workspace, review code)
- **Step 3**: 30 minutes (code integration)
- **Step 4-5**: 15 minutes (create ConfigMaps and update manifests)
- **Step 6**: 5 minutes (update secrets)
- **Step 7**: 10 minutes (deploy)
- **Step 8-9**: 15 minutes (test and verify)

**Total**: ~1.5 hours

## Support

If you encounter issues:
1. Check pod logs: `kubectl logs -n tbi-agent -l app=tbi-agent`
2. Check ArgoCD sync status
3. Verify ConfigMaps and Secrets are created
4. Test Claude API access separately
5. Review this guide's troubleshooting section

---

**Ready to proceed?** Add the tbi-devops-mgt workspace folder and I'll review the current code structure to begin the integration.
