Files
grafana/apps/provisioning/pkg/repository/workflows.go
Roberto Jiménez Sánchez 4eadc823a9 Provisioning: Move repository package to provisioning app (#110228)
* Move repository package to apps

* Move operators to grafana/grafana

* Go mod tidy

* Own package by git sync team for now

* Merged

* Do not use settings in local extra

* Remove dependency on webhook extra

* Hack to work around issue with secure contracts

* Sync Go modules

* Revert "Move operators to grafana/grafana"

This reverts commit 9f19b30a2e.
2025-09-02 09:45:44 +02:00

43 lines
1.1 KiB
Go

package repository
import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
provisioning "github.com/grafana/grafana/apps/provisioning/pkg/apis/provisioning/v0alpha1"
)
func IsWriteAllowed(repo *provisioning.Repository, ref string) error {
if len(repo.Spec.Workflows) == 0 {
return apierrors.NewBadRequest("this repository is read only")
}
var supportsWrite, supportsBranch bool
for _, v := range repo.Spec.Workflows {
switch v {
case provisioning.WriteWorkflow:
supportsWrite = true
case provisioning.BranchWorkflow:
supportsBranch = repo.Spec.Type.IsGit()
}
}
// Ref may be the configured branch for github repositories
if ref != "" && repo.Spec.GitHub != nil && repo.Spec.GitHub.Branch == ref {
ref = ""
}
// Ref may be the configured branch for git repositories
if ref != "" && repo.Spec.Git != nil && repo.Spec.Git.Branch == ref {
ref = ""
}
switch {
case ref == "" && !supportsWrite:
return apierrors.NewBadRequest("this repository does not support the write workflow")
case ref != "" && !supportsBranch:
return apierrors.NewBadRequest("this repository does not support the branch workflow")
default:
return nil
}
}