# Forgejo Integration for Container.mom

This package provides integration with Forgejo repositories for Container.mom templates and applications.

## Overview

The Forgejo integration allows Container.mom to:

1. Create and manage customer repositories in Forgejo
2. Store and version template values for application instances 
3. Create an application entry in a central "app of apps" pattern for ArgoCD GitOps
4. Query template information from Forgejo repositories

## Requirements

- A running Forgejo instance (default: https://git.container.mom)
- Forgejo credentials (either token or username/password)
- ArgoCD configured to watch the Forgejo repositories

## Environment Variables

The Forgejo integration uses the following environment variables:

- `FORGEJO_BASE_URL`: URL of the Forgejo instance (default: https://git.container.mom)
- `FORGEJO_TOKEN`: API token for Forgejo authentication (preferred method)
- `FORGEJO_USERNAME`: Username for Forgejo authentication (if token not provided)
- `FORGEJO_PASSWORD`: Password for Forgejo authentication (if token not provided)
- `FORGEJO_ORG`: Organization name to use for repositories (optional)
- `FORGEJO_CENTRAL_REPO`: Central repository name (default: container-mom-apps)

Legacy environment variables (deprecated, will be removed in future):
- `GITEA_BASE_URL`: Fallback URL
- `GITEA_TOKEN`: Fallback token
- `GITEA_USERNAME`: Fallback username
- `GITEA_PASSWORD`: Fallback password
- `GITEA_ORG`: Fallback organization
- `GITEA_CENTRAL_REPO`: Fallback central repository name

## Using the CLI

### Instantiate a Template and Push to Forgejo

```bash
mom-client template instantiate my-template --git-push --customer-id=customer1 --app-name=myapp
```

### List Available Templates

```bash
mom-client template list
```

### Show Template Details

```bash
mom-client template show my-template
```

## Repository Structure

When using the Forgejo integration, repositories are structured as follows:

```
repository/
├── charts/
│   └── app-of-apps/         # Main Helm chart for ArgoCD app of apps pattern
│       ├── Chart.yaml
│       ├── values.yaml      # Contains entries for all applications
│       └── templates/
│           ├── applications.yaml  # ArgoCD Application resources
│           └── projects.yaml      # ArgoCD Project resources
└── apps/
    ├── app1/                # One directory per application
    │   ├── values.yaml      # Template parameter values
    │   └── template.yaml    # Copy of the original template for reference
    └── app2/
        ├── values.yaml
        └── template.yaml
```

## App of Apps Values Format

The main app-of-apps values.yaml follows this format:

```yaml
default:
  project: default
  app:
    enableAutoSync: true
    autoSyncPrune: true
    # ... other default settings

projects:
  container-mom:
    description: Project for Container Mom applications
    # ... project configuration

applications:
  # Each application is a top-level entry
  app1:
    enabled: true
    project: container-mom
    enableAutoSync: true
    autoSyncPrune: true
    annotations:
      container-mom.cloud/customer-id: 'customer1'
    destination:
      server: "https://kubernetes.default.svc"
      namespace: default
    source:
      path: apps/app1
      targetRevision: main
      repoURL: https://git.container.mom/org/repo.git
      
  app2:
    enabled: true
    # ... similar configuration
```

## Application Values Format

The values.yaml file for each application has the following format:

```yaml
# Template information
template:
  name: my-template
  description: Example template

# Parameters
parameters:
  username:
    value: "admin"
    description: "Administrator username"
    required: true
    displayName: "Admin Username"
    defaultValue: "admin"
  
  password:
    value: "password123"
    description: "Administrator password"
    required: true
    displayName: "Admin Password"
    defaultValue: ""

# Additional values
extra_values:
  customer_id: "customer1"

# Application metadata
application:
  name: myapp
  created_at: "2023-01-01T12:00:00Z"
  customer_id: "customer1"
```

## ArgoCD Integration

The central app-of-apps pattern ensures that:

1. ArgoCD monitors a single Helm chart in your Git repository
2. Each application is defined as an entry in the `applications` section of values.yaml
3. ArgoCD creates Application resources for each enabled application entry
4. Applications are automatically synced based on their settings

## Programmatic Usage

```go
import (
    "context"
    "github.com/pfeifferj/container-mom-go/pkg/gitea"
)

// Create a Forgejo service
svc, err := gitea.NewService(&gitea.Config{
    BaseURL:      "https://git.container.mom",
    Token:        "my-token",
    Organization: "container-mom",
})

// Ensure app-of-apps chart exists in repository
err = svc.CreateAppOfAppsChart(ctx, "container-mom", "template-repo")

// Push template values and add application entry
err = svc.PushTemplateValues(ctx, "container-mom", "template-repo", template, values, "myapp")
``` 