Files
Roberto Jiménez Sánchez 4847882ee7 Provisioning: handle .git extension more gracefully (#108213)
* Move .git to repository packages

* Bump nanogit 2025-07-17

This version handles `.git` extension internally so that the client
doesn't have to worry about it

* Put back mutation for Github

* Mutate also git URL for clarity
2025-07-18 13:25:21 +02:00

56 lines
1.3 KiB
Go

package git
import (
"context"
"fmt"
"strings"
"k8s.io/apimachinery/pkg/runtime"
provisioning "github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1"
"github.com/grafana/grafana/pkg/registry/apis/provisioning/controller"
"github.com/grafana/grafana/pkg/registry/apis/provisioning/secrets"
)
func Mutator(secrets secrets.RepositorySecrets) controller.Mutator {
return func(ctx context.Context, obj runtime.Object) error {
repo, ok := obj.(*provisioning.Repository)
if !ok {
return nil
}
if repo.Spec.Type != provisioning.GitRepositoryType {
return nil
}
if repo.Spec.Git == nil {
return fmt.Errorf("git configuration is required for git repository type")
}
if repo.Spec.Git.URL != "" {
url := strings.TrimSpace(repo.Spec.Git.URL)
if url != "" {
// Remove any trailing slashes
url = strings.TrimRight(url, "/")
// Only add .git if it's not already present
if !strings.HasSuffix(url, ".git") {
url = url + ".git"
}
repo.Spec.Git.URL = url
}
}
if repo.Spec.Git.Token != "" {
secretName := repo.Name + gitTokenSecretSuffix
nameOrValue, err := secrets.Encrypt(ctx, repo, secretName, repo.Spec.Git.Token)
if err != nil {
return err
}
repo.Spec.Git.EncryptedToken = nameOrValue
repo.Spec.Git.Token = ""
}
return nil
}
}