Files
grafana/pkg/registry/apis/provisioning/repository/workflows.go
T
Roberto Jiménez Sánchez 4f05b42275 Provisioning: initial unit tests push job (#103577)
* Add repository resources interface for export worker

* Add mocks for repository resources

* Add unit tests for ExportWorker's IsSupported method

* Add unit tests for ExportWorker's Process method, covering scenarios for missing export settings, write permissions, branch restrictions, and client creation failures.

* Fix unit tests

* Single function

* Add more unit tests

* Add test for failed folder

* Fail export folder errors

* Add another test

* Positive folder export

* Too many folder export errors

* Too many errors on folder export

* Partial folder errors

* Add test for nested folder

* Add test dashboard export

* More cases

* Ignore existing dashboards

* Fix folder tests

* Fix clonable test

* Add clone failure test

* Add test clean up without push

* Working tests

* Use mock clonable

* Add unit tests for IsWriteAllowed

* Add behaviour to cover ref equal to configured branch

* Fix worker test

* Fix linting

* Split clone and push

* Wrapper for clone and push
2025-04-08 12:44:11 +02:00

38 lines
1.0 KiB
Go

package repository
import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
provisioning "github.com/grafana/grafana/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 == provisioning.GitHubRepositoryType
}
}
// Ref may be the configured branch for github repositories
if ref != "" && repo.Spec.GitHub != nil && repo.Spec.GitHub.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
}
}